summaryrefslogtreecommitdiff
path: root/numpy
diff options
context:
space:
mode:
Diffstat (limited to 'numpy')
-rw-r--r--numpy/core/src/umath/ufunc_object.c4
-rw-r--r--numpy/core/tests/test_ufunc.py8
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)])