MCPcopy Create free account
hub / github.com/tox-dev/filelock / __call__

Method __call__

src/filelock/_read_write.py:193–238  ·  view source on GitHub ↗
(
        cls,
        lock_file: str | os.PathLike[str],
        timeout: float = -1,
        *,
        blocking: bool = True,
        is_singleton: bool = True,
    )

Source from the content-addressed store, hash-verified

191 _instances_under_construction: set[pathlib.Path]
192
193 def __call__(
194 cls,
195 lock_file: str | os.PathLike[str],
196 timeout: float = -1,
197 *,
198 blocking: bool = True,
199 is_singleton: bool = True,
200 ) -> ReadWriteLock:
201 _ensure_current_process()
202 if cls._instances_pid != _GETPID():
203 cls._reset_class_after_fork()
204 construction_pid = _GETPID()
205 if not is_singleton:
206 instance = super().__call__(lock_file, timeout, blocking=blocking, is_singleton=is_singleton)
207 if _GETPID() != construction_pid: # pragma: forked child
208 msg = "ReadWriteLock construction cannot continue after fork"
209 raise RuntimeError(msg)
210 return instance
211
212 normalized = pathlib.Path(lock_file).resolve()
213 with cls._instances_lock:
214 if normalized not in cls._instances:
215 if normalized in cls._instances_under_construction: # pragma: no cover - exercised in an audit callback
216 msg = f"Singleton lock construction is already active for {lock_file!s}"
217 raise RuntimeError(msg)
218 construction_registry = cls._instances_under_construction
219 construction_registry.add(normalized)
220 try:
221 instance = super().__call__(lock_file, timeout, blocking=blocking, is_singleton=is_singleton)
222 finally:
223 if _GETPID() == construction_pid:
224 construction_registry.discard(normalized)
225 if _GETPID() != construction_pid:
226 msg = "ReadWriteLock construction cannot continue after fork"
227 raise RuntimeError(msg)
228 cls._instances[normalized] = instance
229 else:
230 instance = cls._instances[normalized]
231
232 if instance.timeout != timeout or instance.blocking != blocking:
233 msg = (
234 f"Singleton lock created with timeout={instance.timeout}, blocking={instance.blocking},"
235 f" cannot be changed to timeout={timeout}, blocking={blocking}"
236 )
237 raise ValueError(msg)
238 return instance
239
240 def _reset_class_after_fork(cls) -> None: # pragma: forked child
241 cls._instances = WeakValueDictionary()

Callers

nothing calls this directly

Calls 2

_ensure_current_processFunction · 0.85

Tested by

no test coverage detected