From 77e0eb25755c3952e5892e02c42588e20d84bca7 Mon Sep 17 00:00:00 2001 From: Tony123-tech Date: Wed, 30 Sep 2026 20:40:11 +0800 Subject: [PATCH] gh-158494: zipimport: raise ZipImportError for corrupt deflate data --- Lib/test/test_zipimport.py | 23 +++++++++++++++++++ Lib/zipimport.py | 5 +++- ...-09-30-19-30-00.gh-issue-158494.AbCdEf.rst | 2 ++ 3 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2026-09-30-19-30-00.gh-issue-158494.AbCdEf.rst diff --git a/Lib/test/test_zipimport.py b/Lib/test/test_zipimport.py index 76cd85709a63af8..0c14d11825bc48a 100644 --- a/Lib/test/test_zipimport.py +++ b/Lib/test/test_zipimport.py @@ -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. + # 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("