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 """ 

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

11 

12 :param lock_file: the path of the file lock 

13 :param holder: a description of the lock's holder, when one is recorded and this host cannot probe it 

14 """ 

15 

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

17 super().__init__() 

18 self._lock_file = lock_file 

19 self._holder = holder 

20 

21 def __reduce__( 

22 self, 

23 ) -> tuple[type[Timeout], tuple[str], dict[str, object]]: # simplint: ignore[SMP602] # __dict__ holds any value 

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

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

26 

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

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

29 if self._holder is None: 

30 return message 

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

32 

33 def __repr__(self) -> str: 

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

35 

36 @property 

37 def lock_file(self) -> str: 

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

39 return self._lock_file 

40 

41 

42class SoftFileLockLifetimeWarning(FutureWarning): 

43 """ 

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

45 

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

47 in ``__main__``. 

48 

49 """ 

50 

51 

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

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

54 

55 

56class SoftFileLockProtocolError(OSError): 

57 """ 

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

59 

60 :param lock_file: the path of the lock 

61 :param claim_name: the name of the claim the state belongs to, or ``None`` when it concerns the lock as a whole 

62 :param reason: why the state cannot be interpreted 

63 """ 

64 

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

66 self._lock_file = lock_file 

67 self._claim_name = claim_name 

68 self._reason = reason 

69 super().__init__(self.__str__()) 

70 

71 def __str__(self) -> str: 

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

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

74 

75 def __reduce__( 

76 self, 

77 ) -> tuple[ 

78 type[SoftFileLockProtocolError], 

79 tuple[str, str | None, str], 

80 dict[str, object], # simplint: ignore[SMP602] # __dict__ holds any value 

81 ]: # pragma: needs hard-link 

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

83 

84 @property 

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

86 """The requested lock path.""" 

87 return self._lock_file 

88 

89 @property 

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

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

92 return self._claim_name 

93 

94 @property 

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

96 """The protocol validation failure.""" 

97 return self._reason 

98 

99 

100__all__ = [ 

101 "LeaseSettingsMismatch", 

102 "SoftFileLockLifetimeWarning", 

103 "SoftFileLockProtocolError", 

104 "Timeout", 

105]