[Python-checkins] r76916 - python/trunk/Modules/mathmodule.c

mark.dickinson python-checkins at python.org
Sun Dec 20 14:58:18 CET 2009


Author: mark.dickinson
Date: Sun Dec 20 14:58:18 2009
New Revision: 76916

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


Modified:
   python/trunk/Modules/mathmodule.c

Modified: python/trunk/Modules/mathmodule.c
==============================================================================
--- python/trunk/Modules/mathmodule.c	(original)
+++ python/trunk/Modules/mathmodule.c	Sun Dec 20 14:58:18 2009
@@ -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) {


More information about the Python-checkins mailing list