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("