Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions numpy/_core/src/multiarray/compiled_base.c
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,12 @@ arr_bincount(PyObject *NPY_UNUSED(self), PyObject *const *args,
}
if (PyArray_SIZE(tmp1) > 0) {
/* The input is not empty, so convert it to NPY_INTP. */
lst = (PyArrayObject *)PyArray_ContiguousFromAny((PyObject *)tmp1,
NPY_INTP, 1, 1);
int flags = NPY_ARRAY_WRITEABLE | NPY_ARRAY_ALIGNED | NPY_ARRAY_C_CONTIGUOUS;
if (PyArray_ISINTEGER(tmp1)) {
flags = flags | NPY_ARRAY_FORCECAST;
}
PyArray_Descr* local_dtype = PyArray_DescrFromType(NPY_INTP);
lst = (PyArrayObject *)PyArray_FromAny((PyObject *)tmp1, local_dtype, 1, 1, flags, NULL);
Py_DECREF(tmp1);
if (lst == NULL) {
/* Failed converting to NPY_INTP. */
Expand All @@ -177,7 +181,13 @@ arr_bincount(PyObject *NPY_UNUSED(self), PyObject *const *args,
}

if (lst == NULL) {
lst = (PyArrayObject *)PyArray_ContiguousFromAny(list, NPY_INTP, 1, 1);
int flags = NPY_ARRAY_WRITEABLE | NPY_ARRAY_ALIGNED | NPY_ARRAY_C_CONTIGUOUS;
if (PyArray_Check((PyObject *)list) &&
PyArray_ISINTEGER((PyArrayObject *)list)) {
flags = flags | NPY_ARRAY_FORCECAST;
}
PyArray_Descr* local_dtype = PyArray_DescrFromType(NPY_INTP);
lst = (PyArrayObject *)PyArray_FromAny(list, local_dtype, 1, 1, flags, NULL);
if (lst == NULL) {
goto fail;
}
Expand Down
21 changes: 21 additions & 0 deletions numpy/lib/tests/test_function_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2925,6 +2925,27 @@ def test_error_not_1d(self, vals):
with assert_raises(ValueError):
np.bincount(vals)

@pytest.mark.parametrize("dt", np.typecodes["AllInteger"])
def test_gh_28354(self, dt):
a = np.array([0, 1, 1, 3, 2, 1, 7], dtype=dt)
actual = np.bincount(a)
expected = [1, 3, 1, 1, 0, 0, 0, 1]
assert_array_equal(actual, expected)

def test_contiguous_handling(self):
# check for absence of hard crash
np.bincount(np.arange(10000)[::2])

def test_gh_28354_array_like(self):
class A:
def __array__(self):
return np.array([0, 1, 1, 3, 2, 1, 7], dtype=np.uint64)

a = A()
actual = np.bincount(a)
expected = [1, 3, 1, 1, 0, 0, 0, 1]
assert_array_equal(actual, expected)


class TestInterp:

Expand Down
Loading