Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/filelock/_soft.py: 41%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1from __future__ import annotations
3import os
4import sys
5from contextlib import suppress
6from errno import EACCES, EEXIST
7from pathlib import Path
9from ._api import BaseFileLock
10from ._util import ensure_directory_exists, raise_on_not_writable_file
13class SoftFileLock(BaseFileLock):
14 """Simply watches the existence of the lock file."""
16 def _acquire(self) -> None:
17 raise_on_not_writable_file(self.lock_file)
18 ensure_directory_exists(self.lock_file)
19 flags = (
20 os.O_WRONLY # open for writing only
21 | os.O_CREAT
22 | os.O_EXCL # together with above raise EEXIST if the file specified by filename exists
23 | os.O_TRUNC # truncate the file to zero byte
24 )
25 o_nofollow = getattr(os, "O_NOFOLLOW", None)
26 if o_nofollow is not None:
27 flags |= o_nofollow
28 try:
29 file_handler = os.open(self.lock_file, flags, self._context.mode)
30 except OSError as exception: # re-raise unless expected exception
31 if not (
32 exception.errno == EEXIST # lock already exist
33 or (exception.errno == EACCES and sys.platform == "win32") # has no access to this lock
34 ): # pragma: win32 no cover
35 raise
36 else:
37 self._context.lock_file_fd = file_handler
39 def _release(self) -> None:
40 assert self._context.lock_file_fd is not None # noqa: S101
41 os.close(self._context.lock_file_fd) # the lock file is definitely not None
42 self._context.lock_file_fd = None
43 with suppress(OSError): # the file is already deleted and that's what we want
44 Path(self.lock_file).unlink()
47__all__ = [
48 "SoftFileLock",
49]