From 2cb90d150a8e11464fcac6345f3005a677fbde34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20M=C2=AA=20Fern=C3=A1ndez?= Date: Tue, 6 Aug 2024 20:17:35 +0200 Subject: [PATCH] Fixed case where code would try to unlock a file handle externally managed --- RWFileLock/__init__.py | 2 +- RWFileLock/rw_file_lock.py | 17 +++++++++++------ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/RWFileLock/__init__.py b/RWFileLock/__init__.py index fc27f4f..f19b45a 100644 --- a/RWFileLock/__init__.py +++ b/RWFileLock/__init__.py @@ -29,7 +29,7 @@ ) # https://www.python.org/dev/peps/pep-0396/ -__version__ = version = "0.2.0" +__version__ = version = "0.2.1" __author__ = "José M. Fernández " __copyright__ = "© 2020-2024 Barcelona Supercomputing Center (BSC), ES" __license__ = "LGPLv2" diff --git a/RWFileLock/rw_file_lock.py b/RWFileLock/rw_file_lock.py index d8a7207..b26913e 100644 --- a/RWFileLock/rw_file_lock.py +++ b/RWFileLock/rw_file_lock.py @@ -98,8 +98,10 @@ def w_lock(self) -> "None": fcntl.lockf(self.lock_fd, (fcntl.LOCK_EX | fcntl.LOCK_NB)) self.isLocked = True self.isShareLock = False - except IOError: - raise LockError("Already locked by others") + except IOError as ioe: + raise LockError("Already locked by others") from ioe + except OSError as ose: + raise LockError("Read only file descriptor?") from ose def w_blocking_lock(self) -> "None": if self.isLocked and not self.isShareLock: @@ -109,15 +111,18 @@ def w_blocking_lock(self) -> "None": fcntl.lockf(self.lock_fd, fcntl.LOCK_EX) self.isLocked = True self.isShareLock = False - except OSError: - raise LockError("Read only file descriptor?") + except OSError as ose: + raise LockError("Read only file descriptor?") from ose def unlock(self) -> "None": if self.isLocked: try: fcntl.lockf(self.lock_fd, fcntl.LOCK_UN) - except IOError: - raise LockError("Already locked by others") + except OSError as ose: + if self.should_close: + raise LockError("Unexpected unlocking error") from ose + except IOError as ioe: + raise LockError("Unexpected unlocking error") from ioe finally: self.isLocked = False else: