gh-157146: Let linecache read sources from zip archives on sys.path - #157147
Merged
Conversation
…path linecache.getlines() and getline() returned nothing for a module imported from a zip archive on sys.path unless the caller passed module_globals: os.stat() fails on the archive-internal path, the loader lookup needs the globals, and the sys.path search only handles relative names. Callers that only have a file name, such as pdb, warnings and doctest, got no source. Read such files through the get_data() method of the path entry finder registered for the archive in sys.path_importer_cache instead.
zipimporter does not absolutize its sys.path entry, so a relative entry gives its modules a relative __file__ and a relative key in sys.path_importer_cache; the isabs() guard made linecache skip exactly that case. Build the cache entry once for both the loader and the archive lookups, and only import importlib.util once there is data to decode. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016oQeaTzLMBouqehAe3W2EU
…down sys.path_importer_cache is set to None early in finalization, before the objects still referenced from sys attributes are released, so a __del__ can reach linecache while every module it imports is still available. Return early in that case, and treat an ImportError from the lookup like the other imports linecache makes lazily. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016oQeaTzLMBouqehAe3W2EU
gpshead
force-pushed
the
linecache-archive-sources
branch
from
September 10, 2026 06:07
b04f63e to
8d5a2b4
Compare
gpshead
marked this pull request as ready for review
September 10, 2026 13:43
|
Thanks @gpshead for the PR 🌮🎉.. I'm working now to backport this PR to: 3.15. |
|
GH-157269 is a backport of this pull request to the 3.15 branch. |
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.
linecache.getline() now works for a .zip archive on sys.path without the caller having to pass module_globals. Callers that only have a file name, such as pdb, warnings, and doctest, now get source. Reads of such files go through the get_data() method of the path entry finder registered for the archive in sys.path_importer_cache.
Prior to this: os.stat() would fail on the archive-internal path, the loader lookup needed the globals, and the sys.path search would only handle relative names and assumed a filesystem rather than using an importer.
(thank you Claude Fable 5.1)