Skip to content

Commit 055bcb3

Browse files
committed
BUG: Fix assert in nditer buffer setup
When using a buffered iteration, there was a new assert to check that strides were set up nicely to ensure that we are not missing dimension coalescing. However, when the strides are set up to track an index, then they were still set up with a 0 for length 1. I _think_ that is just unnecessary (i.e. the assert is correct to point it out). But, let's do this first for backporting at least. Signed-off-by: Sebastian Berg <[email protected]>
1 parent 1a9d731 commit 055bcb3

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

numpy/_core/src/multiarray/nditer_constr.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2077,8 +2077,14 @@ npyiter_find_buffering_setup(NpyIter *iter, npy_intp buffersize)
20772077
npy_intp *strides = NAD_STRIDES(axisdata);
20782078

20792079
for (int iop = 0; iop < nop; iop++) {
2080-
/* Check that we set things up nicely (if shape is ever 1) */
2081-
assert((axisdata->shape == 1) ? (prev_strides[iop] == strides[iop]) : 1);
2080+
/*
2081+
* Check that we set things up nicely so strides coalesc. Except
2082+
* for index operands, which currently disrupts coalescing.
2083+
* NOTE(seberg): presumably `npyiter_compute_index_strides` should
2084+
* not set the strides to 0, but this was safer for backporting.
2085+
*/
2086+
assert((axisdata->shape != 1) || (prev_strides[iop] == strides[iop])
2087+
|| (op_itflags[iop] & (NPY_ITER_C_INDEX|NPY_ITER_F_INDEX)));
20822088

20832089
if (op_single_stride_dims[iop] == idim) {
20842090
/* Best case: the strides still collapse for this operand. */

numpy/lib/tests/test_index_tricks.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,11 @@ def test_empty_array_unravel(self):
199199
with assert_raises(ValueError):
200200
np.unravel_index([1], (2, 1, 0))
201201

202+
def test_regression_size_1_index(self):
203+
# actually tests the nditer size one index tracking
204+
# regression test for gh-29690
205+
np.unravel_index(np.array([[1, 0, 1, 0]], dtype=np.uint32), (4,))
206+
202207
class TestGrid:
203208
def test_basic(self):
204209
a = mgrid[-1:1:10j]

0 commit comments

Comments
 (0)