Skip to content

bpo-46841: Don't jump during throw() #31968

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

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
16 changes: 13 additions & 3 deletions Doc/library/dis.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1313,13 +1313,23 @@ iterations of the loop.
.. versionadded:: 3.11


.. opcode:: SEND
.. opcode:: SEND (delta)

Sends ``None`` to the sub-generator of this generator.
Used in ``yield from`` and ``await`` statements.
Equivalent to ``TOS = TOS1.send(TOS)``. Used in ``yield from`` and ``await``
statements.

If the call raises :exc:`StopIteration`, pop both values, push its return
value, and increment the bytecode counter by *delta*.

If TOS1 is ``NULL`` (set when a ``throw()`` through the current frame
returns), pop both values, push TOS (its return value) back, and increment
the bytecode counter by *delta*.

.. versionadded:: 3.11

.. versionchanged:: 3.12
Added ``NULL`` handling for subiterators that return during ``throw()``.


.. opcode:: ASYNC_GEN_WRAP

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
When a sub-iterator returns a value during a ``throw()`` call, perform the
resulting jump during the next :opcode:`SEND` instruction (rather than as
part of the ``throw()`` implementation).
10 changes: 2 additions & 8 deletions Objects/genobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -491,14 +491,8 @@ _gen_throw(PyGenObject *gen, int close_on_genexit,
ret = _PyFrame_StackPop((_PyInterpreterFrame *)gen->gi_iframe);
assert(ret == yf);
Py_DECREF(ret);
// XXX: Performing this jump ourselves is awkward and problematic.
// See https://github.com/python/cpython/pull/31968.
/* Termination repetition of SEND loop */
assert(_PyInterpreterFrame_LASTI(frame) >= 0);
/* Backup to SEND */
assert(_Py_OPCODE(frame->prev_instr[-1]) == SEND);
int jump = _Py_OPARG(frame->prev_instr[-1]);
frame->prev_instr += jump - 1;
// NULL tells SEND to quit sending:
_PyFrame_StackPush((_PyInterpreterFrame *)gen->gi_iframe, NULL);
if (_PyGen_FetchStopIterationValue(&val) == 0) {
ret = gen_send(gen, val);
Py_DECREF(val);
Expand Down
6 changes: 6 additions & 0 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -2591,6 +2591,12 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
assert(STACK_LEVEL() >= 2);
PyObject *v = POP();
PyObject *receiver = TOP();
if (receiver == NULL) {
// Receiver returned during a throw(). v is its return value:
SET_TOP(v);
JUMPBY(oparg);
DISPATCH();
}
PySendResult gen_status;
PyObject *retval;
if (tstate->c_tracefunc == NULL) {
Expand Down