8000 BUG: incorrect type for objects whose __len__ fails by ahaldane · Pull Request #7397 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: incorrect type for objects whose __len__ fails #7397

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 1 commit into from
Mar 9, 2016
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
BUG: incorrect type for objects whose __len__ fails
Fixes #7393
  • Loading branch information
ahaldane committed Mar 9, 2016
commit 49965d01d7807c6852d77f2615afd16fed2195fb
3 changes: 3 additions & 0 deletions numpy/core/src/multiarray/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,9 @@ PyArray_DTypeFromObjectHelper(PyObject *obj, int maxdims,
* __len__ is not defined.
*/
if (maxdims == 0 || !PySequence_Check(obj) || PySequence_Size(obj) < 0) {
// clear any PySequence_Size error, which corrupts further calls to it
PyErr_Clear();

if (*out_dtype == NULL || (*out_dtype)->type_num != NPY_OBJECT) {
Py_XDECREF(*out_dtype);
*out_dtype = PyArray_DescrFromType(NPY_OBJECT);
Expand Down
14 changes: 14 additions & 0 deletions numpy/core/tests/test_multiarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,20 @@ def __len__(self):

assert_raises(ValueError, np.array, C()) # segfault?

def test_failed_len_sequence(self):
# gh-7393
class A(object):
def __init__(self, data):
self._data = data
def __getitem__(self, item):
return type(self)(self._data[item])
def __len__(self):
return len(self._data)

# len(d) should give 3, but len(d[0]) will fail
d = A([1,2,3])
assert_equal(len(np.array(d)), 3)


class TestStructured(TestCase):
def test_subarray_field_access(self):
Expand Down
0