diff options
author | Mark Shannon <mark@hotpy.org> | 2022-01-24 11:08:53 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-01-24 11:08:53 (GMT) |
commit | 0367a36fdc36b9c909c4d5acf7cde6ceeec0ba69 (patch) | |
tree | 1a87e9cb04dca5ef87abc4bd55540bdb41bd2172 /Python/compile.c | |
parent | d75a51bea3c2442f81d38ff850b81b8b7f3330f0 (diff) | |
download | cpython-0367a36fdc36b9c909c4d5acf7cde6ceeec0ba69.zip cpython-0367a36fdc36b9c909c4d5acf7cde6ceeec0ba69.tar.gz cpython-0367a36fdc36b9c909c4d5acf7cde6ceeec0ba69.tar.bz2 |
bpo-43683: Streamline YIELD_VALUE and SEND (GH-30723)
* Split YIELD_VALUE into ASYNC_GEN_WRAP; YIELD_VALUE for async generators.
* Split SEND into SEND; YIELD_VALUE.
* Document new opcodes.
Diffstat (limited to 'Python/compile.c')
-rw-r--r-- | Python/compile.c | 25 |
1 files changed, 19 insertions, 6 deletions
diff --git a/Python/compile.c b/Python/compile.c index 5d32959..feb9fca 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -910,6 +910,7 @@ stack_effect(int opcode, int oparg, int jump) return -1; case SETUP_ANNOTATIONS: return 0; + case ASYNC_GEN_WRAP: case YIELD_VALUE: return 0; case POP_BLOCK: @@ -1541,6 +1542,9 @@ compiler_addop_j_noline(struct compiler *c, int opcode, basicblock *b) #define POP_EXCEPT_AND_RERAISE(C) \ RETURN_IF_FALSE(compiler_pop_except_and_reraise((C))) +#define ADDOP_YIELD(C) \ + RETURN_IF_FALSE(addop_yield(C)) + #define VISIT(C, TYPE, V) {\ if (!compiler_visit_ ## TYPE((C), (V))) \ return 0; \ @@ -1844,6 +1848,7 @@ compiler_add_yield_from(struct compiler *c, int await) compiler_use_next_block(c, start); ADDOP_JUMP(c, SEND, exit); compiler_use_next_block(c, resume); + ADDOP(c, YIELD_VALUE); ADDOP_I(c, RESUME, await ? 3 : 2); ADDOP_JUMP(c, JUMP_NO_INTERRUPT, start); compiler_use_next_block(c, exit); @@ -4094,6 +4099,17 @@ addop_binary(struct compiler *c, operator_ty binop, bool inplace) return 1; } + +static int +addop_yield(struct compiler *c) { + if (c->u->u_ste->ste_generator && c->u->u_ste->ste_coroutine) { + ADDOP(c, ASYNC_GEN_WRAP); + } + ADDOP(c, YIELD_VALUE); + ADDOP_I(c, RESUME, 1); + return 1; +} + static int compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx) { @@ -5144,8 +5160,7 @@ compiler_sync_comprehension_generator(struct compiler *c, switch (type) { case COMP_GENEXP: VISIT(c, expr, elt); - ADDOP(c, YIELD_VALUE); - ADDOP_I(c, RESUME, 1); + ADDOP_YIELD(c); ADDOP(c, POP_TOP); break; case COMP_LISTCOMP: @@ -5243,8 +5258,7 @@ compiler_async_comprehension_generator(struct compiler *c, switch (type) { case COMP_GENEXP: VISIT(c, expr, elt); - ADDOP(c, YIELD_VALUE); - ADDOP_I(c, RESUME, 1); + ADDOP_YIELD(c); ADDOP(c, POP_TOP); break; case COMP_LISTCOMP: @@ -5714,8 +5728,7 @@ compiler_visit_expr1(struct compiler *c, expr_ty e) else { ADDOP_LOAD_CONST(c, Py_None); } - ADDOP(c, YIELD_VALUE); - ADDOP_I(c, RESUME, 1); + ADDOP_YIELD(c); break; case YieldFrom_kind: if (c->u->u_ste->ste_type != FunctionBlock) |