Author: Brian Kearns <[email protected]>
Branch: 
Changeset: r70289:26e0a68bb6dd
Date: 2014-03-25 13:51 -0400
http://bitbucket.org/pypy/pypy/changeset/26e0a68bb6dd/

Log:    fix ndarray setitem with empty index (issue1719)

diff --git a/pypy/module/micronumpy/ndarray.py 
b/pypy/module/micronumpy/ndarray.py
--- a/pypy/module/micronumpy/ndarray.py
+++ b/pypy/module/micronumpy/ndarray.py
@@ -185,7 +185,7 @@
             return chunks.apply(space, self)
         shape = res_shape + self.get_shape()[len(indexes):]
         w_res = W_NDimArray.from_shape(space, shape, self.get_dtype(),
-                                     self.get_order(), w_instance=self)
+                                       self.get_order(), w_instance=self)
         if not w_res.get_size():
             return w_res
         return loop.getitem_array_int(space, self, w_res, iter_shape, indexes,
@@ -201,6 +201,8 @@
             view = chunks.apply(space, self)
             view.implementation.setslice(space, val_arr)
             return
+        if support.product(iter_shape) == 0:
+            return
         loop.setitem_array_int(space, self, iter_shape, indexes, val_arr,
                                prefix)
 
@@ -1169,7 +1171,7 @@
             raise OperationError(space.w_TypeError, space.wrap(
                 "numpy scalars from buffers not supported yet"))
         totalsize = support.product(shape) * dtype.elsize
-        if totalsize+offset > buf.getlength():
+        if totalsize + offset > buf.getlength():
             raise OperationError(space.w_TypeError, space.wrap(
                 "buffer is too small for requested array"))
         storage = rffi.cast(RAW_STORAGE_PTR, raw_ptr)
diff --git a/pypy/module/micronumpy/test/test_ndarray.py 
b/pypy/module/micronumpy/test/test_ndarray.py
--- a/pypy/module/micronumpy/test/test_ndarray.py
+++ b/pypy/module/micronumpy/test/test_ndarray.py
@@ -2369,6 +2369,19 @@
         assert b.shape == b[...].shape
         assert (b == b[...]).all()
 
+    def test_empty_indexing(self):
+        import numpy as np
+        r = np.ones(3)
+        ind = np.array([], np.int32)
+        tmp = np.array([], np.float64)
+        assert r[ind].shape == (0,)
+        r[ind] = 0
+        assert (r == np.ones(3)).all()
+        r[ind] = tmp
+        assert (r == np.ones(3)).all()
+        r[[]] = 0
+        assert (r == np.ones(3)).all()
+
 
 class AppTestNumArrayFromBuffer(BaseNumpyAppTest):
     spaceconfig = dict(usemodules=["micronumpy", "array", "mmap"])
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to