Skip to content

Commit a04d423

Browse files
committed
Refactor
1 parent 4709825 commit a04d423

File tree

1 file changed

+55
-53
lines changed

1 file changed

+55
-53
lines changed

bpython/filelock.py

Lines changed: 55 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,6 @@
2323
from typing import IO, Literal
2424
from types import TracebackType
2525

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

4127
class BaseLock:
4228
"""Base class for file locking"""
@@ -69,56 +55,72 @@ def __del__(self) -> None:
6955
self.release()
7056

7157

72-
class UnixFileLock(BaseLock):
73-
"""Simple file locking for Unix using fcntl"""
58+
try:
59+
import fcntl
60+
import errno
7461

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

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
8769

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
9177

78+
def release(self) -> None:
79+
self.locked = False
80+
fcntl.flock(self.fileobj, fcntl.LOCK_UN)
9281

93-
class WindowsFileLock(BaseLock):
94-
"""Simple file locking for Windows using msvcrt"""
82+
has_fcntl = True
83+
except ImportError:
84+
has_fcntl = False
9585

96-
def __init__(self, filename: str) -> None:
97-
super().__init__()
98-
self.filename = f"{filename}.lock"
99-
self.fileobj = -1
10086

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
10790

108-
self.locked = True
91+
class WindowsFileLock(BaseLock):
92+
"""Simple file locking for Windows using msvcrt"""
10993

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
112107

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
117110

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
122124

123125

124126
def FileLock(

0 commit comments

Comments
 (0)