diff options
author | Irit Katriel <1055913+iritkatriel@users.noreply.github.com> | 2023-11-03 10:01:36 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-11-03 10:01:36 (GMT) |
commit | d49aba5a7a3c695213810a9f82715809c6332df2 (patch) | |
tree | f949b9af90ff9d697ed2836efaea5b3b3e06e34c /Python | |
parent | 7810b6981ac663b77bc9ee9dc4b1960ec6845ea7 (diff) | |
download | cpython-d49aba5a7a3c695213810a9f82715809c6332df2.zip cpython-d49aba5a7a3c695213810a9f82715809c6332df2.tar.gz cpython-d49aba5a7a3c695213810a9f82715809c6332df2.tar.bz2 |
gh-111354: Simplify _PyGen_yf by moving some of its work to the compiler and frame state (#111648)
Diffstat (limited to 'Python')
-rw-r--r-- | Python/bytecodes.c | 8 | ||||
-rw-r--r-- | Python/compile.c | 4 | ||||
-rw-r--r-- | Python/generated_cases.c.h | 8 | ||||
-rw-r--r-- | Python/opcode_targets.h | 2 |
4 files changed, 15 insertions, 7 deletions
diff --git a/Python/bytecodes.c b/Python/bytecodes.c index ddaae32..f487e95 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -1049,7 +1049,9 @@ dummy_func( assert(frame != &entry_frame); frame->instr_ptr = next_instr; PyGenObject *gen = _PyFrame_GetGenerator(frame); - gen->gi_frame_state = FRAME_SUSPENDED; + assert(FRAME_SUSPENDED_YIELD_FROM == FRAME_SUSPENDED + 1); + assert(oparg == 0 || oparg == 1); + gen->gi_frame_state = FRAME_SUSPENDED + oparg; _PyFrame_SetStackPointer(frame, stack_pointer - 1); int err = _Py_call_instrumentation_arg( tstate, PY_MONITORING_EVENT_PY_YIELD, @@ -1075,7 +1077,9 @@ dummy_func( assert(frame != &entry_frame); frame->instr_ptr = next_instr; PyGenObject *gen = _PyFrame_GetGenerator(frame); - gen->gi_frame_state = FRAME_SUSPENDED; + assert(FRAME_SUSPENDED_YIELD_FROM == FRAME_SUSPENDED + 1); + assert(oparg == 0 || oparg == 1); + gen->gi_frame_state = FRAME_SUSPENDED + oparg; _PyFrame_SetStackPointer(frame, stack_pointer - 1); tstate->exc_info = gen->gi_exc_state.previous_item; gen->gi_exc_state.previous_item = NULL; diff --git a/Python/compile.c b/Python/compile.c index 1604e14..6c64b40 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -1549,7 +1549,7 @@ compiler_add_yield_from(struct compiler *c, location loc, int await) // Set up a virtual try/except to handle when StopIteration is raised during // a close or throw call. The only way YIELD_VALUE raises if they do! ADDOP_JUMP(c, loc, SETUP_FINALLY, fail); - ADDOP(c, loc, YIELD_VALUE); + ADDOP_I(c, loc, YIELD_VALUE, 1); ADDOP(c, NO_LOCATION, POP_BLOCK); ADDOP_I(c, loc, RESUME, await ? RESUME_AFTER_AWAIT : RESUME_AFTER_YIELD_FROM); ADDOP_JUMP(c, loc, JUMP_NO_INTERRUPT, send); @@ -4159,7 +4159,7 @@ addop_yield(struct compiler *c, location loc) { if (c->u->u_ste->ste_generator && c->u->u_ste->ste_coroutine) { ADDOP_I(c, loc, CALL_INTRINSIC_1, INTRINSIC_ASYNC_GEN_WRAP); } - ADDOP(c, loc, YIELD_VALUE); + ADDOP_I(c, loc, YIELD_VALUE, 0); ADDOP_I(c, loc, RESUME, RESUME_AFTER_YIELD); return SUCCESS; } diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h index 5c3a579..12e4f52 100644 --- a/Python/generated_cases.c.h +++ b/Python/generated_cases.c.h @@ -1501,7 +1501,9 @@ assert(frame != &entry_frame); frame->instr_ptr = next_instr; PyGenObject *gen = _PyFrame_GetGenerator(frame); - gen->gi_frame_state = FRAME_SUSPENDED; + assert(FRAME_SUSPENDED_YIELD_FROM == FRAME_SUSPENDED + 1); + assert(oparg == 0 || oparg == 1); + gen->gi_frame_state = FRAME_SUSPENDED + oparg; _PyFrame_SetStackPointer(frame, stack_pointer - 1); int err = _Py_call_instrumentation_arg( tstate, PY_MONITORING_EVENT_PY_YIELD, @@ -1532,7 +1534,9 @@ assert(frame != &entry_frame); frame->instr_ptr = next_instr; PyGenObject *gen = _PyFrame_GetGenerator(frame); - gen->gi_frame_state = FRAME_SUSPENDED; + assert(FRAME_SUSPENDED_YIELD_FROM == FRAME_SUSPENDED + 1); + assert(oparg == 0 || oparg == 1); + gen->gi_frame_state = FRAME_SUSPENDED + oparg; _PyFrame_SetStackPointer(frame, stack_pointer - 1); tstate->exc_info = gen->gi_exc_state.previous_item; gen->gi_exc_state.previous_item = NULL; diff --git a/Python/opcode_targets.h b/Python/opcode_targets.h index d35da27..bcd6ea7 100644 --- a/Python/opcode_targets.h +++ b/Python/opcode_targets.h @@ -44,7 +44,6 @@ static void *opcode_targets[256] = { &&TARGET_UNARY_NEGATIVE, &&TARGET_UNARY_NOT, &&TARGET_WITH_EXCEPT_START, - &&TARGET_YIELD_VALUE, &&TARGET_BINARY_OP, &&TARGET_BUILD_CONST_KEY_MAP, &&TARGET_BUILD_LIST, @@ -118,6 +117,7 @@ static void *opcode_targets[256] = { &&TARGET_SWAP, &&TARGET_UNPACK_EX, &&TARGET_UNPACK_SEQUENCE, + &&TARGET_YIELD_VALUE, &&_unknown_opcode, &&_unknown_opcode, &&_unknown_opcode, |