diff options
| -rw-r--r-- | numpy/core/src/multiarray/multiarraymodule.c | 8 | ||||
| -rw-r--r-- | numpy/core/tests/test_multiarray.py | 20 |
2 files changed, 27 insertions, 1 deletions
diff --git a/numpy/core/src/multiarray/multiarraymodule.c b/numpy/core/src/multiarray/multiarraymodule.c index f206ce2c6..831e68705 100644 --- a/numpy/core/src/multiarray/multiarraymodule.c +++ b/numpy/core/src/multiarray/multiarraymodule.c @@ -129,12 +129,18 @@ PyArray_GetPriority(PyObject *obj, double default_) ret = PyArray_LookupSpecial_OnInstance(obj, "__array_priority__"); if (ret == NULL) { if (PyErr_Occurred()) { - PyErr_Clear(); /* TODO[gh-14801]: propagate crashes during attribute access? */ + /* TODO[gh-14801]: propagate crashes during attribute access? */ + PyErr_Clear(); } return default_; } priority = PyFloat_AsDouble(ret); + if (error_converting(priority)) { + /* TODO[gh-14801]: propagate crashes for bad priority? */ + PyErr_Clear(); + return default_; + } Py_DECREF(ret); return priority; } diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py index 6ba90a97f..64b173051 100644 --- a/numpy/core/tests/test_multiarray.py +++ b/numpy/core/tests/test_multiarray.py @@ -3694,6 +3694,26 @@ class TestBinop: check(make_obj(np.ndarray, array_ufunc=None), True, False, False, check_scalar=False) + def test_ufunc_binop_bad_array_priority(self): + # Mainly checks that this does not crash. The second array has a lower + # priority than -1 ("error value"). If the __radd__ actually exists, + # bad things can happen (I think via the scalar paths). + class BadPriority(): + __array_priority__ = None + + def __radd__(self, other): + return "result" + + class LowPriority(np.ndarray): + __array_priority__ = -1000 + + # Priority failure uses the same as scalars (smaller -1000). So the + # LowPriority wins with 'result' for each element (inner operation). + res = np.arange(3).view(LowPriority) + BadPriority() + assert res.shape == (3,) + assert res[0] == 'result' + + def test_ufunc_override_normalize_signature(self): # gh-5674 class SomeClass: |
