Skip to content

Commit 7ebd746

Browse files
authored
Set line number of END_ASYNC_FOR so that it doesn't show in traces. (GH-27255)
1 parent 9ee12cf commit 7ebd746

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

Lib/test/test_sys_settrace.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,50 @@ def run(tracer):
602602
self.compare_events(doit_async.__code__.co_firstlineno,
603603
tracer.events, events)
604604

605+
def test_21_async_for_else(self):
606+
607+
async def async_gen():
608+
yield -2
609+
610+
async def async_test():
611+
global a
612+
a = 2
613+
async for i in async_gen():
614+
a = 4
615+
else:
616+
a = 6
617+
618+
def run(tracer):
619+
x = async_test()
620+
try:
621+
sys.settrace(tracer)
622+
x.send(None)
623+
finally:
624+
sys.settrace(None)
625+
626+
tracer = self.make_tracer()
627+
events = [
628+
(0, 'call'),
629+
(2, 'line'),
630+
(3, 'line'),
631+
(-3, 'call'),
632+
(-2, 'line'),
633+
(-2, 'return'),
634+
(3, 'exception'),
635+
(4, 'line'),
636+
(3, 'line'),
637+
(-2, 'call'),
638+
(-2, 'return'),
639+
(3, 'exception'),
640+
(6, 'line'),
641+
(6, 'return')]
642+
try:
643+
run(tracer.trace)
644+
except Exception:
645+
pass
646+
self.compare_events(async_test.__code__.co_firstlineno,
647+
tracer.events, events)
648+
605649

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

Python/compile.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2816,6 +2816,12 @@ compiler_async_for(struct compiler *c, stmt_ty s)
28162816

28172817
/* Except block for __anext__ */
28182818
compiler_use_next_block(c, except);
2819+
2820+
/* We don't want to trace the END_ASYNC_FOR, so make sure
2821+
* that it has the same lineno as the following instruction. */
2822+
if (asdl_seq_LEN(s->v.For.orelse)) {
2823+
SET_LOC(c, (stmt_ty)asdl_seq_GET(s->v.For.orelse, 0));
2824+
}
28192825
ADDOP(c, END_ASYNC_FOR);
28202826

28212827
/* `else` block */

0 commit comments

Comments
 (0)