Skip to content

frame.f_lineno is always 1 unless tracing is enabled #2130

Open

Description

@BCSharp

Description

A frame reports line 1 as its current line unless tracing is enabled. Everything that reads the current line from a frame gets that value, including warnings, logging, inspect and traceback.extract_stack().

With default options:

  • warnings point at line 1 of the file and quote that line as the offending source
  • warnings that differ only by line are dropped
  • log records and stack summaries carry the wrong line number

Exception tracebacks track lines separately, so a traceback and a warning from the same program can disagree.

Steps to Reproduce

  1. Save this as run_warn.py:

    pass
    b"abc" == "abc"
    raise Exception()
  2. Run ipy -b run_warn.py.

Expected behaviour:

Output from CPython 3.4.4:

run_warn.py:2: BytesWarning: Comparison between bytes and string
  b"abc" == "abc"
Traceback (most recent call last):
  File "run_warn.py", line 3, in <module>
    raise Exception()
Exception

Actual behaviour:

run_warn.py:1: BytesWarning: Comparison between bytes and string
  pass
Traceback (most recent call last):
  File "run_warn.py", line 3, in <module>
Exception

The warning reports line 1 and the traceback reports the correct line 3. The 1 is a constant: if line 1 of the file is a comment, the warning quotes that comment.

This script shows various readers of the current line:

import inspect, logging, sys, traceback, warnings

logging.basicConfig(format="logging:       %(lineno)d", stream=sys.stdout)

def main():
    print("f_lineno:     ", sys._getframe().f_lineno)
    print("caller:       ", sys._getframe(1).f_lineno)
    print("inspect:      ", inspect.getframeinfo(inspect.currentframe()).lineno)
    print("extract_stack:", traceback.extract_stack()[-1][1])
    logging.warning("message")
    with warnings.catch_warnings(record=True) as caught:
        warnings.simplefilter("always")
        warnings.warn("message")
    print("warnings:     ", caught[0].lineno)

main()
Read through CPython 3.4.4 IronPython
sys._getframe().f_lineno 6 1
sys._getframe(1).f_lineno 16 1
inspect.getframeinfo().lineno 8 1
traceback.extract_stack() 9 1
logging, %(lineno)d 10 1
warnings, WarningMessage.lineno 13 1

This issue is present in IronPython from as far as I remember; I noticed it when working on str/bytes transition from IronPython 2 to 3 (thus -b warnings). If the NumPy-lile operators are implemented and the old use triggers a warning, there should be a working way to filter out or suppress those warnings.

Activity

  1. BCSharp commented on Dec 2, 2026

    @BCSharp
    MemberAuthor

    The compiled code already counts its line numbers. That's why exceptions report correct line numbers. In the compiled body, the current line number is stored in a local $lineNo. It is updated only at statements that can throw, and if an exception happens, a traceback reuses the frame object if the function already has one, copies $lineNo and then tb_frame reports the current line. Otherwise the traceback creates a new frame object. If the traceback comes from sys.exc_info(), the new frame keeps the line at the time the traceback was created. If it comes from __traceback__ of the exception, the new frame reports 1.

    Warnings et al. need the line number at the moment when they are called, not during exception unwinding, and they have no access to $lineNo (which is expression-local). My preferred design for the f_lineno fix publishes each frame's current line through its frame-stack entry. I see two prospective mechanism for this:

    A. Raw pointer to the CLR local $lineNo. The beauty of this approach is that it cost no additional performance, until the line number is actually needed. The clumsiness is the raw pointer itself, although it is read-only.
    B. StrongBox<int> holder. This is architecturally clean, but it costs runtime performance, since the line number update now has to go to the box, rather to the fast local variable update. The cost has to be paid during regular execution, regardless whether the line number is being used or not.

    In dilemmas like this, IronPython traditionally chooses speed over cleanness. So my preference goes to mechanism A. But it can only work if the function frame stays guaranteed where it is through the whole function execution. This is not the case for generators, coroutines, and async generators. Luckily, it is the case the regular functions and classes, which is the vast majority of the code.

    So by necessity, B has to be used in some cases, which leads to a split approach:

    1. For ordinary functions and classes with their methods, module code, exec/eval, when compiled, use mechanism A.
    2. For interpreted code, and generators, coroutines, async generators, use mechanism B.
    3. For async comprehensions — I have no idea, somebody else is implementing it (Async comprehension #2023) 😉
  2. slozier commented on Dec 2, 2026

    @slozier
    Contributor

    3. For async comprehensions — I have no idea, somebody else is implementing it😉

    I guess I should look at wrapping that up or scrapping it... 😜

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions