Skip to content

Commit 4fc4202

Browse files
committed
pythongh-117500: Store interactive source code in an alternative location in linecache
1 parent e338e1a commit 4fc4202

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

Lib/linecache.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
# The cache. Maps filenames to either a thunk which will provide source code,
1515
# or a tuple (size, mtime, lines, fullname) once loaded.
1616
cache = {}
17+
_interactive_cache = {}
1718

1819

1920
def clearcache():
@@ -35,10 +36,11 @@ def getlines(filename, module_globals=None):
3536
"""Get the lines for a Python source file from the cache.
3637
Update the cache if it doesn't contain an entry for this file already."""
3738

38-
if filename in cache:
39-
entry = cache[filename]
40-
if len(entry) != 1:
41-
return cache[filename][2]
39+
for the_cache in (cache, _interactive_cache):
40+
if filename in the_cache:
41+
entry = the_cache[filename]
42+
if len(entry) != 1:
43+
return the_cache[filename][2]
4244

4345
try:
4446
return updatecache(filename, module_globals)
@@ -159,11 +161,12 @@ def lazycache(filename, module_globals):
159161
get_source method must be found, the filename must be a cacheable
160162
filename, and the filename must not be already cached.
161163
"""
162-
if filename in cache:
163-
if len(cache[filename]) == 1:
164-
return True
165-
else:
166-
return False
164+
for the_cache in (cache, _interactive_cache):
165+
if filename in the_cache:
166+
if len(the_cache[filename]) == 1:
167+
return True
168+
else:
169+
return False
167170
if not filename or (filename.startswith('<') and filename.endswith('>')):
168171
return False
169172
# Try for a __loader__, if available
@@ -183,8 +186,8 @@ def get_lines(name=name, *args, **kwargs):
183186
return False
184187

185188

186-
def _register_code(code, string, name):
187-
cache[code] = (
189+
def _register_code(code, filename, string, name):
190+
_interactive_cache[filename] = (
188191
len(string),
189192
None,
190193
[line + '\n' for line in string.splitlines()],

Python/pythonrun.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1351,7 +1351,8 @@ run_mod(mod_ty mod, PyObject *filename, PyObject *globals, PyObject *locals,
13511351
}
13521352

13531353
PyObject* result = PyObject_CallFunction(
1354-
print_tb_func, "OOO",
1354+
print_tb_func, "OOOO",
1355+
co,
13551356
interactive_filename,
13561357
interactive_src,
13571358
filename

0 commit comments

Comments
 (0)