GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-141276: Avoid compiling zipimport source in get_filename by harjothkhara · Pull Request #153799 · python/cpython

Conversation

@harjothkhara

@harjothkhara harjothkhara commented Jul 16, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Summary

  • avoid compiling zipimport source while resolving a module filename
  • preserve bytecode validation and source fallback behavior
  • add a regression test and a NEWS entry

Root cause

zipimporter.get_filename() called _get_module_code(), which compiled source while import machinery was creating the module spec. Module execution then compiled the same source again.

Tests

  • ./python.exe -m test test_zipimport -m test_syntax_warning -m testBadMagic -m testBadMTime
  • ./python.exe -m test test_zipimport

Refs #141276

Duplicate-work check

No open or merged pull request referencing #141276 was found before submission.

AI assistance

OpenAI Codex assisted with investigation, implementation, testing, and review.

  • Issue: Syntax warnings are duplicated in zipimport #141276

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@harjothkhara
pythongh-141276: Avoid compiling zipimport source in get_filename
0d5505d
@harjothkhara
harjothkhara requested review from FFY00, brettcannon, ericsnowcurrently, ncoghlan and warsaw as code owners July 16, 2026 05:16
@bedevere-app bedevere-app Bot added the awaiting review label Jul 16, 2026
@bedevere-app bedevere-app Bot mentioned this pull request Jul 16, 2026
Syntax warnings are duplicated in zipimport #141276
Closed
@harjothkhara
pythongh-141276: Update zipimport warning expectations
05f92d2
@brettcannon
brettcannon removed their request for review July 17, 2026 19:12
@harjothkhara

harjothkhara commented Sep 3, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

This fixes the duplicated SyntaxWarning from #141276, could someone take a look?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

itamaro
itamaro requested changes Oct 3, 2026
View reviewed changes

@itamaro itamaro left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the fix generally looks good to me, thanks for the PR!

see inline comments regarding skipping wasted source load.

a few additional requests:

  1. this is an observable behavior change, which needs to be called out in the NEWS.

the semantic change - previously, py files with syntax errors in zip would raise from find_spec() and get_filename(). after this change, find_spec() and get_filename() would succeed, and only exec_module() would raise.

while this sounds like a significant behavior change, I think it's acceptable and even desirable, because it brings zipimport in line with the standard filesystem based finder/loader - but it does warrant more explicit coverage in NEWS.
this also makes it somewhat risky to backport, so I'd like @serhiy-storchaka's opinion here as well.

  1. I'd like to see a few more test cases covering the expected behavior, and preventing unintended future regressions, especially around the subtler corners.

a test that with a py with SyntaxError. assert that find_spec() and get_filename() succeed and return expected spec / filename, and exec_module() raises SyntaxError.

perhaps also have a test that calls get_filename() directly and asserts no warnings.

I see testBadMagic/testBadMTime already cover stale/bad-magic .pyc falling back to .py. could you add:

  • valid .pyc + .py with a syntax error → spec picks the .pyc and import succeeds
  • package __init__.py with a syntax error + a valid sibling module of the same name → origin is __init__.py and exec raises SyntaxError (no fallthrough to the module)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Lib/zipimport.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/zipimport.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Misc/NEWS.d/next/Library/2026-07-15-21-45-33.gh-issue-141276.xH7qP2.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot added awaiting changes and removed awaiting review labels Oct 3, 2026
@bedevere-app

bedevere-app Bot commented Oct 3, 2026

Copy link
Copy Markdown

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@harjothkhara
pythongh-141276: Return before reading source in _get_module_code
d202f7f
Move the compile_source=False early return above the archive read, so
get_filename() no longer decompresses source it immediately discards.
Also update the NEWS entry with itamaro's suggested wording and add
tests covering the deferred-SyntaxError behavior: find_spec()/
get_filename() succeed and get_filename() emits no warnings for a
module with a syntax error, a valid .pyc still wins over a bad
sibling .py, and a package's __init__.py error doesn't fall through
to a same-named sibling module.
itamaro
itamaro reviewed Oct 10, 2026
View reviewed changes

@itamaro itamaro left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for making the requested changes!

