summaryrefslogtreecommitdiff
path: root/numpy/lib
diff options
context:
space:
mode:
Diffstat (limited to 'numpy/lib')
-rw-r--r--numpy/lib/src/_compiled_base.c29
-rw-r--r--numpy/lib/tests/test_function_base.py19
2 files changed, 32 insertions, 16 deletions
diff --git a/numpy/lib/src/_compiled_base.c b/numpy/lib/src/_compiled_base.c
index f70cd2bab..a461613e3 100644
--- a/numpy/lib/src/_compiled_base.c
+++ b/numpy/lib/src/_compiled_base.c
@@ -161,14 +161,22 @@ arr_bincount(PyObject *NPY_UNUSED(self), PyObject *args, PyObject *kwds)
len = PyArray_SIZE(lst);
type = PyArray_DescrFromType(NPY_INTP);
- /* handle empty list */
- if (len < 1) {
- if (mlength == Py_None) {
- minlength = 0;
- }
- else if (!(minlength = PyArray_PyIntAsIntp(mlength))) {
+ if (mlength == Py_None) {
+ minlength = 0;
+ }
+ else {
+ minlength = PyArray_PyIntAsIntp(mlength);
+ if (minlength <= 0) {
+ if (!PyErr_Occurred()) {
+ PyErr_SetString(PyExc_ValueError,
+ "minlength must be positive");
+ }
goto fail;
}
+ }
+
+ /* handle empty list */
+ if (len == 0) {
if (!(ans = (PyArrayObject *)PyArray_Zeros(1, &minlength, type, 0))){
goto fail;
}
@@ -185,15 +193,6 @@ arr_bincount(PyObject *NPY_UNUSED(self), PyObject *args, PyObject *kwds)
}
ans_size = mx + 1;
if (mlength != Py_None) {
- if (!(minlength = PyArray_PyIntAsIntp(mlength))) {
- goto fail;
- }
- if (minlength <= 0) {
- /* superfluous, but may catch incorrect usage */
- PyErr_SetString(PyExc_ValueError,
- "minlength must be positive");
- goto fail;
- }
if (ans_size < minlength) {
ans_size = minlength;
}
diff --git a/numpy/lib/tests/test_function_base.py b/numpy/lib/tests/test_function_base.py
index 399a5a308..59db23a83 100644
--- a/numpy/lib/tests/test_function_base.py
+++ b/numpy/lib/tests/test_function_base.py
@@ -6,7 +6,7 @@ import numpy as np
from numpy.testing import (
run_module_suite, TestCase, assert_, assert_equal, assert_array_equal,
assert_almost_equal, assert_array_almost_equal, assert_raises,
- assert_allclose, assert_array_max_ulp, assert_warns
+ assert_allclose, assert_array_max_ulp, assert_warns, assert_raises_regex
)
from numpy.random import rand
from numpy.lib import *
@@ -1546,6 +1546,23 @@ class TestBincount(TestCase):
y = np.bincount(x, minlength=5)
assert_array_equal(y, np.zeros(5, dtype=int))
+ def test_with_incorrect_minlength(self):
+ x = np.array([], dtype=int)
+ assert_raises_regex(TypeError, "an integer is required",
+ lambda: np.bincount(x, minlength="foobar"))
+ assert_raises_regex(ValueError, "must be positive",
+ lambda: np.bincount(x, minlength=-1))
+ assert_raises_regex(ValueError, "must be positive",
+ lambda: np.bincount(x, minlength=0))
+
+ x = np.arange(5)
+ assert_raises_regex(TypeError, "an integer is required",
+ lambda: np.bincount(x, minlength="foobar"))
+ assert_raises_regex(ValueError, "minlength must be positive",
+ lambda: np.bincount(x, minlength=-1))
+ assert_raises_regex(ValueError, "minlength must be positive",
+ lambda: np.bincount(x, minlength=0))
+
class TestInterp(TestCase):
def test_exceptions(self):