Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

frame.f_lineno can be None #94485

Closed
fabioz opened this issue Jul 1, 2022 · 6 comments
Closed

frame.f_lineno can be None #94485

fabioz opened this issue Jul 1, 2022 · 6 comments
Assignees
Labels
3.11 only security fixes 3.12 bugs and security fixes type-bug An unexpected behavior, bug, or error

Comments

@fabioz
Copy link
Contributor

fabioz commented Jul 1, 2022

Bug report

As far as I know frame.f_lineno should never be None, yet, in the case below it is:

import sys
def tracefunc(frame, event, arg):
    if frame.f_lineno is None:
        raise AssertionError('frame.f_lineno is None!', frame)
    return tracefunc
sys.settrace(tracefunc)

# Apparently anything we import would give a frame.f_lineno = None during tracing.
import threading

Your environment

  • CPython versions tested on: 3.11.0b3 (main, Jun 1 2022, 13:29:14) [MSC v.1932 64 bit (AMD64)]
  • Operating system and architecture: Windows
@fabioz fabioz added the type-bug An unexpected behavior, bug, or error label Jul 1, 2022
@markshannon
Copy link
Member

The line number is meaningless when calling a module during an import.
If there were a line number, it would create spurious "line" events when tracing.

If you change tracefunc to:

def tracefunc(frame, event, arg):
    if frame.f_lineno is None and event != "call":
        raise AssertionError('frame.f_lineno is None!', frame)
    return tracefunc

No assertion should be raised.

@markshannon
Copy link
Member

We might be able to set the line number to 0, https://peps.python.org/pep-0626/#the-f-lineno-attribute, without producing a line event.

@iritkatriel iritkatriel added deferred-blocker 3.11 only security fixes 3.12 bugs and security fixes labels Jul 1, 2022
@markshannon
Copy link
Member

As far as I know frame.f_lineno should never be None

It is possible in unusual circumstances, such as when the cycle GC invokes Python or a keyboard interrupt, but it shouldn't happen in normal code execution.

@fabioz
Copy link
Contributor Author

fabioz commented Jul 1, 2022

As far as I know frame.f_lineno should never be None

It is possible in unusual circumstances, such as when the cycle GC invokes Python or a keyboard interrupt, but it shouldn't happen in normal code execution.

This seems a bit surprising for me... I went on looking for docs but there's surprisingly few information about this (my use case is that I was typing things with cython and it's breaking due to being None in Python 3.11 -- I had never seen a report for that previously where frame.f_lineno could be anything but an integer).

Given that the first valid line number is 1, if it's possible to set it to 0 to indicate an invalid number while keeping the same type would make things better IMHO.

@pablogsal pablogsal changed the title [Python 3.11] frame.f_lineno can be None frame.f_lineno can be None Jul 4, 2022
@pablogsal
Copy link
Member

Moving this back to release blocker.

ambv pushed a commit that referenced this issue Jul 5, 2022
…cified by PEP 626 (GH-94552)

Co-authored-by: Mark Shannon <mark@hotpy.org>
ambv pushed a commit to ambv/cpython that referenced this issue Jul 5, 2022
… to 0, as specified by PEP 626 (pythonGH-94552)

Co-authored-by: Mark Shannon <mark@hotpy.org>
(cherry picked from commit 324d019)

Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
ambv added a commit that referenced this issue Jul 5, 2022
…as specified by PEP 626 (GH-94552) (GH-94562)

Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
Co-authored-by: Mark Shannon <mark@hotpy.org>

(cherry picked from commit 324d019)
@tiran
Copy link
Member

tiran commented Jul 5, 2022

Main and 3.11 branch are now fixed. Thank you @iritkatriel for the fix and @ambv for the backport.

vpelletier added a commit to vpelletier/pprofile that referenced this issue Sep 23, 2023
Workaround f_lineno being None in some edge cases in some python
3.10 versions. Apparently this only affects module imports, which
are traced as calls:
  python/cpython#94485
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.11 only security fixes 3.12 bugs and security fixes type-bug An unexpected behavior, bug, or error
Projects
5 participants