Skip to content

gh-162710: forward optimize level in PyZipFile.writepy() - #162713

Open
TINGyu123644 wants to merge 1 commit into
python:mainfrom
TINGyu123644:fix/writepy-optimize-level
Open

TINGyu123644 wants to merge 1 commit into
python:mainfrom
TINGyu123644:fix/writepy-optimize-level

Conversation

@TINGyu123644

Copy link
Copy Markdown

Fix PyZipFile.writepy() so that archived bytecode is compiled at the optimization level explicitly requested through the optimize argument, instead of always using the builder interpreter's own level.

Closes gh-162710.

Problem

PyZipFile stores the requested level as self._optimize, but _get_code() calls loader.source_to_code(source_bytes, archivename) without forwarding it. Since gh-144293 switched this call from py_compile.compile() (which did pass optimize) to source_to_code(), the requested level has been silently ignored on 3.15+.

Consequences:

  • PyZipFile(..., optimize=2) under a normal interpreter emits .pyc files that still contain assert statements and docstrings, and import with __debug__ == True.
  • Building an archive under python -O with optimize=0 bakes the optimized bytecode into the distributed .zip.

Fix

Forward _optimize=self._optimize to source_to_code(), which already accepts the keyword (default -1, meaning "use the interpreter's level"). This restores the pre-3.15 behaviour for all four requested levels (-1, 0, 1, 2) under normal, -O, and -OO builders.

Verification

Added test_write_with_optimization_level, which writes a module exposing __debug__ and a docstring, archives it at levels 0/1/2, then executes the emitted .pyc and checks:

  • level 0: __debug__ is True, docstring preserved
  • level 1: __debug__ is False, docstring preserved
  • level 2: __debug__ is False, docstring stripped

The test fails on unpatched main (levels 1 and 2 report __debug__ is True, level 2 keeps the docstring) and passes with the fix. Confirmed locally against the source_to_code() semantics used by main.

AI disclosure

This patch and its regression test were authored with AI assistance and reviewed locally before submission.

_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.
@python-cla-bot

Copy link
Copy Markdown

The following commit authors need to sign the Contributor License Agreement:

CLA not signed

@bedevere-app

bedevere-app Bot commented Nov 27, 2026

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PyZipFile.writepy() ignores the requested optimization level when compiling archived bytecode

1 participant