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

[3.11] gh-107901: Fix missing line number on BACKWARD_JUMP at the end of a for loop #108326

Closed
wants to merge 4 commits into from
Closed
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
14 changes: 14 additions & 0 deletions Lib/test/test_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -990,6 +990,20 @@ async def test(aseq):
code_lines = self.get_code_lines(test.__code__)
self.assertEqual(expected_lines, code_lines)

def test_lineno_of_backward_jump(self):
# Issue gh-107901
def f():
for i in x:
if y:
pass

linenos = list(inst.positions.lineno
for inst in dis.get_instructions(f.__code__)
if inst.opname == 'JUMP_BACKWARD')

self.assertTrue(len(linenos) > 0)
self.assertTrue(all(l is not None for l in linenos))

def test_big_dict_literal(self):
# The compiler has a flushing point in "compiler_dict" that calls compiles
# a portion of the dictionary literal when the loop that iterates over the items
Expand Down
13 changes: 7 additions & 6 deletions Lib/test/test_dis.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,13 @@ def bug708901():
%3d PRECALL 2
CALL 2
GET_ITER
>> FOR_ITER 2 (to 40)
>> FOR_ITER 3 (to 42)
STORE_FAST 0 (res)

%3d JUMP_BACKWARD 3 (to 34)
%3d NOP

%3d >> LOAD_CONST 0 (None)
%3d JUMP_BACKWARD 4 (to 34)
>> LOAD_CONST 0 (None)
RETURN_VALUE
""" % (bug708901.__code__.co_firstlineno,
bug708901.__code__.co_firstlineno + 1,
Expand Down Expand Up @@ -622,9 +623,9 @@ def loop_test():
34 PRECALL_PYFUNC 1
38 CALL_PY_WITH_DEFAULTS 1
48 POP_TOP
50 JUMP_BACKWARD_QUICK 18 (to 16)

%3d >> 52 LOAD_CONST 0 (None)
%3d 50 JUMP_BACKWARD_QUICK 18 (to 16)
>> 52 LOAD_CONST 0 (None)
54 RETURN_VALUE
""" % (loop_test.__code__.co_firstlineno,
loop_test.__code__.co_firstlineno + 1,
Expand Down Expand Up @@ -1434,7 +1435,7 @@ def _prepare_test_cases():
Instruction(opname='POP_JUMP_FORWARD_IF_FALSE', opcode=114, arg=2, argval=96, argrepr='to 96', offset=90, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=92, starts_line=8, is_jump_target=False, positions=None),
Instruction(opname='JUMP_FORWARD', opcode=110, arg=16, argval=128, argrepr='to 128', offset=94, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='JUMP_BACKWARD', opcode=140, arg=33, argval=32, argrepr='to 32', offset=96, starts_line=7, is_jump_target=True, positions=None),
Instruction(opname='JUMP_BACKWARD', opcode=140, arg=33, argval=32, argrepr='to 32', offset=96, starts_line=3, is_jump_target=True, positions=None),
Instruction(opname='LOAD_GLOBAL', opcode=116, arg=3, argval='print', argrepr='NULL + print', offset=98, starts_line=10, is_jump_target=True, positions=None),
Instruction(opname='LOAD_CONST', opcode=100, arg=4, argval='I can haz else clause?', argrepr="'I can haz else clause?'", offset=110, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='PRECALL', opcode=166, arg=1, argval=1, argrepr='', offset=112, starts_line=None, is_jump_target=False, positions=None),
Expand Down
20 changes: 19 additions & 1 deletion Lib/test/test_sys_settrace.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,11 @@ def no_pop_tops(): # 0
(3, 'line'),
(6, 'line'),
(2, 'line'),
(2, 'line'),
(3, 'line'),
(4, 'line'),
(2, 'line'),
(2, 'line'),
(2, 'return')]

def no_pop_blocks():
Expand Down Expand Up @@ -299,7 +301,7 @@ def generator_example():
(-4, 'exception'),
(-1, 'line'),
(-1, 'return')] +
[(5, 'line'), (6, 'line')] * 10 +
[(5, 'line'), (6, 'line'), (5, 'line')] * 10 +
[(5, 'line'), (5, 'return')])


Expand Down Expand Up @@ -451,8 +453,10 @@ def for_example():
(1, 'line'),
(2, 'line'),
(1, 'line'),
(1, 'line'),
(2, 'line'),
(1, 'line'),
(1, 'line'),
(1, 'return')])

