Skip to content

Commit aca7f57

Browse files
bpo-30950: Convert round() to Argument Clinic. (#2740)
1 parent 00987f6 commit aca7f57

File tree

2 files changed

+51
-20
lines changed

2 files changed

+51
-20
lines changed

Python/bltinmodule.c

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2079,19 +2079,23 @@ builtin_repr(PyObject *module, PyObject *obj)
20792079
}
20802080

20812081

2082-
/* AC: cannot convert yet, as needs PEP 457 group support in inspect
2083-
* or a semantic change to accept None for "ndigits"
2084-
*/
2082+
/*[clinic input]
2083+
round as builtin_round
2084+
2085+
number: object
2086+
ndigits: object = NULL
2087+
2088+
Round a number to a given precision in decimal digits.
2089+
2090+
The return value is an integer if ndigits is omitted or None. Otherwise
2091+
the return value has the same type as the number. ndigits may be negative.
2092+
[clinic start generated code]*/
2093+
20852094
static PyObject *
2086-
builtin_round(PyObject *self, PyObject *args, PyObject *kwds)
2095+
builtin_round_impl(PyObject *module, PyObject *number, PyObject *ndigits)
2096+
/*[clinic end generated code: output=ff0d9dd176c02ede input=854bc3a217530c3d]*/
20872097
{
2088-
PyObject *ndigits = NULL;
2089-
static char *kwlist[] = {"number", "ndigits", 0};
2090-
PyObject *number, *round, *result;
2091-
2092-
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:round",
2093-
kwlist, &number, &ndigits))
2094-
return NULL;
2098+
PyObject *round, *result;
20952099

20962100
if (Py_TYPE(number)->tp_dict == NULL) {
20972101
if (PyType_Ready(Py_TYPE(number)) < 0)
@@ -2115,13 +2119,6 @@ builtin_round(PyObject *self, PyObject *args, PyObject *kwds)
21152119
return result;
21162120
}
21172121

2118-
PyDoc_STRVAR(round_doc,
2119-
"round(number[, ndigits]) -> number\n\
2120-
\n\
2121-
Round a number to a given precision in decimal digits (default 0 digits).\n\
2122-
This returns an int when called with one argument, otherwise the\n\
2123-
same type as the number. ndigits may be negative.");
2124-
21252122

21262123
/*AC: we need to keep the kwds dict intact to easily call into the
21272124
* list.sort method, which isn't currently supported in AC. So we just use
@@ -2679,7 +2676,7 @@ static PyMethodDef builtin_methods[] = {
26792676
BUILTIN_POW_METHODDEF
26802677
{"print", (PyCFunction)builtin_print, METH_FASTCALL | METH_KEYWORDS, print_doc},
26812678
BUILTIN_REPR_METHODDEF
2682-
{"round", (PyCFunction)builtin_round, METH_VARARGS | METH_KEYWORDS, round_doc},
2679+
BUILTIN_ROUND_METHODDEF
26832680
BUILTIN_SETATTR_METHODDEF
26842681
BUILTIN_SORTED_METHODDEF
26852682
BUILTIN_SUM_METHODDEF

Python/clinic/bltinmodule.c.h

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,40 @@ PyDoc_STRVAR(builtin_repr__doc__,
573573
#define BUILTIN_REPR_METHODDEF \
574574
{"repr", (PyCFunction)builtin_repr, METH_O, builtin_repr__doc__},
575575

576+
PyDoc_STRVAR(builtin_round__doc__,
577+
"round($module, /, number, ndigits=None)\n"
578+
"--\n"
579+
"\n"
580+
"Round a number to a given precision in decimal digits.\n"
581+
"\n"
582+
"The return value is an integer if ndigits is omitted or None. Otherwise\n"
583+
"the return value has the same type as the number. ndigits may be negative.");
584+
585+
#define BUILTIN_ROUND_METHODDEF \
586+
{"round", (PyCFunction)builtin_round, METH_FASTCALL|METH_KEYWORDS, builtin_round__doc__},
587+
588+
static PyObject *
589+
builtin_round_impl(PyObject *module, PyObject *number, PyObject *ndigits);
590+
591+
static PyObject *
592+
builtin_round(PyObject *module, PyObject **args, Py_ssize_t nargs, PyObject *kwnames)
593+
{
594+
PyObject *return_value = NULL;
595+
static const char * const _keywords[] = {"number", "ndigits", NULL};
596+
static _PyArg_Parser _parser = {"O|O:round", _keywords, 0};
597+
PyObject *number;
598+
PyObject *ndigits = NULL;
599+
600+
if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser,
601+
&number, &ndigits)) {
602+
goto exit;
603+
}
604+
return_value = builtin_round_impl(module, number, ndigits);
605+
606+
exit:
607+
return return_value;
608+
}
609+
576610
PyDoc_STRVAR(builtin_sum__doc__,
577611
"sum($module, iterable, start=0, /)\n"
578612
"--\n"
@@ -676,4 +710,4 @@ builtin_issubclass(PyObject *module, PyObject **args, Py_ssize_t nargs)
676710
exit:
677711
return return_value;
678712
}
679-
/*[clinic end generated code: output=09752daa8cdd6ec7 input=a9049054013a1b77]*/
713+
/*[clinic end generated code: output=d46a224ac804eef1 input=a9049054013a1b77]*/

0 commit comments

Comments
 (0)