Skip to content

Commit 6980d78

Browse files
committed
bpo-1635741: Port itertools module to multiphase initialization.
1 parent 2037502 commit 6980d78

File tree

2 files changed

+40
-33
lines changed

2 files changed

+40
-33
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Port itertools module to multiphase initialization (:pep:`489`).

Modules/itertoolsmodule.c

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4701,31 +4701,9 @@ combinations(p, r)\n\
47014701
combinations_with_replacement(p, r)\n\
47024702
");
47034703

4704-
4705-
static PyMethodDef module_methods[] = {
4706-
ITERTOOLS_TEE_METHODDEF
4707-
{NULL, NULL} /* sentinel */
4708-
};
4709-
4710-
4711-
static struct PyModuleDef itertoolsmodule = {
4712-
PyModuleDef_HEAD_INIT,
4713-
"itertools",
4714-
module_doc,
4715-
-1,
4716-
module_methods,
4717-
NULL,
4718-
NULL,
4719-
NULL,
4720-
NULL
4721-
};
4722-
4723-
PyMODINIT_FUNC
4724-
PyInit_itertools(void)
4704+
static int
4705+
itertoolsmodule_exec(PyObject *m)
47254706
{
4726-
int i;
4727-
PyObject *m;
4728-
const char *name;
47294707
PyTypeObject *typelist[] = {
47304708
&accumulate_type,
47314709
&combinations_type,
@@ -4751,19 +4729,47 @@ PyInit_itertools(void)
47514729
};
47524730

47534731
Py_SET_TYPE(&teedataobject_type, &PyType_Type);
4754-
m = PyModule_Create(&itertoolsmodule);
4755-
if (m == NULL) {
4756-
return NULL;
4757-
}
47584732

4759-
for (i=0 ; typelist[i] != NULL ; i++) {
4733+
for (int i=0 ; typelist[i] != NULL ; i++) {
47604734
if (PyType_Ready(typelist[i]) < 0) {
4761-
return NULL;
4735+
return -1;
47624736
}
4763-
name = _PyType_Name(typelist[i]);
4737+
const char *name = _PyType_Name(typelist[i]);
47644738
Py_INCREF(typelist[i]);
4765-
PyModule_AddObject(m, name, (PyObject *)typelist[i]);
4739+
if (PyModule_AddObject(m, name, (PyObject *)typelist[i]) < 0) {
4740+
Py_DECREF(typelist[i]);
4741+
return -1;
4742+
}
47664743
}
47674744

4768-
return m;
4745+
return 0;
4746+
}
4747+
4748+
static struct PyModuleDef_Slot itertoolsmodule_slots[] = {
4749+
{Py_mod_exec, itertoolsmodule_exec},
4750+
{0, NULL}
4751+
};
4752+
4753+
static PyMethodDef module_methods[] = {
4754+
ITERTOOLS_TEE_METHODDEF
4755+
{NULL, NULL} /* sentinel */
4756+
};
4757+
4758+
4759+
static struct PyModuleDef itertoolsmodule = {
4760+
PyModuleDef_HEAD_INIT,
4761+
"itertools",
4762+
module_doc,
4763+
0,
4764+
module_methods,
4765+
itertoolsmodule_slots,
4766+
NULL,
4767+
NULL,
4768+
NULL
4769+
};
4770+
4771+
PyMODINIT_FUNC
4772+
PyInit_itertools(void)
4773+
{
4774+
return PyModuleDef_Init(&itertoolsmodule);
47694775
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy