Skip to content

Commit c68d46b

Browse files
committed
gh-162027: Fix branch monitoring events for async comprehensions and loops
1 parent 8750d38 commit c68d46b

4 files changed

Lines changed: 104 additions & 16 deletions

File tree

‎Lib/test/test_monitoring.py‎

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2089,6 +2089,56 @@ async def foo():
20892089
('branch right', 'foo', 1, 3),
20902090
('branch left', 'func', 12, 12)])
20912091

2092+
def test_async_comprehension(self):
2093+
2094+
def func():
2095+
async def gen():
2096+
yield 2
2097+
yield 3
2098+
2099+
async def foo():
2100+
return [x async for x in gen()]
2101+
2102+
try:
2103+
foo().send(None)
2104+
except StopIteration:
2105+
pass
2106+
2107+
self.check_events(func, recorders = BRANCHES_RECORDERS, expected = [
2108+
('branch left', 'foo', 1, 1),
2109+
('branch left', 'foo', 1, 1),
2110+
('branch right', 'foo', 1, 1),
2111+
('branch left', 'func', 10, 10)])
2112+
2113+
def test_multiple_async_for(self):
2114+
2115+
def func():
2116+
async def gen():
2117+
yield 2
2118+
yield 3
2119+
2120+
async def foo():
2121+
async for y in gen():
2122+
2
2123+
pass # line 3
2124+
async for y in gen():
2125+
4
2126+
pass # line 5
2127+
2128+
try:
2129+
foo().send(None)
2130+
except StopIteration:
2131+
pass
2132+
2133+
self.check_events(func, recorders = BRANCHES_RECORDERS, expected = [
2134+
('branch left', 'foo', 1, 1),
2135+
('branch left', 'foo', 1, 1),
2136+
('branch right', 'foo', 1, 3),
2137+
('branch left', 'foo', 4, 4),
2138+
('branch left', 'foo', 4, 4),
2139+
('branch right', 'foo', 4, 6),
2140+
('branch left', 'func', 15, 15)])
2141+
20922142

20932143
def test_match(self):
20942144

@@ -2242,6 +2292,41 @@ def func():
22422292

22432293
self.check_branches(func, foo)
22442294

2295+
def test_async_comprehension(self):
2296+
2297+
async def gen():
2298+
yield 2
2299+
yield 3
2300+
2301+
async def foo():
2302+
return [x async for x in gen()]
2303+
2304+
def func():
2305+
try:
2306+
foo().send(None)
2307+
except StopIteration:
2308+
pass
2309+
2310+
self.check_branches(func, foo)
2311+
2312+
def test_multiple_async_for(self):
2313+
2314+
async def gen():
2315+
yield 2
2316+
yield 3
2317+
2318+
async def foo():
2319+
async for x in gen(): pass
2320+
async for x in gen(): pass
2321+
2322+
def func():
2323+
try:
2324+
foo().send(None)
2325+
except StopIteration:
2326+
pass
2327+
2328+
self.check_branches(func, foo)
2329+
22452330

22462331
class TestLoadSuperAttr(CheckEvents):
22472332
RECORDERS = CallRecorder, LineRecorder, CRaiseRecorder, CReturnRecorder
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix wrong branch monitoring events and assertions for async comprehensions and multiple consecutive ``async for`` loops.

‎Python/assemble.c‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -719,9 +719,6 @@ makecode(_PyCompile_CodeUnitMetadata *umd, struct assembler *a, PyObject *const_
719719
}
720720

721721

