Skip to content

gh-128002: simplify all_tasks to use PyList_Extend instead of manual iteration #129942

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 10, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 7 additions & 18 deletions Modules/_asynciomodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -4102,6 +4102,12 @@ _asyncio_all_tasks_impl(PyObject *module, PyObject *loop)
Py_DECREF(loop);
return NULL;
}
if (PyList_Extend(tasks, state->non_asyncio_tasks) < 0) {
Py_DECREF(tasks);
Py_DECREF(loop);
return NULL;
}

PyInterpreterState *interp = PyInterpreterState_Get();
// Stop the world and traverse the per-thread linked list
// of asyncio tasks for every thread, as well as the
Expand All @@ -4127,24 +4133,7 @@ _asyncio_all_tasks_impl(PyObject *module, PyObject *loop)
Py_DECREF(loop);
return NULL;
}
PyObject *scheduled_iter = PyObject_GetIter(state->non_asyncio_tasks);
if (scheduled_iter == NULL) {
Py_DECREF(tasks);
Py_DECREF(loop);
return NULL;
}
PyObject *item;
while ((item = PyIter_Next(scheduled_iter)) != NULL) {
if (PyList_Append(tasks, item) < 0) {
Py_DECREF(tasks);
Py_DECREF(loop);
Py_DECREF(item);
Py_DECREF(scheduled_iter);
return NULL;
}
Py_DECREF(item);
}
Py_DECREF(scheduled_iter);

// All the tasks are now in the list, now filter the tasks which are done
PyObject *res = PySet_New(NULL);
if (res == NULL) {
Expand Down
Loading