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
29 changes: 29 additions & 0 deletions Lib/test/test_zipfile/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -3697,6 +3697,35 @@
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)

Check failure on line 3713 in Lib/test/test_zipfile/test_core.py

View workflow job for this annotation

GitHub Actions / Ubuntu (installed) / build, install and test

test_write_with_optimization_level (test.test_zipfile.test_core.PyZipFileTests.test_write_with_optimization_level)

RuntimeError: Files added with writepy() must end with ".py"

Check failure on line 3713 in Lib/test/test_zipfile/test_core.py

View workflow job for this annotation

GitHub Actions / Ubuntu / build and test (ubuntu-26.04)

test_write_with_optimization_level (test.test_zipfile.test_core.PyZipFileTests.test_write_with_optimization_level)

RuntimeError: Files added with writepy() must end with ".py"

Check failure on line 3713 in Lib/test/test_zipfile/test_core.py

View workflow job for this annotation

GitHub Actions / Lazy imports enabled / Run Tests with lazy_imports=all

test_write_with_optimization_level (test.test_zipfile.test_core.PyZipFileTests.test_write_with_optimization_level)

RuntimeError: Files added with writepy() must end with ".py"

Check failure on line 3713 in Lib/test/test_zipfile/test_core.py

View workflow job for this annotation

GitHub Actions / Ubuntu (free-threading) / build and test (ubuntu-26.04-arm)

test_write_with_optimization_level (test.test_zipfile.test_core.PyZipFileTests.test_write_with_optimization_level)

RuntimeError: Files added with writepy() must end with ".py"

Check failure on line 3713 in Lib/test/test_zipfile/test_core.py

View workflow job for this annotation

GitHub Actions / Windows / Build and test (x64, tail-call)

test_write_with_optimization_level (test.test_zipfile.test_core.PyZipFileTests.test_write_with_optimization_level)

RuntimeError: Files added with writepy() must end with ".py"

Check failure on line 3713 in Lib/test/test_zipfile/test_core.py

View workflow job for this annotation

GitHub Actions / Ubuntu (free-threading) / build and test (ubuntu-26.04)

test_write_with_optimization_level (test.test_zipfile.test_core.PyZipFileTests.test_write_with_optimization_level)

RuntimeError: Files added with writepy() must end with ".py"

Check failure on line 3713 in Lib/test/test_zipfile/test_core.py

View workflow job for this annotation

GitHub Actions / Hypothesis tests on Ubuntu

test_write_with_optimization_level (test.test_zipfile.test_core.PyZipFileTests.test_write_with_optimization_level)

RuntimeError: Files added with writepy() must end with ".py"

Check failure on line 3713 in Lib/test/test_zipfile/test_core.py

View workflow job for this annotation

GitHub Actions / Ubuntu / build and test (ubuntu-26.04-arm)

test_write_with_optimization_level (test.test_zipfile.test_core.PyZipFileTests.test_write_with_optimization_level)

RuntimeError: Files added with writepy() must end with ".py"

Check failure on line 3713 in Lib/test/test_zipfile/test_core.py

View workflow job for this annotation

GitHub Actions / macOS / build and test (macos-26)

test_write_with_optimization_level (test.test_zipfile.test_core.PyZipFileTests.test_write_with_optimization_level)

RuntimeError: Files added with writepy() must end with ".py"

Check failure on line 3713 in Lib/test/test_zipfile/test_core.py

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / Build and test (x64, tail-call)

test_write_with_optimization_level (test.test_zipfile.test_core.PyZipFileTests.test_write_with_optimization_level)

RuntimeError: Files added with writepy() must end with ".py"

Check failure on line 3713 in Lib/test/test_zipfile/test_core.py

View workflow job for this annotation

GitHub Actions / Windows / Build and test (arm64, tail-call)

test_write_with_optimization_level (test.test_zipfile.test_core.PyZipFileTests.test_write_with_optimization_level)

RuntimeError: Files added with writepy() must end with ".py"

Check failure on line 3713 in Lib/test/test_zipfile/test_core.py

View workflow job for this annotation

GitHub Actions / Ubuntu (bolt) / build and test (ubuntu-26.04)

