[pypy-commit] pypy numpy-ndim-size: Added a bounds check on binary operations.
justinpeel
noreply at buildbot.pypy.org
Mon Jul 18 08:06:42 CEST 2011
Author: Justin Peel <notmuchtotell at gmail.com>
Branch: numpy-ndim-size
Changeset: r45705:76e17925faf1
Date: 2011-07-17 23:53 -0600
http://bitbucket.org/pypy/pypy/changeset/76e17925faf1/
Log: Added a bounds check on binary operations.
diff --git a/pypy/module/micronumpy/compile.py b/pypy/module/micronumpy/compile.py
--- a/pypy/module/micronumpy/compile.py
+++ b/pypy/module/micronumpy/compile.py
@@ -4,6 +4,7 @@
"""
from pypy.module.micronumpy.interp_numarray import FloatWrapper, SingleDimArray
+from pypy.rlib.objectmodel import specialize
class BogusBytecode(Exception):
pass
@@ -15,8 +16,11 @@
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()
diff --git a/pypy/module/micronumpy/interp_numarray.py b/pypy/module/micronumpy/interp_numarray.py
--- a/pypy/module/micronumpy/interp_numarray.py
+++ b/pypy/module/micronumpy/interp_numarray.py
@@ -88,6 +88,18 @@
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,
diff --git a/pypy/module/micronumpy/test/test_base.py b/pypy/module/micronumpy/test/test_base.py
--- a/pypy/module/micronumpy/test/test_base.py
+++ b/pypy/module/micronumpy/test/test_base.py
@@ -18,8 +18,8 @@
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)
diff --git a/pypy/module/micronumpy/test/test_numarray.py b/pypy/module/micronumpy/test/test_numarray.py
--- a/pypy/module/micronumpy/test/test_numarray.py
+++ b/pypy/module/micronumpy/test/test_numarray.py
@@ -120,6 +120,12 @@
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))
More information about the pypy-commit
mailing list