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

xoraxax at codespeak.net xoraxax at codespeak.net
Sat Mar 20 18:38:37 CET 2010


Author: xoraxax
Date: Sat Mar 20 18:38:36 2010
New Revision: 72463

Modified:
   pypy/trunk/pypy/module/cpyext/api.py
   pypy/trunk/pypy/module/cpyext/methodobject.py
   pypy/trunk/pypy/module/cpyext/state.py
   pypy/trunk/pypy/module/cpyext/test/test_cpyext.py
Log:
Refactor test code, add new test that checks whether inits exception is respected, fix the bug.

Modified: pypy/trunk/pypy/module/cpyext/api.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/api.py	(original)
+++ pypy/trunk/pypy/module/cpyext/api.py	Sat Mar 20 18:38:36 2010
@@ -225,3 +225,10 @@
 
     return modulename.new(ext='')
 
+def load_extension_module(space, path, name):
+    state = space.fromcache(State)
+    import ctypes
+    initfunc = ctypes.CDLL(path)['init%s' % (name,)]
+    initfunc()
+    state.check_and_raise_exception()
+

Modified: pypy/trunk/pypy/module/cpyext/methodobject.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/methodobject.py	(original)
+++ pypy/trunk/pypy/module/cpyext/methodobject.py	Sat Mar 20 18:38:36 2010
@@ -3,7 +3,6 @@
 from pypy.interpreter.gateway import ObjSpace, W_Root
 from pypy.interpreter.argument import Arguments
 from pypy.interpreter.gateway import interp2app, unwrap_spec
-from pypy.interpreter.error import OperationError
 from pypy.rpython.lltypesystem import rffi, lltype
 from pypy.module.cpyext.api import PyObject, from_ref, NullPointerException, \
         InvalidPointerException
@@ -29,12 +28,8 @@
         ret = from_ref(space, result)
     except NullPointerException:
         state = space.fromcache(State)
-        exc_value = state.exc_value
-        exc_type = state.exc_type
-        assert exc_value is not None and exc_type is not None
-        state.exc_value = None
-        state.exc_type = None
-        raise OperationError(exc_type, exc_value)
+        state.check_and_raise_exception()
+        assert False, "NULL returned but no exception set"
     except InvalidPointerException:
         if not we_are_translated():
             import sys

Modified: pypy/trunk/pypy/module/cpyext/state.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/state.py	(original)
+++ pypy/trunk/pypy/module/cpyext/state.py	Sat Mar 20 18:38:36 2010
@@ -1,6 +1,6 @@
 from pypy.rlib.objectmodel import we_are_translated
 from pypy.lib.identity_dict import identity_dict
-
+from pypy.interpreter.error import OperationError
 
 class State:
     def __init__(self, space):
@@ -9,3 +9,11 @@
         self.exc_type = None
         self.exc_value = None
 
+    def check_and_raise_exception(self):
+        exc_value = self.exc_value
+        exc_type = self.exc_type
+        if exc_type is not None or exc_value is not None:
+            self.exc_value = None
+            self.exc_type = None
+            raise OperationError(exc_type, exc_value)
+

Modified: pypy/trunk/pypy/module/cpyext/test/test_cpyext.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/test/test_cpyext.py	(original)
+++ pypy/trunk/pypy/module/cpyext/test/test_cpyext.py	Sat Mar 20 18:38:36 2010
@@ -26,9 +26,8 @@
             rffi.CCHARP, lltype.Ptr(api.TYPES['PyMethodDef'])]
         assert api.FUNCTIONS['Py_InitModule'].restype == lltype.Void
 
-def compile_module(modname, code, **kwds):
+def compile_module(modname, **kwds):
     eci = ExternalCompilationInfo(
-        separate_module_sources=[code],
         export_symbols=['init%s' % (modname,)],
         include_dirs=api.include_dirs,
         **kwds
@@ -43,25 +42,30 @@
     def setup_class(cls):
         cls.api_library = api.build_bridge(cls.space, rename=True)
 
-    def import_module(self, name, init, body=''):
-        code = """
-        #include <pypy_rename.h>
-        #include <Python.h>
-        %(body)s
+    def import_module(self, name, init=None, body=''):
+        if init is not None:
+            code = """
+            #include <pypy_rename.h>
+            #include <Python.h>
+            %(body)s
+
+            void init%(name)s(void) {
+            %(init)s
+            }
+            """ % dict(name=name, init=init, body=body)
+            kwds = dict(separate_module_sources=[code])
+        else:
+            filename = py.path.local(autopath.pypydir) / 'module' \
+                    / 'cpyext'/ 'test' / (name + ".c")
+            kwds = dict(separate_module_files=[filename])
 
-        void init%(name)s(void) {
-        %(init)s
-        }
-        """ % dict(name=name, init=init, body=body)
         if sys.platform == 'win32':
-            libraries = [self.api_library]
-            mod = compile_module(name, code, libraries=libraries)
+            kwds["libraries"] = [self.api_library]
         else:
-            libraries = [str(self.api_library+'.so')]
-            mod = compile_module(name, code, link_files=libraries)
-        import ctypes
-        initfunc = ctypes.CDLL(mod)['init%s' % (name,)]
-        initfunc()
+            kwds["link_files"] = [str(self.api_library + '.so')]
+        mod = compile_module(name, **kwds)
+
+        api.load_extension_module(self.space, mod, name)
         return self.space.getitem(
             self.space.sys.get('modules'),
             self.space.wrap(name))
@@ -177,6 +181,18 @@
 
         assert exc.value.message == "moo!"
 
+    def test_init_exception(self):
+        import sys
+        init = """
+            PyErr_SetString(PyExc_Exception, "moo!");
+        """
+        exc = raises(Exception, "self.import_module(name='foo', init=init)")
+        if type(exc.value) is not Exception:
+            raise exc.value
+
+        assert exc.value.message == "moo!"
+
+
     def test_internal_exceptions(self):
         skip("Useful to see how programming errors look like")
         import sys



More information about the Pypy-commit mailing list