diff --git a/Lib/test/test_monitoring.py b/Lib/test/test_monitoring.py index 24cc51f4b4053ab..2f1c4a5ebbfc10e 100644 --- a/Lib/test/test_monitoring.py +++ b/Lib/test/test_monitoring.py @@ -2089,6 +2089,56 @@ async def foo(): ('branch right', 'foo', 1, 3), ('branch left', 'func', 12, 12)]) + def test_async_comprehension(self): + + def func(): + async def gen(): + yield 2 + yield 3 + + async def foo(): + return [x async for x in gen()] + + try: + foo().send(None) + except StopIteration: + pass + + self.check_events(func, recorders = BRANCHES_RECORDERS, expected = [ + ('branch left', 'foo', 1, 1), + ('branch left', 'foo', 1, 1), + ('branch right', 'foo', 1, 1), + ('branch left', 'func', 10, 10)]) + + def test_multiple_async_for(self): + + def func(): + async def gen(): + yield 2 + yield 3 + + async def foo(): + async for y in gen(): + 2 + pass # line 3 + async for y in gen(): + 4 + pass # line 5 + + try: + foo().send(None) + except StopIteration: + pass + + self.check_events(func, recorders = BRANCHES_RECORDERS, expected = [ + ('branch left', 'foo', 1, 1), + ('branch left', 'foo', 1, 1), + ('branch right', 'foo', 1, 3), + ('branch left', 'foo', 4, 4), + ('branch left', 'foo', 4, 4), + ('branch right', 'foo', 4, 6), + ('branch left', 'func', 15, 15)]) + def test_match(self): @@ -2242,6 +2292,41 @@ def func(): self.check_branches(func, foo) + def test_async_comprehension(self): + + async def gen(): + yield 2 + yield 3 + + async def foo(): + return [x async for x in gen()] + + def func(): + try: + foo().send(None) + except StopIteration: + pass + + self.check_branches(func, foo) + + def test_multiple_async_for(self): + + async def gen(): + yield 2 + yield 3 + + async def foo(): + async for x in gen(): pass + async for x in gen(): pass + + def func(): + try: + foo().send(None) + except StopIteration: + pass + + self.check_branches(func, foo) + class TestLoadSuperAttr(CheckEvents): RECORDERS = CallRecorder, LineRecorder, CRaiseRecorder, CReturnRecorder diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-11-15-13-42-53.gh-issue-162027.NZVOBA.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-15-13-42-53.gh-issue-162027.NZVOBA.rst new file mode 100644 index 000000000000000..f52dc888741e2e1 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-15-13-42-53.gh-issue-162027.NZVOBA.rst @@ -0,0 +1 @@ +Fix wrong branch monitoring events and assertions for async comprehensions and multiple consecutive ``async for`` loops. diff --git a/Python/assemble.c b/Python/assemble.c index 9f4ffb4050d2598..bb7b373a26c0c8d 100644 --- a/Python/assemble.c +++ b/Python/assemble.c @@ -719,9 +719,6 @@ makecode(_PyCompile_CodeUnitMetadata *umd, struct assembler *a, PyObject *const_ } -// The offset (in code units) of the END_SEND from the SEND in the `yield from` sequence. -#define END_SEND_OFFSET 6 - static int resolve_jump_offsets(instr_sequence *instrs) { @@ -761,9 +758,9 @@ resolve_jump_offsets(instr_sequence *instrs) instruction *target = &instrs->s_instrs[instr->i_target]; instr->i_oparg = target->i_offset; if (instr->i_opcode == END_ASYNC_FOR) { - // sys.monitoring needs to be able to find the matching END_SEND - // but the target is the SEND, so we adjust it here. - instr->i_oparg = offset - instr->i_oparg - END_SEND_OFFSET; + // sys.monitoring needs to be able to find the matching END_SEND. + assert(instr->i_oparg < offset); + instr->i_oparg = offset - instr->i_oparg; } else if (instr->i_oparg < offset) { assert(IS_BACKWARDS_JUMP_OPCODE(instr->i_opcode)); diff --git a/Python/codegen.c b/Python/codegen.c index a975705576a167c..1da809182acf545 100644 --- a/Python/codegen.c +++ b/Python/codegen.c @@ -444,7 +444,10 @@ codegen_addop_j(instr_sequence *seq, location loc, RETURN_IF_ERROR(addop_binary((C), (LOC), (BINOP), true)) #define ADD_YIELD_FROM(C, LOC, await) \ - RETURN_IF_ERROR(codegen_add_yield_from((C), (LOC), (await))) + RETURN_IF_ERROR(codegen_add_yield_from((C), (LOC), (await), NULL)) + +#define ADD_YIELD_FROM_WITH_EXIT(C, LOC, await, P_EXIT) \ + RETURN_IF_ERROR(codegen_add_yield_from((C), (LOC), (await), (P_EXIT))) #define POP_EXCEPT_AND_RERAISE(C, LOC) \ RETURN_IF_ERROR(codegen_pop_except_and_reraise((C), (LOC))) @@ -497,11 +500,14 @@ codegen_call_exit_with_nones(compiler *c, location loc) } static int -codegen_add_yield_from(compiler *c, location loc, int await) +codegen_add_yield_from(compiler *c, location loc, int await, jump_target_label *p_exit) { NEW_JUMP_TARGET_LABEL(c, send); NEW_JUMP_TARGET_LABEL(c, fail); NEW_JUMP_TARGET_LABEL(c, exit); + if (p_exit != NULL) { + *p_exit = exit; + } USE_LABEL(c, send); ADDOP_JUMP(c, loc, SEND, exit); @@ -2212,7 +2218,7 @@ codegen_async_for(compiler *c, stmt_ty s) location loc = LOC(s); NEW_JUMP_TARGET_LABEL(c, start); - NEW_JUMP_TARGET_LABEL(c, send); + NEW_JUMP_TARGET_LABEL(c, end_send); NEW_JUMP_TARGET_LABEL(c, except); NEW_JUMP_TARGET_LABEL(c, end); @@ -2227,8 +2233,7 @@ codegen_async_for(compiler *c, stmt_ty s) ADDOP(c, loc, GET_ANEXT); ADDOP(c, loc, PUSH_NULL); ADDOP_LOAD_CONST(c, loc, Py_None); - USE_LABEL(c, send); - ADD_YIELD_FROM(c, loc, 1); + ADD_YIELD_FROM_WITH_EXIT(c, loc, 1, &end_send); ADDOP(c, loc, POP_BLOCK); /* for SETUP_FINALLY */ ADDOP(c, loc, NOT_TAKEN); @@ -2246,7 +2251,7 @@ codegen_async_for(compiler *c, stmt_ty s) /* Use same line number as the iterator, * as the END_ASYNC_FOR succeeds the `for`, not the body. */ loc = LOC(s->v.AsyncFor.iter); - ADDOP_JUMP(c, loc, END_ASYNC_FOR, send); + ADDOP_JUMP(c, loc, END_ASYNC_FOR, end_send); /* `else` block */ VISIT_SEQ(c, stmt, s->v.AsyncFor.orelse); @@ -4814,7 +4819,7 @@ codegen_async_comprehension_generator(compiler *c, location loc, IterStackPosition iter_pos, bool avoid_creation) { NEW_JUMP_TARGET_LABEL(c, start); - NEW_JUMP_TARGET_LABEL(c, send); + NEW_JUMP_TARGET_LABEL(c, end_send); NEW_JUMP_TARGET_LABEL(c, except); NEW_JUMP_TARGET_LABEL(c, if_cleanup); @@ -4845,9 +4850,9 @@ codegen_async_comprehension_generator(compiler *c, location loc, ADDOP(c, loc, GET_ANEXT); ADDOP(c, loc, PUSH_NULL); ADDOP_LOAD_CONST(c, loc, Py_None); - USE_LABEL(c, send); - ADD_YIELD_FROM(c, loc, 1); + ADD_YIELD_FROM_WITH_EXIT(c, loc, 1, &end_send); ADDOP(c, loc, POP_BLOCK); + ADDOP(c, loc, NOT_TAKEN); VISIT(c, expr, gen->target); Py_ssize_t n = asdl_seq_LEN(gen->ifs); @@ -4886,7 +4891,7 @@ codegen_async_comprehension_generator(compiler *c, location loc, USE_LABEL(c, except); - ADDOP_JUMP(c, loc, END_ASYNC_FOR, send); + ADDOP_JUMP(c, loc, END_ASYNC_FOR, end_send); return SUCCESS; }