diff options
| author | Sebastian Berg <sebastian@sipsolutions.net> | 2021-09-20 13:55:42 -0500 |
|---|---|---|
| committer | Sebastian Berg <sebastian@sipsolutions.net> | 2021-09-20 13:56:21 -0500 |
| commit | 0824a5455f8392997911c6d1f2f707e919a8175a (patch) | |
| tree | 201e820644466e89b58d719230e740edab3031da | |
| parent | 4e09f754e67765a38d2748af3e57f4a4a8006c19 (diff) | |
| download | numpy-0824a5455f8392997911c6d1f2f707e919a8175a.tar.gz | |
BUG: Check whether an error is already set for invalid casting
If no error is set already, we can just continue normally (which
sets the cast error later).
Closes gh-19904
| -rw-r--r-- | numpy/core/src/umath/ufunc_object.c | 4 | ||||
| -rw-r--r-- | numpy/core/tests/test_ufunc.py | 8 |
2 files changed, 10 insertions, 2 deletions
diff --git a/numpy/core/src/umath/ufunc_object.c b/numpy/core/src/umath/ufunc_object.c index 7abcb9513..9c943566b 100644 --- a/numpy/core/src/umath/ufunc_object.c +++ b/numpy/core/src/umath/ufunc_object.c @@ -1062,8 +1062,8 @@ check_for_trivial_loop(PyArrayMethodObject *ufuncimpl, if (dtypes[i] != PyArray_DESCR(op[i])) { NPY_CASTING safety = PyArray_GetCastSafety( PyArray_DESCR(op[i]), dtypes[i], NULL); - if (safety < 0) { - /* A proper error during a cast check should be rare */ + if (safety < 0 && PyErr_Occurred()) { + /* A proper error during a cast check, should be rare */ return -1; } if (!(safety & _NPY_CAST_IS_VIEW)) { diff --git a/numpy/core/tests/test_ufunc.py b/numpy/core/tests/test_ufunc.py index c3ea10d93..30929ce91 100644 --- a/numpy/core/tests/test_ufunc.py +++ b/numpy/core/tests/test_ufunc.py @@ -2308,6 +2308,14 @@ def test_ufunc_casterrors(): assert out[-1] == 1 +def test_trivial_loop_invalid_cast(): + # This tests the fast-path "invalid cast", see gh-19904. + with pytest.raises(TypeError, + match="cast ufunc 'add' input 0"): + # the void dtype definitely cannot cast to double: + np.add(np.array(1, "i,i"), 3, signature="dd->d") + + @pytest.mark.skipif(not HAS_REFCOUNT, reason="Python lacks refcounts") @pytest.mark.parametrize("offset", [0, np.BUFSIZE//2, int(1.5*np.BUFSIZE)]) |
