Commits

Brian Kearns committed 26e0a68

fix ndarray setitem with empty index (issue1719)

Comments (0)

Files changed (2)

pypy/module/micronumpy/ndarray.py

             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,
             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)
 
             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)

pypy/module/micronumpy/test/test_ndarray.py

         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"])