gh-128078: Use PyErr_SetRaisedException in _PyGen_SetStopIterationValue (#128287)

Co-authored-by: Kumar Aditya <kumaraditya@python.org>
This commit is contained in:
Bénédikt Tran 2025-01-13 16:54:13 +01:00 committed by GitHub
parent 3efe28a40b
commit 402b91da87
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 11 additions and 23 deletions

View file

@ -634,30 +634,18 @@ int
_PyGen_SetStopIterationValue(PyObject *value)
{
assert(!PyErr_Occurred());
PyObject *e;
if (value == NULL ||
(!PyTuple_Check(value) && !PyExceptionInstance_Check(value)))
{
/* Delay exception instantiation if we can */
PyErr_SetObject(PyExc_StopIteration, value);
return 0;
}
/* Construct an exception instance manually with
* PyObject_CallOneArg and pass it to PyErr_SetObject.
*
* We do this to handle a situation when "value" is a tuple, in which
* case PyErr_SetObject would set the value of StopIteration to
* the first element of the tuple.
*
* (See PyErr_SetObject/_PyErr_CreateException code for details.)
*/
e = PyObject_CallOneArg(PyExc_StopIteration, value);
if (e == NULL) {
// Construct an exception instance manually with PyObject_CallOneArg()
// but use PyErr_SetRaisedException() instead of PyErr_SetObject() as
// PyErr_SetObject(exc_type, value) has a fast path when 'value'
// is a tuple, where the value of the StopIteration exception would be
// set to 'value[0]' instead of 'value'.
PyObject *exc = value == NULL
? PyObject_CallNoArgs(PyExc_StopIteration)
: PyObject_CallOneArg(PyExc_StopIteration, value);
if (exc == NULL) {
return -1;
}
PyErr_SetObject(PyExc_StopIteration, e);
Py_DECREF(e);
PyErr_SetRaisedException(exc /* stolen */);
return 0;
}