Skip to content

Commit cd942fc

Browse files
committed
fix: python2 compatible and flake8 style
1 parent ae57730 commit cd942fc

File tree

2 files changed

+9
-11
lines changed

2 files changed

+9
-11
lines changed

qiniu/http/regions_provider.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,6 @@ def _file_path(self):
205205
elif is_windows:
206206
import msvcrt
207207

208-
209208
class _FileLocker:
210209
def __init__(self, fd):
211210
self._fd = fd
@@ -235,13 +234,13 @@ def __enter__(self):
235234
open_flags = os.O_EXCL | os.O_RDWR | os.O_CREAT
236235
fd = os.open(self.lock_file_path, open_flags)
237236
os.close(fd)
238-
except FileExistsError:
237+
except IOError:
239238
raise FileAlreadyLocked('File {0} already locked'.format(self._file_path))
240239

241240
def __exit__(self, exc_type, exc_val, exc_tb):
242241
try:
243242
os.remove(self.lock_file_path)
244-
except FileNotFoundError:
243+
except IOError:
245244
pass
246245

247246
@property

tests/cases/test_http/test_regions_provider.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -294,21 +294,20 @@ def test_shrink_with_ignore_expired_regions(self, cached_regions_provider):
294294
list(cached_regions_provider) # trigger __shrink_cache()
295295
assert len(cached_regions_provider._cache_scope.memo_cache[origin_cache_key]) > 0
296296

297-
def test_file_locker(self, temp_file_path):
298-
handled_cnt = 0
299-
skipped_cnt = 0
297+
def test_file_locker(self, temp_file_path, use_ref):
298+
handled_cnt = use_ref(0)
299+
skipped_cnt = use_ref(0)
300300

301301

302302
def process_file(_n):
303-
nonlocal handled_cnt, skipped_cnt
304303
try:
305304
with open(temp_file_path, 'w') as f, _FileThreadingLocker(f), _FileLocker(f):
306305
time.sleep(1)
307-
handled_cnt += 1
306+
handled_cnt.value += 1
308307
except FileAlreadyLocked:
309-
skipped_cnt += 1
308+
skipped_cnt.value += 1
310309

311310

312311
ThreadPool(4).map(process_file, range(20))
313-
assert handled_cnt + skipped_cnt == 20
314-
assert 0 < handled_cnt <= 4
312+
assert handled_cnt.value + skipped_cnt.value == 20
313+
assert 0 < handled_cnt.value <= 4

0 commit comments

Comments
 (0)