Commits

Alex Gaynor committed 7fb3818

Fixed bound checks on setitem (patch from nightless_night). Also a little housekeeping.

  • Participants
  • Parent commits 235fc3e
  • Branches numpy-exp

Comments (0)

Files changed (2)

pypy/module/micronumpy/numarray.py

     def compile(self):
         return Code('l', [self], [])
 
-    @unwrap_spec(item=int)
-    def descr_getitem(self, space, item):
+    def getindex(self, space, item):
         if item >= self.size:
             raise operationerrfmt(space.w_IndexError,
               '%d above array size', item)
         if item < 0:
             raise operationerrfmt(space.w_IndexError,
               '%d below zero', item)
+        return item
+
+    @unwrap_spec(item=int)
+    def descr_getitem(self, space, item):
+        item = self.getindex(space, item)
         return space.wrap(self.storage[item])
 
     @unwrap_spec(item=int, value=float)
     def descr_setitem(self, space, item, value):
-        if item < 0:
-            raise operationerrfmt(space.w_TypeError,
-              '%d below zero', item)
-        if item > self.size:
-            raise operationerrfmt(space.w_TypeError,
-              '%d above array size', item)
+        item = self.getindex(space, item)
         self.invalidated()
         self.storage[item] = value
 

pypy/module/micronumpy/test/test_numpy.py

         assert a[-1] == 8
         raises(IndexError, "a[-6]")
 
+    def test_setitem(self):
+        from numpy import array
+        a = array(range(5))
+        a[-1] = 5.0
+        assert a[4] == 5.0
+        raises(IndexError, "a[5] = 0.0")
+        raises(IndexError, "a[-6] = 3.0")
+
     def test_add(self):
         from numpy import array
         a = array(range(5))