8000 BUG: safer bincount casting by tylerjereddy · Pull Request #28355 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: safer bincount casting #28355

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 3, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

8000
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
MAINT, BUG: PR 28355 revisions
* Based on reviewer feedback, narrow the scope of the `flags`
variable in `arr_bincount()`.

* Based on reviewer feedback, add an array-like test for the `uint64`
casting issue, which indeed fails before and passes after adding
a similar shim to the array-like code path in `arr_bincount()`.

* Based on reviewer feedback, switch the patching from `PyArray_Size`
to `PyArray_Check` in a few places.
  • Loading branch information
tylerjereddy committed Feb 28, 2025
commit 41b3df1661898c791ffe3ed5d41b3c8694e5b2aa
14 changes: 9 additions & 5 deletions numpy/_core/src/multiarray/compiled_base.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ arr_bincount(PyObject *NPY_UNUSED(self), PyObject *const *args,
npy_intp *numbers, *ians, len, mx, mn, ans_size;
npy_intp minlength = 0;
npy_intp i;
int flags;
double *weights , *dans;

NPY_PREPARE_ARGPARSER;
Expand Down Expand Up @@ -151,8 +150,13 @@ 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_Check((PyObject *)tmp1) &&
PyArray_ISINTEGER((PyArrayObject *)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 @@ -178,8 +182,8 @@ arr_bincount(PyObject *NPY_UNUSED(self), PyObject *const *args,
}

if (lst == NULL) {
flags = NPY_ARRAY_WRITEABLE | NPY_ARRAY_ALIGNED | NPY_ARRAY_C_CONTIGUOUS;
if (PyArray_Size((PyObject *)list) &&
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;
}
Expand Down
10 changes: 10 additions & 0 deletions numpy/lib/tests/test_function_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2970,6 +2970,16 @@ 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
0