Skip to content

Commit 7246b91

Browse files
gh-128002: simplify asyncio.all_tasks to use PyList_Extend instead of manual iteration (#129942)
1 parent 2abb6a4 commit 7246b91

File tree

1 file changed

+7
-18
lines changed

1 file changed

+7
-18
lines changed

Modules/_asynciomodule.c

+7-18
Original file line numberDiff line numberDiff line change
@@ -4102,6 +4102,12 @@ _asyncio_all_tasks_impl(PyObject *module, PyObject *loop)
41024102
Py_DECREF(loop);
41034103
return NULL;
41044104
}
4105+
if (PyList_Extend(tasks, state->non_asyncio_tasks) < 0) {
4106+
Py_DECREF(tasks);
4107+
Py_DECREF(loop);
4108+
return NULL;
4109+
}
4110+
41054111
PyInterpreterState *interp = PyInterpreterState_Get();
41064112
// Stop the world and traverse the per-thread linked list
41074113
// of asyncio tasks for every thread, as well as the
@@ -4127,24 +4133,7 @@ _asyncio_all_tasks_impl(PyObject *module, PyObject *loop)
41274133
Py_DECREF(loop);
41284134
return NULL;
41294135
}
4130-
PyObject *scheduled_iter = PyObject_GetIter(state->non_asyncio_tasks);
4131-
if (scheduled_iter == NULL) {
4132-
Py_DECREF(tasks);
4133-
Py_DECREF(loop);
4134-
return NULL;
4135-
}
4136-
PyObject *item;
4137-
while ((item = PyIter_Next(scheduled_iter)) != NULL) {
4138-
if (PyList_Append(tasks, item) < 0) {
4139-
Py_DECREF(tasks);
4140-
Py_DECREF(loop);
4141-
Py_DECREF(item);
4142-
Py_DECREF(scheduled_iter);
4143-
return NULL;
4144-
}
4145-
Py_DECREF(item);
4146-
}
4147-
Py_DECREF(scheduled_iter);
4136+
41484137
// All the tasks are now in the list, now filter the tasks which are done
41494138
PyObject *res = PySet_New(NULL);
41504139
if (res == NULL) {

0 commit comments

Comments
 (0)