[pypy-commit] pypy default: a passing test for binop_impl resolution order

mattip pypy.commits at gmail.com
Mon Jun 27 14:18:47 EDT 2016


Author: Matti Picus <matti.picus at gmail.com>
Branch: 
Changeset: r85414:f0b680130986
Date: 2016-06-27 20:02 +0300
http://bitbucket.org/pypy/pypy/changeset/f0b680130986/

Log:	a passing test for binop_impl resolution order

diff --git a/pypy/module/cpyext/test/array.c b/pypy/module/cpyext/test/array.c
--- a/pypy/module/cpyext/test/array.c
+++ b/pypy/module/cpyext/test/array.c
@@ -1858,6 +1858,56 @@
     }
 }
 
+static PyObject*
+array_multiply(PyObject* obj1, PyObject* obj2)
+{
+    if (PyList_Check(obj1) && ((arrayobject*)obj2)->ob_descr->typecode == 'i' && Py_SIZE(obj2) == 1)
+    {
+        int ii, nn;
+        int n = PyList_Size(obj1);
+        PyObject *v = getarrayitem(obj2, 0);
+        int i = ((PyIntObject*)v)->ob_ival;
+        PyObject * ret = PyList_New(n*i);
+        for (ii = 0; ii < i; ii++)
+            for (nn = 0; nn < n; nn++)
+            {
+                v = PyList_GetItem(obj1, nn);
+                PyList_SetItem(ret, nn+ii*n, v);
+            }
+        return ret;
+    }
+    else if (PyList_Check(obj2) && ((arrayobject*)obj1)->ob_descr->typecode == 'i' && Py_SIZE(obj1) == 1)
+    {
+        int ii, nn;
+        int n = PyList_Size(obj2);
+        PyObject *v = getarrayitem(obj1, 0);
+        int i = ((PyIntObject*)v)->ob_ival;
+        PyObject * ret = PyList_New(n*i);
+        for (ii = 0; ii < i; ii++)
+            for (nn = 0; nn < n; nn++)
+            {
+                v = PyList_GetItem(obj2, nn);
+                PyList_SetItem(ret, nn+ii*n, v);
+            }
+        return ret;
+    }
+    else if(obj1->ob_type == &Arraytype)
+        fprintf(stderr, "\nCannot multiply array of type %c and %s\n",
+            ((arrayobject*)obj1)->ob_descr->typecode, obj2->ob_type->tp_name); 
+    else if(obj2->ob_type == &Arraytype)
+        fprintf(stderr, "\nCannot multiply array of type %c and %s\n",
+            ((arrayobject*)obj2)->ob_descr->typecode, obj1->ob_type->tp_name); 
+    Py_INCREF(Py_NotImplemented);
+    return Py_NotImplemented;
+}
+
+static PyNumberMethods array_as_number = {
+    (binaryfunc)NULL, /* nb_add*/
+    (binaryfunc)NULL, /* nb_subtract */
+    (binaryfunc)array_multiply, /* nb_multiply */
+    (binaryfunc)NULL, /* nb_divide */
+};
+
 static PyMappingMethods array_as_mapping = {
     (lenfunc)array_length,
     (binaryfunc)array_subscr,
@@ -2117,7 +2167,7 @@
     0,                                          /* tp_setattr */
     0,                                          /* tp_compare */
     (reprfunc)array_repr,                       /* tp_repr */
-    0,                                          /* tp_as_number*/
+    &array_as_number,                           /* tp_as_number*/
     &array_as_sequence,                         /* tp_as_sequence*/
     &array_as_mapping,                          /* tp_as_mapping*/
     0,                                          /* tp_hash */
@@ -2126,7 +2176,8 @@
     PyObject_GenericGetAttr,                    /* tp_getattro */
     0,                                          /* tp_setattro */
     &array_as_buffer,                           /* tp_as_buffer*/
-    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_WEAKREFS,  /* tp_flags */
+    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | 
+    Py_TPFLAGS_HAVE_WEAKREFS | Py_TPFLAGS_CHECKTYPES,  /* tp_flags */
     arraytype_doc,                              /* tp_doc */
     0,                                          /* tp_traverse */
     0,                                          /* tp_clear */
diff --git a/pypy/module/cpyext/test/test_arraymodule.py b/pypy/module/cpyext/test/test_arraymodule.py
--- a/pypy/module/cpyext/test/test_arraymodule.py
+++ b/pypy/module/cpyext/test/test_arraymodule.py
@@ -77,3 +77,10 @@
         #assert s == "carray\n_reconstruct\np0\n(S'i'\np1\n(lp2\nI1\naI2\naI3\naI4\natp3\nRp4\n."
         rra = pickle.loads(s) # rra is arr backwards
         #assert arr.tolist() == rra.tolist()
+
+    def test_binop_mul_impl(self):
+        # check that rmul is called
+        module = self.import_module(name='array')
+        arr = module.array('i', [2])
+        res = [1, 2, 3] * arr
+        assert res == [1, 2, 3, 1, 2, 3]


More information about the pypy-commit mailing list