Repository navigation
Description
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
-
Save this as
run_warn.py:pass b"abc" == "abc" raise Exception()
-
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
BCSharp commentedon Dec 2, 2026
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$lineNoand thentb_framereports the current line. Otherwise the traceback creates a new frame object. If the traceback comes fromsys.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 thef_linenofix 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:
exec/eval, when compiled, use mechanism A.slozier commentedon Dec 2, 2026
I guess I should look at wrapping that up or scrapping it... 😜