Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
4 changes: 2 additions & 2 deletions Lib/test/test_inspect/test_inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -5695,8 +5695,8 @@ def test_faulthandler_module_has_signatures(self):
self._test_module_has_signatures(faulthandler, unsupported_signature=unsupported_signature)

def test_functools_module_has_signatures(self):
no_signature = {'reduce'}
self._test_module_has_signatures(functools, no_signature)
unsupported_signature = {"reduce"}
self._test_module_has_signatures(functools, unsupported_signature=unsupported_signature)

def test_gc_module_has_signatures(self):
import gc
Expand Down
41 changes: 24 additions & 17 deletions Modules/_functoolsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -932,15 +932,32 @@ _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]
@text_signature "($module, function, iterable[, initial], /)"
_functools.reduce

function as func: object
iterable as seq: object
initial as result: object = NULL
/

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,
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).
[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=d85ca22bf7ee578b]*/
{
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 +1023,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 +1727,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
46 changes: 45 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.

Loading