Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Split YIELD_VALUE into ASYNC_GEN_WRAP; YIELD_VALUE for async generators.
  • Loading branch information
markshannon committed Jan 20, 2022
commit cd876bcd810f81cacf53061f3ca2c5b4d6207b53
13 changes: 7 additions & 6 deletions Include/opcode.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Lib/importlib/_bootstrap_external.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,7 @@ def _write_atomic(path, data, mode=0o666):
# Python 3.11a4 3473 (Add POP_JUMP_IF_NOT_NONE/POP_JUMP_IF_NONE opcodes)
# Python 3.11a4 3474 (Add RESUME opcode)
# Python 3.11a5 3475 (Add RETURN_GENERATOR opcode)
# Python 3.11a5 3476 (Add ASYNC_GEN_WRAP opcode)

# Python 3.12 will start with magic number 3500

Expand All @@ -394,7 +395,7 @@ def _write_atomic(path, data, mode=0o666):
# Whenever MAGIC_NUMBER is changed, the ranges in the magic_values array
# in PC/launcher.c must also be updated.

MAGIC_NUMBER = (3475).to_bytes(2, 'little') + b'\r\n'
MAGIC_NUMBER = (3476).to_bytes(2, 'little') + b'\r\n'
_RAW_MAGIC_NUMBER = int.from_bytes(MAGIC_NUMBER, 'little') # For import.c

_PYCACHE = '__pycache__'
Expand Down
2 changes: 1 addition & 1 deletion Lib/opcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def jabs_op(name, op):
def_op('IMPORT_STAR', 84)
def_op('SETUP_ANNOTATIONS', 85)
def_op('YIELD_VALUE', 86)

def_op('ASYNC_GEN_WRAP', 87)
def_op('PREP_RERAISE_STAR', 88)
def_op('POP_EXCEPT', 89)

Expand Down
22 changes: 12 additions & 10 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -2663,19 +2663,21 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, InterpreterFrame *frame, int thr
return retval;
}

TARGET(ASYNC_GEN_WRAP) {
PyObject *v = TOP();
assert(frame->f_code->co_flags & CO_ASYNC_GENERATOR);
PyObject *w = _PyAsyncGenValueWrapperNew(v);
if (w == NULL) {
goto error;
}
SET_TOP(w);
Py_DECREF(v);
DISPATCH();
}

TARGET(YIELD_VALUE) {
assert(frame->is_entry);
PyObject *retval = POP();

if (frame->f_code->co_flags & CO_ASYNC_GENERATOR) {
PyObject *w = _PyAsyncGenValueWrapperNew(retval);
Py_DECREF(retval);
if (w == NULL) {
retval = NULL;
goto error;
}
retval = w;
}
frame->f_state = FRAME_SUSPENDED;
_PyFrame_SetStackPointer(frame, stack_pointer);
TRACE_FUNCTION_EXIT();
Expand Down
24 changes: 18 additions & 6 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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; \
Expand Down Expand Up @@ -4094,6 +4098,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)
{
Expand Down Expand Up @@ -5144,8 +5159,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:
Expand Down Expand Up @@ -5243,8 +5257,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:
Expand Down Expand Up @@ -5714,8 +5727,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)
Expand Down
12 changes: 6 additions & 6 deletions Python/opcode_targets.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.