Skip to content

Commit 8ea2a76

Browse files
committed
BUG: Allow any integer type in bincount (fixes numpy#823)
Gives array-likes that cannot be cast to np.intp a second chance, by comparing its entries one by one to NPY_INTP_MAX and forcing the cast if none exceeds it. Fixes numpy#823.
1 parent 8ce3640 commit 8ea2a76

2 files changed

Lines changed: 97 additions & 3 deletions

File tree

‎numpy/lib/src/_compiled_base.c‎

Lines changed: 87 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,69 @@ minmax(const npy_intp *data, npy_intp data_len, npy_intp *mn, npy_intp *mx)
125125
*mn = min;
126126
*mx = max;
127127
}
128+
129+
/*
130+
* Check if a contiguous array, to be used as first argument to bincount,
131+
* can be cast to npy_intp safely
132+
*/
133+
static int
134+
fits_in_intp(PyArrayObject *arr)
135+
{
136+
PyArray_Descr *dtype = PyArray_DESCR(arr);
137+
PyArray_CompareFunc *cmp = dtype->f->compare;
138+
PyArrayObject *arr_cmp1, *arr_cmp2;
139+
npy_intp data[2] = {0, NPY_MAX_INTP};
140+
npy_intp shape = 2;
141+
npy_intp stride = dtype->elsize;
142+
npy_intp size = PyArray_SIZE(arr);
143+
npy_bool dtype_is_signed = PyDataType_ISSIGNED(dtype);
144+
const void *a = (const void *)PyArray_DATA(arr);
145+
const void *min, *max;
146+
147+
if (!PyDataType_ISINTEGER(dtype)) {
148+
PyErr_SetString(PyExc_TypeError,
149+
"The first argument of bincount must be of integer type");
150+
return 0;
151+
}
152+
153+
/*
154+
* Create an array of npy_intp type, with two items equal to 0 and
155+
* NPY_MAX_INTP and cast it to 'dtype'
156+
*/
157+
arr_cmp1 = (PyArrayObject *)PyArray_SimpleNewFromData(1, &shape, NPY_INTP,
158+
(void *)&data);
159+
if (!arr_cmp1) {
160+
return 0;
161+
}
162+
163+
Py_INCREF(dtype);
164+
arr_cmp2 = (PyArrayObject *)PyArray_CastToType(arr_cmp1, dtype, 0);
165+
Py_DECREF(arr_cmp1);
166+
if (!arr_cmp2) {
167+
return 0;
168+
}
169+
170+
min = (const void *)PyArray_DATA(arr_cmp2);
171+
max = min + stride;
172+
while (size--) {
173+
if(dtype_is_signed && cmp(a, min, arr) < 0) {
174+
PyErr_SetString(PyExc_ValueError,
175+
"The first argument of bincount must be non-negative");
176+
Py_DECREF(arr_cmp2);
177+
return 0;
178+
}
179+
else if (cmp(a, max, arr) > 0) {
180+
PyErr_SetString(PyExc_ValueError,
181+
"Value too large in first argument to bincount");
182+
Py_DECREF(arr_cmp2);
183+
return 0;
184+
}
185+
}
186+
187+
Py_DECREF(arr_cmp2);
188+
return 1;
189+
}
190+
128191
/*
129192
* arr_bincount is registered as bincount.
130193
*
@@ -142,8 +205,8 @@ static PyObject *
142205
arr_bincount(PyObject *NPY_UNUSED(self), PyObject *args, PyObject *kwds)
143206
{
144207
PyArray_Descr *type;
145-
PyObject *list = NULL, *weight=Py_None, *mlength=Py_None;
146-
PyArrayObject *lst=NULL, *ans=NULL, *wts=NULL;
208+
PyObject *list = NULL, *weight = Py_None, *mlength = Py_None;
209+
PyArrayObject *lst = NULL, *ans = NULL, *wts = NULL, *lst_bis = NULL;
147210
npy_intp *numbers, *ians, len , mx, mn, ans_size, minlength;
148211
int i;
149212
double *weights , *dans;
@@ -156,7 +219,27 @@ arr_bincount(PyObject *NPY_UNUSED(self), PyObject *args, PyObject *kwds)
156219

157220
lst = (PyArrayObject *)PyArray_ContiguousFromAny(list, NPY_INTP, 1, 1);
158221
if (lst == NULL) {
159-
goto fail;
222+
/*
223+
* Clear the error set by PyArray_ContiguousFromAny, and give it a
224+
* second chance if it is of integer type and the values are in bounds
225+
*/
226+
PyErr_Clear();
227+
lst_bis = (PyArrayObject *)PyArray_FromAny(list, NULL, 1, 1,
228+
NPY_ARRAY_CARRAY, NULL);
229+
if (lst_bis == NULL) {
230+
goto fail;
231+
}
232+
if (!fits_in_intp(lst_bis)) {
233+
Py_DECREF(lst_bis);
234+
goto fail;
235+
}
236+
Py_DECREF(lst_bis);
237+
lst = (PyArrayObject *)PyArray_FROMANY(list, NPY_INTP, 1, 1,
238+
NPY_ARRAY_CARRAY |
239+
NPY_ARRAY_FORCECAST);
240+
if (lst == NULL) {
241+
goto fail;
242+
}
160243
}
161244
len = PyArray_SIZE(lst);
162245
type = PyArray_DescrFromType(NPY_INTP);
@@ -236,6 +319,7 @@ arr_bincount(PyObject *NPY_UNUSED(self), PyObject *args, PyObject *kwds)
236319
Py_DECREF(lst);
237320
Py_DECREF(wts);
238321
}
322+
239323
return (PyObject *)ans;
240324

241325
fail:

‎numpy/lib/tests/test_function_base.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1461,6 +1461,16 @@ def test_empty_with_minlength(self):
14611461
y = np.bincount(x, minlength=5)
14621462
assert_array_equal(y, np.zeros(5, dtype=int))
14631463

1464+
def test_big_int_types(self):
1465+
x = np.arange(5, dtype=np.int64);
1466+
y = np.bincount(x)
1467+
assert_array_equal(y, np.array([1, 1, 1, 1, 1]))
1468+
x = np.arange(5, dtype=np.uint64);
1469+
y = np.bincount(x)
1470+
assert_array_equal(y, np.array([1, 1, 1, 1, 1]))
1471+
x = [np.iinfo(np.intp).max + 1]
1472+
assert_raises(ValueError, np.bincount, x)
1473+
14641474

14651475
class TestInterp(TestCase):
14661476
def test_exceptions(self):

0 commit comments

Comments
 (0)