please don't forget to follow the bot instructions about commenting on the PR once you've made the requested changes (it's needed so the bot updates PR labels and makes it show up in review queues again, otherwise it's more likely to go unnoticed).

we also need to update the docs (Doc/library/zipimport.rst) to match the modified get_filename behavior (raises when "module couldn't be found", not when "module couldn't be imported".

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

harjothkhara reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
Comment thread Misc/NEWS.d/next/Library/2026-07-15-21-45-33.gh-issue-141276.xH7qP2.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/zipimport.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/zipimport.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/zipimport.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@harjothkhara @claude
pythongh-141276: Address round-2 review feedback on zipimport
8082336
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
@harjothkhara

harjothkhara commented Oct 10, 2026

Copy link
Copy Markdown
Contributor Author

I have made the requested changes; please review again.

Pushed 8082336 with the NEWS wording, the docstring, the comment, the redundant modpath line and the Doc/library/zipimport.rst update.

itamaro reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot added awaiting change review and removed awaiting changes labels Oct 10, 2026
@bedevere-app

bedevere-app Bot commented Oct 10, 2026

Copy link
Copy Markdown

Thanks for making the requested changes!

@itamaro: please review the changes made to this pull request.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app
bedevere-app Bot requested a review from itamaro October 10, 2026 17:39
@itamaro itamaro added the topic-importlib label Oct 10, 2026
@itamaro

itamaro commented Oct 10, 2026

Copy link
Copy Markdown
Contributor

thanks, it looks good to me! I'll merge main since it's been a while, and merge if CI passes.

cc @serhiy-storchaka @hugovk what do you think about backporting this? (see previous comment)

harjothkhara reacted with heart emoji
All reactions
  • ❤️ 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@itamaro
Merge branch 'main' into codex/pythongh-141276-zipimport
cb21019
@read-the-docs-community

read-the-docs-community Bot commented Oct 10, 2026

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #35063365 | 📁 Comparing cb21019 against main (5564790)

  🔍 Preview build  

2 files changed
± library/zipimport.html
± whatsnew/changelog.html
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

itamaro
itamaro approved these changes Oct 10, 2026
View reviewed changes
@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting change review labels Oct 10, 2026
@itamaro
itamaro merged commit 230917f into python:main Oct 10, 2026
55 of 56 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the awaiting merge label Oct 10, 2026
orsenthil pushed a commit to orsenthil/cpython that referenced this pull request Oct 11, 2026
@harjothkhara @orsenthil
pythongh-141276: Avoid compiling zipimport source in get_filename (py…
46f0541
…thon#153799)

`zipimporter.get_filename()` compiled source modules just to work out the
module's file name. `find_spec()` calls it through `spec_from_loader()`, so
importing a source module from a ZIP archive compiled it twice and emitted
every `SyntaxWarning` twice.

`get_filename()` now returns before reading or compiling the source.
As a result, a `SyntaxError` in a zipped source module is now raised by
`exec_module()` rather than by `find_spec()` or `get_filename()`, and
the source isn't compiled twice during import, which matches
the filesystem-based loaders.
@itamaro itamaro added needs backport to 3.14 bugs and security fixes needs backport to 3.15 bugs and security fixes labels Oct 19, 2026
@miss-islington-app

miss-islington-app Bot commented Oct 19, 2026

Copy link
Copy Markdown

Thanks @harjothkhara for the PR, and @itamaro for merging it 🌮🎉.. I'm working now to backport this PR to: 3.14.
🐍🍒⛏🤖

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@miss-islington-app

miss-islington-app Bot commented Oct 19, 2026

Copy link
Copy Markdown

Thanks @harjothkhara for the PR, and @itamaro for merging it 🌮🎉.. I'm working now to backport this PR to: 3.15.
🐍🍒⛏🤖

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@miss-islington-app

miss-islington-app Bot commented Oct 19, 2026

Copy link
Copy Markdown

Sorry, @harjothkhara and @itamaro, I could not cleanly backport this to 3.14 due to a conflict.

Please backport manually with cherry_picker, see the devguide for more information.

cherry_picker 230917f5072c5c6b9373374a84d52935d7afa3b5 3.14
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@miss-islington-app miss-islington-app Bot assigned itamaro Oct 19, 2026
@bedevere-app

bedevere-app Bot commented Oct 19, 2026

Copy link
Copy Markdown

GH-159729 is a backport of this pull request to the 3.15 branch.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 bugs and security fixes label Oct 19, 2026
@itamaro

itamaro commented Oct 19, 2026

Copy link
Copy Markdown
Contributor

Sorry, @harjothkhara and @itamaro, I could not cleanly backport this to 3.14 due to a conflict.

Please backport manually with cherry_picker, see the devguide for more information.

cherry_picker 230917f5072c5c6b9373374a84d52935d7afa3b5 3.14

hey @harjothkhara, would you like to look into the backport to 3.14?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

itamaro pushed a commit that referenced this pull request Oct 19, 2026
@miss-islington @harjothkhara
[3.15] gh-141276: Avoid compiling zipimport source in get_filename (G…
47e4ba6
…H-153799) (#159729)

gh-141276: Avoid compiling zipimport source in get_filename (GH-153799)

`zipimporter.get_filename()` compiled source modules just to work out the
module's file name. `find_spec()` calls it through `spec_from_loader()`, so
importing a source module from a ZIP archive compiled it twice and emitted
every `SyntaxWarning` twice.

`get_filename()` now returns before reading or compiling the source.
As a result, a `SyntaxError` in a zipped source module is now raised by
`exec_module()` rather than by `find_spec()` or `get_filename()`, and
the source isn't compiled twice during import, which matches
the filesystem-based loaders.
(cherry picked from commit 230917f)

Co-authored-by: Harjoth Khara <[email protected]>
itamaro added a commit to itamaro/cpython that referenced this pull request Oct 19, 2026
@itamaro @claude
pythongh-141276: Follow-ups to the zipimporter.get_filename() change
4d56783
pythonGH-153799 landed without a few of the review suggestions:

- a versionchanged note on get_filename() in the docs;
- a What's New entry describing both the SyntaxWarning fix and the
  behavior change;
- tests for a stale .pyc next to a .py with a syntax error (falls
  through to the .py, and the SyntaxError is deferred to exec_module()),
  and for get_filename()/find_spec() not reading the source at all.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Kq5LwfysHx5inm3J1TQCdS
@bedevere-app

bedevere-app Bot commented Nov 6, 2026

Copy link
Copy Markdown

GH-161504 is a backport of this pull request to the 3.14 branch.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label Nov 6, 2026
itamaro added a commit that referenced this pull request Nov 6, 2026
@itamaro @harjothkhara
[3.14] gh-141276: Avoid compiling zipimport source in get_filename (G…
310c85a
…H-153799) (#161504)

`zipimporter.get_filename()` compiled source modules just to work out the
module's file name. `find_spec()` calls it through `spec_from_loader()`, so
importing a source module from a ZIP archive compiled it twice and emitted
every `SyntaxWarning` twice.

`get_filename()` now returns before reading or compiling the source.
As a result, a `SyntaxError` in a zipped source module is now raised by
`exec_module()` rather than by `find_spec()` or `get_filename()`, and
the source isn't compiled twice during import, which matches
the filesystem-based loaders.

(cherry picked from commit 230917f)

Co-authored-by: Harjoth Khara <[email protected]>
This was referenced Nov 8, 2026
zipimport reads and unmarshals pyc's twice per import #161644
Open
gh-161644: Avoid unmarshalling zipped .pyc files twice on import #161700
Open
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@itamaro itamaro itamaro approved these changes
@ericsnowcurrently ericsnowcurrently Awaiting requested review from ericsnowcurrently ericsnowcurrently is a code owner
@ncoghlan ncoghlan Awaiting requested review from ncoghlan ncoghlan is a code owner
@warsaw warsaw Awaiting requested review from warsaw warsaw is a code owner
@FFY00 FFY00 Awaiting requested review from FFY00 FFY00 is a code owner

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@itamaro itamaro

Labels

topic-importlib

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@harjothkhara @itamaro
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.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.