gh-154842: Reject repack() while a reading handle is open (GH-154843) · python/cpython@7a845ce · GitHub
Skip to content

Commit 7a845ce

Browse files
authored
gh-154842: Reject repack() while a reading handle is open (GH-154843)
ZipFile.repack() moves member data, but a ZipExtFile from an earlier open() keeps its own absolute position, so it silently returned data from the wrong place and a full read failed with a misleading CRC error. Raise ValueError while _fileRefCnt shows an open reading handle, as the writing-handle case already does.
1 parent 5b96d39 commit 7a845ce

3 files changed

Lines changed: 23 additions & 8 deletions

File tree

Doc/library/zipfile.rst

Lines changed: 3 additions & 1 deletion

Lib/test/test_zipfile/test_core.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2388,6 +2388,18 @@ def test_repack_writing(self, m_repack):
23882388
zh.repack()
23892389
m_repack.assert_not_called()
23902390

2391+
@mock.patch.object(zipfile, '_ZipRepacker')
2392+
def test_repack_reading(self, m_repack):
2393+
self._prepare_zip_from_test_files(TESTFN, self.test_files)
2394+
with zipfile.ZipFile(TESTFN, 'a') as zh:
2395+
with zh.open(self.test_files[0][0]):
2396+
with self.assertRaises(ValueError):
2397+
zh.repack()
2398+
m_repack.assert_not_called()
2399+
# Allowed once the reading handle is closed.
2400+
zh.repack()
2401+
m_repack.assert_called_once()
2402+
23912403
@mock.patch.object(zipfile, '_ZipRepacker')
23922404
def test_repack_mode_r(self, m_repack):
23932405
self._prepare_zip_from_test_files(TESTFN, self.test_files)

Lib/zipfile/__init__.py

Lines changed: 8 additions & 7 deletions

0 commit comments

Comments
 (0)