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

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

57 statements  

1""" 

2A platform independent file lock that supports the with-statement. 

3 

4.. autodata:: filelock.__version__ 

5 :no-value: 

6 

7.. autodata:: filelock.OwnerMode 

8 

9.. autodata:: filelock.CompromiseReason 

10 

11""" 

12 

13from __future__ import annotations 

14 

15__lazy_modules__ = ["filelock._descriptor", "filelock._error", "importlib"] 

16 

17import sys 

18import warnings 

19from importlib import import_module 

20from typing import TYPE_CHECKING, Final 

21 

22from filelock._api import AcquireReturnProxy, BaseFileLock, CloseErrorPolicy, ContextErrorPolicy, LockOptions 

23from filelock._descriptor import lock_descriptor, unlock_descriptor 

24from filelock._error import LeaseSettingsMismatch, SoftFileLockLifetimeWarning, SoftFileLockProtocolError, Timeout 

25from filelock._unix import UnixFileLock 

26from filelock._unix import has_fcntl as _has_fcntl 

27from filelock._windows import WindowsFileLock 

28 

29# Keeps ``filelock.version`` the string 4.0.12 exposed rather than the submodule. 

30from filelock.version import version 

31 

32if TYPE_CHECKING: 

33 from filelock import asyncio 

34 from filelock._async_read_write import AsyncAcquireReadWriteReturnProxy, AsyncReadWriteLock 

35 from filelock._lease import CompromiseReason, LeaseCompromise, SoftFileLease 

36 from filelock._marker import MarkerSoftFileLock, OwnerMode, OwnerRecord 

37 from filelock._read_write import ReadWriteLock 

38 from filelock._soft import SoftFileLock 

39 from filelock._soft_rw import AsyncAcquireSoftReadWriteReturnProxy, AsyncSoftReadWriteLock, SoftReadWriteLock 

40 from filelock._strict import StrictSoftFileClaim, StrictSoftFileClaimState, StrictSoftFileLock 

41 from filelock.asyncio import ( 

42 AsyncAcquireReturnProxy, 

43 AsyncSoftFileLease, 

44 AsyncSoftFileLock, 

45 AsyncStrictSoftFileLock, 

46 AsyncUnixFileLock, 

47 AsyncWindowsFileLock, 

48 BaseAsyncFileLock, 

49 ) 

50 

51#: version of the project as a string 

52__version__: Final[str] = version 

53 

54#: ``True`` when :func:`fcntl.flock` is available. Off Windows, :data:`FileLock` then resolves to 

55#: :class:`UnixFileLock`, otherwise to :class:`SoftFileLock`. Always ``False`` on Windows. 

56#: 

57#: :meta hide-value: 

58has_fcntl: Final[bool] = _has_fcntl 

59 

60 

61if sys.platform == "win32": # pragma: win32 cover 

62 _FileLock: type[BaseFileLock] = WindowsFileLock 

63else: # pragma: win32 no cover # ruff:ignore[collapsible-else-if] # the else carries the win32 no-cover pragma 

64 if has_fcntl: 

65 _FileLock: type[BaseFileLock] = UnixFileLock 

66 else: 

67 from filelock._soft import SoftFileLock 

68 

69 _FileLock = SoftFileLock 

70 warnings.warn("only soft file lock is available", stacklevel=2) 

71 

72if TYPE_CHECKING: 

73 FileLock = SoftFileLock 

74 AsyncFileLock = AsyncSoftFileLock 

75else: 

76 #: Alias for the lock, which should be used for the current platform. 

77 FileLock = _FileLock 

78 

79# Importing asyncio and sqlite3 costs more than the rest of the package, so names that need them load on first access. 

