bpo-1635741: Port _functools module to multiphase initialization (PEP 489) (GH-19151)

This commit is contained in:
Paulo Henrique Silva 2020-03-24 23:19:58 -03:00 committed by GitHub
parent f3d5ac4772
commit 7dd549eb08
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 28 deletions

View File

@ -0,0 +1,2 @@
Port _functools module to multiphase initialization (PEP 489). Patch by
Paulo Henrique Silva.

View File

@ -1400,10 +1400,10 @@ static PyTypeObject lru_cache_type = {
/* module level code ********************************************************/ /* module level code ********************************************************/
PyDoc_STRVAR(module_doc, PyDoc_STRVAR(_functools_doc,
"Tools that operate on functions."); "Tools that operate on functions.");
static PyMethodDef module_methods[] = { static PyMethodDef _functools_methods[] = {
{"reduce", functools_reduce, METH_VARARGS, functools_reduce_doc}, {"reduce", functools_reduce, METH_VARARGS, functools_reduce_doc},
{"cmp_to_key", (PyCFunction)(void(*)(void))functools_cmp_to_key, {"cmp_to_key", (PyCFunction)(void(*)(void))functools_cmp_to_key,
METH_VARARGS | METH_KEYWORDS, functools_cmp_to_key_doc}, METH_VARARGS | METH_KEYWORDS, functools_cmp_to_key_doc},
@ -1411,47 +1411,51 @@ static PyMethodDef module_methods[] = {
}; };
static void static void
module_free(void *m) _functools_free(void *m)
{ {
Py_CLEAR(kwd_mark); Py_CLEAR(kwd_mark);
} }
static struct PyModuleDef _functoolsmodule = { static int
PyModuleDef_HEAD_INIT, _functools_exec(PyObject *module)
"_functools",
module_doc,
-1,
module_methods,
NULL,
NULL,
NULL,
module_free,
};
PyMODINIT_FUNC
PyInit__functools(void)
{ {
PyObject *m;
PyTypeObject *typelist[] = { PyTypeObject *typelist[] = {
&partial_type, &partial_type,
&lru_cache_type &lru_cache_type
}; };
m = PyModule_Create(&_functoolsmodule);
if (m == NULL)
return NULL;
kwd_mark = _PyObject_CallNoArg((PyObject *)&PyBaseObject_Type); kwd_mark = _PyObject_CallNoArg((PyObject *)&PyBaseObject_Type);
if (!kwd_mark) { if (!kwd_mark) {
Py_DECREF(m); return -1;
return NULL;
} }
for (size_t i = 0; i < Py_ARRAY_LENGTH(typelist); i++) { for (size_t i = 0; i < Py_ARRAY_LENGTH(typelist); i++) {
if (PyModule_AddType(m, typelist[i]) < 0) { if (PyModule_AddType(module, typelist[i]) < 0) {
Py_DECREF(m); return -1;
return NULL;
} }
} }
return m; return 0;
}
static struct PyModuleDef_Slot _functools_slots[] = {
{Py_mod_exec, _functools_exec},
{0, NULL}
};
static struct PyModuleDef _functools_module = {
PyModuleDef_HEAD_INIT,
"_functools",
_functools_doc,
0,
_functools_methods,
_functools_slots,
NULL,
NULL,
_functools_free,
};
PyMODINIT_FUNC
PyInit__functools(void)
{
return PyModuleDef_Init(&_functools_module);
} }