Skip to content

gh-122071: Fixed traceback leaks global code when file does not exist #122126

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Lib/linecache.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ def lazycache(filename, module_globals):
loader = getattr(spec, 'loader', None)
if loader is None:
loader = module_globals.get('__loader__')
mod_file = module_globals.get('__file__')
import importlib._bootstrap_external
if isinstance(loader, importlib._bootstrap_external.SourceFileLoader) and (not mod_file or (not mod_file.endswith(filename) and not mod_file.endswith('.pyc'))):
return False
get_source = getattr(loader, 'get_source', None)

if name and get_source:
Expand Down
10 changes: 5 additions & 5 deletions Lib/test/test_linecache.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,8 @@ def test_lazycache_smoke(self):
lines = linecache.getlines(NONEXISTENT_FILENAME, globals())
linecache.clearcache()
self.assertEqual(
True, linecache.lazycache(NONEXISTENT_FILENAME, globals()))
self.assertEqual(1, len(linecache.cache[NONEXISTENT_FILENAME]))
False, linecache.lazycache(NONEXISTENT_FILENAME, globals()))
self.assertEqual(False, NONEXISTENT_FILENAME in linecache.cache)
# Note here that we're looking up a nonexistent filename with no
# globals: this would error if the lazy value wasn't resolved.
self.assertEqual(lines, linecache.getlines(NONEXISTENT_FILENAME))
Expand All @@ -232,11 +232,11 @@ def test_lazycache_bad_filename(self):

def test_lazycache_already_cached(self):
linecache.clearcache()
lines = linecache.getlines(NONEXISTENT_FILENAME, globals())
lines = linecache.getlines(FILENAME, globals())
self.assertEqual(
False,
linecache.lazycache(NONEXISTENT_FILENAME, globals()))
self.assertEqual(4, len(linecache.cache[NONEXISTENT_FILENAME]))
linecache.lazycache(FILENAME, globals()))
self.assertEqual(4, len(linecache.cache[FILENAME]))

def test_memoryerror(self):
lines = linecache.getlines(FILENAME)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix global code leaks when reporting tracebacks in the REPL and :func:`exec`.
Loading