Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions Lib/test/test_monitoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix wrong branch monitoring events and assertions for async comprehensions and multiple consecutive ``async for`` loops.
9 changes: 3 additions & 6 deletions Python/assemble.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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));
Expand Down
25 changes: 15 additions & 10 deletions Python/codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand All @@ -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);

Expand All @@ -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);
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down
Loading