Skip to content

Commit f3d5ac4

Browse files
author
Paulo Henrique Silva
authored
bpo-1635741: Port operator module to multiphase initialization (PEP 489) (GH-19150)
1 parent 8f1ed21 commit f3d5ac4

File tree

2 files changed

+28
-23
lines changed

2 files changed

+28
-23
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Port operator module to multiphase initialization (PEP 489). Patch by Paulo
2+
Henrique Silva.

Modules/_operator.c

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1746,16 +1746,38 @@ static PyTypeObject methodcaller_type = {
17461746
};
17471747

17481748

1749-
/* Initialization function for the module (*must* be called PyInit__operator) */
1749+
static int
1750+
operator_exec(PyObject *module)
1751+
{
1752+
PyTypeObject *types[] = {
1753+
&itemgetter_type,
1754+
&attrgetter_type,
1755+
&methodcaller_type
1756+
};
1757+
1758+
for (size_t i = 0; i < Py_ARRAY_LENGTH(types); i++) {
1759+
if (PyModule_AddType(module, types[i]) < 0) {
1760+
return -1;
1761+
}
1762+
}
1763+
1764+
return 0;
1765+
}
1766+
1767+
1768+
static struct PyModuleDef_Slot operator_slots[] = {
1769+
{Py_mod_exec, operator_exec},
1770+
{0, NULL}
1771+
};
17501772

17511773

17521774
static struct PyModuleDef operatormodule = {
17531775
PyModuleDef_HEAD_INIT,
17541776
"_operator",
17551777
operator_doc,
1756-
-1,
1778+
0,
17571779
operator_methods,
1758-
NULL,
1780+
operator_slots,
17591781
NULL,
17601782
NULL,
17611783
NULL
@@ -1764,24 +1786,5 @@ static struct PyModuleDef operatormodule = {
17641786
PyMODINIT_FUNC
17651787
PyInit__operator(void)
17661788
{
1767-
PyObject *m;
1768-
1769-
/* Create the module and add the functions */
1770-
m = PyModule_Create(&operatormodule);
1771-
if (m == NULL)
1772-
return NULL;
1773-
1774-
PyTypeObject *types[] = {
1775-
&itemgetter_type,
1776-
&attrgetter_type,
1777-
&methodcaller_type
1778-
};
1779-
1780-
for (size_t i = 0; i < Py_ARRAY_LENGTH(types); i++) {
1781-
if (PyModule_AddType(m, types[i]) < 0) {
1782-
return NULL;
1783-
}
1784-
}
1785-
1786-
return m;
1789+
return PyModuleDef_Init(&operatormodule);
17871790
}

0 commit comments

Comments
 (0)