From f5ae963b7c268c33bfa711d687258cff8c61205a Mon Sep 17 00:00:00 2001 From: thobrla Date: Mon, 6 Jun 2016 14:15:53 -0700 Subject: [PATCH] Handle OSError cases in multistore_file (#517) This change fixes an exception in multistore_file that occurs when the lock file is under conection. Windows may raise an OSError in this case, where other platforms typically raise in IOError. --- oauth2client/contrib/multistore_file.py | 2 +- tests/contrib/test_multistore_file.py | 19 +++++++++++-------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/oauth2client/contrib/multistore_file.py b/oauth2client/contrib/multistore_file.py index 483b0fb..879f3b6 100644 --- a/oauth2client/contrib/multistore_file.py +++ b/oauth2client/contrib/multistore_file.py @@ -298,7 +298,7 @@ class _MultiStore(object): self._thread_lock.acquire() try: self._file.open_and_lock() - except IOError as e: + except (IOError, OSError) as e: if e.errno == errno.ENOSYS: logger.warn('File system does not support locking the ' 'credentials file.') diff --git a/tests/contrib/test_multistore_file.py b/tests/contrib/test_multistore_file.py index 53b6542..28271ce 100644 --- a/tests/contrib/test_multistore_file.py +++ b/tests/contrib/test_multistore_file.py @@ -34,14 +34,15 @@ os.close(_filehandle) class _MockLockedFile(object): - def __init__(self, filename_str, error_code): + def __init__(self, filename_str, error_class, error_code): self.filename_str = filename_str + self.error_class = error_class self.error_code = error_code self.open_and_lock_called = False def open_and_lock(self): self.open_and_lock_called = True - raise IOError(self.error_code, '') + raise self.error_class(self.error_code, '') def is_locked(self): return False @@ -110,11 +111,13 @@ class MultistoreFileTests(unittest2.TestCase): try: for error_code in (errno.EDEADLK, errno.ENOSYS, errno.ENOLCK, errno.EACCES): - multistore = multistore_file._MultiStore(filename) - multistore._file = _MockLockedFile(filename, error_code) - # Should not raise even though the underlying file class did. - multistore._lock() - self.assertTrue(multistore._file.open_and_lock_called) + for error_class in (IOError, OSError): + multistore = multistore_file._MultiStore(filename) + multistore._file = _MockLockedFile( + filename, error_class, error_code) + # Should not raise though the underlying file class did. + multistore._lock() + self.assertTrue(multistore._file.open_and_lock_called) finally: os.unlink(filename) @@ -124,7 +127,7 @@ class MultistoreFileTests(unittest2.TestCase): try: multistore = multistore_file._MultiStore(filename) - multistore._file = _MockLockedFile(filename, errno.EBUSY) + multistore._file = _MockLockedFile(filename, IOError, errno.EBUSY) self.assertRaises(IOError, multistore._lock) self.assertTrue(multistore._file.open_and_lock_called) finally: