Skip to content

Commit

Permalink
Work around bug where frames report lineno outside source block
Browse files Browse the repository at this point in the history
  • Loading branch information
cknd committed Mar 16, 2021
1 parent 268dd5e commit 6d94357
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions stackprinter/extraction.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,24 @@ def get_info(tb_or_frame, lineno=None):
source = []
startline = lineno

if lineno < startline:
# Catch an edge case where the frame's current line numer is wrong,
# or the discoverd line number where the frame's code block starts is.
# This looks like a python bug or an `inspect.getsource` bug --
# OR a getsource bug that is ultimately a python bug, because `inspect`
# just uses `frame.f_code.co_firstlineno` (= the frame's own reported
# beginning of its code block), and I can't imagine a situation where
# that can legitimately not contain `frame.f_lineno`.
# I deal with this here by showing a warning in-band that the line
# number can't be 100% trusted, while also moving the active line shown
# down to the first available source line.
corrected_lineno = startline # move the reported active line
source = ["# // Stackprinter: This frame reported a line number outside"
" its reported code scope. Line %d reported, but guessing"
" %d instead.\n" % (lineno, corrected_lineno)] + source
startline -= 1 # account for the in-band warning prepended to our source
lineno = corrected_lineno

source_map, line2names, name2lines, head_lns, lineno = annotate(source, startline, lineno)

if function in NON_FUNCTION_SCOPES:
Expand Down

0 comments on commit 6d94357

Please sign in to comment.