FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-125916: Adapt functools.reduce() to Argument Clinic by skirpichev · Pull Request #125999 · python/cpython · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension .c  (1) .h  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
Next Next commit
gh-125916: convert reduce() to Argument Clinic
  • Loading branch information
skirpichev committed Oct 26, 2024
commit b5ac476b8957927ad96bd4890b43dab03dafd6fb
46 changes: 29 additions & 17 deletions Modules/_functoolsmodule.c
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 @@ -932,15 +932,31 @@ _functools_cmp_to_key_impl(PyObject *module, PyObject *mycmp)

/* reduce (used to be a builtin) ********************************************/

// Not converted to argument clinic, because of `args` in-place modification.
// AC will affect performance.
/*[clinic input]
_functools.reduce

function as func: object
iterable as seq: object
initial as result: object(c_default="NULL") = _functools._initial_missing
Comment thread
skirpichev marked this conversation as resolved.
Resolved
Comment thread
skirpichev marked this conversation as resolved.
Resolved
/

Apply a function of two arguments cumulatively to an iterable, from left to right.

This efficiently reduces the iterable to a single value. If initial is present,
Comment thread
skirpichev marked this conversation as resolved.
Resolved
it is placed before the items of the iterable in the calculation, and serves as
a default when the iterable is empty.

For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5])
calculates ((((1 + 2) + 3) + 4) + 5).
Comment thread
skirpichev marked this conversation as resolved.
Resolved
[clinic start generated code]*/

static PyObject *
functools_reduce(PyObject *self, PyObject *args)
_functools_reduce_impl(PyObject *module, PyObject *func, PyObject *seq,
PyObject *result)
/*[clinic end generated code: output=30d898fe1267c79d input=0315d8e82beb5e6d]*/
{
PyObject *seq, *func, *result = NULL, *it;
PyObject *args, *it;

if (!PyArg_UnpackTuple(args, "reduce", 2, 3, &func, &seq, &result))
return NULL;
if (result != NULL)
Py_INCREF(result);

Expand Down Expand Up @@ -1006,16 +1022,6 @@ functools_reduce(PyObject *self, PyObject *args)
return NULL;
}

PyDoc_STRVAR(functools_reduce_doc,
"reduce(function, iterable[, initial], /) -> value\n\
\n\
Apply a function of two arguments cumulatively to the items of a sequence\n\
or iterable, from left to right, so as to reduce the iterable to a single\n\
value. For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates\n\
((((1+2)+3)+4)+5). If initial is present, it is placed before the items\n\
of the iterable in the calculation, and serves as a default when the\n\
iterable is empty.");

/* lru_cache object **********************************************************/

/* There are four principal algorithmic differences from the pure python version:
Expand Down Expand Up @@ -1720,7 +1726,7 @@ PyDoc_STRVAR(_functools_doc,
"Tools that operate on functions.");

static PyMethodDef _functools_methods[] = {
{"reduce", functools_reduce, METH_VARARGS, functools_reduce_doc},
_FUNCTOOLS_REDUCE_METHODDEF
_FUNCTOOLS_CMP_TO_KEY_METHODDEF
{NULL, NULL} /* sentinel */
};
Expand Down Expand Up @@ -1789,6 +1795,12 @@ _functools_exec(PyObject *module)
// lru_list_elem is used only in _lru_cache_wrapper.
// So we don't expose it in module namespace.

if (PyModule_Add(module, "_initial_missing",
PyObject_New(PyObject, &PyBaseObject_Type)) < 0)
{
return -1;
}

return 0;
}

Expand Down
47 changes: 46 additions & 1 deletion Modules/clinic/_functoolsmodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.


Back | FazBrowse Home | New Git URL