Skip to content
Open
Show file tree
Hide file tree
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
23 changes: 23 additions & 0 deletions Lib/test/test_zipimport.py
Original file line number Diff line number Diff line change
Expand Up @@ -1021,6 +1021,29 @@ def assertDataEntry(name):
class DeflateCompressedZipImportTestCase(UncompressedZipImportTestCase):
compression = ZIP_DEFLATED

def testCorruptDeflateDataRaisesZipImportError(self):
# gh-158494: a corrupt deflate stream in a .pyc must surface as.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# gh-158494: a corrupt deflate stream in a .pyc must surface as.
# gh-158494: a corrupt deflate stream in a .pyc must surface as

# ZipImportError, not zlib.error.
files = {TESTMOD + pyc_ext: test_pyc}
self.makeZip(files)

# Corrupt the deflate stream of the .pyc entry in place.
with open(TEMP_ZIP, "r+b") as f:
data = f.read()
idx = data.find((TESTMOD + pyc_ext).encode())
lh = data.rfind(b"PK\x03\x04")
name_len, extra_len = struct.unpack_from("<HH", data, lh + 26)
data_start = lh + 30 + name_len + extra_len
f.seek(data_start)
f.write(b"\xff" * 8)

zipimport._zip_directory_cache.clear()
sys.path.insert(0, TEMP_ZIP)
self.addCleanup(sys.modules.pop, TESTMOD, None)
importlib.invalidate_caches()
Comment on lines +1040 to +1043

@aisk aisk Oct 1, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
zipimport._zip_directory_cache.clear()
sys.path.insert(0, TEMP_ZIP)
self.addCleanup(sys.modules.pop, TESTMOD, None)
importlib.invalidate_caches()
sys.path.insert(0, TEMP_ZIP)

These lines are not nessesary.


with self.assertRaises(zipimport.ZipImportError):
importlib.import_module(TESTMOD)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

@support.requires_zstd()
class ZStdCompressedZipImportTestCase(UncompressedZipImportTestCase):
Expand Down
5 changes: 4 additions & 1 deletion Lib/zipimport.py
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,10 @@ def _get_data(archive, toc_entry):
decompress = _get_zlib_decompress_func()
except Exception:
raise ZipImportError("can't decompress data; zlib not available")
return decompress(raw_data, -15)
try:
return decompress(raw_data, -15)
except Exception:
raise ZipImportError("could not decompress deflate data")
case 93: # zstd
try:
return _zstd_decompress(raw_data)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix :mod:`zipimport` to raise :exc:`zipimport.ZipImportError` instead of
leaking :exc:`zlib.error` when a zip entry's deflate stream is corrupt.
Loading