]> granicus.if.org Git - python/commitdiff
Merged revisions 76916 via svnmerge from
authorMark Dickinson <dickinsm@gmail.com>
Sun, 20 Dec 2009 14:07:47 +0000 (14:07 +0000)
committerMark Dickinson <dickinsm@gmail.com>
Sun, 20 Dec 2009 14:07:47 +0000 (14:07 +0000)
svn+ssh://pythondev@svn.python.org/python/trunk

........
  r76916 | mark.dickinson | 2009-12-20 13:58:18 +0000 (Sun, 20 Dec 2009) | 3 lines

  math.factorial depends on PyLong_AsLong correctly converting floats; rewrite
  it to do the conversion explicitly instead.  See issue #7550.
........

Modules/mathmodule.c

index f835005f754b5cd4b627f8f553cf1408df1a0706..92f5f42ece9587283f1de4fafea6ae94adcc56b1 100644 (file)
@@ -1137,15 +1137,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 = PyLong_AsLong(arg);
 
-       x = PyLong_AsLong(arg);
        if (x == -1 && PyErr_Occurred())
                return NULL;
        if (x < 0) {