Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/filelock/_soft.py: 42%

24 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-07 07:11 +0000

1from __future__ import annotations 

2 

3import os 

4import sys 

5from errno import EACCES, EEXIST 

6 

7from ._api import BaseFileLock 

8from ._util import raise_on_not_writable_file 

9 

10 

11class SoftFileLock(BaseFileLock): 

12 """Simply watches the existence of the lock file.""" 

13 

14 def _acquire(self) -> None: 

15 raise_on_not_writable_file(self.lock_file) 

16 # first check for exists and read-only mode as the open will mask this case as EEXIST 

17 flags = ( 

18 os.O_WRONLY # open for writing only 

19 | os.O_CREAT 

20 | os.O_EXCL # together with above raise EEXIST if the file specified by filename exists 

21 | os.O_TRUNC # truncate the file to zero byte 

22 ) 

23 try: 

24 file_handler = os.open(self.lock_file, flags, self._context.mode) 

25 except OSError as exception: # re-raise unless expected exception 

26 if not ( 

27 exception.errno == EEXIST # lock already exist 

28 or (exception.errno == EACCES and sys.platform == "win32") # has no access to this lock 

29 ): # pragma: win32 no cover 

30 raise 

31 else: 

32 self._context.lock_file_fd = file_handler 

33 

34 def _release(self) -> None: 

35 os.close(self._context.lock_file_fd) # type: ignore # the lock file is definitely not None 

36 self._context.lock_file_fd = None 

37 try: 

38 os.remove(self.lock_file) 

39 except OSError: # the file is already deleted and that's what we want 

40 pass 

41 

42 

43__all__ = [ 

44 "SoftFileLock", 

45]