Commits

Romain Guillebert  committed 27ffa87

Slices returned by nditer should be readonly by default

  • Participants
  • Parent commits 0466c79
  • Branches numpypy-nditer

Comments (0)

Files changed (3)

File pypy/module/micronumpy/arrayimpl/concrete.py

         return SliceArray(self.start, new_strides, new_backstrides, new_shape,
                           self, orig_array)
 
+    def readonly(self):
+        return NonWritableSlice(self.start, self.strides, self.backstrides, self.shape, self.parent, self.orig_arr, self.dtype)
+
+class NonWritableSlice(SliceArray):
+    def descr_setitem(self, space, orig_array, w_index, w_value):
+        raise OperationError(space.w_ValueError, space.wrap(
+            "Assignment destination is read-only"))
 
 class ArrayBuffer(RWBuffer):
     def __init__(self, impl):

File pypy/module/micronumpy/interp_nditer.py

     return W_NDimArray(res)
 
 def get_readonly_slice(space, array, it):
-    #XXX Not readonly
-    return W_NDimArray(it.getslice())
+    return W_NDimArray(it.getslice().readonly())
 
 def get_readwrite_slice(space, array, it):
     return W_NDimArray(it.getslice())

File pypy/module/micronumpy/test/test_nditer.py

             n += 1
         assert n == 12
         assert (array(r) == [[ 0, 12], [ 4, 16], [ 8, 20], [ 1, 13], [ 5, 17], [ 9, 21], [ 2, 14], [ 6, 18], [10, 22], [ 3, 15], [ 7, 19], [11, 23]]).all()
+        e = None
+        try:
+            r[0][0] = 0
+        except ValueError, ex:
+            e = ex
+        assert e
 
     def test_interface(self):
         from numpypy import arange, nditer, zeros
         assert (it.operands[1] == [[6, 22, 38], [54, 70, 86]]).all()
         assert (it.operands[1] == a.sum(axis=2)).all()
 
+