← 返回首页
bpo-1635741: Port _functools module to multiphase initialization (PEP-489) by phsilva · Pull Request #19151 · python/cpython · GitHub
Skip to content

Navigation Menu

Toggle navigation
Sign in
Appearance settings
Search or jump to...

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Include my email address so I can be contacted

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Resetting focus
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) .rst  (1) All 2 file types selected Viewed files
Conversations
Failed to load comments. Retry
Loading
Jump to
Jump to file
Failed to load files. Retry
Loading
Diff view
Unified
Split
Hide whitespace
Apply and reload
Show whitespace
Diff view
Unified
Split
Hide whitespace
Apply and reload
Show comments View file Edit file Delete file Open in desktop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Port _functools module to multiphase initialization (PEP 489). Patch by
Paulo Henrique Silva.
60 changes: 32 additions & 28 deletions Modules/_functoolsmodule.c
Show comments View file Edit file Delete file Open in desktop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1400,58 +1400,62 @@ static PyTypeObject lru_cache_type = {

/* module level code ********************************************************/

PyDoc_STRVAR(module_doc,
PyDoc_STRVAR(_functools_doc,
"Tools that operate on functions.");

static PyMethodDef module_methods[] = {
static PyMethodDef _functools_methods[] = {
{"reduce", functools_reduce, METH_VARARGS, functools_reduce_doc},
{"cmp_to_key", (PyCFunction)(void(*)(void))functools_cmp_to_key,
METH_VARARGS | METH_KEYWORDS, functools_cmp_to_key_doc},
{NULL, NULL} /* sentinel */
};

static void
module_free(void *m)
_functools_free(void *m)
{
Py_CLEAR(kwd_mark);
}

static struct PyModuleDef _functoolsmodule = {
PyModuleDef_HEAD_INIT,
"_functools",
module_doc,
-1,
module_methods,
NULL,
NULL,
NULL,
module_free,
};

PyMODINIT_FUNC
PyInit__functools(void)
static int
_functools_exec(PyObject *module)
{
PyObject *m;
PyTypeObject *typelist[] = {
&partial_type,
&lru_cache_type
};

m = PyModule_Create(&_functoolsmodule);
if (m == NULL)
return NULL;

kwd_mark = _PyObject_CallNoArg((PyObject *)&PyBaseObject_Type);
if (!kwd_mark) {
Py_DECREF(m);
return NULL;
return -1;
}

for (size_t i = 0; i < Py_ARRAY_LENGTH(typelist); i++) {
if (PyModule_AddType(m, typelist[i]) < 0) {
Py_DECREF(m);
return NULL;
if (PyModule_AddType(module, typelist[i]) < 0) {
return -1;
}
}
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);
}
Toggle all file notes Toggle all file annotations

Footer

© 2026 GitHub, Inc.