10000 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

BUG: properly handle negative indexes in ufunc_at fast path #24183

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 1 commit into from
Jul 14, 2023
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_indexed_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