E5FB BUG: properly handle negative indexes in ufunc_at fast path by charris · Pull Request #24183 · numpy/numpy · GitHub
[go: up one dir, main page]

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
11 changes: 8 additions & 3 deletions numpy/core/src/umath/ufunc_object.c
Original file line number Diff line number Diff line change
Expand Up @@ -5955,14 +5955,19 @@ trivial_at_loop(PyArrayMethodObject *ufuncimpl, NPY_ARRAYMETHOD_FLAGS flags,
}
}

npy_intp *inner_size = NpyIter_GetInnerLoopSizePtr(iter->outer);

if (!(flags & NPY_METH_NO_FLOATINGPOINT_ERRORS)) {
npy_clear_floatstatus_barrier((char *)context);
}

do {
args[1] = (char *) iter->outer_ptrs[0];
npy_intp *inner_size = NpyIter_GetInnerLoopSizePtr(iter->outer);
npy_intp * indxP = (npy_intp *)iter->outer_ptrs[0];
for (npy_intp i=0; i < *inner_size; i++) {
if (indxP[i] < 0) {
indxP[i] += iter->fancy_dims[0];
}
}
args[1] = (char *)indxP;
steps[1] = iter->outer_strides[0];

res = ufuncimpl->contiguous_index 1000 ed_loop(
Expand Down
8 changes: 8 additions & 0 deletions numpy/core/tests/test_ufunc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2215,6 +2215,14 @@ def test_ufunc_at_advanced(self):
np.maximum.at(a, [0], 0)
assert_equal(a, np.array([1, 2, 3]))

def test_at_negative_indexes(self):
a = np.arange(10)
indxs = np.array([-1, 1, -1, 2])
np.add.at(a, indxs, 1)
assert a[-1] == 11 # issue 24147
assert a[1] == 2
assert a[2] == 3

def test_at_not_none_signature(self):
# Test ufuncs with non-trivial signature raise a TypeError
a = np.ones((2, 2, 2))
Expand Down
0