Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DEP: Deprecate error clearing for special method in array-coercion #19001

Merged
merged 4 commits into from
May 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions doc/release/upcoming_changes/19001.deprecation.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
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.
A warning is now given when the exception is anything but AttributeError.
To silence the warning, the type raising the exception has to be adapted
to raise an ``AttributeError``.
48 changes: 41 additions & 7 deletions numpy/core/src/multiarray/ctors.c
Original file line number Diff line number Diff line change
Expand Up @@ -2100,6 +2100,39 @@ _is_default_descr(PyObject *descr, PyObject *typestr) {
return PyObject_RichCompareBool(typestr, typestr2, Py_EQ);
}


/*
* 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)
Expand All @@ -2124,11 +2157,10 @@ PyArray_FromInterface(PyObject *origin)
/* RecursionError and MemoryError are considered fatal */
return NULL;
}
/*
* This probably be deprecated, but at least shapely raised
* a NotImplementedError expecting it to be cleared (gh-17965)
*/
PyErr_Clear();
if (deprecated_lookup_error_clearing(
Py_TYPE(origin), "__array_interface__") < 0) {
return NULL;
}
}
return Py_NotImplemented;
}
Expand Down Expand Up @@ -2401,8 +2433,10 @@ PyArray_FromArrayAttr(PyObject *op, PyArray_Descr *typecode, PyObject *context)
/* RecursionError and MemoryError are considered fatal */
return NULL;
}
/* This probably be deprecated. */
PyErr_Clear();
if (deprecated_lookup_error_clearing(
Py_TYPE(op), "__array__") < 0) {
return NULL;
}
}
return Py_NotImplemented;
}
Expand Down
18 changes: 18 additions & 0 deletions numpy/core/tests/test_deprecations.py
Original file line number Diff line number Diff line change
Expand Up @@ -1174,6 +1174,24 @@ def test_deprecated(self):
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
Expand Down