Python 3.10 improvements.

The coroutines results are now relayed back via PyIter_Send() and
no longer obtainable via _PyGen_FetchStopIterationValue().

This means that our kore.gather() would not be able to return any
values from any of the coroutines it governed.

Fix this by saving the object returned in PyIter_Send() and using it
later in pygather_reap_coro().
This commit is contained in:
Joris Vink 2022-08-04 09:56:41 +02:00
parent 30c8652534
commit fd94fba3c7
1 changed files with 19 additions and 2 deletions

View File

@ -1108,8 +1108,8 @@ python_coro_run(struct python_coro *coro)
*/
res = PyIter_Send(coro->obj, NULL, &item);
#endif
if (item == NULL || res == PYGEN_ERROR) {
Py_XDECREF(item);
if (coro->gatherop == NULL && PyErr_Occurred() &&
PyErr_ExceptionMatches(PyExc_StopIteration)) {
PyErr_Fetch(&type, &coro->result, &traceback);
@ -1128,11 +1128,13 @@ python_coro_run(struct python_coro *coro)
return (KORE_RESULT_OK);
}
#if PY_VERSION_HEX >= 0x030A0000
if (res == PYGEN_RETURN) {
Py_XDECREF(item);
coro->result = item;
coro_running = NULL;
return (KORE_RESULT_OK);
}
#endif
if (item == Py_None) {
Py_DECREF(item);
@ -4497,6 +4499,9 @@ pygather_reap_coro(struct pygather_op *op, struct python_coro *reap)
{
struct pygather_coro *coro;
struct pygather_result *result;
#if PY_VERSION_HEX >= 0x030A0000
PyObject *type, *traceback;
#endif
TAILQ_FOREACH(coro, &op->coroutines, list) {
if (coro->coro->id == reap->id)
@ -4513,10 +4518,22 @@ pygather_reap_coro(struct pygather_op *op, struct python_coro *reap)
result = kore_pool_get(&gather_result_pool);
result->obj = NULL;
#if PY_VERSION_HEX < 0x030A0000
if (_PyGen_FetchStopIterationValue(&result->obj) == -1) {
result->obj = Py_None;
Py_INCREF(Py_None);
}
#else
if (PyErr_Occurred()) {
Py_XDECREF(coro->coro->result);
PyErr_Fetch(&type, &coro->coro->result, &traceback);
Py_DECREF(type);
Py_XDECREF(traceback);
}
result->obj = coro->coro->result;
Py_INCREF(result->obj);
#endif
TAILQ_INSERT_TAIL(&op->results, result, list);