diff --git a/Misc/NEWS.d/next/Core and Builtins/2020-01-16-16-30-18.bpo-1635741.bLqfQb.rst b/Misc/NEWS.d/next/Core and Builtins/2020-01-16-16-30-18.bpo-1635741.bLqfQb.rst new file mode 100644 index 00000000000000..c994066546f8ee --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2020-01-16-16-30-18.bpo-1635741.bLqfQb.rst @@ -0,0 +1 @@ +Port _asyncio extension module to multiphase initialization (:pep:`489`). \ No newline at end of file diff --git a/Modules/_asynciomodule.c b/Modules/_asynciomodule.c index 70da40a8a3b863..5d1c5a07ea5288 100644 --- a/Modules/_asynciomodule.c +++ b/Modules/_asynciomodule.c @@ -3372,76 +3372,80 @@ static PyMethodDef asyncio_methods[] = { {NULL, NULL} }; -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 */ -}; - - -PyMODINIT_FUNC -PyInit__asyncio(void) +static int +_asyncio_exec(PyObject *module) { - if (module_init() < 0) { - return NULL; - } if (PyType_Ready(&FutureType) < 0) { - return NULL; + return -1; } if (PyType_Ready(&FutureIterType) < 0) { - return NULL; + return -1; } if (PyType_Ready(&TaskStepMethWrapper_Type) < 0) { - return NULL; + return -1; } if (PyType_Ready(&TaskWakeupMethWrapper_Type) < 0) { - return NULL; + return -1; } if (PyType_Ready(&TaskType) < 0) { - return NULL; + return -1; } if (PyType_Ready(&PyRunningLoopHolder_Type) < 0) { - return NULL; - } - - PyObject *m = PyModule_Create(&_asynciomodule); - if (m == NULL) { - return NULL; + return -1; } Py_INCREF(&FutureType); - if (PyModule_AddObject(m, "Future", (PyObject *)&FutureType) < 0) { + if (PyModule_AddObject(module, "Future", (PyObject *)&FutureType) < 0) { Py_DECREF(&FutureType); - Py_DECREF(m); - return NULL; + Py_DECREF(module); + return -1; } Py_INCREF(&TaskType); - if (PyModule_AddObject(m, "Task", (PyObject *)&TaskType) < 0) { + if (PyModule_AddObject(module, "Task", (PyObject *)&TaskType) < 0) { Py_DECREF(&TaskType); - Py_DECREF(m); - return NULL; + Py_DECREF(module); + return -1; } Py_INCREF(all_tasks); - if (PyModule_AddObject(m, "_all_tasks", all_tasks) < 0) { + if (PyModule_AddObject(module, "_all_tasks", all_tasks) < 0) { Py_DECREF(all_tasks); - Py_DECREF(m); - return NULL; + Py_DECREF(module); + return -1; } Py_INCREF(current_tasks); - if (PyModule_AddObject(m, "_current_tasks", current_tasks) < 0) { + if (PyModule_AddObject(module, "_current_tasks", current_tasks) < 0) { Py_DECREF(current_tasks); - Py_DECREF(m); - return NULL; + Py_DECREF(module); + return -1; } + return 0; +} + +static PyModuleDef_Slot _asyncio_slots[] = { + {Py_mod_exec, _asyncio_exec}, + {0, NULL} +}; + +static struct PyModuleDef _asynciomodule = { + PyModuleDef_HEAD_INIT, /* m_base */ + "_asyncio", /* m_name */ + module_doc, /* m_doc */ + 0, /* m_size */ + asyncio_methods, /* m_methods */ + _asyncio_slots, /* m_slots */ + NULL, /* m_traverse */ + NULL, /* m_clear */ + (freefunc)module_free /* m_free */ +}; - return m; +PyMODINIT_FUNC +PyInit__asyncio(void) +{ + if (module_init() < 0) { + return NULL; + } + return PyModuleDef_Init(&_asynciomodule); }