80_LAZY_ATTRIBUTES: Final[dict[str, str]] = { 

81 "AsyncAcquireReadWriteReturnProxy": "._async_read_write", 

82 "AsyncAcquireReturnProxy": ".asyncio", 

83 "AsyncAcquireSoftReadWriteReturnProxy": "._soft_rw", 

84 "AsyncFileLock": ".asyncio", 

85 "AsyncReadWriteLock": "._async_read_write", 

86 "AsyncSoftFileLease": ".asyncio", 

87 "AsyncSoftFileLock": ".asyncio", 

88 "AsyncSoftReadWriteLock": "._soft_rw", 

89 "AsyncStrictSoftFileLock": ".asyncio", 

90 "AsyncUnixFileLock": ".asyncio", 

91 "AsyncWindowsFileLock": ".asyncio", 

92 "BaseAsyncFileLock": ".asyncio", 

93 "CompromiseReason": "._lease", 

94 "LeaseCompromise": "._lease", 

95 "MarkerSoftFileLock": "._marker", 

96 "OwnerMode": "._marker", 

97 "OwnerRecord": "._marker", 

98 "ReadWriteLock": "._read_write", 

99 "SoftFileLease": "._lease", 

100 "SoftFileLock": "._soft", 

101 "SoftReadWriteLock": "._soft_rw", 

102 "StrictSoftFileClaim": "._strict", 

103 "StrictSoftFileClaimState": "._strict", 

104 "StrictSoftFileLock": "._strict", 

105 "asyncio": ".asyncio", 

106} 

107 

108 

109def __getattr__(name: str) -> object: 

110 if (module_name := _LAZY_ATTRIBUTES.get(name)) is None: 

111 msg = f"module {__name__!r} has no attribute {name!r}" 

112 raise AttributeError(msg) 

113 try: 

114 module = import_module(module_name, __name__) 

115 except ImportError as error: 

116 # Only a missing sqlite3 module (the package, CPython's _sqlite3, PyPy's _sqlite3_cffi) leaves the read-write 

117 # locks None; a frozen application that left out a filelock module gets the error that names it. 

118 sqlite_missing = (error.name or "").startswith(("sqlite3", "_sqlite3")) 

119 if module_name not in {"._read_write", "._async_read_write"} or not sqlite_missing: 

120 raise 

121 value = None # pragma: lacks sqlite3 

122 else: 

123 if name == "asyncio": 

124 value = module 

125 else: 

126 value = getattr(module, f"Async{_FileLock.__name__}" if name == "AsyncFileLock" else name) 

127 globals()[name] = value 

128 return value 

129 

130 

131def __dir__() -> list[str]: 

132 return sorted({*globals(), *_LAZY_ATTRIBUTES}) 

133 

134 

135__all__ = [ 

136 "AcquireReturnProxy", 

137 "AsyncAcquireReadWriteReturnProxy", 

138 "AsyncAcquireReturnProxy", 

139 "AsyncAcquireSoftReadWriteReturnProxy", 

140 "AsyncFileLock", 

141 "AsyncReadWriteLock", 

142 "AsyncSoftFileLease", 

143 "AsyncSoftFileLock", 

144 "AsyncSoftReadWriteLock", 

145 "AsyncStrictSoftFileLock", 

146 "AsyncUnixFileLock", 

147 "AsyncWindowsFileLock", 

148 "BaseAsyncFileLock", 

149 "BaseFileLock", 

150 "CloseErrorPolicy", 

151 "CompromiseReason", 

152 "ContextErrorPolicy", 

153 "FileLock", 

154 "LeaseCompromise", 

155 "LeaseSettingsMismatch", 

156 "LockOptions", 

157 "MarkerSoftFileLock", 

158 "OwnerMode", 

159 "OwnerRecord", 

160 "ReadWriteLock", 

161 "SoftFileLease", 

162 "SoftFileLock", 

163 "SoftFileLockLifetimeWarning", 

164 "SoftFileLockProtocolError", 

165 "SoftReadWriteLock", 

166 "StrictSoftFileClaim", 

167 "StrictSoftFileClaimState", 

168 "StrictSoftFileLock", 

169 "Timeout", 

170 "UnixFileLock", 

171 "WindowsFileLock", 

172 "__version__", 

173 "asyncio", 

174 "has_fcntl", 

175 "lock_descriptor", 

176 "unlock_descriptor", 

177]