diff Modules/mathmodule.c @ 57807:5ccc5d4068c1 legacy-trunk

math.factorial depends on PyLong_AsLong correctly converting floats; rewrite it to do the conversion explicitly instead. See issue #7550.
author Mark Dickinson <dickinsm@gmail.com>
date Sun, 20 Dec 2009 13:58:18 +0000
parents d350afc337dc
children 7f5cbc28f433
line wrap: on
line diff
--- a/Modules/mathmodule.c	Sun Dec 20 07:29:31 2009 +0000
+++ b/Modules/mathmodule.c	Sun Dec 20 13:58:18 2009 +0000
@@ -1082,15 +1082,22 @@
 	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) {