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
use RESUME oparg to store exception depth info
  • Loading branch information
iritkatriel committed Oct 29, 2023
commit 219e6c136ff70382fe643664b071ac4c6d8c0849
5 changes: 3 additions & 2 deletions Include/internal/pycore_opcode_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@ extern "C" {
/* Values used in the oparg for RESUME */
#define RESUME_AT_FUNC_START 0
#define RESUME_AFTER_YIELD 1
#define RESUME_AFTER_YIELD_FROM 2
#define RESUME_AFTER_AWAIT 3
#define RESUME_AFTER_YIELD_EXC_DEPTH_1 2
#define RESUME_AFTER_YIELD_FROM 3
#define RESUME_AFTER_AWAIT 4


#ifdef __cplusplus
Expand Down
3 changes: 2 additions & 1 deletion Lib/importlib/_bootstrap_external.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,7 @@ def _write_atomic(path, data, mode=0o666):
# Python 3.13a1 3561 (Add cache entry to branch instructions)
# Python 3.13a1 3562 (Assign opcode IDs for internal ops in separate range)
# Python 3.13a1 3563 (Add CALL_KW and remove KW_NAMES)
# Python 3.13a1 3564 (Shifted some of the oparg values of RESUME to add a new one)

# Python 3.14 will start with 3600

Expand All @@ -475,7 +476,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 = (3563).to_bytes(2, 'little') + b'\r\n'
MAGIC_NUMBER = (3564).to_bytes(2, 'little') + b'\r\n'

_RAW_MAGIC_NUMBER = int.from_bytes(MAGIC_NUMBER, 'little') # For import.c

Expand Down
8 changes: 4 additions & 4 deletions Lib/test/test_dis.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ async def _asyncwith(c):
LOAD_CONST 0 (None)
>> SEND 3 (to 24)
YIELD_VALUE 2
RESUME 3
RESUME 4
JUMP_BACKWARD_NO_INTERRUPT 5 (to 14)
>> END_SEND
POP_TOP
Expand All @@ -549,7 +549,7 @@ async def _asyncwith(c):
LOAD_CONST 0 (None)
>> SEND 3 (to 60)
YIELD_VALUE 2
RESUME 3
RESUME 4
JUMP_BACKWARD_NO_INTERRUPT 5 (to 50)
>> END_SEND
POP_TOP
Expand All @@ -572,7 +572,7 @@ async def _asyncwith(c):
LOAD_CONST 0 (None)
>> SEND 4 (to 102)
YIELD_VALUE 3
RESUME 3
RESUME 4
JUMP_BACKWARD_NO_INTERRUPT 5 (to 90)
>> CLEANUP_THROW
>> END_SEND
Expand Down Expand Up @@ -763,7 +763,7 @@ def foo(x):
LOAD_FAST 1 (z)
BINARY_OP 0 (+)
YIELD_VALUE 1
RESUME 1
RESUME 2
POP_TOP
JUMP_BACKWARD 12 (to 10)
>> END_FOR
Expand Down
20 changes: 3 additions & 17 deletions Objects/genobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -343,12 +343,6 @@ is_resume(_Py_CODEUNIT *instr)
);
}

static inline bool
is_yield(_Py_CODEUNIT *instr)
{
return instr->op.code == YIELD_VALUE || instr->op.code == INSTRUMENTED_YIELD_VALUE;
}

PyObject *
_PyGen_yf(PyGenObject *gen)
{
Expand Down Expand Up @@ -397,19 +391,11 @@ gen_close(PyGenObject *gen, PyObject *args)
Py_DECREF(yf);
}
_PyInterpreterFrame *frame = (_PyInterpreterFrame *)gen->gi_iframe;
/* It is possible for the previous instruction to not be a
* YIELD_VALUE if the debugger has changed the lineno. */
assert(_PyOpcode_Caches[YIELD_VALUE] == 0);
assert(_PyOpcode_Caches[INSTRUMENTED_YIELD_VALUE] == 0);
if (err == 0 && is_yield(frame->instr_ptr - 1)) {
_Py_CODEUNIT *yield_instr = frame->instr_ptr - 1;
assert(is_resume(frame->instr_ptr));
int exception_handler_depth = yield_instr->op.arg;
assert(exception_handler_depth > 0);
if (is_resume(frame->instr_ptr)) {
/* We can safely ignore the outermost try block
* as it automatically generated to handle
* as it is automatically generated to handle
* StopIteration. */
if (exception_handler_depth == 1) {
if (frame->instr_ptr->op.arg == RESUME_AFTER_YIELD_EXC_DEPTH_1) {
gen->gi_frame_state = FRAME_COMPLETED;
Py_RETURN_NONE;
}
Expand Down
6 changes: 6 additions & 0 deletions Python/flowgraph.c
Original file line number Diff line number Diff line change
Expand Up @@ -882,6 +882,12 @@ label_exception_targets(basicblock *entryblock) {
if (instr->i_opcode == YIELD_VALUE) {
instr->i_oparg = except_stack->depth;
}
if (instr->i_opcode == RESUME &&
instr->i_oparg == RESUME_AFTER_YIELD &&
except_stack->depth == 1)
{
instr->i_oparg = RESUME_AFTER_YIELD_EXC_DEPTH_1;
}
instr->i_except = handler;
}
}
Expand Down