[pypy-commit] pypy numpy-refactor: __radd__ and scalar iterator

fijal noreply at buildbot.pypy.org
Thu Aug 30 15:43:56 CEST 2012


Author: Maciej Fijalkowski <fijall at gmail.com>
Branch: numpy-refactor
Changeset: r56937:6c91a058e332
Date: 2012-08-30 15:33 +0200
http://bitbucket.org/pypy/pypy/changeset/6c91a058e332/

Log:	__radd__ and scalar iterator

diff --git a/pypy/module/micronumpy/arrayimpl/scalar.py b/pypy/module/micronumpy/arrayimpl/scalar.py
--- a/pypy/module/micronumpy/arrayimpl/scalar.py
+++ b/pypy/module/micronumpy/arrayimpl/scalar.py
@@ -1,6 +1,22 @@
 
 from pypy.module.micronumpy.arrayimpl import base
 
+class ScalarIterator(base.BaseArrayIterator):
+    def __init__(self, v):
+        self.v = v
+
+    def next(self):
+        pass
+
+    def getitem(self):
+        return self.v
+
+    def setitem(self, v):
+        raise Exception("Don't call setitem on scalar iterators")
+
+    def done(self):
+        return False
+
 class Scalar(base.BaseArrayImplementation):
     def __init__(self, dtype):
         self.value = None
@@ -12,6 +28,9 @@
     def get_shape(self):
         return []
 
+    def create_iter(self):
+        return ScalarIterator(self.value)
+
     def set_scalar_value(self, value):
         self.value = value
 
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
@@ -58,7 +58,7 @@
 
     def descr_setitem(self, space, w_idx, w_value):
         if (isinstance(w_idx, W_NDimArray) and w_idx.shape == self.shape and
-            w_idx.find_dtype().is_bool_type()):
+            w_idx.get_dtype().is_bool_type()):
             return self.setitem_filter(space, w_idx,
                                        support.convert_to_array(space, w_value))
         self.implementation.descr_setitem(space, w_idx, w_value)
@@ -75,6 +75,8 @@
     def get_scalar_value(self):
         return self.implementation.get_scalar_value()
 
+    # --------------------- binary operations ----------------------------
+
     def _binop_impl(ufunc_name):
         def impl(self, space, w_other, w_out=None):
             return getattr(interp_ufuncs.get(space), ufunc_name).call(space,
@@ -83,6 +85,18 @@
 
     descr_add = _binop_impl("add")
 
+    def _binop_right_impl(ufunc_name):
+        def impl(self, space, w_other, w_out=None):
+            w_other = scalar_w(space,
+                interp_ufuncs.find_dtype_for_scalar(space, w_other,
+                                                    self.get_dtype()),
+                w_other
+            )
+            return getattr(interp_ufuncs.get(space), ufunc_name).call(space, [w_other, self, w_out])
+        return func_with_new_name(impl, "binop_right_%s_impl" % ufunc_name)
+
+    descr_radd = _binop_right_impl("add")
+
 @unwrap_spec(offset=int)
 def descr_new_array(space, w_subtype, w_shape, w_dtype=None, w_buffer=None,
                     offset=0, w_strides=None, w_order=None):
@@ -97,6 +111,8 @@
 
     __add__ = interp2app(W_NDimArray.descr_add),
 
+    __radd__ = interp2app(W_NDimArray.descr_radd),
+
     __getitem__ = interp2app(W_NDimArray.descr_getitem),
     __setitem__ = interp2app(W_NDimArray.descr_setitem),
 


More information about the pypy-commit mailing list