Skip to content

Commit

Permalink
TST: Add test for bad recursive array-like with sequence
Browse files Browse the repository at this point in the history
This tests that the bug reported in numpygh-17785 is already fixed on
master.
  • Loading branch information
seberg committed Nov 21, 2020
1 parent b88b2c0 commit b4843a4
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions numpy/core/tests/test_array_coercion.py
Expand Up @@ -689,3 +689,29 @@ def test_too_large_array_error_paths(self):
np.array(arr)
with pytest.raises(MemoryError):
np.array([arr])

def test_bad_array_attribute_and_length(self):
# See gh-17785, this test may be be tricky, since there are some
# paths where execution may continue after a RecursionError was
# raised first (these _should_ be harmless, but it is hard to tell).
class MyArray(np.ndarray):
def __new__(cls, input_array):
obj = np.empty(len(input_array), dtype=object)
obj[:] = input_array
obj = obj.view(cls)
return obj

class MyNastyClass:
def __len__(self): return 32
def __getitem__(self, item): return 1
def __array__(self, dtype=None): return MyArray([self])

try:
np.array(MyNastyClass())
except RecursionError:
# A RecurionError is expected here, but if the recursion occurs
# at specific points (probably while checking for sequences) it
# may be that the creation actually succeeds (and may be fine).
# At the time of writing this, something like this was apparently
# happening on PyPy.
pass

0 comments on commit b4843a4

Please sign in to comment.