Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-1635741: Port itertools module to multiphase initialization. #19044

Merged
merged 4 commits into from
Mar 17, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Port itertools module to multiphase initialization (:pep:`489`).
75 changes: 41 additions & 34 deletions Modules/itertoolsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -4751,19 +4729,48 @@ 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++) {
if (PyType_Ready(typelist[i]) < 0) {
return NULL;
for (int i = 0; typelist[i] != NULL; i++) {
PyTypeObject *type = typelist[i];
if (PyType_Ready(type) < 0) {
return -1;
}
name = _PyType_Name(typelist[i]);
const char *name = _PyType_Name(type);
Py_INCREF(typelist[i]);
vstinner marked this conversation as resolved.
Show resolved Hide resolved
PyModule_AddObject(m, name, (PyObject *)typelist[i]);
if (PyModule_AddObject(m, name, (PyObject *)type) < 0) {
Py_DECREF(type);
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);
}