Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions src/core/IronPython/Compiler/Ast/PythonAst.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ public void ParsingFinished(int[] lineLocations, Statement body, ModuleOptions l
_body = body;
_lineLocations = lineLocations;
_languageFeatures = languageFeatures;

// As in CPython 3.4, the code starts at its first statement (which gives co_firstlineno) and ends with its body.
int start = Math.Max(body is SuiteStatement { Statements: [var first, ..] } ? first.StartIndex : body.StartIndex, 0);
IndexSpan = new IndexSpan(start, Math.Max(body.EndIndex - start, 0));
}

/// <summary>
Expand Down Expand Up @@ -369,7 +373,7 @@ internal MSAst.Expression ReduceWorker() {
var end = IndexToLocation(ret.Expression.EndIndex);

return Ast.Block(
MakeEntrySequencePoint(ret.Expression.StartIndex),
MakeEntrySequencePoint(),
Ast.DebugInfo(
_document,
start.Line,
Expand All @@ -385,7 +389,7 @@ internal MSAst.Expression ReduceWorker() {

// Add beginning sequence point: the location of the 'call' trace event, as in FunctionDefinition.
// Without it, the first statement would take that role, and fire no 'line' event of its own.
block.Add(MakeEntrySequencePoint(_body is SuiteStatement { Statements: [var first, ..] } ? first.StartIndex : _body.StartIndex));
block.Add(MakeEntrySequencePoint());
AddInitialization(block);

string doc = GetDocumentation(_body);
Expand Down Expand Up @@ -421,10 +425,14 @@ internal MSAst.Expression ReduceWorker() {
return body;
}

private MSAst.Expression MakeEntrySequencePoint(int index) {
var loc = index >= 0 ? IndexToLocation(index) : new SourceLocation(0, 1, 1);
return AstUtils.AddDebugInfo(
AstUtils.Empty(), _document, new SourceLocation(0, loc.Line, loc.Column), new SourceLocation(0, loc.Line, int.MaxValue));
/// <summary>
/// Makes the sequence point of the 'call' trace event, at the start of the code (see <see cref="ParsingFinished"/>).
/// </summary>
private MSAst.Expression MakeEntrySequencePoint() {
var loc = Start;
return AddDebugInfo(
AstUtils.Empty(),
new SourceSpan(new SourceLocation(0, loc.Line, loc.Column), new SourceLocation(0, loc.Line, int.MaxValue)));
}


Expand Down
2 changes: 1 addition & 1 deletion src/core/IronPython/Compiler/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ public PythonAst ParseTopExpression() {
// TODO: move from source unit .TrimStart(' ', '\t')
_globalParent = new PythonAst(false, _languageFeatures, false, _context);
ReturnStatement ret = new ReturnStatement(ParseTestListAsExpression());
ret.SetLoc(_globalParent, 0, 0);
ret.SetLoc(_globalParent, ret.Expression.IndexSpan);
return FinishParsing(ret);
} catch (DecoderFallbackException dfe) {
throw BadSourceError(dfe);
Expand Down
35 changes: 35 additions & 0 deletions tests/suite/modules/system_related/test_sys.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ def test_settrace_first_line(self):
# The first statement of module code gets a 'line' event of its own, as in a function.
# The 'call' event of module code is on the line of its first statement, as in Python 3.4.
# CPython changed that later: 3.10 puts it always on line 1, and 3.11 on line 0
# That line is also the co_firstlineno of module code, which 3.10 and later set to 1.
check_module_call_line = is_cli or sys.version_info < (3, 10)

cases = [
Expand All @@ -208,6 +209,7 @@ def test_settrace_first_line(self):
("'doc'\nx = 1\n", "exec", [('call', 1), ('line', 1), ('line', 2), ('return', 2)]),
("def g(): pass\ng()\n", "exec", [('call', 1), ('line', 1), ('line', 2), ('call', 1), ('line', 1), ('return', 1), ('return', 2)]),
("1 + 2", "eval", [('call', 1), ('line', 1), ('return', 1)]),
("\n\n(1 +\n 2)", "eval", [('call', 3), ('line', 3), ('return', 3)]),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one is failing for me on CPython 3.4 and 3.6 (but passing on 3.7). Maybe add a conditonal?

("x = 1", "single", [('call', 1), ('line', 1), ('return', 1)]),
]

Expand All @@ -224,6 +226,9 @@ def f(frame, event, arg):
expected = [('call', None)] + expected[1:]

code = compile(source, '<traced>', mode)
if check_module_call_line:
self.assertEqual(code.co_firstlineno, expected[0][1], (source, mode))

try:
sys.settrace(f)
if mode == 'eval':
Expand All @@ -235,6 +240,36 @@ def f(frame, event, arg):

self.assertEqual(events, expected, (source, mode))

def test_settrace_jump_in_module(self):
# A trace function can set f_lineno anywhere within module code, not only on its first line
source = "\n\nout.append(3)\nout.append(4)\n\nout.append(6)\n"
cases = [
(6, 4, [3, 4, 4, 6]), # backward
(3, 6, [6]), # forward
]

orig_sys_trace_func = sys.gettrace()
for jump_from, jump_to, expected in cases:
# TODO: On IronPython, a jump works only in the code compiled first under a given file name, hence a unique name for each case
filename = '<jump from %d to %d>' % (jump_from, jump_to)
jumped = False
def f(frame, event, arg):
nonlocal jumped
if frame.f_code.co_filename == filename and event == 'line' and frame.f_lineno == jump_from and not jumped:
jumped = True
frame.f_lineno = jump_to
return f

out = []
code = compile(source, filename, 'exec')
try:
sys.settrace(f)
exec(code, {'out': out})
finally:
sys.settrace(orig_sys_trace_func)

self.assertEqual(out, expected, (jump_from, jump_to))

def test_cp30130(self):
def f(frame, event, arg):
if event == 'exception':
Expand Down
Loading