Skip to content

Conversation

@graingert
Copy link
Contributor

@graingert graingert commented Mar 22, 2022

Lib/inspect.py Outdated
Copy link
Contributor Author

@graingert graingert Mar 22, 2022

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

@graingert graingert changed the title bpo-issue45563 handle frame.f_lineno is None in inspect.getframeinfo bpo-45563 handle frame.f_lineno is None in inspect.getframeinfo Mar 22, 2022
@graingert graingert force-pushed the fix-inspect-get-frame-info branch from 0354393 to 387b376 Compare March 22, 2022 10:16
@markshannon
Copy link
Member

What are you trying to fix here?

@graingert graingert marked this pull request as draft March 22, 2022 10:56
@graingert graingert force-pushed the fix-inspect-get-frame-info branch from 387b376 to 0fd59f0 Compare March 22, 2022 10:59
@graingert
Copy link
Contributor Author

graingert commented Mar 22, 2022

What are you trying to fix here?

frame can sometimes have a frame.f_lineno=None and so inspect.getframeinfo fails eg:

import sys
import inspect

def demo():
    for i in range(1):
        if i >= 0:
            pass


class Tracer:
    def __init__(self):
        self.events = []

    def trace(self, frame, event, arg):
        inspect.getframeinfo(frame)
        frame.f_trace_lines = True
        frame.f_trace_opcodes = True
        return self.trace

def main():
    t = Tracer()
    old_trace = sys.gettrace()
    try:
        sys.settrace(t.trace)
        demo()
    finally:
        sys.settrace(old_trace)


if __name__ == "__main__":
    sys.exit(main())
Traceback (most recent call last):
  File "/home/graingert/projects/cpython/demo.py", line 31, in <module>
    sys.exit(main())
  File "/home/graingert/projects/cpython/demo.py", line 25, in main
    demo()
  File "/home/graingert/projects/cpython/demo.py", line -1, in demo
  File "/home/graingert/projects/cpython/demo.py", line -1, in demo
  File "/home/graingert/projects/cpython/demo.py", line 15, in trace
    inspect.getframeinfo(frame)
  File "/usr/lib/python3.10/inspect.py", line 1627, in getframeinfo
    start = lineno - 1 - context//2
TypeError: unsupported operand type(s) for -: 'NoneType' and 'int'

@graingert graingert marked this pull request as ready for review March 22, 2022 11:55
@markshannon
Copy link
Member

I would make more sense IMO to fix inspect to not crash, rather than trying to fake a line number.

@graingert graingert changed the title bpo-45563 handle frame.f_lineno is None in inspect.getframeinfo gh-89726 handle frame.f_lineno is None in inspect.getframeinfo Jul 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants