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

agaynor at codespeak.net agaynor at codespeak.net
Fri May 28 22:52:55 CEST 2010


Author: agaynor
Date: Fri May 28 22:52:53 2010
New Revision: 74879

Modified:
   pypy/trunk/pypy/module/cpyext/slotdefs.py
   pypy/trunk/pypy/module/cpyext/test/foo.c
   pypy/trunk/pypy/module/cpyext/test/test_typeobject.py
Log:
Fixed exception raising in __init__ for cpyext (previously they were swallowed).

Modified: pypy/trunk/pypy/module/cpyext/slotdefs.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/slotdefs.py	(original)
+++ pypy/trunk/pypy/module/cpyext/slotdefs.py	Fri May 28 22:52:53 2010
@@ -28,7 +28,9 @@
 
 def wrap_init(space, w_self, w_args, func, w_kwargs):
     func_init = rffi.cast(initproc, func)
-    generic_cpy_call(space, func_init, w_self, w_args, w_kwargs)
+    res = generic_cpy_call(space, func_init, w_self, w_args, w_kwargs)
+    if rffi.cast(lltype.Signed, res) == -1:
+        space.fromcache(State).check_and_raise_exception()
     return None
 
 def wrap_unaryfunc(space, w_self, w_args, func):

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	Fri May 28 22:52:53 2010
@@ -193,8 +193,9 @@
 } FuuObject;
 
 
-void Fuu_init(FuuObject *self, PyObject *args, PyObject *kwargs) {
+static int Fuu_init(FuuObject *self, PyObject *args, PyObject *kwargs) {
     self->val = 42;
+    return 0;
 }
 
 static PyObject *
@@ -417,6 +418,72 @@
 };
 
 
+static int initerrtype_init(PyObject *self, PyObject *args, PyObject *kwargs) {
+    PyErr_SetString(PyExc_ValueError, "init raised an error!");
+    return -1;
+}
+
+
+PyTypeObject InitErrType = {
+    PyObject_HEAD_INIT(NULL)
+    0,
+    "foo.InitErr",
+    sizeof(PyObject),
+    0,
+    0,          /*tp_dealloc*/
+    0,          /*tp_print*/
+    0,          /*tp_getattr*/
+    0,          /*tp_setattr*/
+    0,          /*tp_compare*/
+    0,          /*tp_repr*/
+    0,          /*tp_as_number*/
+    0,          /*tp_as_sequence*/
+    0,          /*tp_as_mapping*/
+    0,          /*tp_hash */
+
+    0,          /*tp_call*/
+    0,          /*tp_str*/
+    0,          /*tp_getattro*/
+    0,          /*tp_setattro*/
+    0,          /*tp_as_buffer*/
+
+    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_CHECKTYPES, /*tp_flags*/
+    0,          /*tp_doc*/
+
+    0,          /*tp_traverse*/
+    0,          /*tp_clear*/
+
+    0,          /*tp_richcompare*/
+    0,          /*tp_weaklistoffset*/
+
+    0,          /*tp_iter*/
+    0,          /*tp_iternext*/
+
+    /* Attribute descriptor and subclassing stuff */
+
+    0,          /*tp_methods*/
+    0,          /*tp_members*/
+    0,          /*tp_getset*/
+    0,          /*tp_base*/
+    0,          /*tp_dict*/
+
+    0,          /*tp_descr_get*/
+    0,          /*tp_descr_set*/
+    0,          /*tp_dictoffset*/
+
+    initerrtype_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 */
+    0,          /*tp_is_gc For PyObject_IS_GC */
+    0,          /*tp_bases*/
+    0,          /*tp_mro method resolution order */
+    0,          /*tp_cache*/
+    0,          /*tp_subclasses*/
+    0           /*tp_weaklist*/
+};
+
+
 /* Initialize this module. */
 
 void initfoo(void)
@@ -437,6 +504,8 @@
         return;
     if (PyType_Ready(&MetaType) < 0)
         return;
+    if (PyType_Ready(&InitErrType) < 0)
+        return;
     m = Py_InitModule("foo", foo_functions);
     if (m == NULL)
         return;
@@ -451,4 +520,6 @@
         return;
     if (PyDict_SetItemString(d, "MetaType", (PyObject *) &MetaType) < 0)
         return;
+    if (PyDict_SetItemString(d, "InitErrType", (PyObject *) &InitErrType) < 0)
+        return;
 }

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	Fri May 28 22:52:53 2010
@@ -173,6 +173,11 @@
         re._cache.clear()
         re._cache_repl.clear()
 
+    def test_init_error(self):
+        module = self.import_module("foo")
+        raises(ValueError, module.InitErrType)
+
+
 class TestTypes(BaseApiTest):
     def test_type_attributes(self, space, api):
         w_class = space.appexec([], """():
@@ -209,7 +214,7 @@
         w_obj = api._PyType_Lookup(w_type, space.wrap("__invalid"))
         assert w_obj is None
         assert api.PyErr_Occurred() is None
-
+    
 class AppTestSlots(AppTestCpythonExtensionBase):
     def test_some_slots(self):
         module = self.import_extension('foo', [



More information about the Pypy-commit mailing list