8000 Merge pull request #2897 from ContinuumIO/ndindex_fix · seberg/numpy@ee6c66a · GitHub
[go: up one dir, main page]

Skip to content

Commit ee6c66a

Browse files
committed
Merge pull request numpy#2897 from ContinuumIO/ndindex_fix
Fix for numpy#2895 ndindex failing
2 parents eb109d2 + d1a055b commit ee6c66a

File tree

4 files changed

+41
-7
lines changed

4 files changed

+ 10000 41
-7
lines changed

numpy/core/src/multiarray/ctors.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2075,11 +2075,6 @@ PyArray_FromInterface(PyObject *origin)
20752075
/* Case for data access through pointer */
20762076
if (attr && PyTuple_Check(attr)) {
20772077
PyObject *dataptr;
2078-
if (n == 0) {
2079-
PyErr_SetString(PyExc_ValueError,
2080-
"__array_interface__ shape must be at least size 1");
2081-
goto fail;
2082-
}
20832078
if (PyTuple_GET_SIZE(attr) != 2) {
20842079
PyErr_SetString(PyExc_TypeError,
20852080
"__array_interface__ data must be a 2-tuple with "

numpy/core/tests/test_multiarray.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2873,6 +2873,13 @@ def __array_interface__(self):
28732873
f.iface['shape'] = (2,)
28742874
assert_raises(ValueError, np.array, f)
28752875

2876+
# test scalar with no shape
2877+
class ArrayLike(object):
2878+
array = np.array(1)
2879+
__array_interface__ = array.__array_interface__
2880+
assert_equal(np.array(ArrayLike()), 1)
2881+
2882+
28762883
def test_flat_element_deletion():
28772884
it = np.ones(3).flat
28782885
try:

numpy/lib/index_tricks.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,6 @@ def next(self):
502502
def __iter__(self):
503503
return self
504504

505-
506505
class ndindex(object):
507506
"""
508507
An N-dimensional iterator object to index arrays.
@@ -532,13 +531,36 @@ class ndindex(object):
532531
(2, 1, 0)
533532
534533
"""
534+
# This is a hack to handle 0-d arrays correctly.
535+
# Fixing nditer would be more work but should be done eventually,
536+
# and then this entire __new__ method can be removed.
537+
def __new__(cls, *shape):
538+
if len(shape) == 0 or (len(shape) == 1 and len(shape[0]) == 0):
539+
class zero_dim_iter(object):
540+
def __init__(self):
541+
self._N = 1
542+
def __iter__(self):
543+
return self
544+
def ndincr(self):
545+
self.next()
546+
def next(self):
547+
if self._N > 0:
548+
self._N -= 1
549+
return ()
550+
raise StopIteration
551+
return zero_dim_iter()
552+
else:
553+
return super(ndindex, cls).__new__(cls)
554+
535555
def __init__(self, *shape):
556+
if len(shape) == 1 and isinstance(shape[0], tuple):
557+
shape = shape[0]
536558
x = as_strided(_nx.zeros(1), shape=shape, strides=_nx.zeros_like(shape))
537559
self._it = _nx.nditer(x, flags=['multi_index'], order='C')
538560

539561
def __iter__(self):
540562
return self
541-
563+
542564
def ndincr(self):
543565
"""
544566
Increment the multi-dimensional index by one.

numpy/lib/tests/test_index_tricks.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,16 @@ def test_ndindex():
242242
expected = [ix for ix, e in np.ndenumerate(np.zeros((1, 2, 3)))]
243243
assert_array_equal(x, expected)
244244

245+
x = list(np.ndindex((1, 2, 3)))
246+
assert_array_equal(x, expected)
247+
248+
# Make sure size argument is optional
249+
x = list(np.ndindex())
250+
assert_equal(x, [()])
251+
252+
x = list(np.ndindex(()))
253+
assert_equal(x, [()])
254+
245255

246256
if __name__ == "__main__":
247257
run_module_suite()

0 commit comments

Comments
 (0)
0