Skip to content

Commit 14e5e9f

Browse files
skirpichevebonnal
authored andcommitted
pythongh-125916: Adapt functools.reduce() to Argument Clinic (python#125999)
1 parent 0a37d67 commit 14e5e9f

File tree

3 files changed

+70
-22
lines changed

3 files changed

+70
-22
lines changed

Lib/test/test_inspect/test_inspect.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -5708,8 +5708,8 @@ def test_faulthandler_module_has_signatures(self):
57085708
self._test_module_has_signatures(faulthandler, unsupported_signature=unsupported_signature)
57095709

57105710
def test_functools_module_has_signatures(self):
5711-
no_signature = {'reduce'}
5712-
self._test_module_has_signatures(functools, no_signature)
5711+
unsupported_signature = {"reduce"}
5712+
self._test_module_has_signatures(functools, unsupported_signature=unsupported_signature)
57135713

57145714
def test_gc_module_has_signatures(self):
57155715
import gc

Modules/_functoolsmodule.c

+23-19
Original file line numberDiff line numberDiff line change
@@ -932,15 +932,31 @@ _functools_cmp_to_key_impl(PyObject *module, PyObject *mycmp)
932932

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

935-
// Not converted to argument clinic, because of `args` in-place modification.
936-
// AC will affect performance.
935+
/*[clinic input]
936+
_functools.reduce
937+
938+
function as func: object
939+
iterable as seq: object
940+
initial as result: object = NULL
941+
/
942+
943+
Apply a function of two arguments cumulatively to the items of an iterable, from left to right.
944+
945+
This effectively reduces the iterable to a single value. If initial is present,
946+
it is placed before the items of the iterable in the calculation, and serves as
947+
a default when the iterable is empty.
948+
949+
For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5])
950+
calculates ((((1 + 2) + 3) + 4) + 5).
951+
[clinic start generated code]*/
952+
937953
static PyObject *
938-
functools_reduce(PyObject *self, PyObject *args)
954+
_functools_reduce_impl(PyObject *module, PyObject *func, PyObject *seq,
955+
PyObject *result)
956+
/*[clinic end generated code: output=30d898fe1267c79d input=d233c2670cba7f66]*/
939957
{
940-
PyObject *seq, *func, *result = NULL, *it;
958+
PyObject *args, *it;
941959

942-
if (!PyArg_UnpackTuple(args, "reduce", 2, 3, &func, &seq, &result))
943-
return NULL;
944960
if (result != NULL)
945961
Py_INCREF(result);
946962

@@ -1006,18 +1022,6 @@ functools_reduce(PyObject *self, PyObject *args)
10061022
return NULL;
10071023
}
10081024

1009-
PyDoc_STRVAR(functools_reduce_doc,
1010-
"reduce(function, iterable[, initial], /) -> value\n\
1011-
\n\
1012-
Apply a function of two arguments cumulatively to the items of an iterable, from left to right.\n\
1013-
\n\
1014-
This effectively reduces the iterable to a single value. If initial is present,\n\
1015-
it is placed before the items of the iterable in the calculation, and serves as\n\
1016-
a default when the iterable is empty.\n\
1017-
\n\
1018-
For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5])\n\
1019-
calculates ((((1 + 2) + 3) + 4) + 5).");
1020-
10211025
/* lru_cache object **********************************************************/
10221026

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

17241728
static PyMethodDef _functools_methods[] = {
1725-
{"reduce", functools_reduce, METH_VARARGS, functools_reduce_doc},
1729+
_FUNCTOOLS_REDUCE_METHODDEF
17261730
_FUNCTOOLS_CMP_TO_KEY_METHODDEF
17271731
{NULL, NULL} /* sentinel */
17281732
};

Modules/clinic/_functoolsmodule.c.h

+45-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)