def while_example():
Expand Down Expand Up @@ -641,20 +645,23 @@ async def f():
(1, 'line'),
(-1, 'call'),
(-2, 'line'),
(-2, 'line'),
(-1, 'line'),
(-1, 'return'),
(1, 'exception'),
(2, 'line'),
(1, 'line'),
(-1, 'call'),
(-2, 'line'),
(-2, 'line'),
(-1, 'line'),
(-1, 'return'),
(1, 'exception'),
(2, 'line'),
(1, 'line'),
(-1, 'call'),
(-2, 'line'),
(-2, 'line'),
(-2, 'return'),
(1, 'exception'),
(1, 'return'),
Expand Down Expand Up @@ -765,14 +772,20 @@ def func():
(2, 'line'),
(3, 'line'),
(2, 'line'),
(2, 'line'),
(3, 'line'),
(2, 'line'),
(2, 'line'),
(1, 'line'),
(1, 'line'),
(2, 'line'),
(3, 'line'),
(2, 'line'),
(2, 'line'),
(3, 'line'),
(2, 'line'),
(2, 'line'),
(1, 'line'),
(1, 'line'),
(4, 'line'),
(4, 'return')])
Expand Down Expand Up @@ -830,6 +843,7 @@ def func():
(8, 'line'),
(10, 'line'),
(3, 'line'),
(3, 'line'),
(4, 'line'),
(5, 'line'),
(6, 'line'),
Expand Down Expand Up @@ -866,6 +880,7 @@ def func():
(8, 'line'),
(10, 'line'),
(3, 'line'),
(3, 'line'),
(4, 'line'),
(5, 'line'),
(6, 'line'),
Expand Down Expand Up @@ -1242,6 +1257,7 @@ def func():
(-2, 'line'),
(-2, 'return'),
(1, 'line'),
(1, 'line'),
(2, 'line'),
(-8, 'call'),
(-7, 'line'),
Expand All @@ -1253,6 +1269,7 @@ def func():
(-2, 'line'),
(-2, 'return'),
(1, 'line'),
(1, 'line'),
(1, 'return')])

def test_no_tracing_of_named_except_cleanup(self):
Expand Down Expand Up @@ -1551,6 +1568,7 @@ def func():
(-2, 'line'),
(-2, 'return'),
(4, 'line'),
(2, 'line'),
(1, 'line'),
(-2, 'call'),
(-2, 'return'),
Expand Down
8 changes: 4 additions & 4 deletions Lib/test/test_trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def test_traced_func_loop(self):
firstlineno = get_firstlineno(traced_func_loop)
expected = {
(self.my_py_filename, firstlineno + 1): 1,
(self.my_py_filename, firstlineno + 2): 6,
(self.my_py_filename, firstlineno + 2): 11,
(self.my_py_filename, firstlineno + 3): 5,
(self.my_py_filename, firstlineno + 4): 1,
}
Expand All @@ -172,10 +172,10 @@ def test_trace_func_generator(self):
firstlineno_gen = get_firstlineno(traced_func_generator)
expected = {
(self.my_py_filename, firstlineno_calling + 1): 1,
(self.my_py_filename, firstlineno_calling + 2): 11,
(self.my_py_filename, firstlineno_calling + 2): 21,
(self.my_py_filename, firstlineno_calling + 3): 10,
(self.my_py_filename, firstlineno_gen + 1): 1,
(self.my_py_filename, firstlineno_gen + 2): 11,
(self.my_py_filename, firstlineno_gen + 2): 21,
(self.my_py_filename, firstlineno_gen + 3): 10,
}
self.assertEqual(self.tracer.results().counts, expected)
Expand Down Expand Up @@ -246,7 +246,7 @@ def test_exec_counts(self):
firstlineno = get_firstlineno(traced_func_loop)
expected = {
(self.my_py_filename, firstlineno + 1): 1,
(self.my_py_filename, firstlineno + 2): 6,
(self.my_py_filename, firstlineno + 2): 11,
(self.my_py_filename, firstlineno + 3): 5,
(self.my_py_filename, firstlineno + 4): 1,
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix missing line number on :opcode:`JUMP_BACKWARD` at the end of a for loop.
6 changes: 3 additions & 3 deletions Programs/test_frozenmain.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -3123,7 +3123,7 @@ compiler_for(struct compiler *c, stmt_ty s)
VISIT(c, expr, s->v.For.target);
VISIT_SEQ(c, stmt, s->v.For.body);
/* Mark jump as artificial */
UNSET_LOC(c);
SET_LOC(c, s->v.For.iter);
ADDOP_JUMP(c, JUMP, start);
compiler_use_next_block(c, cleanup);

Expand Down