Skip to content

Commit

Permalink
added fh attribute to lockexceptions which contains the file handle to
Browse files Browse the repository at this point in the history
…fix #41
  • Loading branch information
wolph committed Dec 17, 2018
1 parent d6d4eaa commit 4d2018a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
4 changes: 4 additions & 0 deletions portalocker/exceptions.py
Expand Up @@ -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
Expand Down
17 changes: 11 additions & 6 deletions portalocker/portalocker.py
Expand Up @@ -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?
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit 4d2018a

Please sign in to comment.