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 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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 @@
Errors will be raised during array-like creation
------------------------------------------------
When an object raised an error during access of the special
attributes ``__array__`` or ``__array_interface__``, this error
was usually ignored.
A warning is now given when the error is anything but AttributeError.
To silence the warning, the type raising a warning has to be adapted
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
To silence the warning, the type raising a warning has to be adapted
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 @@ -2105,6 +2105,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 error was ignored while fetching the attribute `%s` from an "
"object of type '%s'. With the exception of `AttributeError` "
"NumPy will always raise this error in the future. Raise this "
"deprecation warning to see the original error. "
"(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 @@ -2129,11 +2162,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 @@ -2406,8 +2438,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 @@ -1175,3 +1175,21 @@ def test_deprecated(self):
self.assert_deprecated(lambda: np.equal(1, 1, dtype=object))
self.assert_deprecated(
lambda: np.equal(1, 1, sig=(None, None, object)))


class TestSpecialAttributeLookupFailure(_DeprecationTestCase):
message = r"An error 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()))