[pypy-svn] r72980 - in pypy/branch/cpython-extension/pypy/module/cpyext: . test

xoraxax at codespeak.net xoraxax at codespeak.net
Sat Mar 27 22:13:29 CET 2010


Author: xoraxax
Date: Sat Mar 27 22:13:27 2010
New Revision: 72980

Modified:
   pypy/branch/cpython-extension/pypy/module/cpyext/api.py
   pypy/branch/cpython-extension/pypy/module/cpyext/test/test_tupleobject.py
Log:
Refactor exception handling.

Modified: pypy/branch/cpython-extension/pypy/module/cpyext/api.py
==============================================================================
--- pypy/branch/cpython-extension/pypy/module/cpyext/api.py	(original)
+++ pypy/branch/cpython-extension/pypy/module/cpyext/api.py	Sat Mar 27 22:13:27 2010
@@ -110,50 +110,55 @@
 
     def decorate(func):
         api_function = ApiFunction(argtypes, restype, func, borrowed, error)
+        func.api_func = api_function
 
         if error is _NOT_SPECIFIED:
             raise ValueError("function %s has no return value for exceptions"
                              % func)
-        def unwrapper(space, *args):
-            "NOT_RPYTHON: XXX unsure"
-            newargs = []
-            to_decref = []
-            for i, arg in enumerate(args):
-                if api_function.argtypes[i] is PyObject:
-                    if (isinstance(arg, W_Root) and
-                        not api_function.argnames[i].startswith('w_')):
-                        arg = make_ref(space, arg)
-                        to_decref.append(arg)
-                    elif (not isinstance(arg, W_Root) and
-                          api_function.argnames[i].startswith('w_')):
-                        arg = from_ref(space, arg)
-                newargs.append(arg)
-            try:
+        def make_unwrapper(catch_exception):
+            def unwrapper(space, *args):
+                "NOT_RPYTHON: XXX unsure"
+                newargs = []
+                to_decref = []
+                for i, arg in enumerate(args):
+                    if api_function.argtypes[i] is PyObject:
+                        if (isinstance(arg, W_Root) and
+                            not api_function.argnames[i].startswith('w_')):
+                            arg = make_ref(space, arg)
+                            to_decref.append(arg)
+                        elif (not isinstance(arg, W_Root) and
+                              api_function.argnames[i].startswith('w_')):
+                            arg = from_ref(space, arg)
+                    newargs.append(arg)
                 try:
-                    return func(space, *newargs)
-                except OperationError, e:
-                    if not hasattr(api_function, "error_value"):
-                        raise
-                    state = space.fromcache(State)
-                    e.normalize_exception(space)
-                    state.set_exception(e.w_type, e.get_w_value(space))
-                    return api_function.error_value
-            finally:
-                if api_function.borrowed:
-                    state = space.fromcache(State)
-                    state.last_container = 0
-                from pypy.module.cpyext.macros import Py_DECREF
-                for arg in to_decref:
-                    Py_DECREF(space, arg)
+                    try:
+                        return func(space, *newargs)
+                    except OperationError, e:
+                        if not catch_exception:
+                            raise
+                        if not hasattr(api_function, "error_value"):
+                            raise
+                        state = space.fromcache(State)
+                        e.normalize_exception(space)
+                        state.set_exception(e.w_type, e.get_w_value(space))
+                        return api_function.error_value
+                finally:
+                    if api_function.borrowed:
+                        state = space.fromcache(State)
+                        state.last_container = 0
+                    from pypy.module.cpyext.macros import Py_DECREF
+                    for arg in to_decref:
+                        Py_DECREF(space, arg)
+            unwrapper.func = func
+            unwrapper.api_func = api_function
+            return unwrapper
 
-        func.api_func = api_function
-        unwrapper.api_func = api_function
-        unwrapper.func = func
-        unwrapper.__name__ = "uw(%s)" % (func.__name__, )
+        unwrapper_True = make_unwrapper(True)
+        unwrapper_False = make_unwrapper(False)
         if external:
             FUNCTIONS[func.func_name] = api_function
-        INTERPLEVEL_API[func.func_name] = unwrapper
-        return unwrapper
+        INTERPLEVEL_API[func.func_name] = unwrapper_True
+        return unwrapper_False
     return decorate
 
 def cpython_api_c():

Modified: pypy/branch/cpython-extension/pypy/module/cpyext/test/test_tupleobject.py
==============================================================================
--- pypy/branch/cpython-extension/pypy/module/cpyext/test/test_tupleobject.py	(original)
+++ pypy/branch/cpython-extension/pypy/module/cpyext/test/test_tupleobject.py	Sat Mar 27 22:13:27 2010
@@ -4,7 +4,6 @@
 
 class TestTupleObject(BaseApiTest):
     def test_tupleobject(self, space, api):
-        py.test.skip("Needs API refactoring, done by amaury")
         assert not api.PyTuple_Check(space.w_None)
         assert api.PyTuple_SetItem(space.w_None, 0, space.w_None) == -1
         api.PyErr_Clear()



More information about the Pypy-commit mailing list