Author: Matti Picus <matti.pi...@gmail.com>
Branch: 
Changeset: r85478:45fce19205cd
Date: 2016-06-30 23:43 +0300
http://bitbucket.org/pypy/pypy/changeset/45fce19205cd/

Log:    add a base type for ArrayType, make sure child nb_multiply is called

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
@@ -1908,6 +1908,60 @@
     (binaryfunc)NULL, /* nb_divide */
 };
 
+static PyObject*
+array_base_multiply(PyObject* obj1, PyObject* obj2)
+{
+    if (PyList_Check(obj1) && ((arrayobject*)obj2)->ob_descr->typecode == 'i' 
&& Py_SIZE(obj2) == 1)
+    {
+        int nn;
+        int n = PyList_Size(obj1);
+        PyObject *v = getarrayitem(obj2, 0);
+        int i = ((PyIntObject*)v)->ob_ival;
+        PyObject * ret = PyList_New(n);
+        for (nn = 0; nn < n; nn++)
+        {
+            v = PyList_GetItem(obj1, nn);
+            if (PyInt_Check(v))
+                PyList_SetItem(ret, nn, PyLong_FromLong(i * 
((PyIntObject*)v)->ob_ival));
+            else
+                PyList_SetItem(ret, nn, v);
+        }
+        return ret;
+    }
+    else if (PyList_Check(obj2) && ((arrayobject*)obj1)->ob_descr->typecode == 
'i' && Py_SIZE(obj1) == 1)
+    {
+        int nn;
+        int n = PyList_Size(obj2);
+        PyObject *v = getarrayitem(obj1, 0);
+        int i = ((PyIntObject*)v)->ob_ival;
+        PyObject * ret = PyList_New(n);
+        for (nn = 0; nn < n; nn++)
+        {
+            v = PyList_GetItem(obj2, nn);
+            if (PyInt_Check(v))
+                PyList_SetItem(ret, nn, PyLong_FromLong(i * 
((PyIntObject*)v)->ob_ival));
+            else
+                PyList_SetItem(ret, nn, 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_base_as_number = {
+    (binaryfunc)NULL, /* nb_add*/
+    (binaryfunc)NULL, /* nb_subtract */
+    (binaryfunc)array_base_multiply, /* nb_multiply */
+    (binaryfunc)NULL, /* nb_divide */
+};
+
 static PyMappingMethods array_as_mapping = {
     (lenfunc)array_length,
     (binaryfunc)array_subscr,
@@ -2156,6 +2210,49 @@
 
 static PyObject *array_iter(arrayobject *ao);
 
+static PyTypeObject ArrayBasetype = {
+    PyVarObject_HEAD_INIT(NULL, 0)
+    "array.basearray",
+    sizeof(arrayobject),
+    0,
+    (destructor)array_dealloc,                  /* tp_dealloc */
+    0,                                          /* tp_print */
+    0,                                          /* tp_getattr */
+    0,                                          /* tp_setattr */
+    0,                                          /* tp_compare */
+    (reprfunc)array_repr,                       /* tp_repr */
+    &array_base_as_number,                      /* tp_as_number*/
+    &array_as_sequence,                         /* tp_as_sequence*/
+    &array_as_mapping,                          /* tp_as_mapping*/
+    0,                                          /* tp_hash */
+    0,                                          /* tp_call */
+    0,                                          /* tp_str */
+    PyObject_GenericGetAttr,                    /* tp_getattro */
+    0,                                          /* tp_setattro */
+    &array_as_buffer,                           /* tp_as_buffer*/
+    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 */
+    array_richcompare,                          /* tp_richcompare */
+    offsetof(arrayobject, weakreflist),         /* tp_weaklistoffset */
+    (getiterfunc)array_iter,                    /* tp_iter */
+    0,                                          /* tp_iternext */
+    array_methods,                              /* tp_methods */
+    0,                                          /* tp_members */
+    array_getsets,                              /* tp_getset */
+    0,                                          /* tp_base */
+    0,                                          /* tp_dict */
+    0,                                          /* tp_descr_get */
+    0,                                          /* tp_descr_set */
+    0,                                          /* tp_dictoffset */
+    0,                                          /* tp_init */
+    PyType_GenericAlloc,                        /* tp_alloc */
+    array_new,                                  /* tp_new */
+    PyObject_Del,                               /* tp_free */
+};
+
 static PyTypeObject Arraytype = {
     PyVarObject_HEAD_INIT(NULL, 0)
     "array.array",
@@ -2311,17 +2408,19 @@
 {
     PyObject *m;
 
+    ArrayBasetype.ob_type = &PyType_Type;
+    Arraytype.tp_base = &ArrayBasetype;
     Arraytype.ob_type = &PyType_Type;
     PyArrayIter_Type.ob_type = &PyType_Type;
     m = Py_InitModule3("array", a_methods, module_doc);
     if (m == NULL)
         return;
 
-    Py_INCREF((PyObject *)&Arraytype);
+    if (PyType_Ready(&ArrayBasetype) < 0)
+        return;
     if (PyType_Ready(&Arraytype) < 0)
         return;
     PyModule_AddObject(m, "ArrayType", (PyObject *)&Arraytype);
-    Py_INCREF((PyObject *)&Arraytype);
     PyModule_AddObject(m, "array", (PyObject *)&Arraytype);
-    /* No need to check the error here, the caller will do that */
+    PyModule_AddObject(m, "arraybase", (PyObject *)&ArrayBasetype);
 }
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to