Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hotfix: Restore __init__ method; more robust initialization for singleton locks #338

Merged
merged 14 commits into from
Jun 12, 2024
Merged
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/filelock/_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,11 @@ def __new__( # noqa: PLR0913
) -> Self:
"""Create a new lock object or if specified return the singleton instance for the lock file."""
if not is_singleton:
self = super().__new__(cls)
self._initialize(lock_file, timeout, mode, thread_local, blocking=blocking, is_singleton=is_singleton)
return self
return super().__new__(cls)

instance = cls._instances.get(str(lock_file))
if not instance:
self = super().__new__(cls)
self._initialize(lock_file, timeout, mode, thread_local, blocking=blocking, is_singleton=is_singleton)
cls._instances[str(lock_file)] = self
return self

ethanbb marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -117,7 +114,7 @@ def __init_subclass__(cls, **kwargs: dict[str, Any]) -> None:
super().__init_subclass__(**kwargs)
cls._instances = WeakValueDictionary()

def _initialize( # noqa: PLR0913
def __init__( # noqa: PLR0913
self,
lock_file: str | os.PathLike[str],
timeout: float = -1,
Expand All @@ -143,6 +140,9 @@ def _initialize( # noqa: PLR0913
to pass the same object around.

"""
if hasattr(self, "_context"):
gaborbernat marked this conversation as resolved.
Show resolved Hide resolved
return # bypass initialization because object is already initialized

self._is_thread_local = thread_local
self._is_singleton = is_singleton

Expand Down
Loading