Author: Matti Picus <[email protected]>
Branch: cpyext-injection
Changeset: r88376:22a28b31d815
Date: 2016-11-15 08:07 +0200
http://bitbucket.org/pypy/pypy/changeset/22a28b31d815/

Log:    add r ops, test a bit more, fix, fix translation

diff --git a/pypy/module/cpyext/injection/numpy.py 
b/pypy/module/cpyext/injection/numpy.py
--- a/pypy/module/cpyext/injection/numpy.py
+++ b/pypy/module/cpyext/injection/numpy.py
@@ -66,6 +66,8 @@
     data = rffi.cast(rffi.DOUBLEP, py_obj.data)
     return W_Float64Object(data[index])
 
+internal_arr1 = lltype.malloc(rffi.DOUBLEP.TO, 1, flavor='raw', immortal=True)
+internal_arr2 = lltype.malloc(rffi.DOUBLEP.TO, 1, flavor='raw', immortal=True)
 def injected_op(space, w_self, w_other, op):
     # translate array_op in multiarray.c from c to rpython
     if isinstance(w_self, W_ArrayObject):
@@ -73,16 +75,16 @@
         data1 = rffi.cast(rffi.DOUBLEP, arr1.data)
         n1 = arr1.dimensions[0]
     else:
-        # XXX this should be a pointer to a float, not a list
-        data1 = [space.float_w(w_self),]
+        data1 = internal_arr1
+        data1[0] = space.float_w(w_self)
         n1 = 1
     if isinstance(w_other, W_ArrayObject):
         arr2 = rffi.cast(PyArrayObject, w_other.pyobj)
         data2 = rffi.cast(rffi.DOUBLEP, arr2.data)
         n2 = arr2.dimensions[0]
     else:
-        # XXX this should be a pointer to a float, not a list
-        data2 = [space.float_w(w_other),]
+        data2 = internal_arr2
+        data2[0] = space.float_w(w_other)
         n2 = 1
     if not (n1 == n2 or n1 == 1 or n2 == 1):
         raise oefmt(space.w_ValueError, 'dimension mismatch')
@@ -93,8 +95,8 @@
     r = rffi.cast(rffi.DOUBLEP, ret.pyobj.data)
     i1 = 0; i2 = 0
     for j in range(m):
-        if i1 > n1: i1 = 0
-        if i2 > n2: i2 = 0
+        if i1 >= n1: i1 = 0
+        if i2 >= n2: i2 = 0
         if op == 'mul':
             r[j] = data1[i1] * data2[i2]
         elif op == 'add':
@@ -127,13 +129,17 @@
     '__add__': interp2app(injected_add),
     '__sub__': interp2app(injected_sub),
     '__div__': interp2app(injected_div),
+    '__rmul__': interp2app(injected_mul),
+    '__radd__': interp2app(injected_add),
+    '__rsub__': interp2app(injected_sub),
+    '__rdiv__': interp2app(injected_div),
 }
 
 def inject_operator(space, name, dict_w, pto):
     assert name == 'numpy.ndarray'
     org = space.fromcache(Original)
     org.w_original_getitem = dict_w['__getitem__']
-    org.w_original_getitem = dict_w['__mul__']
+    org.w_original_mul = dict_w['__mul__']
     for key, w_value in org.injected_methods_w:
         dict_w[key] = w_value
     return W_ArrayObject.typedef
diff --git a/pypy/module/cpyext/injection/test/multiarray.c 
b/pypy/module/cpyext/injection/test/multiarray.c
--- a/pypy/module/cpyext/injection/test/multiarray.c
+++ b/pypy/module/cpyext/injection/test/multiarray.c
@@ -295,6 +295,7 @@
         if (PyErr_Occurred())
             return NULL;
         v1 = &tmp1;
+        n1 = 1;
     }
     if (obj2->ob_type == &PyArray_Type)
     {
@@ -307,8 +308,9 @@
         if (PyErr_Occurred())
             return NULL;
         v2 = &tmp2;
+        n2 = 1;
     }
-    if (!(n1 == n2 || n1 == 1 || n2 == 1))
+    if ( !(n1 == n2 || n1 == 1 || n2 == 1))
     {
         PyErr_SetString(PyExc_ValueError, "dimension mismatch");
         return NULL;
@@ -326,19 +328,19 @@
         switch (op)
         {
             case MULT:
-                r[j] = v1[i1] * v2[i2];
+                r[j] = v1[i1] * v2[i2] + 3;
                 break;
             case ADD:
-                r[j] = v1[i1] + v2[i2];
+                r[j] = v1[i1] + v2[i2] + 3;
                 break;
             case SUB:
-                r[j] = v1[i1] - v2[i2];
+                r[j] = v1[i1] - v2[i2] + 3;
                 break;
             case DIV:
                 if (v2[i2] == 0)
                     r[j] = Py_NAN;
                 else
-                    r[j] = v1[i1] / v2[i2];
+                    r[j] = v1[i1] / v2[i2] + 3;
                 break;
         }
     } 
diff --git a/pypy/module/cpyext/injection/test/test_numpy.py 
b/pypy/module/cpyext/injection/test/test_numpy.py
--- a/pypy/module/cpyext/injection/test/test_numpy.py
+++ b/pypy/module/cpyext/injection/test/test_numpy.py
@@ -44,7 +44,7 @@
         for i in range(100):
             a[i] = i
         b = a * a
-        assert b[10] == 100.0 + 42.0
+        assert b[10] == 100.0 + 3.0 + 42.0
 
     def test_injected_op(self):
         np = self.import_module(name='numpy.core.multiarray',
@@ -54,3 +54,8 @@
             a[i] = i
         b = a * a
         assert b[10] == 100.0
+        b = a * 2
+        assert b[5] == 10.0
+        b = 2 * a
+        assert b[5] == 10.0
+        
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to