diff options
author | Andrew Murray <radarhere@users.noreply.github.com> | 2022-01-17 19:31:18 +1100 |
---|---|---|
committer | Andrew Murray <radarhere@users.noreply.github.com> | 2022-01-17 19:31:18 +1100 |
commit | 9725ce49b37219ddeb46e1a48e03cd08eb5dda4e (patch) | |
tree | 5e640b8d8937c80b16d31ecc9dc53502534d0d11 | |
parent | bac5117461e86034c569aa8b8603d6c4ad47d187 (diff) | |
download | numpy-9725ce49b37219ddeb46e1a48e03cd08eb5dda4e.tar.gz |
DEP: Removed deprecated error clearing
-rw-r--r-- | doc/release/upcoming_changes/20835.expired.rst | 7 | ||||
-rw-r--r-- | numpy/core/src/multiarray/ctors.c | 52 | ||||
-rw-r--r-- | numpy/core/tests/test_array_coercion.py | 19 | ||||
-rw-r--r-- | numpy/core/tests/test_deprecations.py | 18 |
4 files changed, 28 insertions, 68 deletions
diff --git a/doc/release/upcoming_changes/20835.expired.rst b/doc/release/upcoming_changes/20835.expired.rst new file mode 100644 index 000000000..40d4f63c1 --- /dev/null +++ b/doc/release/upcoming_changes/20835.expired.rst @@ -0,0 +1,7 @@ +Exceptions will be raised during array-like creation +---------------------------------------------------- + +When an object raised an exception during access of the special +attributes ``__array__`` or ``__array_interface__``, this exception +was usually ignored. +This behaviour was deprecated in 1.21, and the exception will now be raised.
\ No newline at end of file diff --git a/numpy/core/src/multiarray/ctors.c b/numpy/core/src/multiarray/ctors.c index 17a49091a..75fef3149 100644 --- a/numpy/core/src/multiarray/ctors.c +++ b/numpy/core/src/multiarray/ctors.c @@ -2163,38 +2163,6 @@ _is_default_descr(PyObject *descr, PyObject *typestr) { } -/* - * A helper function to transition away from ignoring errors during - * special attribute lookups during array coercion. - */ -static NPY_INLINE int -deprecated_lookup_error_clearing(PyTypeObject *type, char *attribute) -{ - PyObject *exc_type, *exc_value, *traceback; - PyErr_Fetch(&exc_type, &exc_value, &traceback); - - /* DEPRECATED 2021-05-12, NumPy 1.21. */ - int res = PyErr_WarnFormat(PyExc_DeprecationWarning, 1, - "An exception was ignored while fetching the attribute `%s` from " - "an object of type '%s'. With the exception of `AttributeError` " - "NumPy will always raise this exception in the future. Raise this " - "deprecation warning to see the original exception. " - "(Warning added NumPy 1.21)", attribute, type->tp_name); - - if (res < 0) { - npy_PyErr_ChainExceptionsCause(exc_type, exc_value, traceback); - return -1; - } - else { - /* `PyErr_Fetch` cleared the original error, delete the references */ - Py_DECREF(exc_type); - Py_XDECREF(exc_value); - Py_XDECREF(traceback); - return 0; - } -} - - /*NUMPY_API*/ NPY_NO_EXPORT PyObject * PyArray_FromInterface(PyObject *origin) @@ -2214,15 +2182,7 @@ PyArray_FromInterface(PyObject *origin) if (iface == NULL) { if (PyErr_Occurred()) { - if (PyErr_ExceptionMatches(PyExc_RecursionError) || - PyErr_ExceptionMatches(PyExc_MemoryError)) { - /* RecursionError and MemoryError are considered fatal */ - return NULL; - } - if (deprecated_lookup_error_clearing( - Py_TYPE(origin), "__array_interface__") < 0) { - return NULL; - } + return NULL; } return Py_NotImplemented; } @@ -2502,15 +2462,7 @@ PyArray_FromArrayAttr_int( array_meth = PyArray_LookupSpecial_OnInstance(op, "__array__"); if (array_meth == NULL) { if (PyErr_Occurred()) { - if (PyErr_ExceptionMatches(PyExc_RecursionError) || - PyErr_ExceptionMatches(PyExc_MemoryError)) { - /* RecursionError and MemoryError are considered fatal */ - return NULL; - } - if (deprecated_lookup_error_clearing( - Py_TYPE(op), "__array__") < 0) { - return NULL; - } + return NULL; } return Py_NotImplemented; } diff --git a/numpy/core/tests/test_array_coercion.py b/numpy/core/tests/test_array_coercion.py index 293f5a68f..d349f9d02 100644 --- a/numpy/core/tests/test_array_coercion.py +++ b/numpy/core/tests/test_array_coercion.py @@ -746,3 +746,22 @@ class TestArrayLikes: with pytest.raises(error): np.array(BadSequence()) + +class TestSpecialAttributeLookupFailure: + # An exception was raised while fetching the attribute + + class WeirdArrayLike: + @property + def __array__(self): + raise RuntimeError("oops!") + + class WeirdArrayInterface: + @property + def __array_interface__(self): + raise RuntimeError("oops!") + + def test_deprecated(self): + with pytest.raises(RuntimeError): + np.array(self.WeirdArrayLike()) + with pytest.raises(RuntimeError): + np.array(self.WeirdArrayInterface()) diff --git a/numpy/core/tests/test_deprecations.py b/numpy/core/tests/test_deprecations.py index d2a69d4cf..89fcc48bb 100644 --- a/numpy/core/tests/test_deprecations.py +++ b/numpy/core/tests/test_deprecations.py @@ -1094,24 +1094,6 @@ class TestComparisonBadObjectDType(_DeprecationTestCase): lambda: np.equal(1, 1, sig=(None, None, object))) -class TestSpecialAttributeLookupFailure(_DeprecationTestCase): - message = r"An exception was ignored while fetching the attribute" - - class WeirdArrayLike: - @property - def __array__(self): - raise RuntimeError("oops!") - - class WeirdArrayInterface: - @property - def __array_interface__(self): - raise RuntimeError("oops!") - - def test_deprecated(self): - self.assert_deprecated(lambda: np.array(self.WeirdArrayLike())) - self.assert_deprecated(lambda: np.array(self.WeirdArrayInterface())) - - class TestCtypesGetter(_DeprecationTestCase): # Deprecated 2021-05-18, Numpy 1.21.0 warning_cls = DeprecationWarning |