[pypy-svn] r74447 - in pypy/trunk/pypy/module/cpyext: . test

agaynor at codespeak.net agaynor at codespeak.net
Sun May 9 00:43:01 CEST 2010


Author: agaynor
Date: Sun May  9 00:42:59 2010
New Revision: 74447

Modified:
   pypy/trunk/pypy/module/cpyext/test/foo.c
   pypy/trunk/pypy/module/cpyext/test/test_typeobject.py
   pypy/trunk/pypy/module/cpyext/typeobject.py
Log:
type.__call__ now correctly calls tp_init correctly.

Modified: pypy/trunk/pypy/module/cpyext/test/foo.c
==============================================================================
--- pypy/trunk/pypy/module/cpyext/test/foo.c	(original)
+++ pypy/trunk/pypy/module/cpyext/test/foo.c	Sun May  9 00:42:59 2010
@@ -161,17 +161,28 @@
 
 typedef struct {
     PyUnicodeObject HEAD;
+    int val;
 } FuuObject;
 
+
+void Fuu_init(FuuObject *self, PyObject *args, PyObject *kwargs) {
+    self->val = 42;
+}
+
 static PyObject *
 Fuu_escape(PyTypeObject* type, PyObject *args)
 {
     Py_RETURN_TRUE;
 }
 
+static PyObject *
+Fuu_get_val(FuuObject *self) {
+    return PyInt_FromLong(self->val);
+}
 
 static PyMethodDef Fuu_methods[] = {
     {"escape", (PyCFunction) Fuu_escape, METH_VARARGS, NULL},
+    {"get_val", (PyCFunction) Fuu_get_val, METH_NOARGS, NULL},
     {NULL}  /* Sentinel */
 };
 
@@ -222,7 +233,7 @@
     0,          /*tp_descr_set*/
     0,          /*tp_dictoffset*/
 
-    0,          /*tp_init*/
+    Fuu_init,          /*tp_init*/
     0,          /*tp_alloc  will be set to PyType_GenericAlloc in module init*/
     0,          /*tp_new*/
     0,          /*tp_free  Low-level free-memory routine */

Modified: pypy/trunk/pypy/module/cpyext/test/test_typeobject.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/test/test_typeobject.py	(original)
+++ pypy/trunk/pypy/module/cpyext/test/test_typeobject.py	Sun May  9 00:42:59 2010
@@ -108,6 +108,11 @@
                 return self
         assert fuu2(u"abc").baz().escape()
         raises(TypeError, module.fooType.object_member.__get__, 1)
+    
+    def test_init(self):
+        module = self.import_module(name="foo")
+        newobj = module.FuuType()
+        assert newobj.get_val() == 42
 
     def test_sre(self):
         module = self.import_module(name='_sre')

Modified: pypy/trunk/pypy/module/cpyext/typeobject.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/typeobject.py	(original)
+++ pypy/trunk/pypy/module/cpyext/typeobject.py	Sun May  9 00:42:59 2010
@@ -274,7 +274,20 @@
                 w_kw = space.newdict()
                 for key, w_obj in kw_w.items():
                     space.setitem(w_kw, space.wrap(key), w_obj)
-                return generic_cpy_call(space, tp_new, pto, w_args, w_kw)
+                w_obj = generic_cpy_call(space, tp_new, pto, w_args, w_kw)
+                if w_obj:
+                    w_obj_type = space.type(w_obj)
+                    if not int(space.is_w(w_obj_type, w_type) or
+                        space.is_true(space.issubtype(w_obj_type, w_type))):
+                        return w_obj
+                    pyo = make_ref(space, w_type)
+                    pto = rffi.cast(PyTypeObjectPtr, pyo)
+                    try:
+                        if pto.c_tp_init:
+                            generic_cpy_call(space, pto.c_tp_init, w_obj, w_args, w_kw)
+                        return w_obj
+                    finally:
+                        Py_DecRef(space, pyo)
             else:
                 raise operationerrfmt(space.w_TypeError,
                     "cannot create '%s' instances", w_type.getname(space, '?'))



More information about the Pypy-commit mailing list