Skip to content

gh-89726 handle frame.f_lineno is None in inspect.getframeinfo - #32044

Open
graingert wants to merge 2 commits into
python:mainfrom
graingert:fix-inspect-get-frame-info
Open

gh-89726 handle frame.f_lineno is None in inspect.getframeinfo#32044
graingert wants to merge 2 commits into
python:mainfrom
graingert:fix-inspect-get-frame-info

Conversation

@graingert

@graingert graingert commented Mar 22, 2022

Copy link
Copy Markdown
Contributor

Comment thread Lib/inspect.py Outdated
@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
Copy Markdown
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

graingert commented Mar 22, 2022

Copy link
Copy Markdown
Contributor Author

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
Copy Markdown
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
@graingert
graingert marked this pull request as draft July 18, 2022 15:41
@github-actions

Copy link
Copy Markdown

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Apr 10, 2026
@graingert
graingert force-pushed the fix-inspect-get-frame-info branch from c83eb39 to f33497b Compare September 2, 2026 09:17
Per review, do not try to recover a line number for a frame stopped on
an instruction that carries no line: just skip the source-context
lookup that assumed lineno was an int, and let getframeinfo() report
lineno, code_context and index as None.

The previous approach did not work anyway.  _lasti2lineno() walked
dis.findlinestarts(), which since 3.11 yields None line numbers of its
own, so it returned None for exactly the frames it was meant to fix.

Add a regression test that reproduces the original TypeError.
@graingert
graingert marked this pull request as ready for review September 2, 2026 09:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review stale Stale PR or inactive for long period of time.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants