From bc9163e0e36808045d7e5d6b1d71fd39466fc4fa Mon Sep 17 00:00:00 2001 From: TINGyu123644 Date: Fri, 27 Nov 2026 10:56:51 +0800 Subject: [PATCH] gh-162710: forward optimize level in PyZipFile.writepy() _get_code() compiled with the builder interpreter's optimization level instead of the level requested via PyZipFile(optimize=...). Pass _optimize through to source_to_code() so archived bytecode matches the requested level. Add a regression test. --- Lib/test/test_zipfile/test_core.py | 29 +++++++++++++++++++ Lib/zipfile/__init__.py | 3 +- ...6-11-27-0239-00.gh-issue-162710.Kx7Wtq.rst | 1 + 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2026-11-27-0239-00.gh-issue-162710.Kx7Wtq.rst diff --git a/Lib/test/test_zipfile/test_core.py b/Lib/test/test_zipfile/test_core.py index ccb8b87023eb5e6..07c8f63392827e4 100644 --- a/Lib/test/test_zipfile/test_core.py +++ b/Lib/test/test_zipfile/test_core.py @@ -3697,6 +3697,35 @@ def test_write_with_optimization(self): self.assertIn('email/__init__' + ext, names) self.assertIn('email/mime/text' + ext, names) + def test_write_with_optimization_level(self): + # gh-162710: writepy() must compile with the requested optimization + # level even when it differs from the builder interpreter's level. + source = ( + '"""module docs"""\n' + 'DEBUG = __debug__\n' + ) + with temp_cwd(None): + with open(TESTFN, "w", encoding="utf-8") as f: + f.write(source) + archive = TESTFN + ".zip" + for level in (0, 1, 2): + with zipfile.PyZipFile(archive, "w", optimize=level) as zipfp: + zipfp.writepy(TESTFN) + path = archive + os.sep + os.path.basename(TESTFN) + "c" + ns = {} + with zipfile.ZipFile(archive) as zipfp: + exec(zipfp.read(os.path.basename(TESTFN) + "c"), ns) + if level == 0: + self.assertIs(ns["DEBUG"], True) + else: + self.assertIs(ns["DEBUG"], False) + if level == 2: + self.assertIsNone(ns["__doc__"]) + else: + self.assertEqual(ns["__doc__"], "module docs") + unlink(TESTFN) + unlink(archive) + def test_write_python_directory(self): os.mkdir(TESTFN2) try: diff --git a/Lib/zipfile/__init__.py b/Lib/zipfile/__init__.py index 4909e680621ed71..47f8918a681981e 100644 --- a/Lib/zipfile/__init__.py +++ b/Lib/zipfile/__init__.py @@ -2911,7 +2911,8 @@ def _get_code(self, pathname, basename): try: if self.debug: print("Compiling", file_py) - code = loader.source_to_code(source_bytes, archivename) + code = loader.source_to_code(source_bytes, archivename, + _optimize=self._optimize) except Exception as err: # Historically, this function prints messages here rather than raising # (see test_zipfile.test_write_filtered_python_package) diff --git a/Misc/NEWS.d/next/Library/2026-11-27-0239-00.gh-issue-162710.Kx7Wtq.rst b/Misc/NEWS.d/next/Library/2026-11-27-0239-00.gh-issue-162710.Kx7Wtq.rst new file mode 100644 index 000000000000000..3c53ce687738452 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-27-0239-00.gh-issue-162710.Kx7Wtq.rst @@ -0,0 +1 @@ +Fix :meth:`zipfile.PyZipFile.writepy` to compile archived bytecode at the optimization level requested by the ``optimize`` argument rather than the builder interpreter's level.