@@ -182,6 +182,17 @@ def _attempts_str(wait_time, nattempts):
182182class LockType :
183183 READ = 0
184184 WRITE = 1
185+ if is_windows :
186+ LOCK_EX = win32con .LOCKFILE_EXCLUSIVE_LOCK # exclusive lock
187+ LOCK_SH = 0 # shared lock, default
188+ LOCK_NB = win32con .LOCKFILE_FAIL_IMMEDIATELY # non-blocking
189+ LOCK_CATCH = pywintypes .error
190+ else :
191+ LOCK_EX = fcntl .LOCK_EX
192+ LOCK_SH = fcntl .LOCK_SH
193+ LOCK_NB = fcntl .LOCK_NB
194+ LOCK_UN = fcntl .LOCK_UN
195+ LOCK_CATCH = IOError
185196
186197 @staticmethod
187198 def to_str (tid ):
@@ -192,9 +203,9 @@ def to_str(tid):
192203
193204 @staticmethod
194205 def to_module (tid ):
195- lock = fcntl .LOCK_SH
206+ lock = LockType .LOCK_SH
196207 if tid == LockType .WRITE :
197- lock = fcntl .LOCK_EX
208+ lock = LockType .LOCK_EX
198209 return lock
199210
200211 @staticmethod
@@ -295,18 +306,6 @@ def __init__(
295306 self .host : Optional [str ] = None
296307 self .old_host : Optional [str ] = None
297308
298- if is_windows :
299- self .LOCK_EX = win32con .LOCKFILE_EXCLUSIVE_LOCK # exclusive lock
300- self .LOCK_SH = 0 # shared lock, default
301- self .LOCK_NB = win32con .LOCKFILE_FAIL_IMMEDIATELY # non-blocking
302- self .LOCK_CATCH = pywintypes .error
303- else :
304- self .LOCK_EX = fcntl .LOCK_EX
305- self .LOCK_SH = fcntl .LOCK_SH
306- self .LOCK_NB = fcntl .LOCK_NB
307- self .LOCK_UN = fcntl .LOCK_UN
308- self .LOCK_CATCH = IOError
309-
310309 # Mapping of supported locks to description
311310 self .lock_type = {self .LOCK_SH : 'read' , self .LOCK_EX : 'write' }
312311 self ._current_lock = None
@@ -385,7 +384,7 @@ def _lock(self, op: int, timeout: Optional[float] = None) -> Tuple[float, int]:
385384 self ._ensure_parent_directory ()
386385 self ._file = FILE_TRACKER .get_fh (self .path )
387386
388- if LockType .to_module (op ) == fcntl .LOCK_EX and self ._file .mode == "rb" :
387+ if LockType .to_module (op ) == LockType .LOCK_EX and self ._file .mode == "rb" :
389388 # Attempt to upgrade to write lock w/a read-only file.
390389 # If the file were writable, we'd have opened it rb+
391390 raise LockROFileError (self .path )
0 commit comments