test_write_with_optimization_level (test.test_zipfile.test_core.PyZipFileTests.test_write_with_optimization_level)

RuntimeError: Files added with writepy() must end with ".py"

Check failure on line 3713 in Lib/test/test_zipfile/test_core.py

View workflow job for this annotation

GitHub Actions / Emscripten / build and test

test_write_with_optimization_level (test.test_zipfile.test_core.PyZipFileTests.test_write_with_optimization_level)

RuntimeError: Files added with writepy() must end with ".py"

Check failure on line 3713 in Lib/test/test_zipfile/test_core.py

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / Build and test (arm64, tail-call)

test_write_with_optimization_level (test.test_zipfile.test_core.PyZipFileTests.test_write_with_optimization_level)

RuntimeError: Files added with writepy() must end with ".py"

Check failure on line 3713 in Lib/test/test_zipfile/test_core.py

View workflow job for this annotation

GitHub Actions / macOS / build and test (macos-26-intel)

test_write_with_optimization_level (test.test_zipfile.test_core.PyZipFileTests.test_write_with_optimization_level)

RuntimeError: Files added with writepy() must end with ".py"

Check failure on line 3713 in Lib/test/test_zipfile/test_core.py

View workflow job for this annotation

GitHub Actions / Windows / Build and test (Win32, switch-case)

test_write_with_optimization_level (test.test_zipfile.test_core.PyZipFileTests.test_write_with_optimization_level)

RuntimeError: Files added with writepy() must end with ".py"

Check failure on line 3713 in Lib/test/test_zipfile/test_core.py

View workflow job for this annotation

GitHub Actions / macOS (free-threading) / build and test (macos-26)

test_write_with_optimization_level (test.test_zipfile.test_core.PyZipFileTests.test_write_with_optimization_level)

RuntimeError: Files added with writepy() must end with ".py"

Check failure on line 3713 in Lib/test/test_zipfile/test_core.py

View workflow job for this annotation

GitHub Actions / Sanitizers / UBSan

test_write_with_optimization_level (test.test_zipfile.test_core.PyZipFileTests.test_write_with_optimization_level)

RuntimeError: Files added with writepy() must end with ".py"

Check failure on line 3713 in Lib/test/test_zipfile/test_core.py

View workflow job for this annotation

GitHub Actions / Sanitizers / ASan

test_write_with_optimization_level (test.test_zipfile.test_core.PyZipFileTests.test_write_with_optimization_level)

RuntimeError: Files added with writepy() must end with ".py"

Check failure on line 3713 in Lib/test/test_zipfile/test_core.py

View workflow job for this annotation

GitHub Actions / Windows / Build and test (arm64, switch-case)

test_write_with_optimization_level (test.test_zipfile.test_core.PyZipFileTests.test_write_with_optimization_level)

RuntimeError: Files added with writepy() must end with ".py"

Check failure on line 3713 in Lib/test/test_zipfile/test_core.py

View workflow job for this annotation

GitHub Actions / Sanitizers / MSan

test_write_with_optimization_level (test.test_zipfile.test_core.PyZipFileTests.test_write_with_optimization_level)

RuntimeError: Files added with writepy() must end with ".py"

Check failure on line 3713 in Lib/test/test_zipfile/test_core.py

View workflow job for this annotation

GitHub Actions / Windows / Build and test (x64, switch-case)

test_write_with_optimization_level (test.test_zipfile.test_core.PyZipFileTests.test_write_with_optimization_level)

RuntimeError: Files added with writepy() must end with ".py"

Check failure on line 3713 in Lib/test/test_zipfile/test_core.py

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / Build and test (x64, switch-case)

test_write_with_optimization_level (test.test_zipfile.test_core.PyZipFileTests.test_write_with_optimization_level)

RuntimeError: Files added with writepy() must end with ".py"

Check failure on line 3713 in Lib/test/test_zipfile/test_core.py

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / Build and test (arm64, switch-case)

test_write_with_optimization_level (test.test_zipfile.test_core.PyZipFileTests.test_write_with_optimization_level)

RuntimeError: Files added with writepy() must end with ".py"
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:
Expand Down
3 changes: 2 additions & 1 deletion Lib/zipfile/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Loading