diff --git a/portalocker/exceptions.py b/portalocker/exceptions.py index 5ce10b4..bb2b35e 100644 --- a/portalocker/exceptions.py +++ b/portalocker/exceptions.py @@ -2,6 +2,10 @@ class BaseLockException(Exception): # Error codes: LOCK_FAILED = 1 + def __init__(self, *args, **kwargs): + self.fh = kwargs.pop('fh', None) + Exception.__init__(self, *args, **kwargs) + class LockException(BaseLockException): pass diff --git a/portalocker/portalocker.py b/portalocker/portalocker.py index 413e887..460cf06 100644 --- a/portalocker/portalocker.py +++ b/portalocker/portalocker.py @@ -41,7 +41,8 @@ def lock(file_, flags): if exc_value.winerror == winerror.ERROR_LOCK_VIOLATION: raise exceptions.LockException( exceptions.LockException.LOCK_FAILED, - exc_value.strerror) + exc_value.strerror, + fh=file_) else: # Q: Are there exceptions/codes we should be dealing with # here? @@ -74,13 +75,15 @@ def lock(file_, flags): # [ ] be more specific here raise exceptions.LockException( exceptions.LockException.LOCK_FAILED, - exc_value.strerror) + exc_value.strerror, + fh=file_) finally: if savepos: file_.seek(savepos) except IOError as exc_value: raise exceptions.LockException( - exceptions.LockException.LOCK_FAILED, exc_value.strerror) + exceptions.LockException.LOCK_FAILED, exc_value.strerror, + fh=file_) def unlock(file_): try: @@ -110,13 +113,15 @@ def unlock(file_): else: raise exceptions.LockException( exceptions.LockException.LOCK_FAILED, - exc_value.strerror) + exc_value.strerror, + fh=file_) finally: if savepos: file_.seek(savepos) except IOError as exc_value: raise exceptions.LockException( - exceptions.LockException.LOCK_FAILED, exc_value.strerror) + exceptions.LockException.LOCK_FAILED, exc_value.strerror, + fh=file_) elif os.name == 'posix': # pragma: no cover import fcntl @@ -133,7 +138,7 @@ def lock(file_, flags): except locking_exceptions as exc_value: # The exception code varies on different systems so we'll catch # every IO error - raise exceptions.LockException(exc_value) + raise exceptions.LockException(exc_value, fh=file_) def unlock(file_): fcntl.flock(file_.fileno(), constants.LOCK_UN)