Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 8530c81

Browse filesBrowse files
committed
BUG: safer bincount casting
* Fixes #28354 * Force usage of `npy_intp` type in `np.bincount()` and avoid unsafe casting errors with i.e., `npy_uint64`. This is similar to our behavior with indexing.
1 parent 5566cc4 commit 8530c81
Copy full SHA for 8530c81

File tree

2 files changed

+12
-0
lines changed
Filter options

2 files changed

+12
-0
lines changed

‎numpy/_core/src/multiarray/compiled_base.c

Copy file name to clipboardExpand all lines: numpy/_core/src/multiarray/compiled_base.c
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,11 @@ arr_bincount(PyObject *NPY_UNUSED(self), PyObject *const *args,
177177
}
178178

179179
if (lst == NULL) {
180+
PyArray_Descr* local_dtype = PyArray_DescrFromType(NPY_INTP);
181+
list = PyArray_FromAny(list, local_dtype, 0, 0, NPY_ARRAY_FORCECAST, NULL);
182+
if (list == NULL) {
183+
goto fail;
184+
}
180185
lst = (PyArrayObject *)PyArray_ContiguousFromAny(list, NPY_INTP, 1, 1);
181186
if (lst == NULL) {
182187
goto fail;

‎numpy/lib/tests/test_function_base.py

Copy file name to clipboardExpand all lines: numpy/lib/tests/test_function_base.py
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2959,6 +2959,13 @@ def test_error_not_1d(self, vals):
29592959
with assert_raises(ValueError):
29602960
np.bincount(vals)
29612961

2962+
@pytest.mark.parametrize("dt", np.typecodes["AllInteger"])
2963+
def test_gh_28354(self, dt):
2964+
a = np.array([0, 1, 1, 3, 2, 1, 7], dtype=dt)
2965+
actual = np.bincount(a)
2966+
expected = [1, 3, 1, 1, 0, 0, 0, 1]
2967+
assert_array_equal(actual, expected)
2968+
29622969

29632970
class TestInterp:
29642971

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.