mirror of
https://github.com/python/cpython.git
synced 2025-08-25 11:15:02 +00:00
GH-96793: Implement PEP 479 in bytecode. (GH-99006)
* Handle converting StopIteration to RuntimeError in bytecode. * Add custom instruction for converting StopIteration into RuntimeError.
This commit is contained in:
parent
e9ac890c02
commit
f4adb97506
14 changed files with 220 additions and 96 deletions
|
@ -1085,6 +1085,49 @@ dummy_func(
|
|||
goto exception_unwind;
|
||||
}
|
||||
|
||||
inst(STOPITERATION_ERROR) {
|
||||
assert(frame->owner == FRAME_OWNED_BY_GENERATOR);
|
||||
PyObject *exc = TOP();
|
||||
assert(PyExceptionInstance_Check(exc));
|
||||
const char *msg = NULL;
|
||||
if (PyErr_GivenExceptionMatches(exc, PyExc_StopIteration)) {
|
||||
msg = "generator raised StopIteration";
|
||||
if (frame->f_code->co_flags & CO_ASYNC_GENERATOR) {
|
||||
msg = "async generator raised StopIteration";
|
||||
}
|
||||
else if (frame->f_code->co_flags & CO_COROUTINE) {
|
||||
msg = "coroutine raised StopIteration";
|
||||
}
|
||||
}
|
||||
else if ((frame->f_code->co_flags & CO_ASYNC_GENERATOR) &&
|
||||
PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration))
|
||||
{
|
||||
/* code in `gen` raised a StopAsyncIteration error:
|
||||
raise a RuntimeError.
|
||||
*/
|
||||
msg = "async generator raised StopAsyncIteration";
|
||||
}
|
||||
if (msg != NULL) {
|
||||
PyObject *message = _PyUnicode_FromASCII(msg, strlen(msg));
|
||||
if (message == NULL) {
|
||||
goto error;
|
||||
}
|
||||
PyObject *error = PyObject_CallOneArg(PyExc_RuntimeError, message);
|
||||
if (error == NULL) {
|
||||
Py_DECREF(message);
|
||||
goto error;
|
||||
}
|
||||
assert(PyExceptionInstance_Check(error));
|
||||
SET_TOP(error);
|
||||
PyException_SetCause(error, exc);
|
||||
Py_INCREF(exc);
|
||||
PyException_SetContext(error, exc);
|
||||
Py_DECREF(message);
|
||||
}
|
||||
DISPATCH();
|
||||
}
|
||||
|
||||
|
||||
// stack effect: ( -- __0)
|
||||
inst(LOAD_ASSERTION_ERROR) {
|
||||
PyObject *value = PyExc_AssertionError;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue