Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit da39dbf

Browse files
committed
Merged revisions 76916 via svnmerge from
svn+ssh://[email protected]/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. ........
1 parent a8f6f1e commit da39dbf

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

Modules/mathmodule.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,15 +1137,22 @@ math_factorial(PyObject *self, PyObject *arg)
11371137
PyObject *result, *iobj, *newresult;
11381138

11391139
if (PyFloat_Check(arg)) {
1140+
PyObject *lx;
11401141
double dx = PyFloat_AS_DOUBLE((PyFloatObject *)arg);
1141-
if (dx != floor(dx)) {
1142+
if (!(Py_IS_FINITE(dx) && dx == floor(dx))) {
11421143
PyErr_SetString(PyExc_ValueError,
11431144
"factorial() only accepts integral values");
11441145
return NULL;
11451146
}
1147+
lx = PyLong_FromDouble(dx);
1148+
if (lx == NULL)
1149+
return NULL;
1150+
x = PyLong_AsLong(lx);
1151+
Py_DECREF(lx);
11461152
}
1153+
else
1154+
x = PyLong_AsLong(arg);
11471155

1148-
x = PyLong_AsLong(arg);
11491156
if (x == -1 && PyErr_Occurred())
11501157
return NULL;
11511158
if (x < 0) {

0 commit comments

Comments
 (0)