Skip to content

Commit c2ad604

Browse files
authored
Merge pull request numpy#19908 from seberg/issue-19904
BUG: Check whether an error is already set for invalid casting
2 parents e467a28 + 0824a54 commit c2ad604

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

numpy/core/src/umath/ufunc_object.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,8 +1062,8 @@ check_for_trivial_loop(PyArrayMethodObject *ufuncimpl,
10621062
if (dtypes[i] != PyArray_DESCR(op[i])) {
10631063
NPY_CASTING safety = PyArray_GetCastSafety(
10641064
PyArray_DESCR(op[i]), dtypes[i], NULL);
1065-
if (safety < 0) {
1066-
/* A proper error during a cast check should be rare */
1065+
if (safety < 0 && PyErr_Occurred()) {
1066+
/* A proper error during a cast check, should be rare */
10671067
return -1;
10681068
}
10691069
if (!(safety & _NPY_CAST_IS_VIEW)) {

numpy/core/tests/test_ufunc.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2308,6 +2308,14 @@ def test_ufunc_casterrors():
23082308
assert out[-1] == 1
23092309

23102310

2311+
def test_trivial_loop_invalid_cast():
2312+
# This tests the fast-path "invalid cast", see gh-19904.
2313+
with pytest.raises(TypeError,
2314+
match="cast ufunc 'add' input 0"):
2315+
# the void dtype definitely cannot cast to double:
2316+
np.add(np.array(1, "i,i"), 3, signature="dd->d")
2317+
2318+
23112319
@pytest.mark.skipif(not HAS_REFCOUNT, reason="Python lacks refcounts")
23122320
@pytest.mark.parametrize("offset",
23132321
[0, np.BUFSIZE//2, int(1.5*np.BUFSIZE)])

0 commit comments

Comments
 (0)