Skip to content

Commit 6e3dca6

Browse files
committed
PR feedback
Signed-off-by: Bernát Gábor <[email protected]>
1 parent 6ba38d2 commit 6e3dca6

File tree

4 files changed

+25
-31
lines changed

4 files changed

+25
-31
lines changed

src/filelock/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,10 @@
3232
if warnings is not None:
3333
warnings.warn("only soft file lock is available", stacklevel=2)
3434

35-
#: Alias for the lock, which should be used for the current platform. On Windows, this is an alias for
36-
# :class:`WindowsFileLock`, on Unix for :class:`UnixFileLock` and otherwise for :class:`SoftFileLock`.
3735
if TYPE_CHECKING:
3836
FileLock = SoftFileLock
3937
else:
38+
#: Alias for the lock, which should be used for the current platform.
4039
FileLock = _FileLock
4140

4241

src/filelock/_api.py

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -41,36 +41,32 @@ def __exit__(
4141
class FileLockContext:
4242
"""
4343
A dataclass which holds the context for a ``BaseFileLock`` object.
44-
45-
The context is held in a separate class to allow optional use of thread local storage
46-
via the ``ThreadLocalFileContext`` class.
4744
"""
4845

49-
# The path to the lock file.
46+
# The context is held in a separate class to allow optional use of thread local storage via the
47+
# ThreadLocalFileContext class.
48+
49+
#: The path to the lock file.
5050
lock_file: str
5151

52-
# The default timeout value.
52+
#: The default timeout value.
5353
timeout: float
5454

55-
# The mode for the lock files
55+
#: The mode for the lock files
5656
mode: int
5757

58-
# The file descriptor for the *_lock_file* as it is returned by the os.open() function.
59-
# This file lock is only NOT None, if the object currently holds the lock.
58+
#: The file descriptor for the *_lock_file* as it is returned by the os.open() function, not None when lock held
6059
lock_file_fd: int | None = None
6160

62-
# The lock counter is used for implementing the nested locking mechanism. Whenever the lock is acquired, the
63-
# counter is increased and the lock is only released, when this value is 0 again.
64-
lock_counter: int = 0
61+
#: The lock counter is used for implementing the nested locking mechanism.
62+
lock_counter: int = 0 # When the lock is acquired is increased and the lock is only released, when this value is 0
6563

6664

6765
class ThreadLocalFileContext(FileLockContext, local):
6866
"""
6967
A thread local version of the ``FileLockContext`` class.
7068
"""
7169

72-
pass
73-
7470

7571
class BaseFileLock(ABC, contextlib.ContextDecorator):
7672
"""Abstract base class for a file lock object."""
@@ -91,23 +87,22 @@ def __init__(
9187
to a negative value. A timeout of 0 means, that there is exactly one attempt to acquire the file lock.
9288
:param mode: file permissions for the lockfile.
9389
:param thread_local: Whether this object's internal context should be thread local or not.
94-
If this is set to ``False`` then the lock will be rentrant across threads.
90+
If this is set to ``False`` then the lock will be reentrant across threads.
9591
"""
96-
# Whether or not this object is thread local or not
97-
self.thread_local = thread_local
92+
self._is_thread_local = thread_local
9893

99-
# Create the context. Note that external code should not work with the context directly
100-
# and should instead use properties of this class.
101-
context_kwargs: dict[str, Any] = {
94+
# Create the context. Note that external code should not work with the context directly and should instead use
95+
# properties of this class.
96+
kwargs: dict[str, Any] = {
10297
"lock_file": os.fspath(lock_file),
10398
"timeout": timeout,
10499
"mode": mode,
105100
}
106-
self._context: FileLockContext | ThreadLocalFileContext
107-
if thread_local:
108-
self._context = ThreadLocalFileContext(**context_kwargs)
109-
else:
110-
self._context = FileLockContext(**context_kwargs)
101+
self._context: FileLockContext = (ThreadLocalFileContext if thread_local else FileLockContext)(**kwargs)
102+
103+
def is_thread_local(self) -> bool:
104+
""":return: a flag indicating if this lock is thread local or not"""
105+
return self._is_thread_local
111106

112107
@property
113108
def lock_file(self) -> str:
@@ -177,7 +172,7 @@ def acquire(
177172
:param poll_interval: interval of trying to acquire the lock file
178173
:param poll_intervall: deprecated, kept for backwards compatibility, use ``poll_interval`` instead
179174
:param blocking: defaults to True. If False, function will return immediately if it cannot obtain a lock on the
180-
first attempt. Otherwise this method will block until the timeout expires or the lock is acquired.
175+
first attempt. Otherwise, this method will block until the timeout expires or the lock is acquired.
181176
:raises Timeout: if fails to acquire lock within the timeout period
182177
:return: a context object that will unlock the file when the context is exited
183178
@@ -253,7 +248,7 @@ def release(self, force: bool = False) -> None:
253248

254249
_LOGGER.debug("Attempting to release lock %s on %s", lock_id, lock_filename)
255250
self._release()
256-
self._lock_counter = 0
251+
self._context.lock_counter = 0
257252
_LOGGER.debug("Lock %s released on %s", lock_id, lock_filename)
258253

259254
def __enter__(self) -> BaseFileLock:

src/filelock/_unix.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def _release(self) -> None:
2424
try:
2525
import fcntl
2626
except ImportError:
27-
pass
27+
fcntl = None
2828
else:
2929
has_fcntl = True
3030

src/filelock/_windows.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import msvcrt
1313

1414
class WindowsFileLock(BaseFileLock):
15-
"""Uses the :func:`msvcrt.locking` function to hard lock the lock file on windows systems."""
15+
"""Uses the :func:`msvcrt.locking` function to hard lock the lock file on Windows systems."""
1616

1717
def _acquire(self) -> None:
1818
raise_on_not_writable_file(self.lock_file)
@@ -51,7 +51,7 @@ def _release(self) -> None:
5151
else: # pragma: win32 no cover
5252

5353
class WindowsFileLock(BaseFileLock):
54-
"""Uses the :func:`msvcrt.locking` function to hard lock the lock file on windows systems."""
54+
"""Uses the :func:`msvcrt.locking` function to hard lock the lock file on Windows systems."""
5555

5656
def _acquire(self) -> None:
5757
raise NotImplementedError

0 commit comments

Comments
 (0)