From: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> Date: Fri, 4 Sep 2020 23:12:48 +0000 (-0700) Subject: On path with known exact float, extract the double with the fast macro. (GH-21072) X-Git-Tag: v3.9.0rc2~38 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=242eac1f0407ba27fd61683eaf34558d5e4ff360;p=thirdparty%2FPython%2Fcpython.git On path with known exact float, extract the double with the fast macro. (GH-21072) (cherry picked from commit 930f4518aea7f3f0f914ce93c3fb92831a7e1d2a) Co-authored-by: Raymond Hettinger --- diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c index f1d59c09e627..4aa7e6559af5 100644 --- a/Modules/mathmodule.c +++ b/Modules/mathmodule.c @@ -1255,9 +1255,15 @@ static PyObject * math_floor(PyObject *module, PyObject *number) /*[clinic end generated code: output=c6a65c4884884b8a input=63af6b5d7ebcc3d6]*/ { + double x; + _Py_IDENTIFIER(__floor__); - if (!PyFloat_CheckExact(number)) { + if (PyFloat_CheckExact(number)) { + x = PyFloat_AS_DOUBLE(number); + } + else + { PyObject *method = _PyObject_LookupSpecial(number, &PyId___floor__); if (method != NULL) { PyObject *result = _PyObject_CallNoArg(method); @@ -1266,11 +1272,10 @@ math_floor(PyObject *module, PyObject *number) } if (PyErr_Occurred()) return NULL; + x = PyFloat_AsDouble(number); + if (x == -1.0 && PyErr_Occurred()) + return NULL; } - double x = PyFloat_AsDouble(number); - if (x == -1.0 && PyErr_Occurred()) - return NULL; - return PyLong_FromDouble(floor(x)); }