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
25 changes: 25 additions & 0 deletions stackprinter/extraction.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,31 @@ def get_info(tb_or_frame, lineno=None):
source = []
startline = lineno

# <Hack>
# Catch a rare case where the tb or frame has a wrong current line number
# or maybe a wrongly reported starting line of its code block.
# This seems to be a python bug or an `inspect.getsource` bug --
# OR a `getsource` bug that is ultimately a python bug, because `inspect`
# ultimately 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`. Instead of crashing,
# 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
# to the first available source line of the scope.
lastline = startline + len(source) - 1
corrected_lineno = lineno
if (lineno < startline):
corrected_lineno = startline
if (lineno > lastline):
corrected_lineno = lastline
if corrected_lineno != lineno:
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
# </Hack>

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

if function in NON_FUNCTION_SCOPES:
Expand Down
3 changes: 2 additions & 1 deletion stackprinter/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,8 @@ def format_exc_info(etype, evalue, tb, style='plaintext', add_summary='auto',
except Exception as exc:
import os
if 'PY_STACKPRINTER_DEBUG' in os.environ:
raise
import pdb
pdb.post_mortem(exc.__traceback__)

our_tb = traceback.format_exception(exc.__class__,
exc,
Expand Down