diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2019-06-16 11:38:07 (GMT) |
---|---|---|
committer | Mark Dickinson <dickinsm@gmail.com> | 2019-06-16 11:38:07 (GMT) |
commit | 599f7ecb70f1b9714c702322b235b4d8155205ae (patch) | |
tree | 3f2a9cf23063fae445d5145e32c58c0e89a5d46c /Modules | |
parent | b0cb988420d7f1fa8e09fd44d053c0af05c635f3 (diff) | |
download | cpython-599f7ecb70f1b9714c702322b235b4d8155205ae.zip cpython-599f7ecb70f1b9714c702322b235b4d8155205ae.tar.gz cpython-599f7ecb70f1b9714c702322b235b4d8155205ae.tar.bz2 |
Simplify negativity checks in math.comb and math.perm. (GH-13870) (#14125)
(cherry picked from commit 45e0411eee023b21acf1615e995d26058d66c0f1)
Co-authored-by: Mark Dickinson <dickinsm@gmail.com>
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/mathmodule.c | 26 |
1 files changed, 16 insertions, 10 deletions
diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c index ed11476..76d821c 100644 --- a/Modules/mathmodule.c +++ b/Modules/mathmodule.c @@ -3056,6 +3056,12 @@ math_perm_impl(PyObject *module, PyObject *n, PyObject *k) "n must be a non-negative integer"); goto error; } + if (Py_SIZE(k) < 0) { + PyErr_SetString(PyExc_ValueError, + "k must be a non-negative integer"); + goto error; + } + cmp = PyObject_RichCompareBool(n, k, Py_LT); if (cmp != 0) { if (cmp > 0) { @@ -3072,11 +3078,8 @@ math_perm_impl(PyObject *module, PyObject *n, PyObject *k) LLONG_MAX); goto error; } - else if (overflow < 0 || factors < 0) { - if (!PyErr_Occurred()) { - PyErr_SetString(PyExc_ValueError, - "k must be a non-negative integer"); - } + else if (factors == -1) { + /* k is nonnegative, so a return value of -1 can only indicate error */ goto error; } @@ -3176,6 +3179,12 @@ math_comb_impl(PyObject *module, PyObject *n, PyObject *k) "n must be a non-negative integer"); goto error; } + if (Py_SIZE(k) < 0) { + PyErr_SetString(PyExc_ValueError, + "k must be a non-negative integer"); + goto error; + } + /* k = min(k, n - k) */ temp = PyNumber_Subtract(n, k); if (temp == NULL) { @@ -3204,11 +3213,8 @@ math_comb_impl(PyObject *module, PyObject *n, PyObject *k) LLONG_MAX); goto error; } - else if (overflow < 0 || factors < 0) { - if (!PyErr_Occurred()) { - PyErr_SetString(PyExc_ValueError, - "k must be a non-negative integer"); - } + if (factors == -1) { + /* k is nonnegative, so a return value of -1 can only indicate error */ goto error; } |