Skip to content
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
2 changes: 1 addition & 1 deletion Lib/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -1626,7 +1626,7 @@ def getframeinfo(frame, context=1):
raise TypeError('{!r} is not a frame or traceback object'.format(frame))

filename = getsourcefile(frame) or getfile(frame)
if context > 0:
if context > 0 and lineno is not None:
start = lineno - 1 - context//2
try:
lines, lnum = findsource(frame)
Expand Down
33 changes: 33 additions & 0 deletions Lib/test/test_inspect/test_inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,39 @@ def test_getframeinfo_get_first_line(self):
self.assertEqual(frame_info.code_context[0], "# line 1\n")
self.assertEqual(frame_info.code_context[1], "'A module docstring.'\n")

def test_getframeinfo_no_lineno(self):
# gh-89726: some instructions carry no line number, so a frame
# stopped on one has f_lineno set to None. getframeinfo() must
# report that rather than failing.
def f():
try:
raise ValueError
except ValueError:
pass

infos = []

def trace(frame, event, arg):
if frame.f_code is f.__code__ and frame.f_lineno is None:
infos.append(inspect.getframeinfo(frame))
frame.f_trace_opcodes = True
return trace

old_trace = sys.gettrace()
sys.settrace(trace)
try:
f()
finally:
sys.settrace(old_trace)

self.assertTrue(infos, 'no frame with f_lineno set to None')
for info in infos:
with self.subTest(info=info):
self.assertIsNone(info.lineno)
self.assertIsNone(info.code_context)
self.assertIsNone(info.index)
self.assertEqual(info.function, 'f')

def test_getsource(self):
self.assertSourceEqual(git.abuse, 29, 39)
self.assertSourceEqual(mod.StupidGit, 21, 51)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
:func:`inspect.getframeinfo` no longer raises :exc:`TypeError` for a frame
stopped on an instruction that carries no line number, such as while tracing
opcodes. ``lineno``, ``code_context`` and ``index`` are reported as ``None``
instead.
Loading