Skip to content

Commit 2fc8841

Browse files
committed
Use Argument Clinic
Taken from patch by Sergey B Kirpichev <[email protected]>
1 parent 47abbd1 commit 2fc8841

File tree

2 files changed

+94
-24
lines changed

2 files changed

+94
-24
lines changed

Modules/_functoolsmodule.c

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -932,19 +932,29 @@ _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+
/
941+
initial as result: object(c_default="NULL") = None
942+
943+
Apply a function of two arguments cumulatively.
944+
945+
Apply it to the items of a sequence or iterable, from left to right, so as to
946+
reduce the iterable to a single value. For example, reduce(lambda x, y: x+y,
947+
[1, 2, 3, 4, 5]) calculates ((((1+2)+3)+4)+5). If initial is present, it is
948+
placed before the items of the iterable in the calculation, and serves as a
949+
default when the iterable is empty.
950+
[clinic start generated code]*/
951+
937952
static PyObject *
938-
functools_reduce(PyObject *self, PyObject *args, PyObject *kwargs)
953+
_functools_reduce_impl(PyObject *module, PyObject *func, PyObject *seq,
954+
PyObject *result)
955+
/*[clinic end generated code: output=30d898fe1267c79d input=b7082b8b1473fdc2]*/
939956
{
940-
PyObject *seq, *func, *result = NULL, *it;
941-
static char *keywords[] = {"", "", "initial", NULL};
942-
943-
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|O:reduce", keywords,
944-
&func, &seq, &result))
945-
{
946-
return NULL;
947-
}
957+
PyObject *args, *it;
948958

949959
if (result != NULL)
950960
Py_INCREF(result);
@@ -1011,16 +1021,6 @@ functools_reduce(PyObject *self, PyObject *args, PyObject *kwargs)
10111021
return NULL;
10121022
}
10131023

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

10261026
/* There are four principal algorithmic differences from the pure python version:
@@ -1725,8 +1725,7 @@ PyDoc_STRVAR(_functools_doc,
17251725
"Tools that operate on functions.");
17261726

17271727
static PyMethodDef _functools_methods[] = {
1728-
{"reduce", _PyCFunction_CAST(functools_reduce), METH_VARARGS|METH_KEYWORDS,
1729-
functools_reduce_doc},
1728+
_FUNCTOOLS_REDUCE_METHODDEF
17301729
_FUNCTOOLS_CMP_TO_KEY_METHODDEF
17311730
{NULL, NULL} /* sentinel */
17321731
};

Modules/clinic/_functoolsmodule.c.h

Lines changed: 72 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)