Skip to content

Commit edee203

Browse files
committed
Rework writing protection
- Set `_writing` to prevent `open('w').write()` during repacking. - Move the protection logic to `ZipFile.repack()`.
1 parent ae01b8c commit edee203

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

Lib/zipfile/__init__.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1442,10 +1442,6 @@ def repack(self, zfile):
14421442
[recorded local file entry 1]
14431443
...
14441444
"""
1445-
with zfile._lock:
1446-
self._repack(zfile)
1447-
1448-
def _repack(self, zfile):
14491445
fp = zfile.fp
14501446

14511447
# get a sorted filelist by header offset, in case the dir order
@@ -2271,7 +2267,12 @@ def repack(self, **opts):
22712267
"Can't write to ZIP archive while an open writing handle exists"
22722268
)
22732269

2274-
_ZipRepacker(**opts).repack(self)
2270+
with self._lock:
2271+
self._writing = True
2272+
try:
2273+
_ZipRepacker(**opts).repack(self)
2274+
finally:
2275+
self._writing = False
22752276

22762277
@classmethod
22772278
def _sanitize_windows_name(cls, arcname, pathsep):

0 commit comments

Comments
 (0)