Skip to content

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

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

Merged
merged 14 commits into from
Jun 12, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Fix previous commit
  • Loading branch information
ethanbb committed Jun 12, 2024
commit fa8fb4e41124bd39e2c6e89d28f6b5bb86732c71
13 changes: 5 additions & 8 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

Expand All @@ -117,10 +114,7 @@ def __init_subclass__(cls, **kwargs: dict[str, Any]) -> None:
super().__init_subclass__(**kwargs)
cls._instances = WeakValueDictionary()

def __init__(self, *args, **kwargs):
pass # for backwards compatibility (don't break super().__init__ calls)

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

"""
if hasattr(self, '_context'):
return # bypass initialization because object is already initialized

self._is_thread_local = thread_local
self._is_singleton = is_singleton

Expand Down