Repository navigation
Conversation
|
I am marking this PR as ready for review, as the mechanism A is now complete. It doesn't mean that the line number will be correct in all circumstances for compiled functions (though it will be for warnings). The main gap is that it relies on |
There was a problem hiding this comment.
🟡 Changes recommended
With adaptive compilation disabled, compiled eval still reports incorrect line numbers because the simple-body branch does not publish a line-number local.
1 open issue
What changed in this PR
Improves f_lineno reporting for compiled, non-suspending IronPython code, addressing incorrect line numbers used by warnings and stack inspection.
Changes:
- Publishes live line numbers through thread-local frame entries.
- Saves line numbers when frames are popped or created for tracebacks.
- Adds regression tests in compiled and debug modes.
| File | Description |
|---|---|
| tests/suite/test_frame_lineno.py | Tests current and retained frame line numbers. |
| tests/suite/test_frame_lineno_debug.py | Runs frame tests in debug mode. |
| tests/IronPython.Tests/Cases/IronPythonCasesManifest.ini | Configures compilation modes for tests. |
| src/core/IronPython/Runtime/Operations/PythonOps.cs | Publishes and saves frame line numbers. |
| src/core/IronPython/Runtime/Exceptions/TraceBack.cs | Reads live or saved frame lines. |
| src/core/IronPython/Modules/sys.cs | Preserves line pointers when creating frames. |
| src/core/IronPython/Compiler/Ast/PythonAst.cs | Configures module-frame line publication. |
| src/core/IronPython/Compiler/Ast/Node.cs | Separates compiled and interpreted frame handling. |
| src/core/IronPython/Compiler/Ast/AstMethods.cs | Registers updated frame helper methods. |
Review effort: Balanced
slozier
left a comment
There was a problem hiding this comment.
Thanks! Looks good to me.
I spent some time chasing a performance regression (~30%) this causes on function calls on .NET Framework (because unfortunately I still care about that). It turns out using pointers in a SecurityTransparent assembly adds a lot of overhead. Anyway, my conclusion is that I'll be dropping [assembly: SecurityTransparent] from the IronPython assemblies (most likely a Silverlight leftover).
Small notes from Claude (take it or leave it):
- I'd make
publishLineNumbera required parameter ofAddFrame.LineNumberExpressionis shared, so a new call site in a nested lambda would silently capture the outer variable into a closure. Release builds would then publish a pointer into the heap. - For the interpreted follow-up: the line could come from the interpreted frame's current instruction and its debug info. Module-level code is always interpreted by default, so that's where module-level warnings still show line 1. Generators and async will need a heap-based line anyway.
- Possible small follow-up:
tb_frame.f_linenois still 1 for frames built fromDynamicStackFrames (functions already unwound), though the line is available there. - Copilot's
evalthread seems low priority to me:evalis a single expression, so line 1 is almost always right.

This PR implements mechanism A from #2130, so it is only in effect for compiled regular code.
Interpreted code, async, and generators still do not update their line number. That's for another PR.
How it works: compiled code that pushes and pops its own frame on one thread (functions, class bodies, and module, exec and eval code) now publishes a pointer to its
$lineNolocal in its frame stack entry.f_linenoreads it only through an entry on the current thread's stack; when the entry is popped, or a traceback creates a frame for it, the frame saves the line number.FramedCodeExpressionis anIInstructionProvider, so the interpreter gets the push without the pointer: it passes the by-ref argument as a copy. Release builds now pop throughPythonOps.PopFrame, which saves the line before removing the entry.This can be further optimized, which I save for a separate commit in this PR.