722-
// The offset (in code units) of the END_SEND from the SEND in the `yield from` sequence.
723-
#define END_SEND_OFFSET 6
724-
725722
static int
726723
resolve_jump_offsets(instr_sequence *instrs)
727724
{
@@ -761,9 +758,9 @@ resolve_jump_offsets(instr_sequence *instrs)
761758
instruction *target = &instrs->s_instrs[instr->i_target];
762759
instr->i_oparg = target->i_offset;
763760
if (instr->i_opcode == END_ASYNC_FOR) {
764-
// sys.monitoring needs to be able to find the matching END_SEND
765-
// but the target is the SEND, so we adjust it here.
766-
instr->i_oparg = offset - instr->i_oparg - END_SEND_OFFSET;
761+
// sys.monitoring needs to be able to find the matching END_SEND.
762+
assert(instr->i_oparg < offset);
763+
instr->i_oparg = offset - instr->i_oparg;
767764
}
768765
else if (instr->i_oparg < offset) {
769766
assert(IS_BACKWARDS_JUMP_OPCODE(instr->i_opcode));

‎Python/codegen.c‎

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,10 @@ codegen_addop_j(instr_sequence *seq, location loc,
444444
RETURN_IF_ERROR(addop_binary((C), (LOC), (BINOP), true))
445445

446446
#define ADD_YIELD_FROM(C, LOC, await) \
447-
RETURN_IF_ERROR(codegen_add_yield_from((C), (LOC), (await)))
447+
RETURN_IF_ERROR(codegen_add_yield_from((C), (LOC), (await), NULL))
448+
449+
#define ADD_YIELD_FROM_WITH_EXIT(C, LOC, await, P_EXIT) \
450+
RETURN_IF_ERROR(codegen_add_yield_from((C), (LOC), (await), (P_EXIT)))
448451

449452
#define POP_EXCEPT_AND_RERAISE(C, LOC) \
450453
RETURN_IF_ERROR(codegen_pop_except_and_reraise((C), (LOC)))
@@ -497,11 +500,14 @@ codegen_call_exit_with_nones(compiler *c, location loc)
497500
}
498501

499502
static int
500-
codegen_add_yield_from(compiler *c, location loc, int await)
503+
codegen_add_yield_from(compiler *c, location loc, int await, jump_target_label *p_exit)
501504
{
502505
NEW_JUMP_TARGET_LABEL(c, send);
503506
NEW_JUMP_TARGET_LABEL(c, fail);
504507
NEW_JUMP_TARGET_LABEL(c, exit);
508+
if (p_exit != NULL) {
509+
*p_exit = exit;
510+
}
505511

506512
USE_LABEL(c, send);
507513
ADDOP_JUMP(c, loc, SEND, exit);
@@ -2212,7 +2218,7 @@ codegen_async_for(compiler *c, stmt_ty s)
22122218
location loc = LOC(s);
22132219

22142220
NEW_JUMP_TARGET_LABEL(c, start);
2215-
NEW_JUMP_TARGET_LABEL(c, send);
2221+
NEW_JUMP_TARGET_LABEL(c, end_send);
22162222
NEW_JUMP_TARGET_LABEL(c, except);
22172223
NEW_JUMP_TARGET_LABEL(c, end);
22182224

@@ -2227,8 +2233,7 @@ codegen_async_for(compiler *c, stmt_ty s)
22272233
ADDOP(c, loc, GET_ANEXT);
22282234
ADDOP(c, loc, PUSH_NULL);
22292235
ADDOP_LOAD_CONST(c, loc, Py_None);
2230-
USE_LABEL(c, send);
2231-
ADD_YIELD_FROM(c, loc, 1);
2236+
ADD_YIELD_FROM_WITH_EXIT(c, loc, 1, &end_send);
22322237
ADDOP(c, loc, POP_BLOCK); /* for SETUP_FINALLY */
22332238
ADDOP(c, loc, NOT_TAKEN);
22342239

@@ -2246,7 +2251,7 @@ codegen_async_for(compiler *c, stmt_ty s)
22462251
/* Use same line number as the iterator,
22472252
* as the END_ASYNC_FOR succeeds the `for`, not the body. */
22482253
loc = LOC(s->v.AsyncFor.iter);
2249-
ADDOP_JUMP(c, loc, END_ASYNC_FOR, send);
2254+
ADDOP_JUMP(c, loc, END_ASYNC_FOR, end_send);
22502255

22512256
/* `else` block */
22522257
VISIT_SEQ(c, stmt, s->v.AsyncFor.orelse);
@@ -4814,7 +4819,7 @@ codegen_async_comprehension_generator(compiler *c, location loc,
48144819
IterStackPosition iter_pos, bool avoid_creation)
48154820
{
48164821
NEW_JUMP_TARGET_LABEL(c, start);
4817-
NEW_JUMP_TARGET_LABEL(c, send);
4822+
NEW_JUMP_TARGET_LABEL(c, end_send);
48184823
NEW_JUMP_TARGET_LABEL(c, except);
48194824
NEW_JUMP_TARGET_LABEL(c, if_cleanup);
48204825

@@ -4845,9 +4850,9 @@ codegen_async_comprehension_generator(compiler *c, location loc,
48454850
ADDOP(c, loc, GET_ANEXT);
48464851
ADDOP(c, loc, PUSH_NULL);
48474852
ADDOP_LOAD_CONST(c, loc, Py_None);
4848-
USE_LABEL(c, send);
4849-
ADD_YIELD_FROM(c, loc, 1);
4853+
ADD_YIELD_FROM_WITH_EXIT(c, loc, 1, &end_send);
48504854
ADDOP(c, loc, POP_BLOCK);
4855+
ADDOP(c, loc, NOT_TAKEN);
48514856
VISIT(c, expr, gen->target);
48524857

48534858
Py_ssize_t n = asdl_seq_LEN(gen->ifs);
@@ -4886,7 +4891,7 @@ codegen_async_comprehension_generator(compiler *c, location loc,
48864891

48874892
USE_LABEL(c, except);
48884893

4889-
ADDOP_JUMP(c, loc, END_ASYNC_FOR, send);
4894+
ADDOP_JUMP(c, loc, END_ASYNC_FOR, end_send);
48904895

48914896
return SUCCESS;
48924897
}

0 commit comments

Comments
 (0)