8000 MAINT, BUG: PR 28355 revisions · numpy/numpy@f7046ed · GitHub
[go: up one dir, main page]

Skip to content

Commit f7046ed

Browse files
committed
MAINT, BUG: PR 28355 revisions
* `arr_bincount()` now only uses unsafe casting for integer input types, and the number of casting operations has been reduced for the code path used in above PR. * a test for non-crashing behavior with non-contiguous `bincount()` input has been added.
1 parent 6948e3d commit f7046ed

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

numpy/_core/src/multiarray/compiled_base.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ arr_bincount(PyObject *NPY_UNUSED(self), PyObject *const *args,
119119
npy_intp *numbers, *ians, len, mx, mn, ans_size;
120120
npy_intp minlength = 0;
121121
npy_intp i;
122+
int flags;
122123
double *weights , *dans;
123124

124125
NPY_PREPARE_ARGPARSER;
@@ -177,12 +178,13 @@ arr_bincount(PyObject *NPY_UNUSED(self), PyObject *const *args,
177178
}
178179

179180
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;
181+
flags = NPY_ARRAY_WRITEABLE | NPY_ARRAY_ALIGNED | NPY_ARRAY_C_CONTIGUOUS;
182+
if (PyArray_Size((PyObject *)list) &&
183+
PyArray_ISINTEGER((PyArrayObject *)list)) {
184+
flags = flags | NPY_ARRAY_FORCECAST;
184185
}
185-
lst = (PyArrayObject *)PyArray_ContiguousFromAny(list, NPY_INTP, 1, 1);
186+
PyArray_Descr* local_dtype = PyArray_DescrFromType(NPY_INTP);
187+
lst = (PyArrayObject *)PyArray_FromAny(list, local_dtype, 1, 1, flags, NULL);
186188
if (lst == NULL) {
187189
goto fail;
188190
}

numpy/lib/tests/test_function_base.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2966,6 +2966,10 @@ def test_gh_28354(self, dt):
29662966
expected = [1, 3, 1, 1, 0, 0, 0, 1]
29672967
assert_array_equal(actual, expecte 6270 d)
29682968

2969+
def test_contiguous_handling(self):
2970+
# check for absence of hard crash
2971+
np.bincount(np.arange(10000)[::2])
2972+
29692973

29702974
class TestInterp:
29712975

0 commit comments

Comments
 (0)
0