Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-41342: Convert int.__round__ to Argument Clinic #21549

Merged
merged 1 commit into from
Jul 20, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:func:`round` with integer argument is now faster (9--60%).
36 changes: 35 additions & 1 deletion Objects/clinic/longobject.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 15 additions & 7 deletions Objects/longobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -5155,10 +5155,22 @@ _PyLong_DivmodNear(PyObject *a, PyObject *b)
return NULL;
}

/*[clinic input]
int.__round__

ndigits as o_ndigits: object = NULL
/

Rounding an Integral returns itself.

Rounding with an ndigits argument also returns an integer.
[clinic start generated code]*/

static PyObject *
long_round(PyObject *self, PyObject *args)
int___round___impl(PyObject *self, PyObject *o_ndigits)
/*[clinic end generated code: output=954fda6b18875998 input=1614cf23ec9e18c3]*/
{
PyObject *o_ndigits=NULL, *temp, *result, *ndigits;
PyObject *temp, *result, *ndigits;

/* To round an integer m to the nearest 10**n (n positive), we make use of
* the divmod_near operation, defined by:
Expand All @@ -5174,8 +5186,6 @@ long_round(PyObject *self, PyObject *args)
*
* m - divmod_near(m, 10**n)[1].
*/
if (!PyArg_ParseTuple(args, "|O", &o_ndigits))
return NULL;
if (o_ndigits == NULL)
return long_long(self);

Expand Down Expand Up @@ -5536,9 +5546,7 @@ static PyMethodDef long_methods[] = {
"Flooring an Integral returns itself."},
{"__ceil__", long_long_meth, METH_NOARGS,
"Ceiling of an Integral returns itself."},
{"__round__", (PyCFunction)long_round, METH_VARARGS,
"Rounding an Integral returns itself.\n"
"Rounding with an ndigits argument also returns an integer."},
INT___ROUND___METHODDEF
INT___GETNEWARGS___METHODDEF
INT___FORMAT___METHODDEF
INT___SIZEOF___METHODDEF
Expand Down