Skip to content
This repository has been archived by the owner on Nov 5, 2019. It is now read-only.

Commit

Permalink
Handle OSError cases in multistore_file (#517)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
thobrla authored and Jon Wayne Parrott committed Jun 6, 2016
1 parent a3cbf51 commit f5ae963
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
2 changes: 1 addition & 1 deletion oauth2client/contrib/multistore_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ def _lock(self):
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.')
Expand Down
19 changes: 11 additions & 8 deletions tests/contrib/test_multistore_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,15 @@

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
Expand Down Expand Up @@ -110,11 +111,13 @@ def test_lock_file_raises_ioerror(self):
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)

Expand All @@ -124,7 +127,7 @@ def test_lock_file_raise_unexpected_error(self):

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:
Expand Down

0 comments on commit f5ae963

Please sign in to comment.