Skip to content

Commit f6f9bb1

Browse files
committed
Work around bug where frames report lineno outside source block
1 parent 268dd5e commit f6f9bb1

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

stackprinter/extraction.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,24 @@ def get_info(tb_or_frame, lineno=None):
9595
source = []
9696
startline = lineno
9797

98+
if lineno < startline:
99+
# Catch rare case where the tb or frame has a wrong current line number,
100+
# or a wrong number for the starting line of its code block.
101+
# This looks like a python bug or an `inspect.getsource` bug --
102+
# OR a getsource bug that is ultimately a python bug, because `inspect`
103+
# just uses `frame.f_code.co_firstlineno` (= the frame's own reported
104+
# beginning of its code block), and I can't imagine a situation where
105+
# that can legitimately not contain `frame.f_lineno`.
106+
# I deal with this here by showing a warning in-band that the line
107+
# number can't be 100% trusted, while also moving the active line shown
108+
# down to the first available source line.
109+
corrected_lineno = startline # move the reported active line
110+
source = ["# // Stackprinter: This frame reported a line number outside"
111+
" its reported code scope. Line %d reported, but guessing"
112+
" %d instead.\n" % (lineno, corrected_lineno)] + source
113+
startline -= 1 # account for the in-band warning prepended to our source
114+
lineno = corrected_lineno
115+
98116
source_map, line2names, name2lines, head_lns, lineno = annotate(source, startline, lineno)
99117

100118
if function in NON_FUNCTION_SCOPES:

0 commit comments

Comments
 (0)