Skip to content

Commit cbcaea1

Browse files
committed
Address review: materialize removed so a one-shot iterable still repacks
1 parent 130e1dd commit cbcaea1

2 files changed

Lines changed: 23 additions & 0 deletions

File tree

‎Lib/test/test_zipfile/test_core.py‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2662,6 +2662,24 @@ def test_overlap_with_archive_comment(self):
26622662
fz.seek(0)
26632663
self.assertEqual(fz.read(), expected)
26642664

2665+
def test_repack_removed_iterator(self):
2666+
# A one-shot iterable of removed entries must still be repacked.
2667+
buf = io.BytesIO()
2668+
with zipfile.ZipFile(buf, 'w') as zh:
2669+
for name in ['a.txt', 'b.txt', 'c.txt']:
2670+
zh.writestr(name, name[0].upper().encode() * 5000)
2671+
full_size = len(buf.getvalue())
2672+
2673+
fz = io.BytesIO(buf.getvalue())
2674+
with zipfile.ZipFile(fz, 'a') as zh:
2675+
zh.repack(iter([zh.remove('b.txt')]))
2676+
2677+
self.assertLess(len(fz.getvalue()), full_size - 5000)
2678+
fz.seek(0)
2679+
with zipfile.ZipFile(fz) as zh:
2680+
self.assertIsNone(zh.testzip())
2681+
self.assertEqual(zh.namelist(), ['a.txt', 'c.txt'])
2682+
26652683
def test_repack_failed_write_restores_header_offsets(self):
26662684
# A failed repack() must leave the in-memory offsets describing the
26672685
# file that is actually on disk, so that a later close() does not

‎Lib/zipfile/__init__.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2434,6 +2434,11 @@ def repack(self, removed=None, *, strict_descriptor=True,
24342434
)
24352435

24362436
self._writing = True
2437+
# *removed* is read twice below, so a one-shot iterable must not
2438+
# be consumed by the first read. None (scan the archive) is not
2439+
# the same as an empty sequence, so keep it as is.
2440+
if removed is not None:
2441+
removed = list(removed)
24372442
header_offsets = [(zinfo, zinfo.header_offset)
24382443
for zinfo in (*self.filelist, *(removed or ()))]
24392444
try:

0 commit comments

Comments
 (0)