|
23 | 23 | from typing import IO, Literal |
24 | 24 | from types import TracebackType |
25 | 25 |
|
26 | | -has_fcntl = True |
27 | | -try: |
28 | | - import fcntl |
29 | | - import errno |
30 | | -except ImportError: |
31 | | - has_fcntl = False |
32 | | - |
33 | | -has_msvcrt = True |
34 | | -try: |
35 | | - import msvcrt |
36 | | - import os |
37 | | -except ImportError: |
38 | | - has_msvcrt = False |
39 | | - |
40 | 26 |
|
41 | 27 | class BaseLock: |
42 | 28 | """Base class for file locking""" |
@@ -69,56 +55,72 @@ def __del__(self) -> None: |
69 | 55 | self.release() |
70 | 56 |
|
71 | 57 |
|
72 | | -class UnixFileLock(BaseLock): |
73 | | - """Simple file locking for Unix using fcntl""" |
| 58 | +try: |
| 59 | + import fcntl |
| 60 | + import errno |
74 | 61 |
|
75 | | - def __init__(self, fileobj, mode: int = 0) -> None: |
76 | | - super().__init__() |
77 | | - self.fileobj = fileobj |
78 | | - self.mode = mode | fcntl.LOCK_EX |
| 62 | + class UnixFileLock(BaseLock): |
| 63 | + """Simple file locking for Unix using fcntl""" |
79 | 64 |
|
80 | | - def acquire(self) -> None: |
81 | | - try: |
82 | | - fcntl.flock(self.fileobj, self.mode) |
83 | | - self.locked = True |
84 | | - except OSError as e: |
85 | | - if e.errno != errno.ENOLCK: |
86 | | - raise e |
| 65 | + def __init__(self, fileobj, mode: int = 0) -> None: |
| 66 | + super().__init__() |
| 67 | + self.fileobj = fileobj |
| 68 | + self.mode = mode | fcntl.LOCK_EX |
87 | 69 |
|
88 | | - def release(self) -> None: |
89 | | - self.locked = False |
90 | | - fcntl.flock(self.fileobj, fcntl.LOCK_UN) |
| 70 | + def acquire(self) -> None: |
| 71 | + try: |
| 72 | + fcntl.flock(self.fileobj, self.mode) |
| 73 | + self.locked = True |
| 74 | + except OSError as e: |
| 75 | + if e.errno != errno.ENOLCK: |
| 76 | + raise e |
91 | 77 |
|
| 78 | + def release(self) -> None: |
| 79 | + self.locked = False |
| 80 | + fcntl.flock(self.fileobj, fcntl.LOCK_UN) |
92 | 81 |
|
93 | | -class WindowsFileLock(BaseLock): |
94 | | - """Simple file locking for Windows using msvcrt""" |
| 82 | + has_fcntl = True |
| 83 | +except ImportError: |
| 84 | + has_fcntl = False |
95 | 85 |
|
96 | | - def __init__(self, filename: str) -> None: |
97 | | - super().__init__() |
98 | | - self.filename = f"{filename}.lock" |
99 | | - self.fileobj = -1 |
100 | 86 |
|
101 | | - def acquire(self) -> None: |
102 | | - # create a lock file and lock it |
103 | | - self.fileobj = os.open( |
104 | | - self.filename, os.O_RDWR | os.O_CREAT | os.O_TRUNC |
105 | | - ) |
106 | | - msvcrt.locking(self.fileobj, msvcrt.LK_NBLCK, 1) |
| 87 | +try: |
| 88 | + import msvcrt |
| 89 | + import os |
107 | 90 |
|
108 | | - self.locked = True |
| 91 | + class WindowsFileLock(BaseLock): |
| 92 | + """Simple file locking for Windows using msvcrt""" |
109 | 93 |
|
110 | | - def release(self) -> None: |
111 | | - self.locked = False |
| 94 | + def __init__(self, filename: str) -> None: |
| 95 | + super().__init__() |
| 96 | + self.filename = f"{filename}.lock" |
| 97 | + self.fileobj = -1 |
| 98 | + |
| 99 | + def acquire(self) -> None: |
| 100 | + # create a lock file and lock it |
| 101 | + self.fileobj = os.open( |
| 102 | + self.filename, os.O_RDWR | os.O_CREAT | os.O_TRUNC |
| 103 | + ) |
| 104 | + msvcrt.locking(self.fileobj, msvcrt.LK_NBLCK, 1) |
| 105 | + |
| 106 | + self.locked = True |
112 | 107 |
|
113 | | - # unlock lock file and remove it |
114 | | - msvcrt.locking(self.fileobj, msvcrt.LK_UNLCK, 1) |
115 | | - os.close(self.fileobj) |
116 | | - self.fileobj = -1 |
| 108 | + def release(self) -> None: |
| 109 | + self.locked = False |
117 | 110 |
|
118 | | - try: |
119 | | - os.remove(self.filename) |
120 | | - except OSError: |
121 | | - pass |
| 111 | + # unlock lock file and remove it |
| 112 | + msvcrt.locking(self.fileobj, msvcrt.LK_UNLCK, 1) |
| 113 | + os.close(self.fileobj) |
| 114 | + self.fileobj = -1 |
| 115 | + |
| 116 | + try: |
| 117 | + os.remove(self.filename) |
| 118 | + except OSError: |
| 119 | + pass |
| 120 | + |
| 121 | + has_msvcrt = True |
| 122 | +except ImportError: |
| 123 | + has_msvcrt = False |
122 | 124 |
|
123 | 125 |
|
124 | 126 | def FileLock( |
|
0 commit comments