From 6980d788751983a1a5104babd9a53617e21a25f9 Mon Sep 17 00:00:00 2001 From: Dong-hee Na Date: Wed, 18 Mar 2020 00:17:29 +0900 Subject: [PATCH 1/4] bpo-1635741: Port itertools module to multiphase initialization. --- ...2020-03-18-00-17-26.bpo-1635741.7AtdhP.rst | 1 + Modules/itertoolsmodule.c | 72 ++++++++++--------- 2 files changed, 40 insertions(+), 33 deletions(-) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2020-03-18-00-17-26.bpo-1635741.7AtdhP.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2020-03-18-00-17-26.bpo-1635741.7AtdhP.rst b/Misc/NEWS.d/next/Core and Builtins/2020-03-18-00-17-26.bpo-1635741.7AtdhP.rst new file mode 100644 index 00000000000000..23472030757696 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2020-03-18-00-17-26.bpo-1635741.7AtdhP.rst @@ -0,0 +1 @@ +Port itertools module to multiphase initialization (:pep:`489`). diff --git a/Modules/itertoolsmodule.c b/Modules/itertoolsmodule.c index 9505fd454b42e6..c22c127126725d 100644 --- a/Modules/itertoolsmodule.c +++ b/Modules/itertoolsmodule.c @@ -4701,31 +4701,9 @@ combinations(p, r)\n\ combinations_with_replacement(p, r)\n\ "); - -static PyMethodDef module_methods[] = { - ITERTOOLS_TEE_METHODDEF - {NULL, NULL} /* sentinel */ -}; - - -static struct PyModuleDef itertoolsmodule = { - PyModuleDef_HEAD_INIT, - "itertools", - module_doc, - -1, - module_methods, - NULL, - NULL, - NULL, - NULL -}; - -PyMODINIT_FUNC -PyInit_itertools(void) +static int +itertoolsmodule_exec(PyObject *m) { - int i; - PyObject *m; - const char *name; PyTypeObject *typelist[] = { &accumulate_type, &combinations_type, @@ -4751,19 +4729,47 @@ PyInit_itertools(void) }; Py_SET_TYPE(&teedataobject_type, &PyType_Type); - m = PyModule_Create(&itertoolsmodule); - if (m == NULL) { - return NULL; - } - for (i=0 ; typelist[i] != NULL ; i++) { + for (int i=0 ; typelist[i] != NULL ; i++) { if (PyType_Ready(typelist[i]) < 0) { - return NULL; + return -1; } - name = _PyType_Name(typelist[i]); + const char *name = _PyType_Name(typelist[i]); Py_INCREF(typelist[i]); - PyModule_AddObject(m, name, (PyObject *)typelist[i]); + if (PyModule_AddObject(m, name, (PyObject *)typelist[i]) < 0) { + Py_DECREF(typelist[i]); + return -1; + } } - return m; + return 0; +} + +static struct PyModuleDef_Slot itertoolsmodule_slots[] = { + {Py_mod_exec, itertoolsmodule_exec}, + {0, NULL} +}; + +static PyMethodDef module_methods[] = { + ITERTOOLS_TEE_METHODDEF + {NULL, NULL} /* sentinel */ +}; + + +static struct PyModuleDef itertoolsmodule = { + PyModuleDef_HEAD_INIT, + "itertools", + module_doc, + 0, + module_methods, + itertoolsmodule_slots, + NULL, + NULL, + NULL +}; + +PyMODINIT_FUNC +PyInit_itertools(void) +{ + return PyModuleDef_Init(&itertoolsmodule); } From ab0ef4e2e3298dd01270e3cf5446d7ce15e03c51 Mon Sep 17 00:00:00 2001 From: Dong-hee Na Date: Wed, 18 Mar 2020 01:59:43 +0900 Subject: [PATCH 2/4] bpo-1635741: Apply Victor's review --- Modules/itertoolsmodule.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/Modules/itertoolsmodule.c b/Modules/itertoolsmodule.c index c22c127126725d..9d89c30ef79aff 100644 --- a/Modules/itertoolsmodule.c +++ b/Modules/itertoolsmodule.c @@ -4730,14 +4730,15 @@ itertoolsmodule_exec(PyObject *m) Py_SET_TYPE(&teedataobject_type, &PyType_Type); - for (int i=0 ; typelist[i] != NULL ; i++) { - if (PyType_Ready(typelist[i]) < 0) { + for (int i = 0; typelist[i] != NULL; i++) { + PyTypeObject *type = typelist[i]; + if (PyType_Ready(type) < 0) { return -1; } - const char *name = _PyType_Name(typelist[i]); + const char *name = _PyType_Name(type); Py_INCREF(typelist[i]); - if (PyModule_AddObject(m, name, (PyObject *)typelist[i]) < 0) { - Py_DECREF(typelist[i]); + if (PyModule_AddObject(m, name, (PyObject *)type) < 0) { + Py_DECREF(type); return -1; } } From b5f113fc10ec55bd217928467f727e9b9ea1ccfa Mon Sep 17 00:00:00 2001 From: Dong-hee Na Date: Wed, 18 Mar 2020 02:01:37 +0900 Subject: [PATCH 3/4] bpo-1635741: nit --- Modules/itertoolsmodule.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/itertoolsmodule.c b/Modules/itertoolsmodule.c index 9d89c30ef79aff..4f628284618333 100644 --- a/Modules/itertoolsmodule.c +++ b/Modules/itertoolsmodule.c @@ -4753,7 +4753,7 @@ static struct PyModuleDef_Slot itertoolsmodule_slots[] = { static PyMethodDef module_methods[] = { ITERTOOLS_TEE_METHODDEF - {NULL, NULL} /* sentinel */ + {NULL, NULL} /* sentinel */ }; From ff9088071786607a55266c8f200e1ba8490f38de Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 17 Mar 2020 18:04:34 +0100 Subject: [PATCH 4/4] Update Modules/itertoolsmodule.c --- Modules/itertoolsmodule.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/itertoolsmodule.c b/Modules/itertoolsmodule.c index 4f628284618333..72fd3d7c551caa 100644 --- a/Modules/itertoolsmodule.c +++ b/Modules/itertoolsmodule.c @@ -4736,7 +4736,7 @@ itertoolsmodule_exec(PyObject *m) return -1; } const char *name = _PyType_Name(type); - Py_INCREF(typelist[i]); + Py_INCREF(type); if (PyModule_AddObject(m, name, (PyObject *)type) < 0) { Py_DECREF(type); return -1;