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

TST: Add test for bad recursive array-like with sequence #17814

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
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
26 changes: 26 additions & 0 deletions numpy/core/tests/test_array_coercion.py
Original file line number Diff line number Diff line change
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