Skip to content

Commit f5e97b7

Browse files
authored
bpo-42635: Mark JUMP_ABSOLUTE at end of 'for' loop as artificial to avoid spurious line events. (GH-23761)
1 parent 56aa20f commit f5e97b7

File tree

4 files changed

+3795
-3767
lines changed

4 files changed

+3795
-3767
lines changed

Lib/test/test_sys_settrace.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,32 @@ def func():
646646
(6, 'line'),
647647
(6, 'return')])
648648

649+
def test_nested_loops(self):
650+
651+
def func():
652+
for i in range(2):
653+
for j in range(2):
654+
a = i + j
655+
return a == 1
656+
657+
self.run_and_compare(func,
658+
[(0, 'call'),
659+
(1, 'line'),
660+
(2, 'line'),
661+
(3, 'line'),
662+
(2, 'line'),
663+
(3, 'line'),
664+
(2, 'line'),
665+
(1, 'line'),
666+
(2, 'line'),
667+
(3, 'line'),
668+
(2, 'line'),
669+
(3, 'line'),
670+
(2, 'line'),
671+
(1, 'line'),
672+
(4, 'line'),
673+
(4, 'return')])
674+
649675

650676
class SkipLineEventsTraceTestCase(TraceTestCase):
651677
"""Repeat the trace tests, but with per-line events skipped"""

Python/compile.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2773,6 +2773,8 @@ compiler_for(struct compiler *c, stmt_ty s)
27732773
compiler_use_next_block(c, body);
27742774
VISIT(c, expr, s->v.For.target);
27752775
VISIT_SEQ(c, stmt, s->v.For.body);
2776+
/* Mark jump as artificial */
2777+
c->u->u_lineno = -1;
27762778
ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
27772779
compiler_use_next_block(c, cleanup);
27782780

0 commit comments

Comments
 (0)