8000 BUG: cannot convert invalid sequence index to tuple by seberg · Pull Request #5066 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: cannot convert invalid sequence index to tuple #5066

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

Merged
merged 2 commits into from
Sep 13, 2014
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
7 changes: 5 additions & 2 deletions numpy/core/src/multiarray/mapping.c
Original file line number Diff line number Diff line change
Expand Up @@ -206,14 +206,17 @@ prepare_index(PyArrayObject *self, PyObject *index,
n = 0;
make_tuple = 1;
}
n = PySequence_Size(index);
else {
n = PySequence_Size(index);
}
if (n < 0 || n >= NPY_MAXDIMS) {
n = 0;
}
for (i = 0; i < n; i++) {
PyObject *tmp_obj = PySequence_GetItem(index, i);
if (tmp_obj == NULL) {
make_tuple = 1;
PyErr_Clear();
make_tuple = 0;
break;
}
if (PyArray_Check(tmp_obj) || PySequence_Check(tmp_obj)
Expand Down
33 changes: 32 additions & 1 deletion numpy/core/tests/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ def test_reverse_strides_and_subspace_bufferinit(self):
c = np.arange(10).reshape(5, 2)[::-1]
a[b, :] = c
assert_equal(a[0], [0, 1])

def test_reversed_strides_result_allocation(self):
# Test a bug when calculating the output strides for a result array
# when the subspace size was 1 (and test other cases as well)
Expand Down Expand Up @@ -367,6 +367,37 @@ def test_unaligned(self):
d[b % 2 == 0]
d[b % 2 == 0] = x[::2]

def test_tuple_subclass(self):
arr = np.ones((5, 5))

# A tuple subclass should also be an nd-index
class TupleSubclass(tuple):
pass
index = ([1], [1])
index = TupleSubclass(index)
assert_(arr[index].shape == (1,))
# Unlike the non nd-index:
assert_(arr[index,].shape != (1,))

def test_broken_sequence_not_nd_index(self):
# See gh-5063:
# If we have an object which claims to be a sequence, but fails
# on item getting, this should not be converted to an nd-index (tuple)
# If this object happens to be a valid index otherwise, it should work
# This object here is very dubious and probably bad though:
class SequenceLike(object):
def __index__(self):
return 0

def __len__(self):
return 1

def __getitem__(self, item):
raise IndexError('Not possible')

arr = np.arange(10)
assert_array_equal(arr[SequenceLike()], arr[SequenceLike(),])


class TestFieldIndexing(TestCase):
def test_scalar_return_type(self):
Expand Down
0