GH-90997: Wrap yield from/await in a virtual try/except StopIteration (GH-96010)

This commit is contained in:
Brandt Bucher 2022-08-19 12:33:44 -07:00 committed by GitHub
parent 2d9f252c0c
commit 5bfb3c372b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 142 additions and 106 deletions

View file

@ -2668,6 +2668,9 @@ handle_eval_breaker:
}
TARGET(YIELD_VALUE) {
// 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->is_entry);
PyObject *retval = POP();
@ -2746,6 +2749,26 @@ handle_eval_breaker:
}
}
TARGET(CLEANUP_THROW) {
assert(throwflag);
PyObject *exc_value = TOP();
assert(exc_value && PyExceptionInstance_Check(exc_value));
if (PyErr_GivenExceptionMatches(exc_value, PyExc_StopIteration)) {
PyObject *value = ((PyStopIterationObject *)exc_value)->value;
Py_INCREF(value);
Py_DECREF(POP()); // The StopIteration.
Py_DECREF(POP()); // The last sent value.
Py_DECREF(POP()); // The delegated sub-iterator.
PUSH(value);
DISPATCH();
}
Py_INCREF(exc_value);
PyObject *exc_type = Py_NewRef(Py_TYPE(exc_value));
PyObject *exc_traceback = PyException_GetTraceback(exc_value);
_PyErr_Restore(tstate, exc_type, exc_value, exc_traceback);
goto exception_unwind;
}
TARGET(LOAD_ASSERTION_ERROR) {
PyObject *value = PyExc_AssertionError;
Py_INCREF(value);