Skip to content

gh-157146: Let linecache read sources from zip archives on sys.path - #157147

Merged
gpshead merged 3 commits into
python:mainfrom
gpshead:linecache-archive-sources
Sep 10, 2026
Merged

gh-157146: Let linecache read sources from zip archives on sys.path#157147
gpshead merged 3 commits into
python:mainfrom
gpshead:linecache-archive-sources

Conversation

@gpshead

@gpshead gpshead commented Sep 8, 2026

Copy link
Copy Markdown
Member

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)

…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.
gpshead and others added 2 commits September 9, 2026 22:05
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
gpshead force-pushed the linecache-archive-sources branch from b04f63e to 8d5a2b4 Compare September 10, 2026 06:07
@gpshead
gpshead marked this pull request as ready for review September 10, 2026 13:43
@gpshead
gpshead merged commit 1d6e7d5 into python:main Sep 10, 2026
53 checks passed
@gpshead gpshead added the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label Sep 10, 2026
@miss-islington-app

Copy link
Copy Markdown

Thanks @gpshead for the PR 🌮🎉.. I'm working now to backport this PR to: 3.15.
🐍🍒⛏🤖

@bedevere-app

bedevere-app Bot commented Sep 10, 2026

Copy link
Copy Markdown

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

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label Sep 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant