Repository navigation
gh-162710: forward optimize level in PyZipFile.writepy() - #162713
Open
TINGyu123644 wants to merge 1 commit into
Open
TINGyu123644 wants to merge 1 commit into
TINGyu123644 wants to merge 1 commit into
Conversation
_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.
|
The following commit authors need to sign the Contributor License Agreement: |
|
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix
PyZipFile.writepy()so that archived bytecode is compiled at the optimization level explicitly requested through theoptimizeargument, instead of always using the builder interpreter's own level.Closes gh-162710.
Problem
PyZipFilestores the requested level asself._optimize, but_get_code()callsloader.source_to_code(source_bytes, archivename)without forwarding it. Since gh-144293 switched this call frompy_compile.compile()(which did passoptimize) tosource_to_code(), the requested level has been silently ignored on 3.15+.Consequences:
PyZipFile(..., optimize=2)under a normal interpreter emits.pycfiles that still containassertstatements and docstrings, and import with__debug__ == True.python -Owithoptimize=0bakes the optimized bytecode into the distributed.zip.Fix
Forward
_optimize=self._optimizetosource_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-OObuilders.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.pycand checks:__debug__ is True, docstring preserved__debug__ is False, docstring preserved__debug__ is False, docstring strippedThe 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 thesource_to_code()semantics used bymain.AI disclosure
This patch and its regression test were authored with AI assistance and reviewed locally before submission.