@@ -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+
937953static 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
17241728static 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};
0 commit comments