From 12942bc963bd8f5fadff1905bbc522119ef98664 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Fri, 23 Sep 2022 10:01:27 +0200 Subject: [PATCH 01/26] Establish global state stub --- Modules/_asynciomodule.c | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/Modules/_asynciomodule.c b/Modules/_asynciomodule.c index 530fb7beb8870c..edcfbdce64e251 100644 --- a/Modules/_asynciomodule.c +++ b/Modules/_asynciomodule.c @@ -16,6 +16,17 @@ module _asyncio /* State of the _asyncio module */ +typedef struct { +} asyncio_state; + +static asyncio_state global_state; + +static inline asyncio_state * +get_asyncio_state(PyObject *Py_UNUSED(mod)) +{ + return &global_state; +} + static PyObject *asyncio_mod; static PyObject *traceback_extract_stack; static PyObject *asyncio_get_event_loop_policy; @@ -3435,15 +3446,15 @@ static PyMethodDef asyncio_methods[] = { }; static struct PyModuleDef _asynciomodule = { - PyModuleDef_HEAD_INIT, /* m_base */ - "_asyncio", /* m_name */ - module_doc, /* m_doc */ - -1, /* m_size */ - asyncio_methods, /* m_methods */ - NULL, /* m_slots */ - NULL, /* m_traverse */ - NULL, /* m_clear */ - (freefunc)module_free /* m_free */ + .m_base = PyModuleDef_HEAD_INIT, + .m_name = "_asyncio", + .m_doc = module_doc, + .m_size = sizeof(asyncio_state), + .m_methods = asyncio_methods, + .m_slots = NULL, + .m_traverse = NULL, + .m_clear = NULL, + .m_free = (freefunc)module_free, }; From 378330cab6173786e5d52817db867fd969b132c0 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Fri, 7 Jan 2022 10:28:50 +0100 Subject: [PATCH 02/26] Convert FutureIterType to heap type --- Modules/_asynciomodule.c | 73 ++++++++++++++++++++++++++-------------- 1 file changed, 48 insertions(+), 25 deletions(-) diff --git a/Modules/_asynciomodule.c b/Modules/_asynciomodule.c index edcfbdce64e251..7c03cbe1020d9b 100644 --- a/Modules/_asynciomodule.c +++ b/Modules/_asynciomodule.c @@ -27,6 +27,7 @@ get_asyncio_state(PyObject *Py_UNUSED(mod)) return &global_state; } +static PyTypeObject *FutureIterType; static PyObject *asyncio_mod; static PyObject *traceback_extract_stack; static PyObject *asyncio_get_event_loop_policy; @@ -1566,8 +1567,9 @@ static Py_ssize_t fi_freelist_len = 0; static void FutureIter_dealloc(futureiterobject *it) { + PyTypeObject *tp = Py_TYPE(it); PyObject_GC_UnTrack(it); - Py_CLEAR(it->future); + tp->tp_clear((PyObject *)it); if (fi_freelist_len < FI_FREELIST_MAXLEN) { fi_freelist_len++; @@ -1576,6 +1578,7 @@ FutureIter_dealloc(futureiterobject *it) } else { PyObject_GC_Del(it); + Py_DECREF(tp); } } @@ -1714,16 +1717,24 @@ FutureIter_throw(futureiterobject *self, PyObject *const *args, Py_ssize_t nargs return NULL; } +static int +FutureIter_clear(futureiterobject *it) +{ + Py_CLEAR(it->future); + return 0; +} + static PyObject * FutureIter_close(futureiterobject *self, PyObject *arg) { - Py_CLEAR(self->future); + FutureIter_clear(self); Py_RETURN_NONE; } static int FutureIter_traverse(futureiterobject *it, visitproc visit, void *arg) { + Py_VISIT(Py_TYPE(it)); Py_VISIT(it->future); return 0; } @@ -1735,27 +1746,26 @@ static PyMethodDef FutureIter_methods[] = { {NULL, NULL} /* Sentinel */ }; -static PyAsyncMethods FutureIterType_as_async = { - 0, /* am_await */ - 0, /* am_aiter */ - 0, /* am_anext */ - (sendfunc)FutureIter_am_send, /* am_send */ +static PyType_Slot FutureIter_slots[] = { + {Py_tp_dealloc, (destructor)FutureIter_dealloc}, + {Py_tp_getattro, PyObject_GenericGetAttr}, + {Py_tp_traverse, (traverseproc)FutureIter_traverse}, + {Py_tp_clear, FutureIter_clear}, + {Py_tp_iter, PyObject_SelfIter}, + {Py_tp_iternext, (iternextfunc)FutureIter_iternext}, + {Py_tp_methods, FutureIter_methods}, + + // async methods + {Py_am_send, (sendfunc)FutureIter_am_send}, + {0, NULL}, }; - -static PyTypeObject FutureIterType = { - PyVarObject_HEAD_INIT(NULL, 0) - "_asyncio.FutureIter", - .tp_basicsize = sizeof(futureiterobject), - .tp_itemsize = 0, - .tp_dealloc = (destructor)FutureIter_dealloc, - .tp_as_async = &FutureIterType_as_async, - .tp_getattro = PyObject_GenericGetAttr, - .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, - .tp_traverse = (traverseproc)FutureIter_traverse, - .tp_iter = PyObject_SelfIter, - .tp_iternext = (iternextfunc)FutureIter_iternext, - .tp_methods = FutureIter_methods, +static PyType_Spec FutureIter_spec = { + .name = "_asyncio.FutureIter", + .basicsize = sizeof(futureiterobject), + .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | + Py_TPFLAGS_IMMUTABLETYPE), + .slots = FutureIter_slots, }; static PyObject * @@ -1778,7 +1788,7 @@ future_new_iter(PyObject *fut) _Py_NewReference((PyObject*) it); } else { - it = PyObject_GC_New(futureiterobject, &FutureIterType); + it = PyObject_GC_New(futureiterobject, FutureIterType); if (it == NULL) { return NULL; } @@ -3464,9 +3474,6 @@ PyInit__asyncio(void) if (module_init() < 0) { return NULL; } - if (PyType_Ready(&FutureIterType) < 0) { - return NULL; - } if (PyType_Ready(&TaskStepMethWrapper_Type) < 0) { return NULL; } @@ -3479,6 +3486,18 @@ PyInit__asyncio(void) return NULL; } +#define CREATE_TYPE(m, tp, spec) \ + do { \ + tp = (PyTypeObject *)PyType_FromMetaclass(NULL, m, spec, NULL); \ + if (tp == NULL) { \ + goto error; \ + } \ + } while (0) + + CREATE_TYPE(m, FutureIterType, &FutureIter_spec); + +#undef CREATE_TYPE + /* FutureType and TaskType are made ready by PyModule_AddType() calls below. */ if (PyModule_AddType(m, &FutureType) < 0) { Py_DECREF(m); @@ -3505,4 +3524,8 @@ PyInit__asyncio(void) } return m; + +error: + Py_DECREF(m); + return NULL; } From 4db4e7fe36d2e34bfc2627996034fa41902768e3 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Wed, 21 Sep 2022 13:40:38 +0200 Subject: [PATCH 03/26] Convert PyRunningLoopHolder_Type to heap type --- Modules/_asynciomodule.c | 60 +++++++++++++++++++++++++++++----------- 1 file changed, 44 insertions(+), 16 deletions(-) diff --git a/Modules/_asynciomodule.c b/Modules/_asynciomodule.c index 7c03cbe1020d9b..ef824c85be48db 100644 --- a/Modules/_asynciomodule.c +++ b/Modules/_asynciomodule.c @@ -114,7 +114,7 @@ typedef struct { static PyTypeObject FutureType; static PyTypeObject TaskType; -static PyTypeObject PyRunningLoopHolder_Type; +static PyTypeObject *PyRunningLoopHolder_Type; #define Future_CheckExact(obj) Py_IS_TYPE(obj, &FutureType) @@ -260,7 +260,7 @@ get_running_loop(PyObject **loop) cached_running_holder_tsid = ts_id; } - assert(Py_IS_TYPE(rl, &PyRunningLoopHolder_Type)); + assert(Py_IS_TYPE(rl, PyRunningLoopHolder_Type)); PyObject *running_loop = ((PyRunningLoopHolder *)rl)->rl_loop; if (running_loop == Py_None) { @@ -3269,8 +3269,8 @@ _asyncio__leave_task_impl(PyObject *module, PyObject *loop, PyObject *task) static PyRunningLoopHolder * new_running_loop_holder(PyObject *loop) { - PyRunningLoopHolder *rl = PyObject_New( - PyRunningLoopHolder, &PyRunningLoopHolder_Type); + PyRunningLoopHolder *rl = PyObject_GC_New( + PyRunningLoopHolder, PyRunningLoopHolder_Type); if (rl == NULL) { return NULL; } @@ -3282,28 +3282,58 @@ new_running_loop_holder(PyObject *loop) Py_INCREF(loop); rl->rl_loop = loop; + PyObject_GC_Track(rl); return rl; } +static int +PyRunningLoopHolder_clear(PyRunningLoopHolder *rl) +{ + Py_CLEAR(rl->rl_loop); + return 0; +} + + +static int +PyRunningLoopHolder_traverse(PyRunningLoopHolder *rl, visitproc visit, + void *arg) +{ + Py_VISIT(Py_TYPE(rl)); + Py_VISIT(rl->rl_loop); + return 0; +} + + static void PyRunningLoopHolder_tp_dealloc(PyRunningLoopHolder *rl) { if (cached_running_holder == (PyObject *)rl) { cached_running_holder = NULL; } - Py_CLEAR(rl->rl_loop); - PyObject_Free(rl); + PyTypeObject *tp = Py_TYPE(rl); + PyObject_GC_UnTrack(rl); + PyRunningLoopHolder_clear(rl); + PyObject_GC_Del(rl); + Py_DECREF(tp); } -static PyTypeObject PyRunningLoopHolder_Type = { - PyVarObject_HEAD_INIT(NULL, 0) - "_RunningLoopHolder", - sizeof(PyRunningLoopHolder), - .tp_getattro = PyObject_GenericGetAttr, - .tp_flags = Py_TPFLAGS_DEFAULT, - .tp_dealloc = (destructor)PyRunningLoopHolder_tp_dealloc, +static PyType_Slot PyRunningLoopHolder_slots[] = { + {Py_tp_getattro, PyObject_GenericGetAttr}, + {Py_tp_dealloc, (destructor)PyRunningLoopHolder_tp_dealloc}, + {Py_tp_traverse, (traverseproc)PyRunningLoopHolder_traverse}, + {Py_tp_clear, PyRunningLoopHolder_clear}, + {0, NULL}, +}; + + +static PyType_Spec PyRunningLoopHolder_spec = { + .name = "_asyncio._RunningLoopHolder", + .basicsize = sizeof(PyRunningLoopHolder), + .slots = PyRunningLoopHolder_slots, + .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | + Py_TPFLAGS_IMMUTABLETYPE), }; @@ -3477,9 +3507,6 @@ PyInit__asyncio(void) if (PyType_Ready(&TaskStepMethWrapper_Type) < 0) { return NULL; } - if (PyType_Ready(&PyRunningLoopHolder_Type) < 0) { - return NULL; - } PyObject *m = PyModule_Create(&_asynciomodule); if (m == NULL) { @@ -3494,6 +3521,7 @@ PyInit__asyncio(void) } \ } while (0) + CREATE_TYPE(m, PyRunningLoopHolder_Type, &PyRunningLoopHolder_spec); CREATE_TYPE(m, FutureIterType, &FutureIter_spec); #undef CREATE_TYPE From 319df115b25ec63a1f2997dfda620cb17388c9ea Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Wed, 21 Sep 2022 13:46:43 +0200 Subject: [PATCH 04/26] Convert TaskStepMethWrapper_Type to heap type --- Modules/_asynciomodule.c | 39 ++++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/Modules/_asynciomodule.c b/Modules/_asynciomodule.c index ef824c85be48db..7a592c4413f3b7 100644 --- a/Modules/_asynciomodule.c +++ b/Modules/_asynciomodule.c @@ -28,6 +28,7 @@ get_asyncio_state(PyObject *Py_UNUSED(mod)) } static PyTypeObject *FutureIterType; +static PyTypeObject *TaskStepMethWrapper_Type; static PyObject *asyncio_mod; static PyObject *traceback_extract_stack; static PyObject *asyncio_get_event_loop_policy; @@ -1826,9 +1827,11 @@ TaskStepMethWrapper_clear(TaskStepMethWrapper *o) static void TaskStepMethWrapper_dealloc(TaskStepMethWrapper *o) { + PyTypeObject *tp = Py_TYPE(o); PyObject_GC_UnTrack(o); (void)TaskStepMethWrapper_clear(o); Py_TYPE(o)->tp_free(o); + Py_DECREF(tp); } static PyObject * @@ -1850,6 +1853,7 @@ static int TaskStepMethWrapper_traverse(TaskStepMethWrapper *o, visitproc visit, void *arg) { + Py_VISIT(Py_TYPE(o)); Py_VISIT(o->sw_task); Py_VISIT(o->sw_arg); return 0; @@ -1870,25 +1874,29 @@ static PyGetSetDef TaskStepMethWrapper_getsetlist[] = { {NULL} /* Sentinel */ }; -static PyTypeObject TaskStepMethWrapper_Type = { - PyVarObject_HEAD_INIT(NULL, 0) - "TaskStepMethWrapper", - .tp_basicsize = sizeof(TaskStepMethWrapper), - .tp_itemsize = 0, - .tp_getset = TaskStepMethWrapper_getsetlist, - .tp_dealloc = (destructor)TaskStepMethWrapper_dealloc, - .tp_call = (ternaryfunc)TaskStepMethWrapper_call, - .tp_getattro = PyObject_GenericGetAttr, - .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, - .tp_traverse = (traverseproc)TaskStepMethWrapper_traverse, - .tp_clear = (inquiry)TaskStepMethWrapper_clear, +static PyType_Slot TaskStepMethWrapper_slots[] = { + {Py_tp_getset, TaskStepMethWrapper_getsetlist}, + {Py_tp_dealloc, (destructor)TaskStepMethWrapper_dealloc}, + {Py_tp_call, (ternaryfunc)TaskStepMethWrapper_call}, + {Py_tp_getattro, PyObject_GenericGetAttr}, + {Py_tp_traverse, (traverseproc)TaskStepMethWrapper_traverse}, + {Py_tp_clear, (inquiry)TaskStepMethWrapper_clear}, + {0, NULL}, +}; + +static PyType_Spec TaskStepMethWrapper_spec = { + .name = "_asyncio.TaskStepMethWrapper", + .basicsize = sizeof(TaskStepMethWrapper), + .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | + Py_TPFLAGS_IMMUTABLETYPE), + .slots = TaskStepMethWrapper_slots, }; static PyObject * TaskStepMethWrapper_new(TaskObj *task, PyObject *arg) { TaskStepMethWrapper *o; - o = PyObject_GC_New(TaskStepMethWrapper, &TaskStepMethWrapper_Type); + o = PyObject_GC_New(TaskStepMethWrapper, TaskStepMethWrapper_Type); if (o == NULL) { return NULL; } @@ -3504,10 +3512,6 @@ PyInit__asyncio(void) if (module_init() < 0) { return NULL; } - if (PyType_Ready(&TaskStepMethWrapper_Type) < 0) { - return NULL; - } - PyObject *m = PyModule_Create(&_asynciomodule); if (m == NULL) { return NULL; @@ -3521,6 +3525,7 @@ PyInit__asyncio(void) } \ } while (0) + CREATE_TYPE(m, TaskStepMethWrapper_Type, &TaskStepMethWrapper_spec); CREATE_TYPE(m, PyRunningLoopHolder_Type, &PyRunningLoopHolder_spec); CREATE_TYPE(m, FutureIterType, &FutureIter_spec); From 46793d0513d63767949e60a53de8b1de01f8c5e6 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Fri, 23 Sep 2022 09:44:48 +0200 Subject: [PATCH 05/26] Convert TaskType to heap type --- Modules/_asynciomodule.c | 89 +++++++++++++++++++++++++--------------- 1 file changed, 55 insertions(+), 34 deletions(-) diff --git a/Modules/_asynciomodule.c b/Modules/_asynciomodule.c index 7a592c4413f3b7..162f1c9e67b923 100644 --- a/Modules/_asynciomodule.c +++ b/Modules/_asynciomodule.c @@ -6,6 +6,7 @@ #include "pycore_pyerrors.h" // _PyErr_ClearExcState() #include "pycore_pystate.h" // _PyThreadState_GET() #include "pycore_runtime_init.h" // _Py_ID() +#include "structmember.h" // PyMemberDef #include // offsetof() @@ -114,15 +115,15 @@ typedef struct { static PyTypeObject FutureType; -static PyTypeObject TaskType; +static PyTypeObject *TaskType; static PyTypeObject *PyRunningLoopHolder_Type; #define Future_CheckExact(obj) Py_IS_TYPE(obj, &FutureType) -#define Task_CheckExact(obj) Py_IS_TYPE(obj, &TaskType) +#define Task_CheckExact(obj) Py_IS_TYPE(obj, TaskType) #define Future_Check(obj) PyObject_TypeCheck(obj, &FutureType) -#define Task_Check(obj) PyObject_TypeCheck(obj, &TaskType) +#define Task_Check(obj) PyObject_TypeCheck(obj, TaskType) #include "clinic/_asynciomodule.c.h" @@ -2084,6 +2085,7 @@ TaskObj_clear(TaskObj *task) static int TaskObj_traverse(TaskObj *task, visitproc visit, void *arg) { + Py_VISIT(Py_TYPE(task)); Py_VISIT(task->task_context); Py_VISIT(task->task_coro); Py_VISIT(task->task_name); @@ -2531,6 +2533,12 @@ static PyMethodDef TaskType_methods[] = { {NULL, NULL} /* Sentinel */ }; +static PyMemberDef TaskType_members[] = { + {"__weaklistoffset__", T_PYSSIZET, offsetof(TaskObj, task_weakreflist), READONLY}, + {"__dictoffset__", T_PYSSIZET, offsetof(TaskObj, dict), READONLY}, + {NULL}, +}; + static PyGetSetDef TaskType_getsetlist[] = { FUTURE_COMMON_GETSETLIST {"_log_destroy_pending", (getter)TaskObj_get_log_destroy_pending, @@ -2541,26 +2549,31 @@ static PyGetSetDef TaskType_getsetlist[] = { {NULL} /* Sentinel */ }; -static PyTypeObject TaskType = { - PyVarObject_HEAD_INIT(NULL, 0) - "_asyncio.Task", - sizeof(TaskObj), /* tp_basicsize */ - .tp_base = &FutureType, - .tp_dealloc = TaskObj_dealloc, - .tp_as_async = &FutureType_as_async, - .tp_repr = (reprfunc)TaskObj_repr, - .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE, - .tp_doc = _asyncio_Task___init____doc__, - .tp_traverse = (traverseproc)TaskObj_traverse, - .tp_clear = (inquiry)TaskObj_clear, - .tp_weaklistoffset = offsetof(TaskObj, task_weakreflist), - .tp_iter = (getiterfunc)future_new_iter, - .tp_methods = TaskType_methods, - .tp_getset = TaskType_getsetlist, - .tp_dictoffset = offsetof(TaskObj, dict), - .tp_init = (initproc)_asyncio_Task___init__, - .tp_new = PyType_GenericNew, - .tp_finalize = (destructor)TaskObj_finalize, +static PyType_Slot Task_slots[] = { + {Py_tp_dealloc, TaskObj_dealloc}, + {Py_tp_repr, (reprfunc)TaskObj_repr}, + {Py_tp_doc, (void *)_asyncio_Task___init____doc__}, + {Py_tp_traverse, (traverseproc)TaskObj_traverse}, + {Py_tp_clear, (inquiry)TaskObj_clear}, + {Py_tp_iter, (getiterfunc)future_new_iter}, + {Py_tp_methods, TaskType_methods}, + {Py_tp_members, TaskType_members}, + {Py_tp_getset, TaskType_getsetlist}, + {Py_tp_init, (initproc)_asyncio_Task___init__}, + {Py_tp_new, PyType_GenericNew}, + {Py_tp_finalize, (destructor)TaskObj_finalize}, + + // async slots + {Py_am_await, (unaryfunc)future_new_iter}, + {0, NULL}, +}; + +static PyType_Spec Task_spec = { + .name = "_asyncio.Task", + .basicsize = sizeof(TaskObj), + .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE | + Py_TPFLAGS_IMMUTABLETYPE), + .slots = Task_slots, }; static void @@ -2578,6 +2591,7 @@ TaskObj_dealloc(PyObject *self) } } + PyTypeObject *tp = Py_TYPE(task); PyObject_GC_UnTrack(self); if (task->task_weakreflist != NULL) { @@ -2585,7 +2599,8 @@ TaskObj_dealloc(PyObject *self) } (void)TaskObj_clear(task); - Py_TYPE(task)->tp_free(task); + tp->tp_free(task); + Py_DECREF(tp); } static int @@ -3517,17 +3532,23 @@ PyInit__asyncio(void) return NULL; } -#define CREATE_TYPE(m, tp, spec) \ - do { \ - tp = (PyTypeObject *)PyType_FromMetaclass(NULL, m, spec, NULL); \ - if (tp == NULL) { \ - goto error; \ - } \ + if (PyType_Ready(&FutureType) < 0) { + return NULL; + } + +#define CREATE_TYPE(m, tp, spec, base) \ + do { \ + tp = (PyTypeObject *)PyType_FromMetaclass(NULL, m, spec, \ + (PyObject *)base); \ + if (tp == NULL) { \ + goto error; \ + } \ } while (0) - CREATE_TYPE(m, TaskStepMethWrapper_Type, &TaskStepMethWrapper_spec); - CREATE_TYPE(m, PyRunningLoopHolder_Type, &PyRunningLoopHolder_spec); - CREATE_TYPE(m, FutureIterType, &FutureIter_spec); + CREATE_TYPE(m, TaskStepMethWrapper_Type, &TaskStepMethWrapper_spec, NULL); + CREATE_TYPE(m, PyRunningLoopHolder_Type, &PyRunningLoopHolder_spec, NULL); + CREATE_TYPE(m, FutureIterType, &FutureIter_spec, NULL); + CREATE_TYPE(m, TaskType, &Task_spec, &FutureType); #undef CREATE_TYPE @@ -3537,7 +3558,7 @@ PyInit__asyncio(void) return NULL; } - if (PyModule_AddType(m, &TaskType) < 0) { + if (PyModule_AddType(m, TaskType) < 0) { Py_DECREF(m); return NULL; } From 1109b163207eb1bed29f1b8106ab59c490dfa384 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Fri, 23 Sep 2022 09:53:40 +0200 Subject: [PATCH 06/26] Convert FutureType to heap type --- Modules/_asynciomodule.c | 80 +++++++++++++++++++++------------------- 1 file changed, 42 insertions(+), 38 deletions(-) diff --git a/Modules/_asynciomodule.c b/Modules/_asynciomodule.c index 162f1c9e67b923..215a22badd93da 100644 --- a/Modules/_asynciomodule.c +++ b/Modules/_asynciomodule.c @@ -114,15 +114,15 @@ typedef struct { } PyRunningLoopHolder; -static PyTypeObject FutureType; +static PyTypeObject *FutureType; static PyTypeObject *TaskType; static PyTypeObject *PyRunningLoopHolder_Type; -#define Future_CheckExact(obj) Py_IS_TYPE(obj, &FutureType) +#define Future_CheckExact(obj) Py_IS_TYPE(obj, FutureType) #define Task_CheckExact(obj) Py_IS_TYPE(obj, TaskType) -#define Future_Check(obj) PyObject_TypeCheck(obj, &FutureType) +#define Future_Check(obj) PyObject_TypeCheck(obj, FutureType) #define Task_Check(obj) PyObject_TypeCheck(obj, TaskType) #include "clinic/_asynciomodule.c.h" @@ -823,6 +823,7 @@ FutureObj_clear(FutureObj *fut) static int FutureObj_traverse(FutureObj *fut, visitproc visit, void *arg) { + Py_VISIT(Py_TYPE(fut)); Py_VISIT(fut->fut_loop); Py_VISIT(fut->fut_callback0); Py_VISIT(fut->fut_context0); @@ -1461,13 +1462,6 @@ FutureObj_finalize(FutureObj *fut) PyErr_Restore(error_type, error_value, error_traceback); } -static PyAsyncMethods FutureType_as_async = { - (unaryfunc)future_new_iter, /* am_await */ - 0, /* am_aiter */ - 0, /* am_anext */ - 0, /* am_send */ -}; - static PyMethodDef FutureType_methods[] = { _ASYNCIO_FUTURE_RESULT_METHODDEF _ASYNCIO_FUTURE_EXCEPTION_METHODDEF @@ -1484,6 +1478,12 @@ static PyMethodDef FutureType_methods[] = { {NULL, NULL} /* Sentinel */ }; +static PyMemberDef FutureType_members[] = { + {"__weaklistoffset__", T_PYSSIZET, offsetof(FutureObj, fut_weakreflist), READONLY}, + {"__dictoffset__", T_PYSSIZET, offsetof(FutureObj, dict), READONLY}, + {NULL}, +}; + #define FUTURE_COMMON_GETSETLIST \ {"_state", (getter)FutureObj_get_state, NULL, NULL}, \ {"_asyncio_future_blocking", (getter)FutureObj_get_blocking, \ @@ -1506,25 +1506,31 @@ static PyGetSetDef FutureType_getsetlist[] = { static void FutureObj_dealloc(PyObject *self); -static PyTypeObject FutureType = { - PyVarObject_HEAD_INIT(NULL, 0) - "_asyncio.Future", - sizeof(FutureObj), /* tp_basicsize */ - .tp_dealloc = FutureObj_dealloc, - .tp_as_async = &FutureType_as_async, - .tp_repr = (reprfunc)FutureObj_repr, - .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE, - .tp_doc = _asyncio_Future___init____doc__, - .tp_traverse = (traverseproc)FutureObj_traverse, - .tp_clear = (inquiry)FutureObj_clear, - .tp_weaklistoffset = offsetof(FutureObj, fut_weakreflist), - .tp_iter = (getiterfunc)future_new_iter, - .tp_methods = FutureType_methods, - .tp_getset = FutureType_getsetlist, - .tp_dictoffset = offsetof(FutureObj, dict), - .tp_init = (initproc)_asyncio_Future___init__, - .tp_new = PyType_GenericNew, - .tp_finalize = (destructor)FutureObj_finalize, +static PyType_Slot Future_slots[] = { + {Py_tp_dealloc, FutureObj_dealloc}, + {Py_tp_repr, (reprfunc)FutureObj_repr}, + {Py_tp_doc, (void *)_asyncio_Future___init____doc__}, + {Py_tp_traverse, (traverseproc)FutureObj_traverse}, + {Py_tp_clear, (inquiry)FutureObj_clear}, + {Py_tp_iter, (getiterfunc)future_new_iter}, + {Py_tp_methods, FutureType_methods}, + {Py_tp_members, FutureType_members}, + {Py_tp_getset, FutureType_getsetlist}, + {Py_tp_init, (initproc)_asyncio_Future___init__}, + {Py_tp_new, PyType_GenericNew}, + {Py_tp_finalize, (destructor)FutureObj_finalize}, + + // async slots + {Py_am_await, (unaryfunc)future_new_iter}, + {0, NULL}, +}; + +static PyType_Spec Future_spec = { + .name = "_asyncio.Future", + .basicsize = sizeof(FutureObj), + .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE | + Py_TPFLAGS_IMMUTABLETYPE), + .slots = Future_slots, }; static void @@ -1542,6 +1548,7 @@ FutureObj_dealloc(PyObject *self) } } + PyTypeObject *tp = Py_TYPE(fut); PyObject_GC_UnTrack(self); if (fut->fut_weakreflist != NULL) { @@ -1549,7 +1556,8 @@ FutureObj_dealloc(PyObject *self) } (void)FutureObj_clear(fut); - Py_TYPE(fut)->tp_free(fut); + tp->tp_free(fut); + Py_DECREF(tp); } @@ -1775,7 +1783,7 @@ future_new_iter(PyObject *fut) { futureiterobject *it; - if (!PyObject_TypeCheck(fut, &FutureType)) { + if (!Future_Check(fut)) { PyErr_BadInternalCall(); return NULL; } @@ -3532,10 +3540,6 @@ PyInit__asyncio(void) return NULL; } - if (PyType_Ready(&FutureType) < 0) { - return NULL; - } - #define CREATE_TYPE(m, tp, spec, base) \ do { \ tp = (PyTypeObject *)PyType_FromMetaclass(NULL, m, spec, \ @@ -3548,12 +3552,12 @@ PyInit__asyncio(void) CREATE_TYPE(m, TaskStepMethWrapper_Type, &TaskStepMethWrapper_spec, NULL); CREATE_TYPE(m, PyRunningLoopHolder_Type, &PyRunningLoopHolder_spec, NULL); CREATE_TYPE(m, FutureIterType, &FutureIter_spec, NULL); - CREATE_TYPE(m, TaskType, &Task_spec, &FutureType); + CREATE_TYPE(m, FutureType, &Future_spec, NULL); + CREATE_TYPE(m, TaskType, &Task_spec, FutureType); #undef CREATE_TYPE - /* FutureType and TaskType are made ready by PyModule_AddType() calls below. */ - if (PyModule_AddType(m, &FutureType) < 0) { + if (PyModule_AddType(m, FutureType) < 0) { Py_DECREF(m); return NULL; } From d382cc1221b7076e857ee73c81e9b1f56a2e15dc Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Wed, 28 Sep 2022 14:57:24 +0200 Subject: [PATCH 07/26] Add types to state --- Modules/_asynciomodule.c | 78 ++++++++++++++++++++++------------------ 1 file changed, 44 insertions(+), 34 deletions(-) diff --git a/Modules/_asynciomodule.c b/Modules/_asynciomodule.c index 215a22badd93da..aa5930e39ea3f6 100644 --- a/Modules/_asynciomodule.c +++ b/Modules/_asynciomodule.c @@ -18,6 +18,11 @@ module _asyncio /* State of the _asyncio module */ typedef struct { + PyTypeObject *FutureIterType; + PyTypeObject *TaskStepMethWrapper_Type; + PyTypeObject *FutureType; + PyTypeObject *TaskType; + PyTypeObject *PyRunningLoopHolder_Type; } asyncio_state; static asyncio_state global_state; @@ -28,8 +33,6 @@ get_asyncio_state(PyObject *Py_UNUSED(mod)) return &global_state; } -static PyTypeObject *FutureIterType; -static PyTypeObject *TaskStepMethWrapper_Type; static PyObject *asyncio_mod; static PyObject *traceback_extract_stack; static PyObject *asyncio_get_event_loop_policy; @@ -114,16 +117,11 @@ typedef struct { } PyRunningLoopHolder; -static PyTypeObject *FutureType; -static PyTypeObject *TaskType; -static PyTypeObject *PyRunningLoopHolder_Type; +#define Future_CheckExact(state, obj) Py_IS_TYPE(obj, state->FutureType) +#define Task_CheckExact(state, obj) Py_IS_TYPE(obj, state->TaskType) - -#define Future_CheckExact(obj) Py_IS_TYPE(obj, FutureType) -#define Task_CheckExact(obj) Py_IS_TYPE(obj, TaskType) - -#define Future_Check(obj) PyObject_TypeCheck(obj, FutureType) -#define Task_Check(obj) PyObject_TypeCheck(obj, TaskType) +#define Future_Check(state, obj) PyObject_TypeCheck(obj, state->FutureType) +#define Task_Check(state, obj) PyObject_TypeCheck(obj, state->TaskType) #include "clinic/_asynciomodule.c.h" @@ -211,7 +209,8 @@ get_future_loop(PyObject *fut) PyObject *getloop; - if (Future_CheckExact(fut) || Task_CheckExact(fut)) { + asyncio_state *state = get_asyncio_state(NULL); + if (Future_CheckExact(state, fut) || Task_CheckExact(state, fut)) { PyObject *loop = ((FutureObj *)fut)->fut_loop; Py_INCREF(loop); return loop; @@ -234,6 +233,7 @@ static int get_running_loop(PyObject **loop) { PyObject *rl; + asyncio_state *state = get_asyncio_state(NULL); PyThreadState *ts = _PyThreadState_GET(); uint64_t ts_id = PyThreadState_GetID(ts); @@ -262,7 +262,7 @@ get_running_loop(PyObject **loop) cached_running_holder_tsid = ts_id; } - assert(Py_IS_TYPE(rl, PyRunningLoopHolder_Type)); + assert(Py_IS_TYPE(rl, state->PyRunningLoopHolder_Type)); PyObject *running_loop = ((PyRunningLoopHolder *)rl)->rl_loop; if (running_loop == Py_None) { @@ -418,12 +418,13 @@ future_ensure_alive(FutureObj *fut) } -#define ENSURE_FUTURE_ALIVE(fut) \ - do { \ - assert(Future_Check(fut) || Task_Check(fut)); \ - if (future_ensure_alive((FutureObj*)fut)) { \ - return NULL; \ - } \ +#define ENSURE_FUTURE_ALIVE(fut) \ + do { \ + asyncio_state *state = get_asyncio_state(NULL); \ + assert(Future_Check(state, fut) || Task_Check(state, fut)); \ + if (future_ensure_alive((FutureObj*)fut)) { \ + return NULL; \ + } \ } while(0); @@ -1538,7 +1539,8 @@ FutureObj_dealloc(PyObject *self) { FutureObj *fut = (FutureObj *)self; - if (Future_CheckExact(fut)) { + asyncio_state *state = get_asyncio_state(NULL); + if (Future_CheckExact(state, fut)) { /* When fut is subclass of Future, finalizer is called from * subtype_dealloc. */ @@ -1783,7 +1785,8 @@ future_new_iter(PyObject *fut) { futureiterobject *it; - if (!Future_Check(fut)) { + asyncio_state *state = get_asyncio_state(NULL); + if (!Future_Check(state, fut)) { PyErr_BadInternalCall(); return NULL; } @@ -1798,7 +1801,8 @@ future_new_iter(PyObject *fut) _Py_NewReference((PyObject*) it); } else { - it = PyObject_GC_New(futureiterobject, FutureIterType); + asyncio_state *state = get_asyncio_state(NULL); + it = PyObject_GC_New(futureiterobject, state->FutureIterType); if (it == NULL) { return NULL; } @@ -1905,7 +1909,8 @@ static PyObject * TaskStepMethWrapper_new(TaskObj *task, PyObject *arg) { TaskStepMethWrapper *o; - o = PyObject_GC_New(TaskStepMethWrapper, TaskStepMethWrapper_Type); + asyncio_state *state = get_asyncio_state(NULL); + o = PyObject_GC_New(TaskStepMethWrapper, state->TaskStepMethWrapper_Type); if (o == NULL) { return NULL; } @@ -2589,7 +2594,8 @@ TaskObj_dealloc(PyObject *self) { TaskObj *task = (TaskObj *)self; - if (Task_CheckExact(self)) { + asyncio_state *state = get_asyncio_state(NULL); + if (Task_CheckExact(state, self)) { /* When fut is subclass of Task, finalizer is called from * subtype_dealloc. */ @@ -2822,7 +2828,8 @@ task_step_impl(TaskObj *task, PyObject *exc) } /* Check if `result` is FutureObj or TaskObj (and not a subclass) */ - if (Future_CheckExact(result) || Task_CheckExact(result)) { + asyncio_state *state = get_asyncio_state(NULL); + if (Future_CheckExact(state, result) || Task_CheckExact(state, result)) { PyObject *wrapper; PyObject *tmp; FutureObj *fut = (FutureObj*)result; @@ -3055,7 +3062,8 @@ task_wakeup(TaskObj *task, PyObject *o) PyObject *result; assert(o); - if (Future_CheckExact(o) || Task_CheckExact(o)) { + asyncio_state *state = get_asyncio_state(NULL); + if (Future_CheckExact(state, o) || Task_CheckExact(state, o)) { PyObject *fut_result = NULL; int res = future_get_result((FutureObj*)o, &fut_result); @@ -3300,8 +3308,9 @@ _asyncio__leave_task_impl(PyObject *module, PyObject *loop, PyObject *task) static PyRunningLoopHolder * new_running_loop_holder(PyObject *loop) { + asyncio_state *state = get_asyncio_state(NULL); PyRunningLoopHolder *rl = PyObject_GC_New( - PyRunningLoopHolder, PyRunningLoopHolder_Type); + PyRunningLoopHolder, state->PyRunningLoopHolder_Type); if (rl == NULL) { return NULL; } @@ -3539,6 +3548,7 @@ PyInit__asyncio(void) if (m == NULL) { return NULL; } + asyncio_state *state = get_asyncio_state(m); #define CREATE_TYPE(m, tp, spec, base) \ do { \ @@ -3549,20 +3559,20 @@ PyInit__asyncio(void) } \ } while (0) - CREATE_TYPE(m, TaskStepMethWrapper_Type, &TaskStepMethWrapper_spec, NULL); - CREATE_TYPE(m, PyRunningLoopHolder_Type, &PyRunningLoopHolder_spec, NULL); - CREATE_TYPE(m, FutureIterType, &FutureIter_spec, NULL); - CREATE_TYPE(m, FutureType, &Future_spec, NULL); - CREATE_TYPE(m, TaskType, &Task_spec, FutureType); + CREATE_TYPE(m, state->TaskStepMethWrapper_Type, &TaskStepMethWrapper_spec, NULL); + CREATE_TYPE(m, state->PyRunningLoopHolder_Type, &PyRunningLoopHolder_spec, NULL); + CREATE_TYPE(m, state->FutureIterType, &FutureIter_spec, NULL); + CREATE_TYPE(m, state->FutureType, &Future_spec, NULL); + CREATE_TYPE(m, state->TaskType, &Task_spec, state->FutureType); #undef CREATE_TYPE - if (PyModule_AddType(m, FutureType) < 0) { + if (PyModule_AddType(m, state->FutureType) < 0) { Py_DECREF(m); return NULL; } - if (PyModule_AddType(m, TaskType) < 0) { + if (PyModule_AddType(m, state->TaskType) < 0) { Py_DECREF(m); return NULL; } From b7c19528512fba48e0ca6ca4dd3ff059f4c8f326 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Wed, 28 Sep 2022 15:17:23 +0200 Subject: [PATCH 08/26] Put imports into module state --- Modules/_asynciomodule.c | 238 ++++++++++++++++++++++++--------------- 1 file changed, 145 insertions(+), 93 deletions(-) diff --git a/Modules/_asynciomodule.c b/Modules/_asynciomodule.c index aa5930e39ea3f6..e02366c84db754 100644 --- a/Modules/_asynciomodule.c +++ b/Modules/_asynciomodule.c @@ -23,6 +23,40 @@ typedef struct { PyTypeObject *FutureType; PyTypeObject *TaskType; PyTypeObject *PyRunningLoopHolder_Type; + + PyObject *asyncio_mod; + PyObject *context_kwname; + + /* Dictionary containing tasks that are currently active in + all running event loops. {EventLoop: Task} */ + PyObject *current_tasks; + + /* WeakSet containing all alive tasks. */ + PyObject *all_tasks; + + /* An isinstance type cache for the 'is_coroutine()' function. */ + PyObject *iscoroutine_typecache; + + /* Imports from asyncio.events. */ + PyObject *asyncio_get_event_loop_policy; + + /* Imports from asyncio.base_futures. */ + PyObject *asyncio_future_repr_func; + + /* Imports from asyncio.exceptions. */ + PyObject *asyncio_CancelledError; + PyObject *asyncio_InvalidStateError; + + /* Imports from asyncio.base_tasks. */ + PyObject *asyncio_task_get_stack_func; + PyObject *asyncio_task_print_stack_func; + PyObject *asyncio_task_repr_func; + + /* Imports from asyncio.coroutines. */ + PyObject *asyncio_iscoroutine_func; + + /* Imports from traceback. */ + PyObject *traceback_extract_stack; } asyncio_state; static asyncio_state global_state; @@ -33,17 +67,6 @@ get_asyncio_state(PyObject *Py_UNUSED(mod)) return &global_state; } -static PyObject *asyncio_mod; -static PyObject *traceback_extract_stack; -static PyObject *asyncio_get_event_loop_policy; -static PyObject *asyncio_future_repr_func; -static PyObject *asyncio_iscoroutine_func; -static PyObject *asyncio_task_get_stack_func; -static PyObject *asyncio_task_print_stack_func; -static PyObject *asyncio_task_repr_func; -static PyObject *asyncio_InvalidStateError; -static PyObject *asyncio_CancelledError; -static PyObject *context_kwname; static int module_initialized; static PyObject *cached_running_holder; @@ -52,16 +75,6 @@ static volatile uint64_t cached_running_holder_tsid; /* Counter for autogenerated Task names */ static uint64_t task_name_counter = 0; -/* WeakSet containing all alive tasks. */ -static PyObject *all_tasks; - -/* Dictionary containing tasks that are currently active in - all running event loops. {EventLoop: Task} */ -static PyObject *current_tasks; - -/* An isinstance type cache for the 'is_coroutine()' function. */ -static PyObject *iscoroutine_typecache; - typedef enum { STATE_PENDING, @@ -147,7 +160,8 @@ _is_coroutine(PyObject *coro) Do this check after 'future_init()'; in case we need to raise an error, __del__ needs a properly initialized object. */ - PyObject *res = PyObject_CallOneArg(asyncio_iscoroutine_func, coro); + asyncio_state *state = get_asyncio_state(NULL); + PyObject *res = PyObject_CallOneArg(state->asyncio_iscoroutine_func, coro); if (res == NULL) { return -1; } @@ -158,12 +172,12 @@ _is_coroutine(PyObject *coro) return is_res_true; } - if (PySet_GET_SIZE(iscoroutine_typecache) < 100) { + if (PySet_GET_SIZE(state->iscoroutine_typecache) < 100) { /* Just in case we don't want to cache more than 100 positive types. That shouldn't ever happen, unless someone stressing the system on purpose. */ - if (PySet_Add(iscoroutine_typecache, (PyObject*) Py_TYPE(coro))) { + if (PySet_Add(state->iscoroutine_typecache, (PyObject*) Py_TYPE(coro))) { return -1; } } @@ -188,8 +202,9 @@ is_coroutine(PyObject *coro) This cache allows us to avoid the cost of even calling a pure-Python function in 99.9% cases. */ + asyncio_state *state = get_asyncio_state(NULL); int has_it = PySet_Contains( - iscoroutine_typecache, (PyObject*) Py_TYPE(coro)); + state->iscoroutine_typecache, (PyObject*) Py_TYPE(coro)); if (has_it == 0) { /* type(coro) is not in iscoroutine_typecache */ return _is_coroutine(coro); @@ -348,7 +363,8 @@ get_event_loop(int stacklevel) return NULL; } - policy = PyObject_CallNoArgs(asyncio_get_event_loop_policy); + asyncio_state *state = get_asyncio_state(NULL); + policy = PyObject_CallNoArgs(state->asyncio_get_event_loop_policy); if (policy == NULL) { return NULL; } @@ -387,7 +403,9 @@ call_soon(PyObject *loop, PyObject *func, PyObject *arg, PyObject *ctx) } stack[nargs] = (PyObject *)ctx; EVAL_CALL_STAT_INC_IF_FUNCTION(EVAL_CALL_API, callable); - handle = PyObject_Vectorcall(callable, stack, nargs, context_kwname); + asyncio_state *state = get_asyncio_state(NULL); + handle = PyObject_Vectorcall(callable, stack, nargs, + state->context_kwname); Py_DECREF(callable); } @@ -533,7 +551,8 @@ future_init(FutureObj *fut, PyObject *loop) method, which is called during the interpreter shutdown and the traceback module is already unloaded. */ - fut->fut_source_tb = PyObject_CallNoArgs(traceback_extract_stack); + asyncio_state *state = get_asyncio_state(NULL); + fut->fut_source_tb = PyObject_CallNoArgs(state->traceback_extract_stack); if (fut->fut_source_tb == NULL) { return -1; } @@ -550,7 +569,8 @@ future_set_result(FutureObj *fut, PyObject *res) } if (fut->fut_state != STATE_PENDING) { - PyErr_SetString(asyncio_InvalidStateError, "invalid state"); + asyncio_state *state = get_asyncio_state(NULL); + PyErr_SetString(state->asyncio_InvalidStateError, "invalid state"); return NULL; } @@ -571,7 +591,8 @@ future_set_exception(FutureObj *fut, PyObject *exc) PyObject *exc_val = NULL; if (fut->fut_state != STATE_PENDING) { - PyErr_SetString(asyncio_InvalidStateError, "invalid state"); + asyncio_state *state = get_asyncio_state(NULL); + PyErr_SetString(state->asyncio_InvalidStateError, "invalid state"); return NULL; } @@ -582,7 +603,8 @@ future_set_exception(FutureObj *fut, PyObject *exc) } if (fut->fut_state != STATE_PENDING) { Py_DECREF(exc_val); - PyErr_SetString(asyncio_InvalidStateError, "invalid state"); + asyncio_state *state = get_asyncio_state(NULL); + PyErr_SetString(state->asyncio_InvalidStateError, "invalid state"); return NULL; } } @@ -628,10 +650,11 @@ create_cancelled_error(FutureObj *fut) return exc; } PyObject *msg = fut->fut_cancel_msg; + asyncio_state *state = get_asyncio_state(NULL); if (msg == NULL || msg == Py_None) { - exc = PyObject_CallNoArgs(asyncio_CancelledError); + exc = PyObject_CallNoArgs(state->asyncio_CancelledError); } else { - exc = PyObject_CallOneArg(asyncio_CancelledError, msg); + exc = PyObject_CallOneArg(state->asyncio_CancelledError, msg); } return exc; } @@ -643,7 +666,8 @@ future_set_cancelled_error(FutureObj *fut) if (exc == NULL) { return; } - PyErr_SetObject(asyncio_CancelledError, exc); + asyncio_state *state = get_asyncio_state(NULL); + PyErr_SetObject(state->asyncio_CancelledError, exc); Py_DECREF(exc); } @@ -656,7 +680,9 @@ future_get_result(FutureObj *fut, PyObject **result) } if (fut->fut_state != STATE_FINISHED) { - PyErr_SetString(asyncio_InvalidStateError, "Result is not set."); + asyncio_state *state = get_asyncio_state(NULL); + PyErr_SetString(state->asyncio_InvalidStateError, + "Result is not set."); return -1; } @@ -856,7 +882,8 @@ _asyncio_Future_result_impl(FutureObj *self) PyObject *result; if (!future_is_alive(self)) { - PyErr_SetString(asyncio_InvalidStateError, + asyncio_state *state = get_asyncio_state(NULL); + PyErr_SetString(state->asyncio_InvalidStateError, "Future object is not initialized."); return NULL; } @@ -894,7 +921,8 @@ _asyncio_Future_exception_impl(FutureObj *self) /*[clinic end generated code: output=88b20d4f855e0710 input=733547a70c841c68]*/ { if (!future_is_alive(self)) { - PyErr_SetString(asyncio_InvalidStateError, + asyncio_state *state = get_asyncio_state(NULL); + PyErr_SetString(state->asyncio_InvalidStateError, "Future object is not initialized."); return NULL; } @@ -905,7 +933,9 @@ _asyncio_Future_exception_impl(FutureObj *self) } if (self->fut_state != STATE_FINISHED) { - PyErr_SetString(asyncio_InvalidStateError, "Exception is not set."); + asyncio_state *state = get_asyncio_state(NULL); + PyErr_SetString(state->asyncio_InvalidStateError, + "Exception is not set."); return NULL; } @@ -1383,8 +1413,9 @@ FutureObj_get_state(FutureObj *fut, void *Py_UNUSED(ignored)) static PyObject * FutureObj_repr(FutureObj *fut) { + asyncio_state *state = get_asyncio_state(NULL); ENSURE_FUTURE_ALIVE(fut) - return PyObject_CallOneArg(asyncio_future_repr_func, (PyObject *)fut); + return PyObject_CallOneArg(state->asyncio_future_repr_func, (PyObject *)fut); } /*[clinic input] @@ -1939,8 +1970,9 @@ static PyMethodDef TaskWakeupDef = { static int register_task(PyObject *task) { - PyObject *res = PyObject_CallMethodOneArg(all_tasks, - &_Py_ID(add), task); + asyncio_state *state = get_asyncio_state(NULL); + PyObject *res = PyObject_CallMethodOneArg(state->all_tasks, + &_Py_ID(add), task); if (res == NULL) { return -1; } @@ -1952,8 +1984,16 @@ register_task(PyObject *task) static int unregister_task(PyObject *task) { +<<<<<<< HEAD PyObject *res = PyObject_CallMethodOneArg(all_tasks, &_Py_ID(discard), task); +======= + _Py_IDENTIFIER(discard); + + asyncio_state *state = get_asyncio_state(NULL); + PyObject *res = _PyObject_CallMethodIdOneArg(state->all_tasks, + &PyId_discard, task); +>>>>>>> 11da6fbe0b (Put imports into module state) if (res == NULL) { return -1; } @@ -1971,7 +2011,8 @@ enter_task(PyObject *loop, PyObject *task) if (hash == -1) { return -1; } - item = _PyDict_GetItem_KnownHash(current_tasks, loop, hash); + asyncio_state *state = get_asyncio_state(NULL); + item = _PyDict_GetItem_KnownHash(state->current_tasks, loop, hash); if (item != NULL) { Py_INCREF(item); PyErr_Format( @@ -1985,7 +2026,7 @@ enter_task(PyObject *loop, PyObject *task) if (PyErr_Occurred()) { return -1; } - return _PyDict_SetItem_KnownHash(current_tasks, loop, task, hash); + return _PyDict_SetItem_KnownHash(state->current_tasks, loop, task, hash); } @@ -1999,7 +2040,8 @@ leave_task(PyObject *loop, PyObject *task) if (hash == -1) { return -1; } - item = _PyDict_GetItem_KnownHash(current_tasks, loop, hash); + asyncio_state *state = get_asyncio_state(NULL); + item = _PyDict_GetItem_KnownHash(state->current_tasks, loop, hash); if (item != task) { if (item == NULL) { /* Not entered, replace with None */ @@ -2011,7 +2053,7 @@ leave_task(PyObject *loop, PyObject *task) task, item, NULL); return -1; } - return _PyDict_DelItem_KnownHash(current_tasks, loop, hash); + return _PyDict_DelItem_KnownHash(state->current_tasks, loop, hash); } /* ----- Task */ @@ -2169,7 +2211,9 @@ TaskObj_get_fut_waiter(TaskObj *task, void *Py_UNUSED(ignored)) static PyObject * TaskObj_repr(TaskObj *task) { - return PyObject_CallOneArg(asyncio_task_repr_func, (PyObject *)task); + asyncio_state *state = get_asyncio_state(NULL); + return PyObject_CallOneArg(state->asyncio_task_repr_func, + (PyObject *)task); } @@ -2334,8 +2378,9 @@ static PyObject * _asyncio_Task_get_stack_impl(TaskObj *self, PyObject *limit) /*[clinic end generated code: output=c9aeeeebd1e18118 input=05b323d42b809b90]*/ { + asyncio_state *state = get_asyncio_state(NULL); return PyObject_CallFunctionObjArgs( - asyncio_task_get_stack_func, self, limit, NULL); + state->asyncio_task_get_stack_func, self, limit, NULL); } /*[clinic input] @@ -2359,8 +2404,9 @@ _asyncio_Task_print_stack_impl(TaskObj *self, PyObject *limit, PyObject *file) /*[clinic end generated code: output=7339e10314cd3f4d input=1a0352913b7fcd92]*/ { + asyncio_state *state = get_asyncio_state(NULL); return PyObject_CallFunctionObjArgs( - asyncio_task_print_stack_func, self, limit, file, NULL); + state->asyncio_task_print_stack_func, self, limit, file, NULL); } /*[clinic input] @@ -2683,7 +2729,8 @@ task_step_impl(TaskObj *task, PyObject *exc) PyObject *o; if (task->task_state != STATE_PENDING) { - PyErr_Format(asyncio_InvalidStateError, + asyncio_state *state = get_asyncio_state(NULL); + PyErr_Format(state->asyncio_InvalidStateError, "_step(): already done: %R %R", task, exc ? exc : Py_None); @@ -2695,7 +2742,8 @@ task_step_impl(TaskObj *task, PyObject *exc) if (exc) { /* Check if exc is a CancelledError */ - res = PyObject_IsInstance(exc, asyncio_CancelledError); + asyncio_state *state = get_asyncio_state(NULL); + res = PyObject_IsInstance(exc, state->asyncio_CancelledError); if (res == -1) { /* An error occurred, abort */ goto fail; @@ -2770,7 +2818,8 @@ task_step_impl(TaskObj *task, PyObject *exc) Py_RETURN_NONE; } - if (PyErr_ExceptionMatches(asyncio_CancelledError)) { + asyncio_state *state = get_asyncio_state(NULL); + if (PyErr_ExceptionMatches(state->asyncio_CancelledError)) { /* CancelledError */ PyErr_Fetch(&et, &ev, &tb); assert(et); @@ -2943,7 +2992,8 @@ task_step_impl(TaskObj *task, PyObject *exc) stack[0] = wrapper; stack[1] = (PyObject *)task->task_context; EVAL_CALL_STAT_INC_IF_FUNCTION(EVAL_CALL_API, add_cb); - tmp = PyObject_Vectorcall(add_cb, stack, 1, context_kwname); + asyncio_state *state = get_asyncio_state(NULL); + tmp = PyObject_Vectorcall(add_cb, stack, 1, state->context_kwname); Py_DECREF(add_cb); Py_DECREF(wrapper); if (tmp == NULL) { @@ -3403,22 +3453,23 @@ module_free_freelists(void) static void module_free(void *m) { - Py_CLEAR(asyncio_mod); - Py_CLEAR(traceback_extract_stack); - Py_CLEAR(asyncio_future_repr_func); - Py_CLEAR(asyncio_get_event_loop_policy); - Py_CLEAR(asyncio_iscoroutine_func); - Py_CLEAR(asyncio_task_get_stack_func); - Py_CLEAR(asyncio_task_print_stack_func); - Py_CLEAR(asyncio_task_repr_func); - Py_CLEAR(asyncio_InvalidStateError); - Py_CLEAR(asyncio_CancelledError); - - Py_CLEAR(all_tasks); - Py_CLEAR(current_tasks); - Py_CLEAR(iscoroutine_typecache); - - Py_CLEAR(context_kwname); + asyncio_state *state = get_asyncio_state(NULL); + Py_CLEAR(state->asyncio_mod); + Py_CLEAR(state->traceback_extract_stack); + Py_CLEAR(state->asyncio_future_repr_func); + Py_CLEAR(state->asyncio_get_event_loop_policy); + Py_CLEAR(state->asyncio_iscoroutine_func); + Py_CLEAR(state->asyncio_task_get_stack_func); + Py_CLEAR(state->asyncio_task_print_stack_func); + Py_CLEAR(state->asyncio_task_repr_func); + Py_CLEAR(state->asyncio_InvalidStateError); + Py_CLEAR(state->asyncio_CancelledError); + + Py_CLEAR(state->all_tasks); + Py_CLEAR(state->current_tasks); + Py_CLEAR(state->iscoroutine_typecache); + + Py_CLEAR(state->context_kwname); module_free_freelists(); @@ -3428,29 +3479,30 @@ module_free(void *m) static int module_init(void) { + asyncio_state *state = get_asyncio_state(NULL); PyObject *module = NULL; if (module_initialized) { return 0; } - asyncio_mod = PyImport_ImportModule("asyncio"); - if (asyncio_mod == NULL) { + state->asyncio_mod = PyImport_ImportModule("asyncio"); + if (state->asyncio_mod == NULL) { goto fail; } - current_tasks = PyDict_New(); - if (current_tasks == NULL) { + state->current_tasks = PyDict_New(); + if (state->current_tasks == NULL) { goto fail; } - iscoroutine_typecache = PySet_New(NULL); - if (iscoroutine_typecache == NULL) { + state->iscoroutine_typecache = PySet_New(NULL); + if (state->iscoroutine_typecache == NULL) { goto fail; } - context_kwname = Py_BuildValue("(s)", "context"); - if (context_kwname == NULL) { + state->context_kwname = Py_BuildValue("(s)", "context"); + if (state->context_kwname == NULL) { goto fail; } @@ -3468,32 +3520,32 @@ module_init(void) } WITH_MOD("asyncio.events") - GET_MOD_ATTR(asyncio_get_event_loop_policy, "get_event_loop_policy") + GET_MOD_ATTR(state->asyncio_get_event_loop_policy, "get_event_loop_policy") WITH_MOD("asyncio.base_futures") - GET_MOD_ATTR(asyncio_future_repr_func, "_future_repr") + GET_MOD_ATTR(state->asyncio_future_repr_func, "_future_repr") WITH_MOD("asyncio.exceptions") - GET_MOD_ATTR(asyncio_InvalidStateError, "InvalidStateError") - GET_MOD_ATTR(asyncio_CancelledError, "CancelledError") + GET_MOD_ATTR(state->asyncio_InvalidStateError, "InvalidStateError") + GET_MOD_ATTR(state->asyncio_CancelledError, "CancelledError") WITH_MOD("asyncio.base_tasks") - GET_MOD_ATTR(asyncio_task_repr_func, "_task_repr") - GET_MOD_ATTR(asyncio_task_get_stack_func, "_task_get_stack") - GET_MOD_ATTR(asyncio_task_print_stack_func, "_task_print_stack") + GET_MOD_ATTR(state->asyncio_task_repr_func, "_task_repr") + GET_MOD_ATTR(state->asyncio_task_get_stack_func, "_task_get_stack") + GET_MOD_ATTR(state->asyncio_task_print_stack_func, "_task_print_stack") WITH_MOD("asyncio.coroutines") - GET_MOD_ATTR(asyncio_iscoroutine_func, "iscoroutine") + GET_MOD_ATTR(state->asyncio_iscoroutine_func, "iscoroutine") WITH_MOD("traceback") - GET_MOD_ATTR(traceback_extract_stack, "extract_stack") + GET_MOD_ATTR(state->traceback_extract_stack, "extract_stack") PyObject *weak_set; WITH_MOD("weakref") GET_MOD_ATTR(weak_set, "WeakSet"); - all_tasks = PyObject_CallNoArgs(weak_set); + state->all_tasks = PyObject_CallNoArgs(weak_set); Py_CLEAR(weak_set); - if (all_tasks == NULL) { + if (state->all_tasks == NULL) { goto fail; } @@ -3577,16 +3629,16 @@ PyInit__asyncio(void) return NULL; } - Py_INCREF(all_tasks); - if (PyModule_AddObject(m, "_all_tasks", all_tasks) < 0) { - Py_DECREF(all_tasks); + Py_INCREF(state->all_tasks); + if (PyModule_AddObject(m, "_all_tasks", state->all_tasks) < 0) { + Py_DECREF(state->all_tasks); Py_DECREF(m); return NULL; } - Py_INCREF(current_tasks); - if (PyModule_AddObject(m, "_current_tasks", current_tasks) < 0) { - Py_DECREF(current_tasks); + Py_INCREF(state->current_tasks); + if (PyModule_AddObject(m, "_current_tasks", state->current_tasks) < 0) { + Py_DECREF(state->current_tasks); Py_DECREF(m); return NULL; } From a147e8717f9d5b8542b729f7f7694a427cdd7e9e Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Wed, 28 Sep 2022 15:22:55 +0200 Subject: [PATCH 09/26] Put remaining global into module struct --- Modules/_asynciomodule.c | 47 ++++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/Modules/_asynciomodule.c b/Modules/_asynciomodule.c index e02366c84db754..a3c169cb4c311c 100644 --- a/Modules/_asynciomodule.c +++ b/Modules/_asynciomodule.c @@ -57,6 +57,14 @@ typedef struct { /* Imports from traceback. */ PyObject *traceback_extract_stack; + + int module_initialized; + + PyObject *cached_running_holder; + volatile uint64_t cached_running_holder_tsid; + + /* Counter for autogenerated Task names */ + uint64_t task_name_counter; } asyncio_state; static asyncio_state global_state; @@ -67,15 +75,6 @@ get_asyncio_state(PyObject *Py_UNUSED(mod)) return &global_state; } -static int module_initialized; - -static PyObject *cached_running_holder; -static volatile uint64_t cached_running_holder_tsid; - -/* Counter for autogenerated Task names */ -static uint64_t task_name_counter = 0; - - typedef enum { STATE_PENDING, STATE_CANCELLED, @@ -252,9 +251,11 @@ get_running_loop(PyObject **loop) PyThreadState *ts = _PyThreadState_GET(); uint64_t ts_id = PyThreadState_GetID(ts); - if (ts_id == cached_running_holder_tsid && cached_running_holder != NULL) { + if (state->cached_running_holder_tsid == ts_id && + state->cached_running_holder != NULL) + { // Fast path, check the cache. - rl = cached_running_holder; // borrowed + rl = state->cached_running_holder; // borrowed } else { PyObject *ts_dict = _PyThreadState_GetDict(ts); // borrowed @@ -273,8 +274,8 @@ get_running_loop(PyObject **loop) } } - cached_running_holder = rl; // borrowed - cached_running_holder_tsid = ts_id; + state->cached_running_holder = rl; // borrowed + state->cached_running_holder_tsid = ts_id; } assert(Py_IS_TYPE(rl, state->PyRunningLoopHolder_Type)); @@ -336,8 +337,9 @@ set_running_loop(PyObject *loop) } Py_DECREF(rl); - cached_running_holder = (PyObject *)rl; - cached_running_holder_tsid = PyThreadState_GetID(tstate); + asyncio_state *state = get_asyncio_state(NULL); + state->cached_running_holder = (PyObject *)rl; + state->cached_running_holder_tsid = PyThreadState_GetID(tstate); return 0; } @@ -2109,7 +2111,9 @@ _asyncio_Task___init___impl(TaskObj *self, PyObject *coro, PyObject *loop, Py_XSETREF(self->task_coro, coro); if (name == Py_None) { - name = PyUnicode_FromFormat("Task-%" PRIu64, ++task_name_counter); + asyncio_state *state = get_asyncio_state(NULL); + name = PyUnicode_FromFormat("Task-%" PRIu64, + ++state->task_name_counter); } else if (!PyUnicode_CheckExact(name)) { name = PyObject_Str(name); } else { @@ -3398,8 +3402,9 @@ PyRunningLoopHolder_traverse(PyRunningLoopHolder *rl, visitproc visit, static void PyRunningLoopHolder_tp_dealloc(PyRunningLoopHolder *rl) { - if (cached_running_holder == (PyObject *)rl) { - cached_running_holder = NULL; + asyncio_state *state = get_asyncio_state(NULL); + if (state->cached_running_holder == (PyObject *)rl) { + state->cached_running_holder = NULL; } PyTypeObject *tp = Py_TYPE(rl); PyObject_GC_UnTrack(rl); @@ -3473,7 +3478,7 @@ module_free(void *m) module_free_freelists(); - module_initialized = 0; + state->module_initialized = 0; } static int @@ -3481,7 +3486,7 @@ module_init(void) { asyncio_state *state = get_asyncio_state(NULL); PyObject *module = NULL; - if (module_initialized) { + if (state->module_initialized) { return 0; } @@ -3549,7 +3554,7 @@ module_init(void) goto fail; } - module_initialized = 1; + state->module_initialized = 1; Py_DECREF(module); return 0; From 3938201ab8403a21436b861a8a743d8849d61574 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Wed, 28 Sep 2022 15:25:32 +0200 Subject: [PATCH 10/26] Pass state as argument to ENSURE_FUTURE_ALIVE --- Modules/_asynciomodule.c | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/Modules/_asynciomodule.c b/Modules/_asynciomodule.c index a3c169cb4c311c..33b993c7af4f19 100644 --- a/Modules/_asynciomodule.c +++ b/Modules/_asynciomodule.c @@ -438,9 +438,8 @@ future_ensure_alive(FutureObj *fut) } -#define ENSURE_FUTURE_ALIVE(fut) \ +#define ENSURE_FUTURE_ALIVE(state, fut) \ do { \ - asyncio_state *state = get_asyncio_state(NULL); \ assert(Future_Check(state, fut) || Task_Check(state, fut)); \ if (future_ensure_alive((FutureObj*)fut)) { \ return NULL; \ @@ -966,7 +965,8 @@ static PyObject * _asyncio_Future_set_result(FutureObj *self, PyObject *result) /*[clinic end generated code: output=1ec2e6bcccd6f2ce input=8b75172c2a7b05f1]*/ { - ENSURE_FUTURE_ALIVE(self) + asyncio_state *state = get_asyncio_state(NULL); + ENSURE_FUTURE_ALIVE(state, self) return future_set_result(self, result); } @@ -986,7 +986,8 @@ static PyObject * _asyncio_Future_set_exception(FutureObj *self, PyObject *exception) /*[clinic end generated code: output=f1c1b0cd321be360 input=e45b7d7aa71cc66d]*/ { - ENSURE_FUTURE_ALIVE(self) + asyncio_state *state = get_asyncio_state(NULL); + ENSURE_FUTURE_ALIVE(state, self) return future_set_exception(self, exception); } @@ -1041,7 +1042,8 @@ _asyncio_Future_remove_done_callback(FutureObj *self, PyObject *fn) Py_ssize_t len, i, j=0; Py_ssize_t cleared_callback0 = 0; - ENSURE_FUTURE_ALIVE(self) + asyncio_state *state = get_asyncio_state(NULL); + ENSURE_FUTURE_ALIVE(state, self) if (self->fut_callback0 != NULL) { int cmp = PyObject_RichCompareBool(self->fut_callback0, fn, Py_EQ); @@ -1151,7 +1153,8 @@ static PyObject * _asyncio_Future_cancel_impl(FutureObj *self, PyObject *msg) /*[clinic end generated code: output=3edebbc668e5aba3 input=925eb545251f2c5a]*/ { - ENSURE_FUTURE_ALIVE(self) + asyncio_state *state = get_asyncio_state(NULL); + ENSURE_FUTURE_ALIVE(state, self) return future_cancel(self, msg); } @@ -1204,7 +1207,8 @@ static PyObject * _asyncio_Future_get_loop_impl(FutureObj *self) /*[clinic end generated code: output=119b6ea0c9816c3f input=cba48c2136c79d1f]*/ { - ENSURE_FUTURE_ALIVE(self) + asyncio_state *state = get_asyncio_state(NULL); + ENSURE_FUTURE_ALIVE(state, self) Py_INCREF(self->fut_loop); return self->fut_loop; } @@ -1242,7 +1246,8 @@ FutureObj_set_blocking(FutureObj *fut, PyObject *val, void *Py_UNUSED(ignored)) static PyObject * FutureObj_get_log_traceback(FutureObj *fut, void *Py_UNUSED(ignored)) { - ENSURE_FUTURE_ALIVE(fut) + asyncio_state *state = get_asyncio_state(NULL); + ENSURE_FUTURE_ALIVE(state, fut) if (fut->fut_log_tb) { Py_RETURN_TRUE; } @@ -1286,7 +1291,8 @@ FutureObj_get_callbacks(FutureObj *fut, void *Py_UNUSED(ignored)) { Py_ssize_t i; - ENSURE_FUTURE_ALIVE(fut) + asyncio_state *state = get_asyncio_state(NULL); + ENSURE_FUTURE_ALIVE(state, fut) if (fut->fut_callback0 == NULL) { if (fut->fut_callbacks == NULL) { @@ -1336,7 +1342,8 @@ FutureObj_get_callbacks(FutureObj *fut, void *Py_UNUSED(ignored)) static PyObject * FutureObj_get_result(FutureObj *fut, void *Py_UNUSED(ignored)) { - ENSURE_FUTURE_ALIVE(fut) + asyncio_state *state = get_asyncio_state(NULL); + ENSURE_FUTURE_ALIVE(state, fut) if (fut->fut_result == NULL) { Py_RETURN_NONE; } @@ -1347,7 +1354,8 @@ FutureObj_get_result(FutureObj *fut, void *Py_UNUSED(ignored)) static PyObject * FutureObj_get_exception(FutureObj *fut, void *Py_UNUSED(ignored)) { - ENSURE_FUTURE_ALIVE(fut) + asyncio_state *state = get_asyncio_state(NULL); + ENSURE_FUTURE_ALIVE(state, fut) if (fut->fut_exception == NULL) { Py_RETURN_NONE; } @@ -1393,7 +1401,8 @@ FutureObj_get_state(FutureObj *fut, void *Py_UNUSED(ignored)) { PyObject *ret = NULL; - ENSURE_FUTURE_ALIVE(fut) + asyncio_state *state = get_asyncio_state(NULL); + ENSURE_FUTURE_ALIVE(state, fut) switch (fut->fut_state) { case STATE_PENDING: @@ -1416,7 +1425,7 @@ static PyObject * FutureObj_repr(FutureObj *fut) { asyncio_state *state = get_asyncio_state(NULL); - ENSURE_FUTURE_ALIVE(fut) + ENSURE_FUTURE_ALIVE(state, fut) return PyObject_CallOneArg(state->asyncio_future_repr_func, (PyObject *)fut); } @@ -1824,7 +1833,7 @@ future_new_iter(PyObject *fut) return NULL; } - ENSURE_FUTURE_ALIVE(fut) + ENSURE_FUTURE_ALIVE(state, fut) if (fi_freelist_len) { fi_freelist_len--; From 3b4babaaef94161d39d2240c6b7efe55ae71ca09 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Wed, 28 Sep 2022 15:58:56 +0200 Subject: [PATCH 11/26] Add helpers to get asyncio state from defining class and module def --- Modules/_asynciomodule.c | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/Modules/_asynciomodule.c b/Modules/_asynciomodule.c index 33b993c7af4f19..8d5632863a3bd7 100644 --- a/Modules/_asynciomodule.c +++ b/Modules/_asynciomodule.c @@ -75,6 +75,31 @@ get_asyncio_state(PyObject *Py_UNUSED(mod)) return &global_state; } +static inline asyncio_state * +get_asyncio_state_by_cls(PyTypeObject *cls) +{ + /* + asyncio_state *state = (asyncio_state *)PyType_GetModuleState(cls); + assert(state != NULL); + return state; + */ + return &global_state; +} + +static struct PyModuleDef _asynciomodule; + +static inline asyncio_state * +get_asyncio_state_by_def(PyObject *self) +{ + /* + PyTypeObject *tp = Py_TYPE(self); + PyObject *mod = PyType_GetModuleByDef(tp, &_asynciomodule); + assert(mod != NULL); + return get_asyncio_state(mod); + */ + return get_asyncio_state(NULL); +} + typedef enum { STATE_PENDING, STATE_CANCELLED, @@ -1995,16 +2020,9 @@ register_task(PyObject *task) static int unregister_task(PyObject *task) { -<<<<<<< HEAD - PyObject *res = PyObject_CallMethodOneArg(all_tasks, - &_Py_ID(discard), task); -======= - _Py_IDENTIFIER(discard); - asyncio_state *state = get_asyncio_state(NULL); - PyObject *res = _PyObject_CallMethodIdOneArg(state->all_tasks, - &PyId_discard, task); ->>>>>>> 11da6fbe0b (Put imports into module state) + PyObject *res = PyObject_CallMethodOneArg(state->all_tasks, + &_Py_ID(discard), task); if (res == NULL) { return -1; } From 06e8216513e987823b4c703d350344e632c2fb40 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Mon, 3 Oct 2022 23:43:11 +0200 Subject: [PATCH 12/26] Prepare for module state, batch 1 --- Modules/_asynciomodule.c | 89 ++++++++++++++++--------------- Modules/clinic/_asynciomodule.c.h | 21 ++++---- 2 files changed, 56 insertions(+), 54 deletions(-) diff --git a/Modules/_asynciomodule.c b/Modules/_asynciomodule.c index 8d5632863a3bd7..ea9864ef85831d 100644 --- a/Modules/_asynciomodule.c +++ b/Modules/_asynciomodule.c @@ -176,7 +176,7 @@ static PyRunningLoopHolder * new_running_loop_holder(PyObject *); static int -_is_coroutine(PyObject *coro) +_is_coroutine(asyncio_state *state, PyObject *coro) { /* 'coro' is not a native coroutine, call asyncio.iscoroutine() to check if it's another coroutine flavour. @@ -184,7 +184,6 @@ _is_coroutine(PyObject *coro) Do this check after 'future_init()'; in case we need to raise an error, __del__ needs a properly initialized object. */ - asyncio_state *state = get_asyncio_state(NULL); PyObject *res = PyObject_CallOneArg(state->asyncio_iscoroutine_func, coro); if (res == NULL) { return -1; @@ -211,7 +210,7 @@ _is_coroutine(PyObject *coro) static inline int -is_coroutine(PyObject *coro) +is_coroutine(asyncio_state *state, PyObject *coro) { if (PyCoro_CheckExact(coro)) { return 1; @@ -226,12 +225,11 @@ is_coroutine(PyObject *coro) This cache allows us to avoid the cost of even calling a pure-Python function in 99.9% cases. */ - asyncio_state *state = get_asyncio_state(NULL); int has_it = PySet_Contains( state->iscoroutine_typecache, (PyObject*) Py_TYPE(coro)); if (has_it == 0) { /* type(coro) is not in iscoroutine_typecache */ - return _is_coroutine(coro); + return _is_coroutine(state, coro); } /* either an error has occurred or @@ -242,13 +240,12 @@ is_coroutine(PyObject *coro) static PyObject * -get_future_loop(PyObject *fut) +get_future_loop(asyncio_state *state, PyObject *fut) { /* Implementation of `asyncio.futures._get_loop` */ PyObject *getloop; - asyncio_state *state = get_asyncio_state(NULL); if (Future_CheckExact(state, fut) || Task_CheckExact(state, fut)) { PyObject *loop = ((FutureObj *)fut)->fut_loop; Py_INCREF(loop); @@ -269,10 +266,9 @@ get_future_loop(PyObject *fut) static int -get_running_loop(PyObject **loop) +get_running_loop(asyncio_state *state, PyObject **loop) { PyObject *rl; - asyncio_state *state = get_asyncio_state(NULL); PyThreadState *ts = _PyThreadState_GET(); uint64_t ts_id = PyThreadState_GetID(ts); @@ -371,12 +367,12 @@ set_running_loop(PyObject *loop) static PyObject * -get_event_loop(int stacklevel) +get_event_loop(asyncio_state *state, int stacklevel) { PyObject *loop; PyObject *policy; - if (get_running_loop(&loop)) { + if (get_running_loop(state, &loop)) { return NULL; } if (loop != NULL) { @@ -390,7 +386,6 @@ get_event_loop(int stacklevel) return NULL; } - asyncio_state *state = get_asyncio_state(NULL); policy = PyObject_CallNoArgs(state->asyncio_get_event_loop_policy); if (policy == NULL) { return NULL; @@ -551,7 +546,8 @@ future_init(FutureObj *fut, PyObject *loop) fut->fut_blocking = 0; if (loop == Py_None) { - loop = get_event_loop(1); + asyncio_state *state = get_asyncio_state_by_def((PyObject *)fut); + loop = get_event_loop(state, 1); if (loop == NULL) { return -1; } @@ -577,7 +573,7 @@ future_init(FutureObj *fut, PyObject *loop) method, which is called during the interpreter shutdown and the traceback module is already unloaded. */ - asyncio_state *state = get_asyncio_state(NULL); + asyncio_state *state = get_asyncio_state_by_def((PyObject *)fut); fut->fut_source_tb = PyObject_CallNoArgs(state->traceback_extract_stack); if (fut->fut_source_tb == NULL) { return -1; @@ -1892,7 +1888,7 @@ class _asyncio.Task "TaskObj *" "&Task_Type" static int task_call_step_soon(TaskObj *, PyObject *); static PyObject * task_wakeup(TaskObj *, PyObject *); -static PyObject * task_step(TaskObj *, PyObject *); +static PyObject * task_step(asyncio_state *, TaskObj *, PyObject *); /* ----- Task._step wrapper */ @@ -1926,7 +1922,8 @@ TaskStepMethWrapper_call(TaskStepMethWrapper *o, PyErr_SetString(PyExc_TypeError, "function takes no positional arguments"); return NULL; } - return task_step(o->sw_task, o->sw_arg); + asyncio_state *state = get_asyncio_state(NULL); + return task_step(state, o->sw_task, o->sw_arg); } static int @@ -2109,7 +2106,8 @@ _asyncio_Task___init___impl(TaskObj *self, PyObject *coro, PyObject *loop, return -1; } - int is_coro = is_coroutine(coro); + asyncio_state *state = get_asyncio_state_by_def((PyObject *)self); + int is_coro = is_coroutine(state, coro); if (is_coro == -1) { return -1; } @@ -2138,7 +2136,6 @@ _asyncio_Task___init___impl(TaskObj *self, PyObject *coro, PyObject *loop, Py_XSETREF(self->task_coro, coro); if (name == Py_None) { - asyncio_state *state = get_asyncio_state(NULL); name = PyUnicode_FromFormat("Task-%" PRIu64, ++state->task_name_counter); } else if (!PyUnicode_CheckExact(name)) { @@ -2242,7 +2239,7 @@ TaskObj_get_fut_waiter(TaskObj *task, void *Py_UNUSED(ignored)) static PyObject * TaskObj_repr(TaskObj *task) { - asyncio_state *state = get_asyncio_state(NULL); + asyncio_state *state = get_asyncio_state_by_def((PyObject *)task); return PyObject_CallOneArg(state->asyncio_task_repr_func, (PyObject *)task); } @@ -2381,6 +2378,8 @@ _asyncio_Task_uncancel_impl(TaskObj *self) /*[clinic input] _asyncio.Task.get_stack + cls: defining_class + / * limit: object = None @@ -2406,10 +2405,11 @@ returned for a suspended coroutine. [clinic start generated code]*/ static PyObject * -_asyncio_Task_get_stack_impl(TaskObj *self, PyObject *limit) -/*[clinic end generated code: output=c9aeeeebd1e18118 input=05b323d42b809b90]*/ +_asyncio_Task_get_stack_impl(TaskObj *self, PyTypeObject *cls, + PyObject *limit) +/*[clinic end generated code: output=6774dfc10d3857fa input=8e01c9b2618ae953]*/ { - asyncio_state *state = get_asyncio_state(NULL); + asyncio_state *state = get_asyncio_state_by_cls(cls); return PyObject_CallFunctionObjArgs( state->asyncio_task_get_stack_func, self, limit, NULL); } @@ -2417,6 +2417,8 @@ _asyncio_Task_get_stack_impl(TaskObj *self, PyObject *limit) /*[clinic input] _asyncio.Task.print_stack + cls: defining_class + / * limit: object = None file: object = None @@ -2431,11 +2433,11 @@ to sys.stderr. [clinic start generated code]*/ static PyObject * -_asyncio_Task_print_stack_impl(TaskObj *self, PyObject *limit, - PyObject *file) -/*[clinic end generated code: output=7339e10314cd3f4d input=1a0352913b7fcd92]*/ +_asyncio_Task_print_stack_impl(TaskObj *self, PyTypeObject *cls, + PyObject *limit, PyObject *file) +/*[clinic end generated code: output=b38affe9289ec826 input=150b35ba2d3a7dee]*/ { - asyncio_state *state = get_asyncio_state(NULL); + asyncio_state *state = get_asyncio_state_by_cls(cls); return PyObject_CallFunctionObjArgs( state->asyncio_task_print_stack_func, self, limit, file, NULL); } @@ -2671,7 +2673,7 @@ TaskObj_dealloc(PyObject *self) { TaskObj *task = (TaskObj *)self; - asyncio_state *state = get_asyncio_state(NULL); + asyncio_state *state = get_asyncio_state_by_def(self); if (Task_CheckExact(state, self)) { /* When fut is subclass of Task, finalizer is called from * subtype_dealloc. @@ -2751,7 +2753,7 @@ gen_status_from_result(PyObject **result) } static PyObject * -task_step_impl(TaskObj *task, PyObject *exc) +task_step_impl(asyncio_state *state, TaskObj *task, PyObject *exc) { int res; int clear_exc = 0; @@ -2760,7 +2762,6 @@ task_step_impl(TaskObj *task, PyObject *exc) PyObject *o; if (task->task_state != STATE_PENDING) { - asyncio_state *state = get_asyncio_state(NULL); PyErr_Format(state->asyncio_InvalidStateError, "_step(): already done: %R %R", task, @@ -2773,7 +2774,6 @@ task_step_impl(TaskObj *task, PyObject *exc) if (exc) { /* Check if exc is a CancelledError */ - asyncio_state *state = get_asyncio_state(NULL); res = PyObject_IsInstance(exc, state->asyncio_CancelledError); if (res == -1) { /* An error occurred, abort */ @@ -2849,7 +2849,6 @@ task_step_impl(TaskObj *task, PyObject *exc) Py_RETURN_NONE; } - asyncio_state *state = get_asyncio_state(NULL); if (PyErr_ExceptionMatches(state->asyncio_CancelledError)) { /* CancelledError */ PyErr_Fetch(&et, &ev, &tb); @@ -2908,7 +2907,6 @@ task_step_impl(TaskObj *task, PyObject *exc) } /* Check if `result` is FutureObj or TaskObj (and not a subclass) */ - asyncio_state *state = get_asyncio_state(NULL); if (Future_CheckExact(state, result) || Task_CheckExact(state, result)) { PyObject *wrapper; PyObject *tmp; @@ -2987,7 +2985,7 @@ task_step_impl(TaskObj *task, PyObject *exc) } /* Check if `result` future is attached to a different loop */ - PyObject *oloop = get_future_loop(result); + PyObject *oloop = get_future_loop(state, result); if (oloop == NULL) { goto fail; } @@ -3023,7 +3021,6 @@ task_step_impl(TaskObj *task, PyObject *exc) stack[0] = wrapper; stack[1] = (PyObject *)task->task_context; EVAL_CALL_STAT_INC_IF_FUNCTION(EVAL_CALL_API, add_cb); - asyncio_state *state = get_asyncio_state(NULL); tmp = PyObject_Vectorcall(add_cb, stack, 1, state->context_kwname); Py_DECREF(add_cb); Py_DECREF(wrapper); @@ -3108,7 +3105,7 @@ task_step_impl(TaskObj *task, PyObject *exc) } static PyObject * -task_step(TaskObj *task, PyObject *exc) +task_step(asyncio_state *state, TaskObj *task, PyObject *exc) { PyObject *res; @@ -3116,7 +3113,7 @@ task_step(TaskObj *task, PyObject *exc) return NULL; } - res = task_step_impl(task, exc); + res = task_step_impl(state, task, exc); if (res == NULL) { PyObject *et, *ev, *tb; @@ -3154,10 +3151,10 @@ task_wakeup(TaskObj *task, PyObject *o) break; /* exception raised */ case 0: Py_DECREF(fut_result); - return task_step(task, NULL); + return task_step(state, task, NULL); default: assert(res == 1); - result = task_step(task, fut_result); + result = task_step(state, task, fut_result); Py_DECREF(fut_result); return result; } @@ -3166,7 +3163,7 @@ task_wakeup(TaskObj *task, PyObject *o) PyObject *fut_result = PyObject_CallMethod(o, "result", NULL); if (fut_result != NULL) { Py_DECREF(fut_result); - return task_step(task, NULL); + return task_step(state, task, NULL); } /* exception raised */ } @@ -3178,7 +3175,7 @@ task_wakeup(TaskObj *task, PyObject *o) PyException_SetTraceback(ev, tb); } - result = task_step(task, ev); + result = task_step(state, task, ev); Py_DECREF(et); Py_XDECREF(tb); @@ -3206,7 +3203,8 @@ _asyncio__get_running_loop_impl(PyObject *module) /*[clinic end generated code: output=b4390af721411a0a input=0a21627e25a4bd43]*/ { PyObject *loop; - if (get_running_loop(&loop)) { + asyncio_state *state = get_asyncio_state(module); + if (get_running_loop(state, &loop)) { return NULL; } if (loop == NULL) { @@ -3254,7 +3252,8 @@ static PyObject * _asyncio_get_event_loop_impl(PyObject *module) /*[clinic end generated code: output=2a2d8b2f824c648b input=9364bf2916c8655d]*/ { - return get_event_loop(1); + asyncio_state *state = get_asyncio_state(module); + return get_event_loop(state, 1); } /*[clinic input] @@ -3266,7 +3265,8 @@ static PyObject * _asyncio__get_event_loop_impl(PyObject *module, int stacklevel) /*[clinic end generated code: output=9c1d6d3c802e67c9 input=d17aebbd686f711d]*/ { - return get_event_loop(stacklevel-1); + asyncio_state *state = get_asyncio_state(module); + return get_event_loop(state, stacklevel-1); } /*[clinic input] @@ -3282,7 +3282,8 @@ _asyncio_get_running_loop_impl(PyObject *module) /*[clinic end generated code: output=c247b5f9e529530e input=2a3bf02ba39f173d]*/ { PyObject *loop; - if (get_running_loop(&loop)) { + asyncio_state *state = get_asyncio_state(module); + if (get_running_loop(state, &loop)) { return NULL; } if (loop == NULL) { diff --git a/Modules/clinic/_asynciomodule.c.h b/Modules/clinic/_asynciomodule.c.h index ddec54c8d7c2bc..dddac09020563f 100644 --- a/Modules/clinic/_asynciomodule.c.h +++ b/Modules/clinic/_asynciomodule.c.h @@ -612,13 +612,14 @@ PyDoc_STRVAR(_asyncio_Task_get_stack__doc__, "returned for a suspended coroutine."); #define _ASYNCIO_TASK_GET_STACK_METHODDEF \ - {"get_stack", _PyCFunction_CAST(_asyncio_Task_get_stack), METH_FASTCALL|METH_KEYWORDS, _asyncio_Task_get_stack__doc__}, + {"get_stack", _PyCFunction_CAST(_asyncio_Task_get_stack), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _asyncio_Task_get_stack__doc__}, static PyObject * -_asyncio_Task_get_stack_impl(TaskObj *self, PyObject *limit); +_asyncio_Task_get_stack_impl(TaskObj *self, PyTypeObject *cls, + PyObject *limit); static PyObject * -_asyncio_Task_get_stack(TaskObj *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) +_asyncio_Task_get_stack(TaskObj *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) { PyObject *return_value = NULL; #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) @@ -659,7 +660,7 @@ _asyncio_Task_get_stack(TaskObj *self, PyObject *const *args, Py_ssize_t nargs, } limit = args[0]; skip_optional_kwonly: - return_value = _asyncio_Task_get_stack_impl(self, limit); + return_value = _asyncio_Task_get_stack_impl(self, cls, limit); exit: return return_value; @@ -678,14 +679,14 @@ PyDoc_STRVAR(_asyncio_Task_print_stack__doc__, "to sys.stderr."); #define _ASYNCIO_TASK_PRINT_STACK_METHODDEF \ - {"print_stack", _PyCFunction_CAST(_asyncio_Task_print_stack), METH_FASTCALL|METH_KEYWORDS, _asyncio_Task_print_stack__doc__}, + {"print_stack", _PyCFunction_CAST(_asyncio_Task_print_stack), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _asyncio_Task_print_stack__doc__}, static PyObject * -_asyncio_Task_print_stack_impl(TaskObj *self, PyObject *limit, - PyObject *file); +_asyncio_Task_print_stack_impl(TaskObj *self, PyTypeObject *cls, + PyObject *limit, PyObject *file); static PyObject * -_asyncio_Task_print_stack(TaskObj *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) +_asyncio_Task_print_stack(TaskObj *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) { PyObject *return_value = NULL; #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) @@ -733,7 +734,7 @@ _asyncio_Task_print_stack(TaskObj *self, PyObject *const *args, Py_ssize_t nargs } file = args[1]; skip_optional_kwonly: - return_value = _asyncio_Task_print_stack_impl(self, limit, file); + return_value = _asyncio_Task_print_stack_impl(self, cls, limit, file); exit: return return_value; @@ -1189,4 +1190,4 @@ _asyncio__leave_task(PyObject *module, PyObject *const *args, Py_ssize_t nargs, exit: return return_value; } -/*[clinic end generated code: output=f117b2246eaf7a55 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=a1c30ebb88aa7b0c input=a9049054013a1b77]*/ From 8a7ac68779fa1cd1f8557dd5ab2f929dc6807b47 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Mon, 3 Oct 2022 23:55:11 +0200 Subject: [PATCH 13/26] Prepare for module state, batch 2 --- Modules/_asynciomodule.c | 122 ++++++++++++++++-------------- Modules/clinic/_asynciomodule.c.h | 95 ++++++++++++++++++++--- 2 files changed, 148 insertions(+), 69 deletions(-) diff --git a/Modules/_asynciomodule.c b/Modules/_asynciomodule.c index ea9864ef85831d..4fc467150acac6 100644 --- a/Modules/_asynciomodule.c +++ b/Modules/_asynciomodule.c @@ -172,7 +172,7 @@ class _asyncio.Future "FutureObj *" "&Future_Type" /* Get FutureIter from Future */ static PyObject * future_new_iter(PyObject *); -static PyRunningLoopHolder * new_running_loop_holder(PyObject *); +static PyRunningLoopHolder * new_running_loop_holder(asyncio_state *, PyObject *); static int @@ -330,7 +330,7 @@ get_running_loop(asyncio_state *state, PyObject **loop) static int -set_running_loop(PyObject *loop) +set_running_loop(asyncio_state *state, PyObject *loop) { PyObject *ts_dict = NULL; @@ -345,7 +345,7 @@ set_running_loop(PyObject *loop) return -1; } - PyRunningLoopHolder *rl = new_running_loop_holder(loop); + PyRunningLoopHolder *rl = new_running_loop_holder(state, loop); if (rl == NULL) { return -1; } @@ -358,7 +358,6 @@ set_running_loop(PyObject *loop) } Py_DECREF(rl); - asyncio_state *state = get_asyncio_state(NULL); state->cached_running_holder = (PyObject *)rl; state->cached_running_holder_tsid = PyThreadState_GetID(tstate); @@ -398,7 +397,8 @@ get_event_loop(asyncio_state *state, int stacklevel) static int -call_soon(PyObject *loop, PyObject *func, PyObject *arg, PyObject *ctx) +call_soon(asyncio_state *state, PyObject *loop, PyObject *func, PyObject *arg, + PyObject *ctx) { PyObject *handle; PyObject *stack[3]; @@ -425,7 +425,6 @@ call_soon(PyObject *loop, PyObject *func, PyObject *arg, PyObject *ctx) } stack[nargs] = (PyObject *)ctx; EVAL_CALL_STAT_INC_IF_FUNCTION(EVAL_CALL_API, callable); - asyncio_state *state = get_asyncio_state(NULL); handle = PyObject_Vectorcall(callable, stack, nargs, state->context_kwname); Py_DECREF(callable); @@ -468,7 +467,7 @@ future_ensure_alive(FutureObj *fut) static int -future_schedule_callbacks(FutureObj *fut) +future_schedule_callbacks(asyncio_state *state, FutureObj *fut) { Py_ssize_t len; Py_ssize_t i; @@ -476,7 +475,7 @@ future_schedule_callbacks(FutureObj *fut) if (fut->fut_callback0 != NULL) { /* There's a 1st callback */ - int ret = call_soon( + int ret = call_soon(state, fut->fut_loop, fut->fut_callback0, (PyObject *)fut, fut->fut_context0); @@ -510,7 +509,7 @@ future_schedule_callbacks(FutureObj *fut) PyObject *cb = PyTuple_GET_ITEM(cb_tup, 0); PyObject *ctx = PyTuple_GET_ITEM(cb_tup, 1); - if (call_soon(fut->fut_loop, cb, (PyObject *)fut, ctx)) { + if (call_soon(state, fut->fut_loop, cb, (PyObject *)fut, ctx)) { /* If an error occurs in pure-Python implementation, all callbacks are cleared. */ Py_CLEAR(fut->fut_callbacks); @@ -584,14 +583,13 @@ future_init(FutureObj *fut, PyObject *loop) } static PyObject * -future_set_result(FutureObj *fut, PyObject *res) +future_set_result(asyncio_state *state, FutureObj *fut, PyObject *res) { if (future_ensure_alive(fut)) { return NULL; } if (fut->fut_state != STATE_PENDING) { - asyncio_state *state = get_asyncio_state(NULL); PyErr_SetString(state->asyncio_InvalidStateError, "invalid state"); return NULL; } @@ -601,19 +599,18 @@ future_set_result(FutureObj *fut, PyObject *res) fut->fut_result = res; fut->fut_state = STATE_FINISHED; - if (future_schedule_callbacks(fut) == -1) { + if (future_schedule_callbacks(state, fut) == -1) { return NULL; } Py_RETURN_NONE; } static PyObject * -future_set_exception(FutureObj *fut, PyObject *exc) +future_set_exception(asyncio_state *state, FutureObj *fut, PyObject *exc) { PyObject *exc_val = NULL; if (fut->fut_state != STATE_PENDING) { - asyncio_state *state = get_asyncio_state(NULL); PyErr_SetString(state->asyncio_InvalidStateError, "invalid state"); return NULL; } @@ -625,7 +622,6 @@ future_set_exception(FutureObj *fut, PyObject *exc) } if (fut->fut_state != STATE_PENDING) { Py_DECREF(exc_val); - asyncio_state *state = get_asyncio_state(NULL); PyErr_SetString(state->asyncio_InvalidStateError, "invalid state"); return NULL; } @@ -653,7 +649,7 @@ future_set_exception(FutureObj *fut, PyObject *exc) fut->fut_exception_tb = PyException_GetTraceback(exc_val); fut->fut_state = STATE_FINISHED; - if (future_schedule_callbacks(fut) == -1) { + if (future_schedule_callbacks(state, fut) == -1) { return NULL; } @@ -729,7 +725,8 @@ future_get_result(FutureObj *fut, PyObject **result) } static PyObject * -future_add_done_callback(FutureObj *fut, PyObject *arg, PyObject *ctx) +future_add_done_callback(asyncio_state *state, FutureObj *fut, PyObject *arg, + PyObject *ctx) { if (!future_is_alive(fut)) { PyErr_SetString(PyExc_RuntimeError, "uninitialized Future object"); @@ -739,7 +736,7 @@ future_add_done_callback(FutureObj *fut, PyObject *arg, PyObject *ctx) if (fut->fut_state != STATE_PENDING) { /* The future is done/cancelled, so schedule the callback right away. */ - if (call_soon(fut->fut_loop, arg, (PyObject*) fut, ctx)) { + if (call_soon(state, fut->fut_loop, arg, (PyObject*) fut, ctx)) { return NULL; } } @@ -805,7 +802,7 @@ future_add_done_callback(FutureObj *fut, PyObject *arg, PyObject *ctx) } static PyObject * -future_cancel(FutureObj *fut, PyObject *msg) +future_cancel(asyncio_state *state, FutureObj *fut, PyObject *msg) { fut->fut_log_tb = 0; @@ -817,7 +814,7 @@ future_cancel(FutureObj *fut, PyObject *msg) Py_XINCREF(msg); Py_XSETREF(fut->fut_cancel_msg, msg); - if (future_schedule_callbacks(fut) == -1) { + if (future_schedule_callbacks(state, fut) == -1) { return NULL; } @@ -973,6 +970,7 @@ _asyncio_Future_exception_impl(FutureObj *self) /*[clinic input] _asyncio.Future.set_result + cls: defining_class result: object / @@ -983,17 +981,19 @@ InvalidStateError. [clinic start generated code]*/ static PyObject * -_asyncio_Future_set_result(FutureObj *self, PyObject *result) -/*[clinic end generated code: output=1ec2e6bcccd6f2ce input=8b75172c2a7b05f1]*/ +_asyncio_Future_set_result_impl(FutureObj *self, PyTypeObject *cls, + PyObject *result) +/*[clinic end generated code: output=99afbbe78f99c32d input=d5a41c1e353acc2e]*/ { - asyncio_state *state = get_asyncio_state(NULL); + asyncio_state *state = get_asyncio_state_by_cls(cls); ENSURE_FUTURE_ALIVE(state, self) - return future_set_result(self, result); + return future_set_result(state, self, result); } /*[clinic input] _asyncio.Future.set_exception + cls: defining_class exception: object / @@ -1004,17 +1004,19 @@ InvalidStateError. [clinic start generated code]*/ static PyObject * -_asyncio_Future_set_exception(FutureObj *self, PyObject *exception) -/*[clinic end generated code: output=f1c1b0cd321be360 input=e45b7d7aa71cc66d]*/ +_asyncio_Future_set_exception_impl(FutureObj *self, PyTypeObject *cls, + PyObject *exception) +/*[clinic end generated code: output=0a5e8b5a52f058d6 input=a245cd49d3df939b]*/ { - asyncio_state *state = get_asyncio_state(NULL); + asyncio_state *state = get_asyncio_state_by_cls(cls); ENSURE_FUTURE_ALIVE(state, self) - return future_set_exception(self, exception); + return future_set_exception(state, self, exception); } /*[clinic input] _asyncio.Future.add_done_callback + cls: defining_class fn: object / * @@ -1028,20 +1030,21 @@ scheduled with call_soon. [clinic start generated code]*/ static PyObject * -_asyncio_Future_add_done_callback_impl(FutureObj *self, PyObject *fn, - PyObject *context) -/*[clinic end generated code: output=7ce635bbc9554c1e input=15ab0693a96e9533]*/ +_asyncio_Future_add_done_callback_impl(FutureObj *self, PyTypeObject *cls, + PyObject *fn, PyObject *context) +/*[clinic end generated code: output=922e9a4cbd601167 input=599261c521458cc2]*/ { + asyncio_state *state = get_asyncio_state_by_cls(cls); if (context == NULL) { context = PyContext_CopyCurrent(); if (context == NULL) { return NULL; } - PyObject *res = future_add_done_callback(self, fn, context); + PyObject *res = future_add_done_callback(state, self, fn, context); Py_DECREF(context); return res; } - return future_add_done_callback(self, fn, context); + return future_add_done_callback(state, self, fn, context); } /*[clinic input] @@ -1161,6 +1164,8 @@ _asyncio_Future_remove_done_callback(FutureObj *self, PyObject *fn) /*[clinic input] _asyncio.Future.cancel + cls: defining_class + / msg: object = None Cancel the future and schedule callbacks. @@ -1171,12 +1176,13 @@ return True. [clinic start generated code]*/ static PyObject * -_asyncio_Future_cancel_impl(FutureObj *self, PyObject *msg) -/*[clinic end generated code: output=3edebbc668e5aba3 input=925eb545251f2c5a]*/ +_asyncio_Future_cancel_impl(FutureObj *self, PyTypeObject *cls, + PyObject *msg) +/*[clinic end generated code: output=074956f35904b034 input=bba8f8b786941a94]*/ { - asyncio_state *state = get_asyncio_state(NULL); + asyncio_state *state = get_asyncio_state_by_cls(cls); ENSURE_FUTURE_ALIVE(state, self) - return future_cancel(self, msg); + return future_cancel(state, self, msg); } /*[clinic input] @@ -1886,7 +1892,7 @@ class _asyncio.Task "TaskObj *" "&Task_Type" [clinic start generated code]*/ /*[clinic end generated code: output=da39a3ee5e6b4b0d input=719dcef0fcc03b37]*/ -static int task_call_step_soon(TaskObj *, PyObject *); +static int task_call_step_soon(asyncio_state *state, TaskObj *, PyObject *); static PyObject * task_wakeup(TaskObj *, PyObject *); static PyObject * task_step(asyncio_state *, TaskObj *, PyObject *); @@ -2148,7 +2154,7 @@ _asyncio_Task___init___impl(TaskObj *self, PyObject *coro, PyObject *loop, return -1; } - if (task_call_step_soon(self, NULL)) { + if (task_call_step_soon(state, self, NULL)) { return -1; } return register_task((PyObject*)self); @@ -2697,20 +2703,21 @@ TaskObj_dealloc(PyObject *self) } static int -task_call_step_soon(TaskObj *task, PyObject *arg) +task_call_step_soon(asyncio_state *state, TaskObj *task, PyObject *arg) { PyObject *cb = TaskStepMethWrapper_new(task, arg); if (cb == NULL) { return -1; } - int ret = call_soon(task->task_loop, cb, NULL, task->task_context); + int ret = call_soon(state, task->task_loop, cb, NULL, task->task_context); Py_DECREF(cb); return ret; } static PyObject * -task_set_error_soon(TaskObj *task, PyObject *et, const char *format, ...) +task_set_error_soon(asyncio_state *state, TaskObj *task, PyObject *et, + const char *format, ...) { PyObject* msg; @@ -2729,7 +2736,7 @@ task_set_error_soon(TaskObj *task, PyObject *et, const char *format, ...) return NULL; } - if (task_call_step_soon(task, e) == -1) { + if (task_call_step_soon(state, task, e) == -1) { Py_DECREF(e); return NULL; } @@ -2834,10 +2841,11 @@ task_step_impl(asyncio_state *state, TaskObj *task, PyObject *exc) if (task->task_must_cancel) { // Task is cancelled right before coro stops. task->task_must_cancel = 0; - tmp = future_cancel((FutureObj*)task, task->task_cancel_msg); + tmp = future_cancel(state, (FutureObj*)task, + task->task_cancel_msg); } else { - tmp = future_set_result((FutureObj*)task, result); + tmp = future_set_result(state, (FutureObj*)task, result); } Py_DECREF(result); @@ -2864,7 +2872,7 @@ task_step_impl(asyncio_state *state, TaskObj *task, PyObject *exc) /* transfer ownership */ fut->fut_cancelled_exc = ev; - return future_cancel(fut, NULL); + return future_cancel(state, fut, NULL); } /* Some other exception; pop it and call Task.set_exception() */ @@ -2875,7 +2883,7 @@ task_step_impl(asyncio_state *state, TaskObj *task, PyObject *exc) PyException_SetTraceback(ev, tb); } - o = future_set_exception((FutureObj*)task, ev); + o = future_set_exception(state, (FutureObj*)task, ev); if (!o) { /* An exception in Task.set_exception() */ Py_DECREF(et); @@ -2928,7 +2936,7 @@ task_step_impl(asyncio_state *state, TaskObj *task, PyObject *exc) if (wrapper == NULL) { goto fail; } - tmp = future_add_done_callback( + tmp = future_add_done_callback(state, (FutureObj*)result, wrapper, task->task_context); Py_DECREF(wrapper); if (tmp == NULL) { @@ -2963,7 +2971,7 @@ task_step_impl(asyncio_state *state, TaskObj *task, PyObject *exc) /* Check if `result` is None */ if (result == Py_None) { /* Bare yield relinquishes control for one event loop iteration. */ - if (task_call_step_soon(task, NULL)) { + if (task_call_step_soon(state, task, NULL)) { goto fail; } return result; @@ -3062,7 +3070,7 @@ task_step_impl(asyncio_state *state, TaskObj *task, PyObject *exc) if (res) { /* `result` is a generator */ o = task_set_error_soon( - task, PyExc_RuntimeError, + state, task, PyExc_RuntimeError, "yield was used instead of yield from for " "generator in task %R with %R", task, result); Py_DECREF(result); @@ -3071,20 +3079,20 @@ task_step_impl(asyncio_state *state, TaskObj *task, PyObject *exc) /* The `result` is none of the above */ o = task_set_error_soon( - task, PyExc_RuntimeError, "Task got bad yield: %R", result); + state, task, PyExc_RuntimeError, "Task got bad yield: %R", result); Py_DECREF(result); return o; self_await: o = task_set_error_soon( - task, PyExc_RuntimeError, + state, task, PyExc_RuntimeError, "Task cannot await on itself: %R", task); Py_DECREF(result); return o; yield_insteadof_yf: o = task_set_error_soon( - task, PyExc_RuntimeError, + state, task, PyExc_RuntimeError, "yield was used instead of yield from " "in task %R with %R", task, result); @@ -3093,7 +3101,7 @@ task_step_impl(asyncio_state *state, TaskObj *task, PyObject *exc) different_loop: o = task_set_error_soon( - task, PyExc_RuntimeError, + state, task, PyExc_RuntimeError, "Task %R got Future %R attached to a different loop", task, result); Py_DECREF(result); @@ -3229,7 +3237,8 @@ static PyObject * _asyncio__set_running_loop(PyObject *module, PyObject *loop) /*[clinic end generated code: output=ae56bf7a28ca189a input=4c9720233d606604]*/ { - if (set_running_loop(loop)) { + asyncio_state *state = get_asyncio_state(module); + if (set_running_loop(state, loop)) { return NULL; } Py_RETURN_NONE; @@ -3388,9 +3397,8 @@ _asyncio__leave_task_impl(PyObject *module, PyObject *loop, PyObject *task) static PyRunningLoopHolder * -new_running_loop_holder(PyObject *loop) +new_running_loop_holder(asyncio_state *state, PyObject *loop) { - asyncio_state *state = get_asyncio_state(NULL); PyRunningLoopHolder *rl = PyObject_GC_New( PyRunningLoopHolder, state->PyRunningLoopHolder_Type); if (rl == NULL) { diff --git a/Modules/clinic/_asynciomodule.c.h b/Modules/clinic/_asynciomodule.c.h index dddac09020563f..34e275f7235549 100644 --- a/Modules/clinic/_asynciomodule.c.h +++ b/Modules/clinic/_asynciomodule.c.h @@ -133,7 +133,42 @@ PyDoc_STRVAR(_asyncio_Future_set_result__doc__, "InvalidStateError."); #define _ASYNCIO_FUTURE_SET_RESULT_METHODDEF \ - {"set_result", (PyCFunction)_asyncio_Future_set_result, METH_O, _asyncio_Future_set_result__doc__}, + {"set_result", _PyCFunction_CAST(_asyncio_Future_set_result), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _asyncio_Future_set_result__doc__}, + +static PyObject * +_asyncio_Future_set_result_impl(FutureObj *self, PyTypeObject *cls, + PyObject *result); + +static PyObject * +_asyncio_Future_set_result(FutureObj *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) +{ + PyObject *return_value = NULL; + #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) + # define KWTUPLE (PyObject *)&_Py_SINGLETON(tuple_empty) + #else + # define KWTUPLE NULL + #endif + + static const char * const _keywords[] = {"", NULL}; + static _PyArg_Parser _parser = { + .keywords = _keywords, + .fname = "set_result", + .kwtuple = KWTUPLE, + }; + #undef KWTUPLE + PyObject *argsbuf[1]; + PyObject *result; + + args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf); + if (!args) { + goto exit; + } + result = args[0]; + return_value = _asyncio_Future_set_result_impl(self, cls, result); + +exit: + return return_value; +} PyDoc_STRVAR(_asyncio_Future_set_exception__doc__, "set_exception($self, exception, /)\n" @@ -145,7 +180,42 @@ PyDoc_STRVAR(_asyncio_Future_set_exception__doc__, "InvalidStateError."); #define _ASYNCIO_FUTURE_SET_EXCEPTION_METHODDEF \ - {"set_exception", (PyCFunction)_asyncio_Future_set_exception, METH_O, _asyncio_Future_set_exception__doc__}, + {"set_exception", _PyCFunction_CAST(_asyncio_Future_set_exception), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _asyncio_Future_set_exception__doc__}, + +static PyObject * +_asyncio_Future_set_exception_impl(FutureObj *self, PyTypeObject *cls, + PyObject *exception); + +static PyObject * +_asyncio_Future_set_exception(FutureObj *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) +{ + PyObject *return_value = NULL; + #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) + # define KWTUPLE (PyObject *)&_Py_SINGLETON(tuple_empty) + #else + # define KWTUPLE NULL + #endif + + static const char * const _keywords[] = {"", NULL}; + static _PyArg_Parser _parser = { + .keywords = _keywords, + .fname = "set_exception", + .kwtuple = KWTUPLE, + }; + #undef KWTUPLE + PyObject *argsbuf[1]; + PyObject *exception; + + args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf); + if (!args) { + goto exit; + } + exception = args[0]; + return_value = _asyncio_Future_set_exception_impl(self, cls, exception); + +exit: + return return_value; +} PyDoc_STRVAR(_asyncio_Future_add_done_callback__doc__, "add_done_callback($self, fn, /, *, context=)\n" @@ -158,14 +228,14 @@ PyDoc_STRVAR(_asyncio_Future_add_done_callback__doc__, "scheduled with call_soon."); #define _ASYNCIO_FUTURE_ADD_DONE_CALLBACK_METHODDEF \ - {"add_done_callback", _PyCFunction_CAST(_asyncio_Future_add_done_callback), METH_FASTCALL|METH_KEYWORDS, _asyncio_Future_add_done_callback__doc__}, + {"add_done_callback", _PyCFunction_CAST(_asyncio_Future_add_done_callback), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _asyncio_Future_add_done_callback__doc__}, static PyObject * -_asyncio_Future_add_done_callback_impl(FutureObj *self, PyObject *fn, - PyObject *context); +_asyncio_Future_add_done_callback_impl(FutureObj *self, PyTypeObject *cls, + PyObject *fn, PyObject *context); static PyObject * -_asyncio_Future_add_done_callback(FutureObj *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) +_asyncio_Future_add_done_callback(FutureObj *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) { PyObject *return_value = NULL; #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) @@ -208,7 +278,7 @@ _asyncio_Future_add_done_callback(FutureObj *self, PyObject *const *args, Py_ssi } context = args[1]; skip_optional_kwonly: - return_value = _asyncio_Future_add_done_callback_impl(self, fn, context); + return_value = _asyncio_Future_add_done_callback_impl(self, cls, fn, context); exit: return return_value; @@ -236,13 +306,14 @@ PyDoc_STRVAR(_asyncio_Future_cancel__doc__, "return True."); #define _ASYNCIO_FUTURE_CANCEL_METHODDEF \ - {"cancel", _PyCFunction_CAST(_asyncio_Future_cancel), METH_FASTCALL|METH_KEYWORDS, _asyncio_Future_cancel__doc__}, + {"cancel", _PyCFunction_CAST(_asyncio_Future_cancel), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _asyncio_Future_cancel__doc__}, static PyObject * -_asyncio_Future_cancel_impl(FutureObj *self, PyObject *msg); +_asyncio_Future_cancel_impl(FutureObj *self, PyTypeObject *cls, + PyObject *msg); static PyObject * -_asyncio_Future_cancel(FutureObj *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) +_asyncio_Future_cancel(FutureObj *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) { PyObject *return_value = NULL; #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) @@ -283,7 +354,7 @@ _asyncio_Future_cancel(FutureObj *self, PyObject *const *args, Py_ssize_t nargs, } msg = args[0]; skip_optional_pos: - return_value = _asyncio_Future_cancel_impl(self, msg); + return_value = _asyncio_Future_cancel_impl(self, cls, msg); exit: return return_value; @@ -1190,4 +1261,4 @@ _asyncio__leave_task(PyObject *module, PyObject *const *args, Py_ssize_t nargs, exit: return return_value; } -/*[clinic end generated code: output=a1c30ebb88aa7b0c input=a9049054013a1b77]*/ +/*[clinic end generated code: output=d6704f726e5f7ece input=a9049054013a1b77]*/ From 2e51638ba66c846cf795ccd2a0c909673f8b7bc2 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Tue, 4 Oct 2022 00:03:40 +0200 Subject: [PATCH 14/26] Prepare for module state, batch 3 --- Modules/_asynciomodule.c | 43 +++++++++++++++++-------------- Modules/clinic/_asynciomodule.c.h | 14 ++++++---- 2 files changed, 32 insertions(+), 25 deletions(-) diff --git a/Modules/_asynciomodule.c b/Modules/_asynciomodule.c index 4fc467150acac6..08b3d8df96970b 100644 --- a/Modules/_asynciomodule.c +++ b/Modules/_asynciomodule.c @@ -658,7 +658,7 @@ future_set_exception(asyncio_state *state, FutureObj *fut, PyObject *exc) } static PyObject * -create_cancelled_error(FutureObj *fut) +create_cancelled_error(asyncio_state *state, FutureObj *fut) { PyObject *exc; if (fut->fut_cancelled_exc != NULL) { @@ -668,7 +668,6 @@ create_cancelled_error(FutureObj *fut) return exc; } PyObject *msg = fut->fut_cancel_msg; - asyncio_state *state = get_asyncio_state(NULL); if (msg == NULL || msg == Py_None) { exc = PyObject_CallNoArgs(state->asyncio_CancelledError); } else { @@ -678,22 +677,21 @@ create_cancelled_error(FutureObj *fut) } static void -future_set_cancelled_error(FutureObj *fut) +future_set_cancelled_error(asyncio_state *state, FutureObj *fut) { - PyObject *exc = create_cancelled_error(fut); + PyObject *exc = create_cancelled_error(state, fut); if (exc == NULL) { return; } - asyncio_state *state = get_asyncio_state(NULL); PyErr_SetObject(state->asyncio_CancelledError, exc); Py_DECREF(exc); } static int -future_get_result(FutureObj *fut, PyObject **result) +future_get_result(asyncio_state *state, FutureObj *fut, PyObject **result) { if (fut->fut_state == STATE_CANCELLED) { - future_set_cancelled_error(fut); + future_set_cancelled_error(state, fut); return -1; } @@ -898,16 +896,16 @@ static PyObject * _asyncio_Future_result_impl(FutureObj *self) /*[clinic end generated code: output=f35f940936a4b1e5 input=49ecf9cf5ec50dc5]*/ { + asyncio_state *state = get_asyncio_state_by_def((PyObject *)self); PyObject *result; if (!future_is_alive(self)) { - asyncio_state *state = get_asyncio_state(NULL); PyErr_SetString(state->asyncio_InvalidStateError, "Future object is not initialized."); return NULL; } - int res = future_get_result(self, &result); + int res = future_get_result(state, self, &result); if (res == -1) { return NULL; @@ -927,6 +925,9 @@ _asyncio_Future_result_impl(FutureObj *self) /*[clinic input] _asyncio.Future.exception + cls: defining_class + / + Return the exception that was set on this future. The exception (or None if no exception was set) is returned only if @@ -936,23 +937,24 @@ InvalidStateError. [clinic start generated code]*/ static PyObject * -_asyncio_Future_exception_impl(FutureObj *self) -/*[clinic end generated code: output=88b20d4f855e0710 input=733547a70c841c68]*/ +_asyncio_Future_exception_impl(FutureObj *self, PyTypeObject *cls) +/*[clinic end generated code: output=ce75576b187c905b input=3faf15c22acdb60d]*/ { if (!future_is_alive(self)) { - asyncio_state *state = get_asyncio_state(NULL); + asyncio_state *state = get_asyncio_state_by_cls(cls); PyErr_SetString(state->asyncio_InvalidStateError, "Future object is not initialized."); return NULL; } if (self->fut_state == STATE_CANCELLED) { - future_set_cancelled_error(self); + asyncio_state *state = get_asyncio_state_by_cls(cls); + future_set_cancelled_error(state, self); return NULL; } if (self->fut_state != STATE_FINISHED) { - asyncio_state *state = get_asyncio_state(NULL); + asyncio_state *state = get_asyncio_state_by_cls(cls); PyErr_SetString(state->asyncio_InvalidStateError, "Exception is not set."); return NULL; @@ -1469,7 +1471,8 @@ static PyObject * _asyncio_Future__make_cancelled_error_impl(FutureObj *self) /*[clinic end generated code: output=a5df276f6c1213de input=ac6effe4ba795ecc]*/ { - return create_cancelled_error(self); + asyncio_state *state = get_asyncio_state_by_def((PyObject *)self); + return create_cancelled_error(state, self); } static void @@ -2794,7 +2797,7 @@ task_step_impl(asyncio_state *state, TaskObj *task, PyObject *exc) if (!exc) { /* exc was not a CancelledError */ - exc = create_cancelled_error((FutureObj*)task); + exc = create_cancelled_error(state, (FutureObj*)task); if (!exc) { goto fail; @@ -3148,10 +3151,10 @@ task_wakeup(TaskObj *task, PyObject *o) PyObject *result; assert(o); - asyncio_state *state = get_asyncio_state(NULL); + asyncio_state *state = get_asyncio_state_by_def((PyObject *)task); if (Future_CheckExact(state, o) || Task_CheckExact(state, o)) { PyObject *fut_result = NULL; - int res = future_get_result((FutureObj*)o, &fut_result); + int res = future_get_result(state, (FutureObj*)o, &fut_result); switch(res) { case -1: @@ -3438,7 +3441,7 @@ PyRunningLoopHolder_traverse(PyRunningLoopHolder *rl, visitproc visit, static void PyRunningLoopHolder_tp_dealloc(PyRunningLoopHolder *rl) { - asyncio_state *state = get_asyncio_state(NULL); + asyncio_state *state = get_asyncio_state_by_def((PyObject *)rl); if (state->cached_running_holder == (PyObject *)rl) { state->cached_running_holder = NULL; } @@ -3494,7 +3497,7 @@ module_free_freelists(void) static void module_free(void *m) { - asyncio_state *state = get_asyncio_state(NULL); + asyncio_state *state = get_asyncio_state(m); Py_CLEAR(state->asyncio_mod); Py_CLEAR(state->traceback_extract_stack); Py_CLEAR(state->asyncio_future_repr_func); diff --git a/Modules/clinic/_asynciomodule.c.h b/Modules/clinic/_asynciomodule.c.h index 34e275f7235549..4d706123442e3f 100644 --- a/Modules/clinic/_asynciomodule.c.h +++ b/Modules/clinic/_asynciomodule.c.h @@ -112,15 +112,19 @@ PyDoc_STRVAR(_asyncio_Future_exception__doc__, "InvalidStateError."); #define _ASYNCIO_FUTURE_EXCEPTION_METHODDEF \ - {"exception", (PyCFunction)_asyncio_Future_exception, METH_NOARGS, _asyncio_Future_exception__doc__}, + {"exception", _PyCFunction_CAST(_asyncio_Future_exception), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _asyncio_Future_exception__doc__}, static PyObject * -_asyncio_Future_exception_impl(FutureObj *self); +_asyncio_Future_exception_impl(FutureObj *self, PyTypeObject *cls); static PyObject * -_asyncio_Future_exception(FutureObj *self, PyObject *Py_UNUSED(ignored)) +_asyncio_Future_exception(FutureObj *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) { - return _asyncio_Future_exception_impl(self); + if (nargs) { + PyErr_SetString(PyExc_TypeError, "exception() takes no arguments"); + return NULL; + } + return _asyncio_Future_exception_impl(self, cls); } PyDoc_STRVAR(_asyncio_Future_set_result__doc__, @@ -1261,4 +1265,4 @@ _asyncio__leave_task(PyObject *module, PyObject *const *args, Py_ssize_t nargs, exit: return return_value; } -/*[clinic end generated code: output=d6704f726e5f7ece input=a9049054013a1b77]*/ +/*[clinic end generated code: output=c3b3681942e8b672 input=a9049054013a1b77]*/ From 7ebf1e9c8741d170cfd61e8f924000d9ea9da59b Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Wed, 5 Oct 2022 09:41:32 +0200 Subject: [PATCH 15/26] Prepare for module state, batch 4 --- Modules/_asynciomodule.c | 92 ++++++++++++++++++------------- Modules/clinic/_asynciomodule.c.h | 51 +++++++++++++++-- 2 files changed, 100 insertions(+), 43 deletions(-) diff --git a/Modules/_asynciomodule.c b/Modules/_asynciomodule.c index 08b3d8df96970b..9693324e4c90da 100644 --- a/Modules/_asynciomodule.c +++ b/Modules/_asynciomodule.c @@ -696,7 +696,6 @@ future_get_result(asyncio_state *state, FutureObj *fut, PyObject **result) } if (fut->fut_state != STATE_FINISHED) { - asyncio_state *state = get_asyncio_state(NULL); PyErr_SetString(state->asyncio_InvalidStateError, "Result is not set."); return -1; @@ -1052,6 +1051,7 @@ _asyncio_Future_add_done_callback_impl(FutureObj *self, PyTypeObject *cls, /*[clinic input] _asyncio.Future.remove_done_callback + cls: defining_class fn: object / @@ -1061,14 +1061,15 @@ Returns the number of callbacks removed. [clinic start generated code]*/ static PyObject * -_asyncio_Future_remove_done_callback(FutureObj *self, PyObject *fn) -/*[clinic end generated code: output=5ab1fb52b24ef31f input=0a43280a149d505b]*/ +_asyncio_Future_remove_done_callback_impl(FutureObj *self, PyTypeObject *cls, + PyObject *fn) +/*[clinic end generated code: output=2da35ccabfe41b98 input=c7518709b86fc747]*/ { PyObject *newlist; Py_ssize_t len, i, j=0; Py_ssize_t cleared_callback0 = 0; - asyncio_state *state = get_asyncio_state(NULL); + asyncio_state *state = get_asyncio_state_by_cls(cls); ENSURE_FUTURE_ALIVE(state, self) if (self->fut_callback0 != NULL) { @@ -1229,14 +1230,17 @@ _asyncio_Future_done_impl(FutureObj *self) /*[clinic input] _asyncio.Future.get_loop + cls: defining_class + / + Return the event loop the Future is bound to. [clinic start generated code]*/ static PyObject * -_asyncio_Future_get_loop_impl(FutureObj *self) -/*[clinic end generated code: output=119b6ea0c9816c3f input=cba48c2136c79d1f]*/ +_asyncio_Future_get_loop_impl(FutureObj *self, PyTypeObject *cls) +/*[clinic end generated code: output=f50ea6c374d9ee97 input=163c2c498b45a1f0]*/ { - asyncio_state *state = get_asyncio_state(NULL); + asyncio_state *state = get_asyncio_state_by_cls(cls); ENSURE_FUTURE_ALIVE(state, self) Py_INCREF(self->fut_loop); return self->fut_loop; @@ -1275,7 +1279,7 @@ FutureObj_set_blocking(FutureObj *fut, PyObject *val, void *Py_UNUSED(ignored)) static PyObject * FutureObj_get_log_traceback(FutureObj *fut, void *Py_UNUSED(ignored)) { - asyncio_state *state = get_asyncio_state(NULL); + asyncio_state *state = get_asyncio_state_by_def((PyObject *)fut); ENSURE_FUTURE_ALIVE(state, fut) if (fut->fut_log_tb) { Py_RETURN_TRUE; @@ -1318,9 +1322,9 @@ FutureObj_get_loop(FutureObj *fut, void *Py_UNUSED(ignored)) static PyObject * FutureObj_get_callbacks(FutureObj *fut, void *Py_UNUSED(ignored)) { + asyncio_state *state = get_asyncio_state_by_def((PyObject *)fut); Py_ssize_t i; - asyncio_state *state = get_asyncio_state(NULL); ENSURE_FUTURE_ALIVE(state, fut) if (fut->fut_callback0 == NULL) { @@ -1371,7 +1375,7 @@ FutureObj_get_callbacks(FutureObj *fut, void *Py_UNUSED(ignored)) static PyObject * FutureObj_get_result(FutureObj *fut, void *Py_UNUSED(ignored)) { - asyncio_state *state = get_asyncio_state(NULL); + asyncio_state *state = get_asyncio_state_by_def((PyObject *)fut); ENSURE_FUTURE_ALIVE(state, fut) if (fut->fut_result == NULL) { Py_RETURN_NONE; @@ -1383,7 +1387,7 @@ FutureObj_get_result(FutureObj *fut, void *Py_UNUSED(ignored)) static PyObject * FutureObj_get_exception(FutureObj *fut, void *Py_UNUSED(ignored)) { - asyncio_state *state = get_asyncio_state(NULL); + asyncio_state *state = get_asyncio_state_by_def((PyObject *)fut); ENSURE_FUTURE_ALIVE(state, fut) if (fut->fut_exception == NULL) { Py_RETURN_NONE; @@ -1428,9 +1432,15 @@ FutureObj_set_cancel_message(FutureObj *fut, PyObject *msg, static PyObject * FutureObj_get_state(FutureObj *fut, void *Py_UNUSED(ignored)) { +<<<<<<< HEAD +======= + _Py_IDENTIFIER(PENDING); + _Py_IDENTIFIER(CANCELLED); + _Py_IDENTIFIER(FINISHED); + asyncio_state *state = get_asyncio_state_by_def((PyObject *)fut); +>>>>>>> 1807e8688e (Prepare for module state, batch 4) PyObject *ret = NULL; - asyncio_state *state = get_asyncio_state(NULL); ENSURE_FUTURE_ALIVE(state, fut) switch (fut->fut_state) { @@ -1453,7 +1463,7 @@ FutureObj_get_state(FutureObj *fut, void *Py_UNUSED(ignored)) static PyObject * FutureObj_repr(FutureObj *fut) { - asyncio_state *state = get_asyncio_state(NULL); + asyncio_state *state = get_asyncio_state_by_def((PyObject *)fut); ENSURE_FUTURE_ALIVE(state, fut) return PyObject_CallOneArg(state->asyncio_future_repr_func, (PyObject *)fut); } @@ -1609,9 +1619,9 @@ static PyType_Spec Future_spec = { static void FutureObj_dealloc(PyObject *self) { + asyncio_state *state = get_asyncio_state_by_def(self); FutureObj *fut = (FutureObj *)self; - asyncio_state *state = get_asyncio_state(NULL); if (Future_CheckExact(state, fut)) { /* When fut is subclass of Future, finalizer is called from * subtype_dealloc. @@ -1857,7 +1867,7 @@ future_new_iter(PyObject *fut) { futureiterobject *it; - asyncio_state *state = get_asyncio_state(NULL); + asyncio_state *state = get_asyncio_state_by_def((PyObject *)fut); if (!Future_Check(state, fut)) { PyErr_BadInternalCall(); return NULL; @@ -1873,7 +1883,6 @@ future_new_iter(PyObject *fut) _Py_NewReference((PyObject*) it); } else { - asyncio_state *state = get_asyncio_state(NULL); it = PyObject_GC_New(futureiterobject, state->FutureIterType); if (it == NULL) { return NULL; @@ -1931,7 +1940,7 @@ TaskStepMethWrapper_call(TaskStepMethWrapper *o, PyErr_SetString(PyExc_TypeError, "function takes no positional arguments"); return NULL; } - asyncio_state *state = get_asyncio_state(NULL); + asyncio_state *state = get_asyncio_state_by_def((PyObject *)o); return task_step(state, o->sw_task, o->sw_arg); } @@ -1981,8 +1990,8 @@ static PyType_Spec TaskStepMethWrapper_spec = { static PyObject * TaskStepMethWrapper_new(TaskObj *task, PyObject *arg) { + asyncio_state *state = get_asyncio_state_by_def((PyObject *)task); TaskStepMethWrapper *o; - asyncio_state *state = get_asyncio_state(NULL); o = PyObject_GC_New(TaskStepMethWrapper, state->TaskStepMethWrapper_Type); if (o == NULL) { return NULL; @@ -2010,7 +2019,7 @@ static PyMethodDef TaskWakeupDef = { /* ----- Task introspection helpers */ static int -register_task(PyObject *task) +register_task(asyncio_state *state, PyObject *task) { asyncio_state *state = get_asyncio_state(NULL); PyObject *res = PyObject_CallMethodOneArg(state->all_tasks, @@ -2024,11 +2033,18 @@ register_task(PyObject *task) static int -unregister_task(PyObject *task) +unregister_task(asyncio_state *state, PyObject *task) { +<<<<<<< HEAD asyncio_state *state = get_asyncio_state(NULL); PyObject *res = PyObject_CallMethodOneArg(state->all_tasks, &_Py_ID(discard), task); +======= + _Py_IDENTIFIER(discard); + + PyObject *res = _PyObject_CallMethodIdOneArg(state->all_tasks, + &PyId_discard, task); +>>>>>>> 1807e8688e (Prepare for module state, batch 4) if (res == NULL) { return -1; } @@ -2038,7 +2054,7 @@ unregister_task(PyObject *task) static int -enter_task(PyObject *loop, PyObject *task) +enter_task(asyncio_state *state, PyObject *loop, PyObject *task) { PyObject *item; Py_hash_t hash; @@ -2046,7 +2062,6 @@ enter_task(PyObject *loop, PyObject *task) if (hash == -1) { return -1; } - asyncio_state *state = get_asyncio_state(NULL); item = _PyDict_GetItem_KnownHash(state->current_tasks, loop, hash); if (item != NULL) { Py_INCREF(item); @@ -2066,7 +2081,7 @@ enter_task(PyObject *loop, PyObject *task) static int -leave_task(PyObject *loop, PyObject *task) +leave_task(asyncio_state *state, PyObject *loop, PyObject *task) /*[clinic end generated code: output=0ebf6db4b858fb41 input=51296a46313d1ad8]*/ { PyObject *item; @@ -2075,7 +2090,6 @@ leave_task(PyObject *loop, PyObject *task) if (hash == -1) { return -1; } - asyncio_state *state = get_asyncio_state(NULL); item = _PyDict_GetItem_KnownHash(state->current_tasks, loop, hash); if (item != task) { if (item == NULL) { @@ -2160,7 +2174,7 @@ _asyncio_Task___init___impl(TaskObj *self, PyObject *coro, PyObject *loop, if (task_call_step_soon(state, self, NULL)) { return -1; } - return register_task((PyObject*)self); + return register_task(state, (PyObject*)self); } static int @@ -3120,7 +3134,7 @@ task_step(asyncio_state *state, TaskObj *task, PyObject *exc) { PyObject *res; - if (enter_task(task->task_loop, (PyObject*)task) < 0) { + if (enter_task(state, task->task_loop, (PyObject*)task) < 0) { return NULL; } @@ -3129,12 +3143,12 @@ task_step(asyncio_state *state, TaskObj *task, PyObject *exc) if (res == NULL) { PyObject *et, *ev, *tb; PyErr_Fetch(&et, &ev, &tb); - leave_task(task->task_loop, (PyObject*)task); + leave_task(state, task->task_loop, (PyObject*)task); _PyErr_ChainExceptions(et, ev, tb); /* Normalizes (et, ev, tb) */ return NULL; } else { - if (leave_task(task->task_loop, (PyObject*)task) < 0) { + if (leave_task(state, task->task_loop, (PyObject*)task) < 0) { Py_DECREF(res); return NULL; } @@ -3320,7 +3334,8 @@ static PyObject * _asyncio__register_task_impl(PyObject *module, PyObject *task) /*[clinic end generated code: output=8672dadd69a7d4e2 input=21075aaea14dfbad]*/ { - if (register_task(task) < 0) { + asyncio_state *state = get_asyncio_state(module); + if (register_task(state, task) < 0) { return NULL; } Py_RETURN_NONE; @@ -3341,7 +3356,8 @@ static PyObject * _asyncio__unregister_task_impl(PyObject *module, PyObject *task) /*[clinic end generated code: output=6e5585706d568a46 input=28fb98c3975f7bdc]*/ { - if (unregister_task(task) < 0) { + asyncio_state *state = get_asyncio_state(module); + if (unregister_task(state, task) < 0) { return NULL; } Py_RETURN_NONE; @@ -3365,7 +3381,8 @@ static PyObject * _asyncio__enter_task_impl(PyObject *module, PyObject *loop, PyObject *task) /*[clinic end generated code: output=a22611c858035b73 input=de1b06dca70d8737]*/ { - if (enter_task(loop, task) < 0) { + asyncio_state *state = get_asyncio_state(module); + if (enter_task(state, loop, task) < 0) { return NULL; } Py_RETURN_NONE; @@ -3389,7 +3406,8 @@ static PyObject * _asyncio__leave_task_impl(PyObject *module, PyObject *loop, PyObject *task) /*[clinic end generated code: output=0ebf6db4b858fb41 input=51296a46313d1ad8]*/ { - if (leave_task(loop, task) < 0) { + asyncio_state *state = get_asyncio_state(module); + if (leave_task(state, loop, task) < 0) { return NULL; } Py_RETURN_NONE; @@ -3521,9 +3539,8 @@ module_free(void *m) } static int -module_init(void) +module_init(asyncio_state *state) { - asyncio_state *state = get_asyncio_state(NULL); PyObject *module = NULL; if (state->module_initialized) { return 0; @@ -3637,14 +3654,15 @@ static struct PyModuleDef _asynciomodule = { PyMODINIT_FUNC PyInit__asyncio(void) { - if (module_init() < 0) { - return NULL; - } PyObject *m = PyModule_Create(&_asynciomodule); if (m == NULL) { return NULL; } asyncio_state *state = get_asyncio_state(m); + if (module_init(state) < 0) { + Py_DECREF(m); + return NULL; + } #define CREATE_TYPE(m, tp, spec, base) \ do { \ diff --git a/Modules/clinic/_asynciomodule.c.h b/Modules/clinic/_asynciomodule.c.h index 4d706123442e3f..11db478a8b4827 100644 --- a/Modules/clinic/_asynciomodule.c.h +++ b/Modules/clinic/_asynciomodule.c.h @@ -297,7 +297,42 @@ PyDoc_STRVAR(_asyncio_Future_remove_done_callback__doc__, "Returns the number of callbacks removed."); #define _ASYNCIO_FUTURE_REMOVE_DONE_CALLBACK_METHODDEF \ - {"remove_done_callback", (PyCFunction)_asyncio_Future_remove_done_callback, METH_O, _asyncio_Future_remove_done_callback__doc__}, + {"remove_done_callback", _PyCFunction_CAST(_asyncio_Future_remove_done_callback), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _asyncio_Future_remove_done_callback__doc__}, + +static PyObject * +_asyncio_Future_remove_done_callback_impl(FutureObj *self, PyTypeObject *cls, + PyObject *fn); + +static PyObject * +_asyncio_Future_remove_done_callback(FutureObj *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) +{ + PyObject *return_value = NULL; + #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) + # define KWTUPLE (PyObject *)&_Py_SINGLETON(tuple_empty) + #else + # define KWTUPLE NULL + #endif + + static const char * const _keywords[] = {"", NULL}; + static _PyArg_Parser _parser = { + .keywords = _keywords, + .fname = "remove_done_callback", + .kwtuple = KWTUPLE, + }; + #undef KWTUPLE + PyObject *argsbuf[1]; + PyObject *fn; + + args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf); + if (!args) { + goto exit; + } + fn = args[0]; + return_value = _asyncio_Future_remove_done_callback_impl(self, cls, fn); + +exit: + return return_value; +} PyDoc_STRVAR(_asyncio_Future_cancel__doc__, "cancel($self, /, msg=None)\n" @@ -410,15 +445,19 @@ PyDoc_STRVAR(_asyncio_Future_get_loop__doc__, "Return the event loop the Future is bound to."); #define _ASYNCIO_FUTURE_GET_LOOP_METHODDEF \ - {"get_loop", (PyCFunction)_asyncio_Future_get_loop, METH_NOARGS, _asyncio_Future_get_loop__doc__}, + {"get_loop", _PyCFunction_CAST(_asyncio_Future_get_loop), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _asyncio_Future_get_loop__doc__}, static PyObject * -_asyncio_Future_get_loop_impl(FutureObj *self); +_asyncio_Future_get_loop_impl(FutureObj *self, PyTypeObject *cls); static PyObject * -_asyncio_Future_get_loop(FutureObj *self, PyObject *Py_UNUSED(ignored)) +_asyncio_Future_get_loop(FutureObj *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) { - return _asyncio_Future_get_loop_impl(self); + if (nargs) { + PyErr_SetString(PyExc_TypeError, "get_loop() takes no arguments"); + return NULL; + } + return _asyncio_Future_get_loop_impl(self, cls); } PyDoc_STRVAR(_asyncio_Future__make_cancelled_error__doc__, @@ -1265,4 +1304,4 @@ _asyncio__leave_task(PyObject *module, PyObject *const *args, Py_ssize_t nargs, exit: return return_value; } -/*[clinic end generated code: output=c3b3681942e8b672 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=550bc6603df89ed9 input=a9049054013a1b77]*/ From 54d4900fa2176d15d22310d2d51afc5ae163c866 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Wed, 5 Oct 2022 09:56:35 +0200 Subject: [PATCH 16/26] Convert to module state --- Modules/_asynciomodule.c | 71 ++++++++++++++++++++++++++++++++-------- 1 file changed, 58 insertions(+), 13 deletions(-) diff --git a/Modules/_asynciomodule.c b/Modules/_asynciomodule.c index 9693324e4c90da..c39dbe15652bf5 100644 --- a/Modules/_asynciomodule.c +++ b/Modules/_asynciomodule.c @@ -60,30 +60,27 @@ typedef struct { int module_initialized; - PyObject *cached_running_holder; + PyObject *cached_running_holder; // Borrowed ref. volatile uint64_t cached_running_holder_tsid; /* Counter for autogenerated Task names */ uint64_t task_name_counter; } asyncio_state; -static asyncio_state global_state; - static inline asyncio_state * -get_asyncio_state(PyObject *Py_UNUSED(mod)) +get_asyncio_state(PyObject *mod) { - return &global_state; + asyncio_state *state = PyModule_GetState(mod); + assert(state != NULL); + return state; } static inline asyncio_state * get_asyncio_state_by_cls(PyTypeObject *cls) { - /* asyncio_state *state = (asyncio_state *)PyType_GetModuleState(cls); assert(state != NULL); return state; - */ - return &global_state; } static struct PyModuleDef _asynciomodule; @@ -91,13 +88,10 @@ static struct PyModuleDef _asynciomodule; static inline asyncio_state * get_asyncio_state_by_def(PyObject *self) { - /* PyTypeObject *tp = Py_TYPE(self); PyObject *mod = PyType_GetModuleByDef(tp, &_asynciomodule); assert(mod != NULL); return get_asyncio_state(mod); - */ - return get_asyncio_state(NULL); } typedef enum { @@ -3511,11 +3505,55 @@ module_free_freelists(void) fi_freelist = NULL; } +static int +module_traverse(PyObject *mod, visitproc visit, void *arg) +{ + asyncio_state *state = get_asyncio_state(mod); + + Py_VISIT(state->FutureIterType); + Py_VISIT(state->TaskStepMethWrapper_Type); + Py_VISIT(state->FutureType); + Py_VISIT(state->TaskType); + Py_VISIT(state->PyRunningLoopHolder_Type); + + Py_VISIT(state->asyncio_mod); + Py_VISIT(state->traceback_extract_stack); + Py_VISIT(state->asyncio_future_repr_func); + Py_VISIT(state->asyncio_get_event_loop_policy); + Py_VISIT(state->asyncio_iscoroutine_func); + Py_VISIT(state->asyncio_task_get_stack_func); + Py_VISIT(state->asyncio_task_print_stack_func); + Py_VISIT(state->asyncio_task_repr_func); + Py_VISIT(state->asyncio_InvalidStateError); + Py_VISIT(state->asyncio_CancelledError); + + Py_VISIT(state->all_tasks); + Py_VISIT(state->current_tasks); + Py_VISIT(state->iscoroutine_typecache); + + Py_VISIT(state->context_kwname); + + // Visit freelist. + PyObject *next = (PyObject*) fi_freelist; + while (next != NULL) { + PyObject *current = next; + Py_VISIT(current); + next = (PyObject*) ((futureiterobject*) current)->future; + } + return 0; +} static void module_free(void *m) { asyncio_state *state = get_asyncio_state(m); + + Py_CLEAR(state->FutureIterType); + Py_CLEAR(state->TaskStepMethWrapper_Type); + Py_CLEAR(state->FutureType); + Py_CLEAR(state->TaskType); + Py_CLEAR(state->PyRunningLoopHolder_Type); + Py_CLEAR(state->asyncio_mod); Py_CLEAR(state->traceback_extract_stack); Py_CLEAR(state->asyncio_future_repr_func); @@ -3538,6 +3576,13 @@ module_free(void *m) state->module_initialized = 0; } +static int +module_clear(PyObject *mod) +{ + module_free((PyObject *)mod); + return 0; +} + static int module_init(asyncio_state *state) { @@ -3645,8 +3690,8 @@ static struct PyModuleDef _asynciomodule = { .m_size = sizeof(asyncio_state), .m_methods = asyncio_methods, .m_slots = NULL, - .m_traverse = NULL, - .m_clear = NULL, + .m_traverse = module_traverse, + .m_clear = module_clear, .m_free = (freefunc)module_free, }; From 7772c82a3006f944770e34df79d7e1f11018574c Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Fri, 14 Oct 2022 12:51:18 +0200 Subject: [PATCH 17/26] WIP --- Modules/_asynciomodule.c | 103 ++++++++++++++++++--------------------- 1 file changed, 48 insertions(+), 55 deletions(-) diff --git a/Modules/_asynciomodule.c b/Modules/_asynciomodule.c index c39dbe15652bf5..1555698476dc13 100644 --- a/Modules/_asynciomodule.c +++ b/Modules/_asynciomodule.c @@ -3543,10 +3543,10 @@ module_traverse(PyObject *mod, visitproc visit, void *arg) return 0; } -static void -module_free(void *m) +static int +module_clear(PyObject *mod) { - asyncio_state *state = get_asyncio_state(m); + asyncio_state *state = get_asyncio_state(mod); Py_CLEAR(state->FutureIterType); Py_CLEAR(state->TaskStepMethWrapper_Type); @@ -3574,13 +3574,13 @@ module_free(void *m) module_free_freelists(); state->module_initialized = 0; + return 0; } -static int -module_clear(PyObject *mod) +static void +module_free(void *mod) { - module_free((PyObject *)mod); - return 0; + (void)module_clear((PyObject *)mod); } static int @@ -3683,30 +3683,12 @@ static PyMethodDef asyncio_methods[] = { {NULL, NULL} }; -static struct PyModuleDef _asynciomodule = { - .m_base = PyModuleDef_HEAD_INIT, - .m_name = "_asyncio", - .m_doc = module_doc, - .m_size = sizeof(asyncio_state), - .m_methods = asyncio_methods, - .m_slots = NULL, - .m_traverse = module_traverse, - .m_clear = module_clear, - .m_free = (freefunc)module_free, -}; - - -PyMODINIT_FUNC -PyInit__asyncio(void) +static int +module_exec(PyObject *mod) { - PyObject *m = PyModule_Create(&_asynciomodule); - if (m == NULL) { - return NULL; - } - asyncio_state *state = get_asyncio_state(m); + asyncio_state *state = get_asyncio_state(mod); if (module_init(state) < 0) { - Py_DECREF(m); - return NULL; + return -1; } #define CREATE_TYPE(m, tp, spec, base) \ @@ -3714,45 +3696,56 @@ PyInit__asyncio(void) tp = (PyTypeObject *)PyType_FromMetaclass(NULL, m, spec, \ (PyObject *)base); \ if (tp == NULL) { \ - goto error; \ + return -1; \ } \ } while (0) - CREATE_TYPE(m, state->TaskStepMethWrapper_Type, &TaskStepMethWrapper_spec, NULL); - CREATE_TYPE(m, state->PyRunningLoopHolder_Type, &PyRunningLoopHolder_spec, NULL); - CREATE_TYPE(m, state->FutureIterType, &FutureIter_spec, NULL); - CREATE_TYPE(m, state->FutureType, &Future_spec, NULL); - CREATE_TYPE(m, state->TaskType, &Task_spec, state->FutureType); + CREATE_TYPE(mod, state->TaskStepMethWrapper_Type, &TaskStepMethWrapper_spec, NULL); + CREATE_TYPE(mod, state->PyRunningLoopHolder_Type, &PyRunningLoopHolder_spec, NULL); + CREATE_TYPE(mod, state->FutureIterType, &FutureIter_spec, NULL); + CREATE_TYPE(mod, state->FutureType, &Future_spec, NULL); + CREATE_TYPE(mod, state->TaskType, &Task_spec, state->FutureType); #undef CREATE_TYPE - if (PyModule_AddType(m, state->FutureType) < 0) { - Py_DECREF(m); - return NULL; + if (PyModule_AddType(mod, state->FutureType) < 0) { + return -1; } - if (PyModule_AddType(m, state->TaskType) < 0) { - Py_DECREF(m); - return NULL; + if (PyModule_AddType(mod, state->TaskType) < 0) { + return -1; } - Py_INCREF(state->all_tasks); - if (PyModule_AddObject(m, "_all_tasks", state->all_tasks) < 0) { - Py_DECREF(state->all_tasks); - Py_DECREF(m); - return NULL; + if (PyModule_AddObjectRef(mod, "_all_tasks", state->all_tasks) < 0) { + return -1; } - Py_INCREF(state->current_tasks); - if (PyModule_AddObject(m, "_current_tasks", state->current_tasks) < 0) { - Py_DECREF(state->current_tasks); - Py_DECREF(m); - return NULL; + if (PyModule_AddObjectRef(mod, "_current_tasks", state->current_tasks) < 0) { + return -1; } - return m; + return 0; +} -error: - Py_DECREF(m); - return NULL; +static struct PyModuleDef_Slot module_slots[] = { + {Py_mod_exec, module_exec}, + {0, NULL}, +}; + +static struct PyModuleDef _asynciomodule = { + .m_base = PyModuleDef_HEAD_INIT, + .m_name = "_asyncio", + .m_doc = module_doc, + .m_size = sizeof(asyncio_state), + .m_methods = asyncio_methods, + .m_slots = module_slots, + .m_traverse = module_traverse, + .m_clear = module_clear, + .m_free = (freefunc)module_free, +}; + +PyMODINIT_FUNC +PyInit__asyncio(void) +{ + return PyModuleDef_Init(&_asynciomodule); } From a24f12c132b7348fc0898a76a7e41577a1ce76c6 Mon Sep 17 00:00:00 2001 From: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Date: Sat, 5 Nov 2022 07:33:07 +0000 Subject: [PATCH 18/26] wip --- Modules/_asynciomodule.c | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/Modules/_asynciomodule.c b/Modules/_asynciomodule.c index 1555698476dc13..2a12a319f93208 100644 --- a/Modules/_asynciomodule.c +++ b/Modules/_asynciomodule.c @@ -1426,13 +1426,7 @@ FutureObj_set_cancel_message(FutureObj *fut, PyObject *msg, static PyObject * FutureObj_get_state(FutureObj *fut, void *Py_UNUSED(ignored)) { -<<<<<<< HEAD -======= - _Py_IDENTIFIER(PENDING); - _Py_IDENTIFIER(CANCELLED); - _Py_IDENTIFIER(FINISHED); asyncio_state *state = get_asyncio_state_by_def((PyObject *)fut); ->>>>>>> 1807e8688e (Prepare for module state, batch 4) PyObject *ret = NULL; ENSURE_FUTURE_ALIVE(state, fut) @@ -2015,7 +2009,6 @@ static PyMethodDef TaskWakeupDef = { static int register_task(asyncio_state *state, PyObject *task) { - asyncio_state *state = get_asyncio_state(NULL); PyObject *res = PyObject_CallMethodOneArg(state->all_tasks, &_Py_ID(add), task); if (res == NULL) { @@ -2029,16 +2022,8 @@ register_task(asyncio_state *state, PyObject *task) static int unregister_task(asyncio_state *state, PyObject *task) { -<<<<<<< HEAD - asyncio_state *state = get_asyncio_state(NULL); PyObject *res = PyObject_CallMethodOneArg(state->all_tasks, &_Py_ID(discard), task); -======= - _Py_IDENTIFIER(discard); - - PyObject *res = _PyObject_CallMethodIdOneArg(state->all_tasks, - &PyId_discard, task); ->>>>>>> 1807e8688e (Prepare for module state, batch 4) if (res == NULL) { return -1; } @@ -2190,7 +2175,18 @@ TaskObj_traverse(TaskObj *task, visitproc visit, void *arg) Py_VISIT(task->task_coro); Py_VISIT(task->task_name); Py_VISIT(task->task_fut_waiter); - (void)FutureObj_traverse((FutureObj*) task, visit, arg); + FutureObj *fut = (FutureObj *)task; + Py_VISIT(fut->fut_loop); + Py_VISIT(fut->fut_callback0); + Py_VISIT(fut->fut_context0); + Py_VISIT(fut->fut_callbacks); + Py_VISIT(fut->fut_result); + Py_VISIT(fut->fut_exception); + Py_VISIT(fut->fut_exception_tb); + Py_VISIT(fut->fut_source_tb); + Py_VISIT(fut->fut_cancel_msg); + Py_VISIT(fut->fut_cancelled_exc); + Py_VISIT(fut->dict); return 0; } From bf0cb43a5e1dd876208cb81d8f6a7a07751423d4 Mon Sep 17 00:00:00 2001 From: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Date: Sat, 5 Nov 2022 08:06:13 +0000 Subject: [PATCH 19/26] make it work! --- Modules/_asynciomodule.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Modules/_asynciomodule.c b/Modules/_asynciomodule.c index 2a12a319f93208..5d4313ccff3df9 100644 --- a/Modules/_asynciomodule.c +++ b/Modules/_asynciomodule.c @@ -3683,9 +3683,6 @@ static int module_exec(PyObject *mod) { asyncio_state *state = get_asyncio_state(mod); - if (module_init(state) < 0) { - return -1; - } #define CREATE_TYPE(m, tp, spec, base) \ do { \ @@ -3711,6 +3708,10 @@ module_exec(PyObject *mod) if (PyModule_AddType(mod, state->TaskType) < 0) { return -1; } + // Must be done after types are added to avoid a circular dependency + if (module_init(state) < 0) { + return -1; + } if (PyModule_AddObjectRef(mod, "_all_tasks", state->all_tasks) < 0) { return -1; @@ -3720,6 +3721,7 @@ module_exec(PyObject *mod) return -1; } + return 0; } From 9bd1a5689461471f3431bbcefd3a2336211622bd Mon Sep 17 00:00:00 2001 From: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Date: Sat, 5 Nov 2022 08:52:45 +0000 Subject: [PATCH 20/26] skip test --- Lib/test/test_coroutines.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Lib/test/test_coroutines.py b/Lib/test/test_coroutines.py index f91c9cc47741b5..eeeb8467781111 100644 --- a/Lib/test/test_coroutines.py +++ b/Lib/test/test_coroutines.py @@ -2415,6 +2415,7 @@ async def corofn(): class UnawaitedWarningDuringShutdownTest(unittest.TestCase): # https://bugs.python.org/issue32591#msg310726 + @unittest.skip def test_unawaited_warning_during_shutdown(self): code = ("import asyncio\n" "async def f(): pass\n" From d539152f637ba7d5ca3853f9494ac35f3e0f5c18 Mon Sep 17 00:00:00 2001 From: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Date: Sat, 5 Nov 2022 10:03:32 +0000 Subject: [PATCH 21/26] buildbottest --- .github/workflows/build.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index f0544c0962e123..89446c5c82678a 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -225,7 +225,8 @@ jobs: run: sudo mount $CPYTHON_RO_SRCDIR -oremount,rw - name: Tests working-directory: ${{ env.CPYTHON_BUILDDIR }} - run: xvfb-run make buildbottest TESTOPTS="-j4 -uall,-cpu" + run: ./python -m test -R 3:3 test_asyncio + # run: xvfb-run make buildbottest TESTOPTS="-j4 -uall,-cpu" build_ubuntu_ssltests: name: 'Ubuntu SSL tests with OpenSSL' From 83d1b5434fec3e4d2c6196d58b6ec85bd9983c21 Mon Sep 17 00:00:00 2001 From: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Date: Sat, 5 Nov 2022 10:15:37 +0000 Subject: [PATCH 22/26] Revert "buildbottest" This reverts commit d539152f637ba7d5ca3853f9494ac35f3e0f5c18. --- .github/workflows/build.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 89446c5c82678a..f0544c0962e123 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -225,8 +225,7 @@ jobs: run: sudo mount $CPYTHON_RO_SRCDIR -oremount,rw - name: Tests working-directory: ${{ env.CPYTHON_BUILDDIR }} - run: ./python -m test -R 3:3 test_asyncio - # run: xvfb-run make buildbottest TESTOPTS="-j4 -uall,-cpu" + run: xvfb-run make buildbottest TESTOPTS="-j4 -uall,-cpu" build_ubuntu_ssltests: name: 'Ubuntu SSL tests with OpenSSL' From ac753f3824aa9fdfc8e3b677c39eee44803f3785 Mon Sep 17 00:00:00 2001 From: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Date: Sat, 5 Nov 2022 10:45:50 +0000 Subject: [PATCH 23/26] fix finalization --- Lib/test/test_coroutines.py | 1 - Modules/_asynciomodule.c | 24 ++++++------------------ 2 files changed, 6 insertions(+), 19 deletions(-) diff --git a/Lib/test/test_coroutines.py b/Lib/test/test_coroutines.py index eeeb8467781111..f91c9cc47741b5 100644 --- a/Lib/test/test_coroutines.py +++ b/Lib/test/test_coroutines.py @@ -2415,7 +2415,6 @@ async def corofn(): class UnawaitedWarningDuringShutdownTest(unittest.TestCase): # https://bugs.python.org/issue32591#msg310726 - @unittest.skip def test_unawaited_warning_during_shutdown(self): code = ("import asyncio\n" "async def f(): pass\n" diff --git a/Modules/_asynciomodule.c b/Modules/_asynciomodule.c index 5d4313ccff3df9..abe5352dcca95c 100644 --- a/Modules/_asynciomodule.c +++ b/Modules/_asynciomodule.c @@ -1607,17 +1607,11 @@ static PyType_Spec Future_spec = { static void FutureObj_dealloc(PyObject *self) { - asyncio_state *state = get_asyncio_state_by_def(self); FutureObj *fut = (FutureObj *)self; - if (Future_CheckExact(state, fut)) { - /* When fut is subclass of Future, finalizer is called from - * subtype_dealloc. - */ - if (PyObject_CallFinalizerFromDealloc(self) < 0) { - // resurrected. - return; - } + if (PyObject_CallFinalizerFromDealloc(self) < 0) { + // resurrected. + return; } PyTypeObject *tp = Py_TYPE(fut); @@ -2686,15 +2680,9 @@ TaskObj_dealloc(PyObject *self) { TaskObj *task = (TaskObj *)self; - asyncio_state *state = get_asyncio_state_by_def(self); - if (Task_CheckExact(state, self)) { - /* When fut is subclass of Task, finalizer is called from - * subtype_dealloc. - */ - if (PyObject_CallFinalizerFromDealloc(self) < 0) { - // resurrected. - return; - } + if (PyObject_CallFinalizerFromDealloc(self) < 0) { + // resurrected. + return; } PyTypeObject *tp = Py_TYPE(task); From 452467f56314f73dd5b209609b4fcffed6369fa0 Mon Sep 17 00:00:00 2001 From: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Date: Sat, 5 Nov 2022 16:55:13 +0000 Subject: [PATCH 24/26] fix warnings --- Modules/_asynciomodule.c | 1 + 1 file changed, 1 insertion(+) diff --git a/Modules/_asynciomodule.c b/Modules/_asynciomodule.c index abe5352dcca95c..d7272448866625 100644 --- a/Modules/_asynciomodule.c +++ b/Modules/_asynciomodule.c @@ -454,6 +454,7 @@ future_ensure_alive(FutureObj *fut) #define ENSURE_FUTURE_ALIVE(state, fut) \ do { \ assert(Future_Check(state, fut) || Task_Check(state, fut)); \ + (void)state; \ if (future_ensure_alive((FutureObj*)fut)) { \ return NULL; \ } \ From 0f9f2768545d5c0cc2046944be0946618c92e2f4 Mon Sep 17 00:00:00 2001 From: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Date: Sun, 6 Nov 2022 14:17:54 +0000 Subject: [PATCH 25/26] use faster _PyModule_GetState --- Modules/_asynciomodule.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Modules/_asynciomodule.c b/Modules/_asynciomodule.c index d7272448866625..563c1af332937b 100644 --- a/Modules/_asynciomodule.c +++ b/Modules/_asynciomodule.c @@ -6,6 +6,7 @@ #include "pycore_pyerrors.h" // _PyErr_ClearExcState() #include "pycore_pystate.h" // _PyThreadState_GET() #include "pycore_runtime_init.h" // _Py_ID() +#include "pycore_moduleobject.h" // _PyModule_GetState() #include "structmember.h" // PyMemberDef #include // offsetof() @@ -70,7 +71,7 @@ typedef struct { static inline asyncio_state * get_asyncio_state(PyObject *mod) { - asyncio_state *state = PyModule_GetState(mod); + asyncio_state *state = _PyModule_GetState(mod); assert(state != NULL); return state; } From c7c13e5ef46d7c8da4352cf766cf6dfc18f78d23 Mon Sep 17 00:00:00 2001 From: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Date: Mon, 7 Nov 2022 10:46:45 +0000 Subject: [PATCH 26/26] code review --- Modules/_asynciomodule.c | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/Modules/_asynciomodule.c b/Modules/_asynciomodule.c index 563c1af332937b..729deb563016f1 100644 --- a/Modules/_asynciomodule.c +++ b/Modules/_asynciomodule.c @@ -59,8 +59,6 @@ typedef struct { /* Imports from traceback. */ PyObject *traceback_extract_stack; - int module_initialized; - PyObject *cached_running_holder; // Borrowed ref. volatile uint64_t cached_running_holder_tsid; @@ -1805,7 +1803,7 @@ FutureIter_clear(futureiterobject *it) static PyObject * FutureIter_close(futureiterobject *self, PyObject *arg) { - FutureIter_clear(self); + (void)FutureIter_clear(self); Py_RETURN_NONE; } @@ -3428,7 +3426,7 @@ PyRunningLoopHolder_clear(PyRunningLoopHolder *rl) static int PyRunningLoopHolder_traverse(PyRunningLoopHolder *rl, visitproc visit, - void *arg) + void *arg) { Py_VISIT(Py_TYPE(rl)); Py_VISIT(rl->rl_loop); @@ -3559,7 +3557,6 @@ module_clear(PyObject *mod) module_free_freelists(); - state->module_initialized = 0; return 0; } @@ -3573,9 +3570,6 @@ static int module_init(asyncio_state *state) { PyObject *module = NULL; - if (state->module_initialized) { - return 0; - } state->asyncio_mod = PyImport_ImportModule("asyncio"); if (state->asyncio_mod == NULL) { @@ -3641,13 +3635,11 @@ module_init(asyncio_state *state) goto fail; } - state->module_initialized = 1; Py_DECREF(module); return 0; fail: Py_CLEAR(module); - module_free(NULL); return -1; #undef WITH_MOD