From fb35d81cb8c406a9552b7f841728246dae37a2cd Mon Sep 17 00:00:00 2001 From: Dong-hee Na Date: Thu, 19 Mar 2020 23:34:33 +0900 Subject: [PATCH 1/3] bpo-1635741: Port _collections module to multiphase initialization. --- ...2020-03-19-23-34-22.bpo-1635741.ayunLM.rst | 1 + Modules/_collectionsmodule.c | 81 +++++++++---------- 2 files changed, 40 insertions(+), 42 deletions(-) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2020-03-19-23-34-22.bpo-1635741.ayunLM.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2020-03-19-23-34-22.bpo-1635741.ayunLM.rst b/Misc/NEWS.d/next/Core and Builtins/2020-03-19-23-34-22.bpo-1635741.ayunLM.rst new file mode 100644 index 00000000000000..458df4a88c1007 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2020-03-19-23-34-22.bpo-1635741.ayunLM.rst @@ -0,0 +1 @@ +Port _collections module to multiphase initialization (:pep:`489`). diff --git a/Modules/_collectionsmodule.c b/Modules/_collectionsmodule.c index fd0e4edcddfdbe..5140a789a68b4e 100644 --- a/Modules/_collectionsmodule.c +++ b/Modules/_collectionsmodule.c @@ -2546,24 +2546,56 @@ static PyTypeObject tuplegetter_type = { /* module level code ********************************************************/ -PyDoc_STRVAR(module_doc, +PyDoc_STRVAR(collections_doc, "High performance data structures.\n\ - deque: ordered collection accessible from endpoints only\n\ - defaultdict: dict subclass with a default value factory\n\ "); -static struct PyMethodDef module_functions[] = { +static struct PyMethodDef collections_methods[] = { _COLLECTIONS__COUNT_ELEMENTS_METHODDEF {NULL, NULL} /* sentinel */ }; +static int +collections_exec(PyObject *m) { + PyTypeObject *typelist[] = { + &deque_type, + &defdict_type, + &PyODict_Type, + &dequeiter_type, + &dequereviter_type, + &tuplegetter_type, + }; + + for (int i = 0; typelist[i] != NULL; i++) { + PyTypeObject *type = typelist[i]; + if (PyType_Ready(type) < 0) { + return -1; + } + const char *name = _PyType_Name(type); + Py_INCREF(type); + if (PyModule_AddObject(m, name, (PyObject *)type) < 0) { + Py_DECREF(type); + return -1; + } + } + + return 0; +} + +static struct PyModuleDef_Slot collections_slots[] = { + {Py_mod_exec, collections_exec}, + {0, NULL} +}; + static struct PyModuleDef _collectionsmodule = { PyModuleDef_HEAD_INIT, "_collections", - module_doc, - -1, - module_functions, - NULL, + collections_doc, + 0, + collections_methods, + collections_slots, NULL, NULL, NULL @@ -2572,40 +2604,5 @@ static struct PyModuleDef _collectionsmodule = { PyMODINIT_FUNC PyInit__collections(void) { - PyObject *m; - - m = PyModule_Create(&_collectionsmodule); - if (m == NULL) - return NULL; - - if (PyType_Ready(&deque_type) < 0) - return NULL; - Py_INCREF(&deque_type); - PyModule_AddObject(m, "deque", (PyObject *)&deque_type); - - defdict_type.tp_base = &PyDict_Type; - if (PyType_Ready(&defdict_type) < 0) - return NULL; - Py_INCREF(&defdict_type); - PyModule_AddObject(m, "defaultdict", (PyObject *)&defdict_type); - - Py_INCREF(&PyODict_Type); - PyModule_AddObject(m, "OrderedDict", (PyObject *)&PyODict_Type); - - if (PyType_Ready(&dequeiter_type) < 0) - return NULL; - Py_INCREF(&dequeiter_type); - PyModule_AddObject(m, "_deque_iterator", (PyObject *)&dequeiter_type); - - if (PyType_Ready(&dequereviter_type) < 0) - return NULL; - Py_INCREF(&dequereviter_type); - PyModule_AddObject(m, "_deque_reverse_iterator", (PyObject *)&dequereviter_type); - - if (PyType_Ready(&tuplegetter_type) < 0) - return NULL; - Py_INCREF(&tuplegetter_type); - PyModule_AddObject(m, "_tuplegetter", (PyObject *)&tuplegetter_type); - - return m; + return PyModuleDef_Init(&_collectionsmodule); } From ac5d78be7acca2ef3163ae371eb54878b8524ba4 Mon Sep 17 00:00:00 2001 From: Dong-hee Na Date: Fri, 20 Mar 2020 00:19:22 +0900 Subject: [PATCH 2/3] bpo-1635741: fix --- Modules/_collectionsmodule.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Modules/_collectionsmodule.c b/Modules/_collectionsmodule.c index 5140a789a68b4e..ee9fe1fab99853 100644 --- a/Modules/_collectionsmodule.c +++ b/Modules/_collectionsmodule.c @@ -2566,8 +2566,11 @@ collections_exec(PyObject *m) { &dequeiter_type, &dequereviter_type, &tuplegetter_type, + NULL, }; + defdict_type.tp_base = &PyDict_Type; + for (int i = 0; typelist[i] != NULL; i++) { PyTypeObject *type = typelist[i]; if (PyType_Ready(type) < 0) { From 08612b0ab226a03148968e17ebdcd712b26eee8d Mon Sep 17 00:00:00 2001 From: Dong-hee Na Date: Fri, 20 Mar 2020 00:51:58 +0900 Subject: [PATCH 3/3] bpo-1635741: Apply Victor's review --- Modules/_collectionsmodule.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Modules/_collectionsmodule.c b/Modules/_collectionsmodule.c index ee9fe1fab99853..a595e5b5ea341c 100644 --- a/Modules/_collectionsmodule.c +++ b/Modules/_collectionsmodule.c @@ -2558,7 +2558,7 @@ static struct PyMethodDef collections_methods[] = { }; static int -collections_exec(PyObject *m) { +collections_exec(PyObject *module) { PyTypeObject *typelist[] = { &deque_type, &defdict_type, @@ -2578,7 +2578,7 @@ collections_exec(PyObject *m) { } const char *name = _PyType_Name(type); Py_INCREF(type); - if (PyModule_AddObject(m, name, (PyObject *)type) < 0) { + if (PyModule_AddObject(module, name, (PyObject *)type) < 0) { Py_DECREF(type); return -1; }