Skip to content

GH-100762: Don't call gen.throw() in gen.close(), unless necessary. #101013

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jan 24, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 2 additions & 2 deletions Doc/library/dis.rst
Original file line number Diff line number Diff line change
Expand Up @@ -699,8 +699,8 @@ iterations of the loop.

Yields ``STACK.pop()`` from a :term:`generator`.

.. versionchanged:: 3.11
oparg set to be the stack depth, for efficient handling on frames.
.. versionchanged:: 3.12
oparg set to be the exception block depth, for efficient closing of generators.


.. opcode:: SETUP_ANNOTATIONS
Expand Down
4 changes: 3 additions & 1 deletion Lib/importlib/_bootstrap_external.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,8 @@ def _write_atomic(path, data, mode=0o666):
# Python 3.12a1 3514 (Remove ASYNC_GEN_WRAP, LIST_TO_TUPLE, and UNARY_POSITIVE)
# Python 3.12a1 3515 (Embed jump mask in COMPARE_OP oparg)

# Python 3.12a1 3517 (Change YIELD_VALUE oparg to exception block depth)

# Python 3.13 will start with 3550

# MAGIC must change whenever the bytecode emitted by the compiler may no
Expand All @@ -441,7 +443,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 = (3515).to_bytes(2, 'little') + b'\r\n'
MAGIC_NUMBER = (3517).to_bytes(2, 'little') + b'\r\n'

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

Expand Down
4 changes: 2 additions & 2 deletions Lib/test/test_dis.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ async def _asyncwith(c):
GET_AWAITABLE 1
LOAD_CONST 0 (None)
>> SEND 3 (to 22)
YIELD_VALUE 3
YIELD_VALUE 2
RESUME 3
JUMP_BACKWARD_NO_INTERRUPT 4 (to 14)
>> POP_TOP
Expand Down Expand Up @@ -525,7 +525,7 @@ async def _asyncwith(c):
GET_AWAITABLE 2
LOAD_CONST 0 (None)
>> SEND 4 (to 92)
YIELD_VALUE 6
YIELD_VALUE 3
RESUME 3
JUMP_BACKWARD_NO_INTERRUPT 4 (to 82)
>> CLEANUP_THROW
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Record the (virtual) exception block depth in the oparg of
:opcode:`YIELD_VALUE`. Use this to avoid the expensive ``throw()`` when
closing generators (and coroutines) that can be closed trivially.
24 changes: 23 additions & 1 deletion Objects/genobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -354,15 +354,37 @@ gen_close(PyGenObject *gen, PyObject *args)
PyObject *yf = _PyGen_yf(gen);
int err = 0;

if (gen->gi_frame_state == FRAME_CREATED) {
gen->gi_frame_state = FRAME_COMPLETED;
Py_RETURN_NONE;
}
if (gen->gi_frame_state >= FRAME_COMPLETED) {
Py_RETURN_NONE;
}
if (yf) {
PyFrameState state = gen->gi_frame_state;
gen->gi_frame_state = FRAME_EXECUTING;
err = gen_close_iter(yf);
gen->gi_frame_state = state;
Py_DECREF(yf);
}
if (err == 0)
_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. */
if (err == 0 && frame->prev_instr->opcode == YIELD_VALUE) {
assert(frame->prev_instr[1].opcode == RESUME);
int exception_handler_depth = frame->prev_instr->oparg;
assert(exception_handler_depth > 0);
/* We can safely ignore the outermost try block
* as it automatically generated to handle
* StopIteration. */
if (exception_handler_depth == 1) {
Py_RETURN_NONE;
}
}
if (err == 0) {
PyErr_SetNone(PyExc_GeneratorExit);
}
retval = gen_send_ex(gen, Py_None, 1, 1);
if (retval) {
const char *msg = "generator ignored GeneratorExit";
Expand Down
1 change: 0 additions & 1 deletion Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,6 @@ dummy_func(
// NOTE: It's important that YIELD_VALUE never raises an exception!
// The compiler treats any exception raised here as a failed close()
// or throw() call.
assert(oparg == STACK_LEVEL());
assert(frame != &entry_frame);
PyGenObject *gen = _PyFrame_GetGenerator(frame);
gen->gi_frame_state = FRAME_SUSPENDED;
Expand Down
6 changes: 3 additions & 3 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -7164,9 +7164,6 @@ stackdepth(basicblock *entryblock, int code_flags)
next = NULL;
break;
}
if (instr->i_opcode == YIELD_VALUE) {
instr->i_oparg = depth;
}
}
if (next != NULL) {
assert(BB_HAS_FALLTHROUGH(b));
Expand Down Expand Up @@ -7334,6 +7331,9 @@ label_exception_targets(basicblock *entryblock) {
}
}
else {
if (instr->i_opcode == YIELD_VALUE) {
instr->i_oparg = except_stack->depth;
}
instr->i_except = handler;
}
}
Expand Down
1 change: 0 additions & 1 deletion Python/generated_cases.c.h

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