linecache cannot read the source of modules imported from a .zip when given only the file name
@gpshead ci sta già lavorando.
Dal 8/9/2026.
Valutazione
Questa issue non è ancora stata valutata.
Descrizione
Bug report
Bug description:
linecache.getlines(filename) and .getline(filename, lineno) return nothing for a module that was imported from a zip archive on sys.path via zipimport, unless the caller also passes module_globals.
For such modules, __file__ and .co_filename look like /path/to/archive.zip/pkg/mod.py. In linecache.updatecache(), the os.stat() on that path fails (NotADirectoryError), the lazy-loader lookup only works when module_globals is supplied, and the final fallback that searches sys.path only handles relative file names. So the lookup gives up and returns [].
Many callers only have the file name: pdb's list command, warnings (when the module globals are gone), doctest, inspect.getsourcefile() fallbacks, and traceback formatting of code objects whose frames have been cleared. For a file on disk these all work; for the same module inside a zip they silently show no source.
import linecache, sys, zipfile
with zipfile.ZipFile('mods.zip', 'w') as zf:
zf.writestr('zipmod.py', 'def f():\n return 1\n')
sys.path.insert(0, 'mods.zip')
import zipmod
print(linecache.getlines(zipmod.__file__)) # [] but should be the two source lines
print(linecache.getline(zipmod.f.__code__.co_filename,
zipmod.f.__code__.co_firstlineno)) # '' but should be 'def f():\n'
The information needed is already available: zipimport.zipimporter implements get_data(pathname), which accepts exactly these absolute paths, and sys.path_importer_cache maps the archive path to that importer.
Proposed fix: after the lazy-loader attempt fails, have updatecache() check whether the file name lies under a sys.path_importer_cache entry whose finder has get_data()...
This also benefits any other path entry finder that provides get_data() for an archive.
CPython versions tested on:
CPython main branch
Operating systems tested on:
Linux
Linked PRs
- gh-157147
- gh-157269
- Lingua principale
- Python
- Stelle
- 77.2k
- Fork
- 36k
- Merge medio
- 1g 9h
- PR unite (30g)
- 558
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di python/cpython
-
docs pending
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
-
stdlib type-feature
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
-
stdlib type-feature
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
-
build type-bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
-
stdlib topic-email type-feature
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
Tutte le issue di python/cpython
Issue simili
-
link-check link-check:sphinx-theme
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 65/100
qgis/QGIS-Documentation#11275 ·
-
bug priority:normal ready-for-dev
Difficoltà 2/5 1-3 ore Idoneità per principianti 88/100
OpenHands/extensions#626 · 1 commento ·
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 90/100
CSCfi/sd-search-api#39 ·
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 90/100