Skip to content

Commit 63e1632

Browse files
committed
gh-117500: Store interactive source code in an alternative location in linecache
1 parent e338e1a commit 63e1632

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

Lib/linecache.py

+16-11
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,12 @@ 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+
print(the_cache)
41+
if filename in the_cache:
42+
entry = the_cache[filename]
43+
if len(entry) != 1:
44+
return the_cache[filename][2]
4245

4346
try:
4447
return updatecache(filename, module_globals)
@@ -159,11 +162,13 @@ def lazycache(filename, module_globals):
159162
get_source method must be found, the filename must be a cacheable
160163
filename, and the filename must not be already cached.
161164
"""
162-
if filename in cache:
163-
if len(cache[filename]) == 1:
164-
return True
165-
else:
166-
return False
165+
for the_cache in (cache, _interactive_cache):
166+
print(the_cache)
167+
if filename in the_cache:
168+
if len(the_cache[filename]) == 1:
169+
return True
170+
else:
171+
return False
167172
if not filename or (filename.startswith('<') and filename.endswith('>')):
168173
return False
169174
# Try for a __loader__, if available
@@ -183,8 +188,8 @@ def get_lines(name=name, *args, **kwargs):
183188
return False
184189

185190

186-
def _register_code(code, string, name):
187-
cache[code] = (
191+
def _register_code(code, filename, string, name):
192+
_interactive_cache[filename] = (
188193
len(string),
189194
None,
190195
[line + '\n' for line in string.splitlines()],

Python/pythonrun.c

+2-1
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)