Author: Armin Rigo <ar...@tunes.org>
Branch: cpyext-ext
Changeset: r82648:51eac7129726
Date: 2016-03-01 22:15 +0100
http://bitbucket.org/pypy/pypy/changeset/51eac7129726/

Log:    test and fix: slot_tp_init

diff --git a/pypy/module/cpyext/slotdefs.py b/pypy/module/cpyext/slotdefs.py
--- a/pypy/module/cpyext/slotdefs.py
+++ b/pypy/module/cpyext/slotdefs.py
@@ -329,13 +329,6 @@
     w_args_new = space.newtuple(args_w)
     return space.call(w_func, w_args_new, w_kwds)
 
-@cpython_api([PyObject, PyObject, PyObject], rffi.INT_real, error=-1, 
header=None)
-def slot_tp_init(space, w_self, w_args, w_kwds):
-    w_descr = space.lookup(w_self, '__init__')
-    args = Arguments.frompacked(space, w_args, w_kwds)
-    space.get_and_call_args(w_descr, w_self, args)
-    return 0
-
 from rpython.rlib.nonconst import NonConstant
 
 SLOTS = {}
@@ -456,6 +449,21 @@
                 return None
         api_func = slot_tp_iternext.api_func
 
+    elif name == 'tp_init':
+        init_fn = w_type.getdictvalue(space, '__init__')
+        if init_fn is None:
+            return
+
+        @cpython_api([PyObject, PyObject, PyObject], rffi.INT_real, error=-1,
+                     header=header)
+        @func_renamer("cpyext_%s_%s" % (name.replace('.', '_'), typedef.name))
+        def slot_tp_init(space, w_self, w_args, w_kwds):
+            args = Arguments(space, [w_self],
+                             w_stararg=w_args, w_starstararg=w_kwds)
+            space.call_args(init_fn, args)
+            return 0
+        api_func = slot_tp_init.api_func
+
     else:
         return
 
diff --git a/pypy/module/cpyext/test/test_typeobject.py 
b/pypy/module/cpyext/test/test_typeobject.py
--- a/pypy/module/cpyext/test/test_typeobject.py
+++ b/pypy/module/cpyext/test/test_typeobject.py
@@ -559,6 +559,35 @@
         #assert module.tp_call(D, typ1, ()) == "foo! ()" XXX not working so far
         assert isinstance(module.tp_call(type, typ1, ()), typ1)
 
+    def test_tp_init(self):
+        module = self.import_extension('foo', [
+            ("tp_init", "METH_VARARGS",
+             '''
+                 PyTypeObject *type = (PyTypeObject *)PyTuple_GET_ITEM(args, 
0);
+                 PyObject *obj = PyTuple_GET_ITEM(args, 1);
+                 PyObject *c_args = PyTuple_GET_ITEM(args, 2);
+                 if (!type->tp_init)
+                 {
+                     PyErr_SetNone(PyExc_ValueError);
+                     return NULL;
+                 }
+                 if (type->tp_init(obj, c_args, NULL) < 0)
+                     return NULL;
+                 Py_INCREF(Py_None);
+                 return Py_None;
+             '''
+             )
+            ])
+        x = [42]
+        assert module.tp_init(list, x, ("hi",)) is None
+        assert x == ["h", "i"]
+        class LL(list):
+            def __init__(self, *ignored):
+                raise Exception
+        x = LL.__new__(LL)
+        assert module.tp_init(list, x, ("hi",)) is None
+        assert x == ["h", "i"]
+
     def test_tp_str(self):
         module = self.import_extension('foo', [
            ("tp_str", "METH_VARARGS",
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to