Skip to content

Commit a458be3

Browse files
authored
gh-93061: Mark as artificial: backwards jump after async for (GH-93062)
1 parent fc00667 commit a458be3

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

Lib/test/test_sys_settrace.py

+52
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,58 @@ def run(tracer):
609609
self.compare_events(doit_async.__code__.co_firstlineno,
610610
tracer.events, events)
611611

612+
def test_async_for_backwards_jump_has_no_line(self):
613+
async def arange(n):
614+
for i in range(n):
615+
yield i
616+
async def f():
617+
async for i in arange(3):
618+
if i > 100:
619+
break # should never be traced
620+
621+
tracer = self.make_tracer()
622+
coro = f()
623+
try:
624+
sys.settrace(tracer.trace)
625+
coro.send(None)
626+
except Exception:
627+
pass
628+
finally:
629+
sys.settrace(None)
630+
631+
events = [
632+
(0, 'call'),
633+
(1, 'line'),
634+
(-3, 'call'),
635+
(-2, 'line'),
636+
(-1, 'line'),
637+
(-1, 'return'),
638+
(1, 'exception'),
639+
(2, 'line'),
640+
(1, 'line'),
641+
(-1, 'call'),
642+
(-2, 'line'),
643+
(-1, 'line'),
644+
(-1, 'return'),
645+
(1, 'exception'),
646+
(2, 'line'),
647+
(1, 'line'),
648+
(-1, 'call'),
649+
(-2, 'line'),
650+
(-1, 'line'),
651+
(-1, 'return'),
652+
(1, 'exception'),
653+
(2, 'line'),
654+
(1, 'line'),
655+
(-1, 'call'),
656+
(-2, 'line'),
657+
(-2, 'return'),
658+
(1, 'exception'),
659+
(1, 'return'),
660+
]
661+
self.compare_events(f.__code__.co_firstlineno,
662+
tracer.events, events)
663+
612664
def test_21_repeated_pass(self):
613665
def func():
614666
pass
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

+2
Original file line numberDiff line numberDiff line change
@@ -3148,6 +3148,8 @@ compiler_async_for(struct compiler *c, stmt_ty s)
31483148
/* Success block for __anext__ */
31493149
VISIT(c, expr, s->v.AsyncFor.target);
31503150
VISIT_SEQ(c, stmt, s->v.AsyncFor.body);
3151+
/* Mark jump as artificial */
3152+
UNSET_LOC(c);
31513153
ADDOP_JUMP(c, JUMP, start);
31523154

31533155
compiler_pop_fblock(c, FOR_LOOP, start);

0 commit comments

Comments
 (0)