Author: Matti Picus <matti.pi...@gmail.com>
Branch: PyTuple_Type-subclass
Changeset: r85338:ddf360d978b7
Date: 2016-06-20 23:36 +0300
http://bitbucket.org/pypy/pypy/changeset/ddf360d978b7/

Log:    test, fix for PyTuple_Type subclassing

diff --git a/pypy/module/cpyext/test/foo.c b/pypy/module/cpyext/test/foo.c
--- a/pypy/module/cpyext/test/foo.c
+++ b/pypy/module/cpyext/test/foo.c
@@ -620,17 +620,34 @@
     (destructor)custom_dealloc, /*tp_dealloc*/
 };
 
+static PyTypeObject TupleLike = {
+    PyObject_HEAD_INIT(NULL)
+    0,
+    "foo.TupleLike",         /*tp_name*/
+    sizeof(PyObject),        /*tp_size*/
+};
+
+
 static PyObject *size_of_instances(PyObject *self, PyObject *t)
 {
     return PyInt_FromLong(((PyTypeObject *)t)->tp_basicsize);
 }
 
+
+static PyObject * is_TupleLike(PyObject *self, PyObject * t)
+{
+    int tf = t->ob_type == &TupleLike;
+    Py_DECREF(t);
+    return PyInt_FromLong(tf);
+}
+
 /* List of functions exported by this module */
 
 static PyMethodDef foo_functions[] = {
     {"new",        (PyCFunction)foo_new, METH_NOARGS, NULL},
     {"newCustom",  (PyCFunction)newCustom, METH_NOARGS, NULL},
     {"size_of_instances", (PyCFunction)size_of_instances, METH_O, NULL},
+    {"is_TupleLike", (PyCFunction)is_TupleLike, METH_O, NULL},
     {NULL,        NULL}    /* Sentinel */
 };
 
@@ -680,6 +697,10 @@
     if (PyType_Ready(&UnicodeSubtype3) < 0)
         return;
 
+    TupleLike.tp_base = &PyTuple_Type;
+    if (PyType_Ready(&TupleLike) < 0)
+        return;
+
     m = Py_InitModule("foo", foo_functions);
     if (m == NULL)
         return;
@@ -702,4 +723,6 @@
         return;
     if (PyDict_SetItemString(d, "Custom", (PyObject *) &CustomType) < 0)
         return;
+    if (PyDict_SetItemString(d, "TupleLike", (PyObject *) &TupleLike) < 0)
+        return;
 }
diff --git a/pypy/module/cpyext/test/test_tupleobject.py 
b/pypy/module/cpyext/test/test_tupleobject.py
--- a/pypy/module/cpyext/test/test_tupleobject.py
+++ b/pypy/module/cpyext/test/test_tupleobject.py
@@ -151,3 +151,8 @@
              """),
             ])
         module.run()
+
+    def test_tuple_subclass(self):
+        module = self.import_module(name='foo')
+        a = module.TupleLike([1, 2, 3])
+        assert module.is_TupleLike(a)
diff --git a/pypy/module/cpyext/tupleobject.py 
b/pypy/module/cpyext/tupleobject.py
--- a/pypy/module/cpyext/tupleobject.py
+++ b/pypy/module/cpyext/tupleobject.py
@@ -108,7 +108,9 @@
                 "converting a PyTupleObject into a W_TupleObject, "
                 "but found NULLs as items")
         items_w[i] = w_item
-    w_obj = space.newtuple(items_w)
+    w_type = from_ref(space, rffi.cast(PyObject, py_obj.c_ob_type))
+    w_obj = space.allocate_instance(W_TupleObject, w_type)
+    w_obj.__init__(items_w)
     track_reference(space, py_obj, w_obj)
     return w_obj
 
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to