Skip to content

Commit

Permalink
Merge pull request #18546 from ahaldane/backport_18530
Browse files Browse the repository at this point in the history
BUG: incorrect error fallthrough in nditer
  • Loading branch information
charris committed Mar 6, 2021
2 parents 69b4fbb + 33398ca commit b83d574
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
7 changes: 6 additions & 1 deletion numpy/core/src/umath/ufunc_object.c
Expand Up @@ -3525,7 +3525,7 @@ reduce_loop(NpyIter *iter, char **dataptrs, npy_intp const *strides,
strides_copy, innerloopdata);

if (needs_api && PyErr_Occurred()) {
break;
goto finish_loop;
}

/* Jump to the faster loop when skipping is done */
Expand All @@ -3539,6 +3539,11 @@ reduce_loop(NpyIter *iter, char **dataptrs, npy_intp const *strides,
}
} while (iternext(iter));
}

if (needs_api && PyErr_Occurred()) {
goto finish_loop;
}

do {
/* Turn the two items into three for the inner loop */
dataptrs_copy[0] = dataptrs[0];
Expand Down
8 changes: 8 additions & 0 deletions numpy/core/tests/test_nditer.py
Expand Up @@ -2738,6 +2738,14 @@ def test_object_iter_cleanup():
oarr[:, -1] = None
assert_raises(TypeError, lambda: np.add(oarr[:, ::-1], arr[:, ::-1]))

# followup: this tests for a bug introduced in the first pass of gh-18450,
# caused by an incorrect fallthrough of the TypeError
class T:
def __bool__(self):
raise TypeError("Ambiguous")
assert_raises(TypeError, np.logical_or.reduce,
np.array([T(), T()], dtype='O'))

def test_iter_too_large():
# The total size of the iterator must not exceed the maximum intp due
# to broadcasting. Dividing by 1024 will keep it small enough to
Expand Down

0 comments on commit b83d574

Please sign in to comment.