Skip to content

Commit

Permalink
Fix ndindex for 0-d arrays.
Browse files Browse the repository at this point in the history
  • Loading branch information
teoliphant committed Jan 10, 2013
1 parent dd146b6 commit 6d980e7
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion numpy/lib/index_tricks.py
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,6 @@ def next(self):
def __iter__(self):
return self


class ndindex(object):
"""
An N-dimensional iterator object to index arrays.
Expand Down Expand Up @@ -535,6 +534,9 @@ class ndindex(object):
def __init__(self, *shape):
x = as_strided(_nx.zeros(1), shape=shape, strides=_nx.zeros_like(shape))
self._it = _nx.nditer(x, flags=['multi_index'], order='C')
# This is a patch to handle 0-d arrays correctly on the Python side.
# We might want to revisit nditer in the future to handle this
self._zerod = (len(shape)==0)

def __iter__(self):
return self
Expand All @@ -558,6 +560,13 @@ def next(self):
"""
self._it.next()
# This is a hack with an un-necessary check in every next call
# But, it's much simpler than writing another iterator for 0-d arrays
# because the Python iterator protocol does not respect monkey-patching
# the next method on an instance.
# Given that we should fix nditer eventually, we do this for now.
if self._zerod:
return ()
return self._it.multi_index


Expand Down

0 comments on commit 6d980e7

Please sign in to comment.