diff options
author | Mark Dickinson <dickinsm@gmail.com> | 2009-12-20 13:58:18 (GMT) |
---|---|---|
committer | Mark Dickinson <dickinsm@gmail.com> | 2009-12-20 13:58:18 (GMT) |
commit | 5698977186bc348223b5fdc5eda677316baa0f5e (patch) | |
tree | 07a0f77688edbdba7e0ac4b2e46685c6237b6e9d /Modules/mathmodule.c | |
parent | 0732fd952baf34eb0636fd6ce4c7093d2d70e98b (diff) | |
download | cpython-5698977186bc348223b5fdc5eda677316baa0f5e.zip cpython-5698977186bc348223b5fdc5eda677316baa0f5e.tar.gz cpython-5698977186bc348223b5fdc5eda677316baa0f5e.tar.bz2 |
math.factorial depends on PyLong_AsLong correctly converting floats; rewrite
it to do the conversion explicitly instead. See issue #7550.
Diffstat (limited to 'Modules/mathmodule.c')
-rw-r--r-- | Modules/mathmodule.c | 11 |
1 files changed, 9 insertions, 2 deletions
diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c index 5dedf04..a7299a5 100644 --- a/Modules/mathmodule.c +++ b/Modules/mathmodule.c @@ -1082,15 +1082,22 @@ math_factorial(PyObject *self, PyObject *arg) PyObject *result, *iobj, *newresult; if (PyFloat_Check(arg)) { + PyObject *lx; double dx = PyFloat_AS_DOUBLE((PyFloatObject *)arg); - if (dx != floor(dx)) { + if (!(Py_IS_FINITE(dx) && dx == floor(dx))) { PyErr_SetString(PyExc_ValueError, "factorial() only accepts integral values"); return NULL; } + lx = PyLong_FromDouble(dx); + if (lx == NULL) + return NULL; + x = PyLong_AsLong(lx); + Py_DECREF(lx); } + else + x = PyInt_AsLong(arg); - x = PyInt_AsLong(arg); if (x == -1 && PyErr_Occurred()) return NULL; if (x < 0) { |