Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Draft
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
32 changes: 30 additions & 2 deletions 32 Lib/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -1673,6 +1673,10 @@ def getframeinfo(frame, context=1):
The optional second argument specifies the number of lines of context
to return, which are centered around the current line."""
if istraceback(frame):
lineno = _tblineno(frame)
frame = frame.tb_frame
else:
lineno = getlineno(frame)
positions = _get_code_position_from_tb(frame)
lineno = frame.tb_lineno
frame = frame.tb_frame
Expand Down Expand Up @@ -1707,10 +1711,34 @@ def getframeinfo(frame, context=1):
return Traceback(filename, lineno, frame.f_code.co_name, lines,
index, positions=dis.Positions(*positions))


def _tblineno(tb):
tb_lineno = tb.tb_lineno
if tb_lineno is not None:
return tb_lineno

return _lasti2lineno(tb.tb_frame.f_code, tb.tb_lasti)


def _lasti2lineno(code, lasti):

@graingert graingert Mar 22, 2022

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there's a lasti2lineno implementation here - but it uses a reversed list(dis.finelinestarts(code)) and starts at 0 instead of co_firstlineno

cpython/Lib/pdb.py

Lines 119 to 126 in deeaac4

def lasti2lineno(code, lasti):
linestarts = list(dis.findlinestarts(code))
linestarts.reverse()
for i, lineno in linestarts:
if lasti >= i:
return lineno
return 0

prev_line = code.co_firstlineno

for start, next_line in dis.findlinestarts(code):
if lasti < start:
return prev_line
prev_line = next_line

return prev_line

def getlineno(frame):
"""Get the line number from a frame object, allowing for optimization."""
# FrameType.f_lineno is now a descriptor that grovels co_lnotab
return frame.f_lineno
# FrameType.f_lineno is now a descriptor that often grovels co_lnotab
f_lineno = frame.f_lineno
if f_lineno is not None:
return f_lineno

return _lasti2lineno(frame.f_code, frame.f_lasti)


_FrameInfo = namedtuple('_FrameInfo', ('frame',) + Traceback._fields)
class FrameInfo(_FrameInfo):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
handle frame.f_lineno is None in inspect.getframeinfo
Morty Proxy This is a proxified and sanitized view of the page, visit original site.