Skip to content

Commit c1b1249

Browse files
authored
gh-93061: Mark as artificial: backwards jump after async for (GH-93120)
1 parent 251104f commit c1b1249

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

Lib/test/test_sys_settrace.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,58 @@ def run(tracer):
607607
self.compare_events(doit_async.__code__.co_firstlineno,
608608
tracer.events, events)
609609

610+
def test_async_for_backwards_jump_has_no_line(self):
611+
async def arange(n):
612+
for i in range(n):
613+
yield i
614+
async def f():
615+
async for i in arange(3):
616+
if i > 100:
617+
break # should never be traced
618+
619+
tracer = self.make_tracer()
620+
coro = f()
621+
try:
622+
sys.settrace(tracer.trace)
623+
coro.send(None)
624+
except Exception:
625+
pass
626+
finally:
627+
sys.settrace(None)
628+
629+
events = [
630+
(0, 'call'),
631+
(1, 'line'),
632+
(-3, 'call'),
633+
(-2, 'line'),
634+
(-1, 'line'),
635+
(-1, 'return'),
636+
(1, 'exception'),
637+
(2, 'line'),
638+
(1, 'line'),
639+
(-1, 'call'),
640+
(-2, 'line'),
641+
(-1, 'line'),
642+
(-1, 'return'),
643+
(1, 'exception'),
644+
(2, 'line'),
645+
(1, 'line'),
646+
(-1, 'call'),
647+
(-2, 'line'),
648+
(-1, 'line'),
649+
(-1, 'return'),
650+
(1, 'exception'),
651+
(2, 'line'),
652+
(1, 'line'),
653+
(-1, 'call'),
654+
(-2, 'line'),
655+
(-2, 'return'),
656+
(1, 'exception'),
657+
(1, 'return'),
658+
]
659+
self.compare_events(f.__code__.co_firstlineno,
660+
tracer.events, events)
661+
610662
def test_21_repeated_pass(self):
611663
def func():
612664
pass
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Backward jumps after ``async for`` loops are no longer given dubious line numbers.

Python/compile.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2925,6 +2925,8 @@ compiler_async_for(struct compiler *c, stmt_ty s)
29252925
/* Success block for __anext__ */
29262926
VISIT(c, expr, s->v.AsyncFor.target);
29272927
VISIT_SEQ(c, stmt, s->v.AsyncFor.body);
2928+
/* Mark jump as artificial */
2929+
c->u->u_lineno = -1;
29282930
ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
29292931

29302932
compiler_pop_fblock(c, FOR_LOOP, start);

0 commit comments

Comments
 (0)