8000 Merge pull request #18197 from seberg/array-protocol-errors · numpy/numpy@72be9ca · GitHub
[go: up one dir, main page]

Skip to content

Commit 72be9ca

Browse files
authored
Merge pull request #18197 from seberg/array-protocol-errors
BUG: Keep ignoring most errors during array-protocol lookup
2 parents f10da39 + a054ffd commit 72be9ca

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

numpy/core/src/multiarray/ctors.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2124,7 +2124,16 @@ PyArray_FromInterface(PyObject *origin)
21242124

21252125
if (iface == NULL) {
21262126
if (PyErr_Occurred()) {
2127-
return NULL;
2127+
if (PyErr_ExceptionMatches(PyExc_RecursionError) ||
2128+
PyErr_ExceptionMatches(PyExc_MemoryError)) {
2129+
/* RecursionError and MemoryError are considered fatal */
2130+
return NULL;
2131+
}
2132+
/*
2133+
* This probably be deprecated, but at least shapely raised
2134+
* a NotImplementedError expecting it to be cleared (gh-17965)
2135+
*/
2136+
PyErr_Clear();
21282137
}
21292138
return Py_NotImplemented;
21302139
}
@@ -2392,7 +2401,13 @@ PyArray_FromArrayAttr(PyObject *op, PyArray_Descr *typecode, PyObject *context)
23922401
array_meth = PyArray_LookupSpecial_OnInstance(op, "__array__");
23932402
if (array_meth == NULL) {
23942403
if (PyErr_Occurred()) {
2395-
return NULL;
2404+
if (PyErr_ExceptionMatches(PyExc_RecursionError) ||
2405+
PyErr_ExceptionMatches(PyExc_MemoryError)) {
2406+
/* RecursionError and MemoryError are considered fatal */
2407+
return NULL;
2408+
}
2409+
/* This probably be deprecated. */
2410+
PyErr_Clear();
23962411
}
23972412
return Py_NotImplemented;
23982413
}

numpy/core/tests/test_array_coercion.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -702,17 +702,19 @@ def test_too_large_array_error_paths(self):
702702

703703
@pytest.mark.parametrize("attribute",
704704
["__array_interface__", "__array__", "__array_struct__"])
705-
def test_bad_array_like_attributes(self, attribute):
706-
# Check that errors during attribute retrieval are raised unless
707-
# they are Attribute errors.
705+
@pytest.mark.parametrize("error", [RecursionError, MemoryError])
706+
def test_bad_array_like_attributes(self, attribute, error):
707+
# RecursionError and MemoryError are considered fatal. All errors
708+
# (except AttributeError) should probably be raised in the future,
709+
# but shapely made use of it, so it will require a deprecation.
708710

709711
class BadInterface:
710712
def __getattr__(self, attr):
711713
if attr == attribute:
712-
raise RuntimeError
714+
raise error
713715
super().__getattr__(attr)
714716

715-
with pytest.raises(RuntimeError):
717+
with pytest.raises(error):
716718
np.array(BadInterface())
717719

718720
@pytest.mark.parametrize("error", [RecursionError, MemoryError])

0 commit comments

Comments
 (0)
0