Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions qiniu/http/regions_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,19 @@
self._origin_file = origin_file

def __enter__(self):
if os.access(self.lock_file_path, os.R_OK | os.W_OK):
try:
# Atomic file creation
open_flags = os.O_EXCL | os.O_RDWR | os.O_CREAT
fd = os.open(self.lock_file_path, open_flags)
os.close(fd)
except FileExistsError:
raise FileAlreadyLocked('File {0} already locked'.format(self._origin_file))

Check warning on line 151 in qiniu/http/regions_provider.py

View check run for this annotation

qiniu-x / pylint

qiniu/http/regions_provider.py#L151

Consider explicitly re-raising using 'except FileExistsError as exc' and 'raise FileAlreadyLocked('File {0} already locked'.format(self._origin_file)) from exc' (raise-missing-from)

Check warning on line 151 in qiniu/http/regions_provider.py

View check run for this annotation

qiniu-x / pylint

qiniu/http/regions_provider.py#L151

Formatting a regular string which could be an f-string (consider-using-f-string)
with open(self.lock_file_path, 'w'):
pass

def __exit__(self, exc_type, exc_val, exc_tb):
os.remove(self.lock_file_path)
try:
os.remove(self.lock_file_path)
except FileNotFoundError:
pass # Lock file might have been removed

@property
def lock_file_path(self):
Expand Down