Commits

Justin Peel committed 76e1792

Added a bounds check on binary operations.

Comments (0)

Files changed (4)

pypy/module/micronumpy/compile.py

 """
 
 from pypy.module.micronumpy.interp_numarray import FloatWrapper, SingleDimArray
+from pypy.rlib.objectmodel import specialize
 
 class BogusBytecode(Exception):
     pass
     return a
 
 class TrivialSpace(object):
-    def wrap(self, x):
-        return x
+    w_ValueError = None
+
+    @specialize.argtype(1)
+    def wrap(self, w_obj):
+        return w_obj
 
 def numpy_compile(bytecode, array_size):
     space = TrivialSpace()

pypy/module/micronumpy/interp_numarray.py

         signature = Signature()
         def impl(self, space, w_other):
             w_other = convert_to_array(space, w_other)
+            try:
+                w_other_size = w_other.find_size()
+                self_size = self.find_size()
+            except ValueError:
+                # this will be raised if one of the arrays is a scalar.
+                pass
+            else:
+                # Need a better dimension check here for N-dim arrays
+                if w_other_size != self_size:
+                    raise OperationError(space.w_ValueError,
+                        space.wrap("Cannot %s arrays of unequal dimensions" \
+                        % function.__name__))
             new_sig = self.signature.transition(signature)
             res = Call2(
                 function,

pypy/module/micronumpy/test/test_base.py

 
     def test_slice_signature(self, space):
         ar = SingleDimArray(10)
-        v1 = ar.descr_getitem(space, space.wrap(slice(1, 5, 1)))
-        v2 = ar.descr_getitem(space, space.wrap(slice(4, 6, 1)))
+        v1 = ar.descr_getitem(space, space.wrap(slice(0, 10, 1)))
+        v2 = ar.descr_getitem(space, space.wrap(slice(9, None, -1)))
         assert v1.signature is v2.signature
 
         v3 = ar.descr_add(space, v1)

pypy/module/micronumpy/test/test_numarray.py

         for i in range(5):
             assert c[i] == 4
 
+    def test_add_unequal_size(self):
+        from numpy import array
+        a = array(range(5))
+        b = array(range(3))
+        raises(ValueError, "a + b")
+
     def test_subtract(self):
         from numpy import array
         a = array(range(5))