task: Backport fix for generators without send()

Backport of 00b5357b8e
This commit is contained in:
rdb 2025-08-28 10:24:34 +02:00
parent 66c9264f30
commit aa554a2130
1 changed files with 9 additions and 3 deletions

View File

@ -534,9 +534,15 @@ do_python_task() {
// We are calling a generator. Use "send" rather than PyIter_Next since
// we need to be able to read the value from a StopIteration exception.
PyObject *func = PyObject_GetAttrString(_generator, "send");
nassertr(func != nullptr, DS_interrupt);
result = PyObject_CallFunctionObjArgs(func, Py_None, nullptr);
Py_DECREF(func);
if (func != nullptr) {
result = PyObject_CallOneArg(func, Py_None);
Py_DECREF(func);
} else {
// It has no send(), just call next() directly.
nassertr(Py_TYPE(_generator)->tp_iternext != nullptr, DS_interrupt);
PyErr_Clear();
result = Py_TYPE(_generator)->tp_iternext(_generator);
}
if (result == nullptr) {
// An error happened. If StopIteration, that indicates the task has