diff options
author | Pablo Galindo <Pablogsal@gmail.com> | 2018-09-03 21:20:06 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-09-03 21:20:06 (GMT) |
commit | e9ba3705de656215d52b8f8f4a2e7ad60190e944 (patch) | |
tree | fd2e22464103a3a36efd27788e02c697b199b43a /Modules/mathmodule.c | |
parent | 65fc98e7b1f62c2e621f04780a3a77c3498cc195 (diff) | |
download | cpython-e9ba3705de656215d52b8f8f4a2e7ad60190e944.zip cpython-e9ba3705de656215d52b8f8f4a2e7ad60190e944.tar.gz cpython-e9ba3705de656215d52b8f8f4a2e7ad60190e944.tar.bz2 |
bpo-33083 - Make math.factorial reject arguments that are not int-like (GH-6149)
math.factorial() was accepting non-integral Decimal instances. This is inconsistent with the actual behaviour for floats, which are not accepted.
Diffstat (limited to 'Modules/mathmodule.c')
-rw-r--r-- | Modules/mathmodule.c | 12 |
1 files changed, 9 insertions, 3 deletions
diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c index 06a969c..e872e47 100644 --- a/Modules/mathmodule.c +++ b/Modules/mathmodule.c @@ -1656,7 +1656,7 @@ math_factorial(PyObject *module, PyObject *arg) { long x; int overflow; - PyObject *result, *odd_part, *two_valuation; + PyObject *result, *odd_part, *two_valuation, *pyint_form; if (PyFloat_Check(arg)) { PyObject *lx; @@ -1672,8 +1672,14 @@ math_factorial(PyObject *module, PyObject *arg) x = PyLong_AsLongAndOverflow(lx, &overflow); Py_DECREF(lx); } - else - x = PyLong_AsLongAndOverflow(arg, &overflow); + else { + pyint_form = PyNumber_Index(arg); + if (pyint_form == NULL) { + return NULL; + } + x = PyLong_AsLongAndOverflow(pyint_form, &overflow); + Py_DECREF(pyint_form); + } if (x == -1 && PyErr_Occurred()) { return NULL; |