Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/filelock/_error.py: 53%

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

43 statements  

1from __future__ import annotations 

2 

3__lazy_modules__ = ["typing"] 

4 

5from typing import Final 

6 

7 

8class Timeout(TimeoutError): # ruff:ignore[error-suffix-on-exception-name] # public exception name; renaming breaks the API 

9 """Raised when the lock could not be acquired in *timeout* seconds.""" 

10 

11 def __init__(self, lock_file: str, holder: str | None = None) -> None: 

12 super().__init__() 

13 self._lock_file = lock_file 

14 self._holder = holder 

15 

16 def __reduce__(self) -> tuple[type[Timeout], tuple[str], dict[str, object]]: 

17 # holder rides in the state so a subclass taking only lock_file, and a 4.0.12 reader, can still unpickle it. 

18 return self.__class__, (self._lock_file,), self.__dict__ 

19 

20 def __str__(self) -> str: # pragma: needs hard-link 

21 message: Final = f"The file lock '{self._lock_file}' could not be acquired." 

22 if self._holder is None: 

23 return message 

24 return f"{message} It is held by {self._holder}, which this host cannot probe." 

25 

26 def __repr__(self) -> str: 

27 return f"{self.__class__.__name__}({self.lock_file!r})" 

28 

29 @property 

30 def lock_file(self) -> str: 

31 """The path of the file lock.""" 

32 return self._lock_file 

33 

34 

35class SoftFileLockLifetimeWarning(FutureWarning): 

36 """ 

37 The configured soft-lock lifetime permits overlapping live holders after expiry. 

38 

39 A :class:`FutureWarning` shows by default wherever the lock is configured; a :class:`DeprecationWarning` shows only 

40 in ``__main__``. 

41 

42 """ 

43 

44 

45class LeaseSettingsMismatch(ValueError): # ruff:ignore[error-suffix-on-exception-name] # public exception name; renaming breaks the API 

46 """A lease contender disagrees with the published claim about how long the lease lasts.""" 

47 

48 

49class SoftFileLockProtocolError(OSError): 

50 """Raised when strict soft-lock state cannot be interpreted without risking overlap.""" 

51 

52 def __init__(self, lock_file: str, claim_name: str | None, reason: str) -> None: 

53 self._lock_file = lock_file 

54 self._claim_name = claim_name 

55 self._reason = reason 

56 super().__init__(self.__str__()) 

57 

58 def __reduce__( 

59 self, 

60 ) -> tuple[ 

61 type[SoftFileLockProtocolError], tuple[str, str | None, str], dict[str, object] 

62 ]: # pragma: needs hard-link 

63 return self.__class__, (self._lock_file, self._claim_name, self._reason), self.__dict__ 

64 

65 def __str__(self) -> str: 

66 location = self._lock_file if self._claim_name is None else f"{self._lock_file}: claim {self._claim_name!r}" 

67 return f"Invalid strict soft-lock state at {location}: {self._reason}" 

68 

69 @property 

70 def lock_file(self) -> str: # pragma: needs hard-link 

71 """The requested lock path.""" 

72 return self._lock_file 

73 

74 @property 

75 def claim_name(self) -> str | None: # pragma: needs hard-link 

76 """The claim that caused the error, if scanning identified one.""" 

77 return self._claim_name 

78 

79 @property 

80 def reason(self) -> str: # pragma: needs hard-link 

81 """The protocol validation failure.""" 

82 return self._reason 

83 

84 

85__all__ = [ 

86 "LeaseSettingsMismatch", 

87 "SoftFileLockLifetimeWarning", 

88 "SoftFileLockProtocolError", 

89 "Timeout", 

90]