Skip to content

Commit 2c6217c

Browse files
committed
Pass down code objects and not frames
1 parent 0a90562 commit 2c6217c

File tree

3 files changed

+14
-12
lines changed

3 files changed

+14
-12
lines changed

Lib/linecache.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,13 @@ def updatecache(filename, module_globals=None):
104104
# These imports are not at top level because linecache is in the critical
105105
# path of the interpreter startup and importing os and sys take a lot of time
106106
# and slows down the startup sequence.
107-
import os
108-
import sys
109-
import tokenize
107+
try:
108+
import os
109+
import sys
110+
import tokenize
111+
except ImportError:
112+
# These import can fail if the interpreter is shutting down
113+
return []
110114

111115
if filename in cache:
112116
if len(cache[filename]) != 1:

Lib/test/test_repl.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ def bar(x):
213213
p.stdin.write(user_input)
214214
user_input2 = dedent("""
215215
import linecache
216-
print(linecache.cache['<stdin>-1'])
216+
print(linecache._interactive_cache[foo.__code__])
217217
""")
218218
p.stdin.write(user_input2)
219219
output = kill_python(p)

Lib/traceback.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ class FrameSummary:
288288
"""
289289

290290
__slots__ = ('filename', 'lineno', 'end_lineno', 'colno', 'end_colno',
291-
'name', '_lines', '_lines_dedented', 'locals', '_frame')
291+
'name', '_lines', '_lines_dedented', 'locals', '_code')
292292

293293
def __init__(self, filename, lineno, name, *, lookup_line=True,
294294
locals=None, line=None,
@@ -308,7 +308,7 @@ def __init__(self, filename, lineno, name, *, lookup_line=True,
308308
self.colno = colno
309309
self.end_colno = end_colno
310310
self.name = name
311-
self._frame = kwargs.get("_frame")
311+
self._code = kwargs.get("_code")
312312
self._lines = line
313313
self._lines_dedented = None
314314
if lookup_line:
@@ -348,11 +348,9 @@ def _set_lines(self):
348348
lines = []
349349
for lineno in range(self.lineno, self.end_lineno + 1):
350350
# treat errors (empty string) and empty lines (newline) as the same
351-
line = None
352-
if self._frame is not None and self.filename.startswith("<"):
353-
line = linecache._getline_from_code(self._frame.f_code, lineno, self._frame.f_globals).rstrip()
354-
if line is None:
355-
line = linecache.getline(self.filename, lineno).rstrip()
351+
line = linecache.getline(self.filename, lineno).rstrip()
352+
if not line and self._code is not None and self.filename.startswith("<"):
353+
line = linecache._getline_from_code(self._code, lineno).rstrip()
356354
lines.append(line)
357355
self._lines = "\n".join(lines) + "\n"
358356

@@ -490,7 +488,7 @@ def _extract_from_extended_frame_gen(klass, frame_gen, *, limit=None,
490488
FrameSummary(filename, lineno, name,
491489
lookup_line=False, locals=f_locals,
492490
end_lineno=end_lineno, colno=colno, end_colno=end_colno,
493-
_frame=f,
491+
_code=f.f_code,
494492
)
495493
)
496494
for filename in fnames:

0 commit comments

Comments
 (0)