[pypy-svn] r50743 - pypy/dist/pypy/rpython/lltypesystem

arigo at codespeak.net arigo at codespeak.net
Fri Jan 18 11:36:47 CET 2008


Author: arigo
Date: Fri Jan 18 11:36:46 2008
New Revision: 50743

Modified:
   pypy/dist/pypy/rpython/lltypesystem/rffi.py
Log:
(fijal, arigo)
Attempt to fix the callbacks GIL handling.


Modified: pypy/dist/pypy/rpython/lltypesystem/rffi.py
==============================================================================
--- pypy/dist/pypy/rpython/lltypesystem/rffi.py	(original)
+++ pypy/dist/pypy/rpython/lltypesystem/rffi.py	Fri Jan 18 11:36:46 2008
@@ -1,4 +1,4 @@
-
+import py
 from pypy.annotation import model as annmodel
 from pypy.rpython.lltypesystem import lltype
 from pypy.rpython.lltypesystem import ll2ctypes
@@ -168,20 +168,31 @@
     else:
         before = None
         after = None
-    def wrapper(*args):
-        try:
-            if before:
-                before()
-            result = callable(*args)
+    args = ', '.join(['a%d' % i for i in range(len(TP.TO.ARGS))])
+    source = py.code.Source(r"""
+        def wrapper(%s):    # no *args - no GIL for mallocing the tuple
             if after:
                 after()
+            # from now on we hold the GIL
+            try:
+                result = callable(%s)
+            except Exception, e:
+                os.write(2,
+                    "Warning: uncaught exception in callback: %%s %%s\n" %%
+                    (str(callable), str(e)))
+                result = errorcode
+            if before:
+                before()
+            # here we don't hold the GIL any more. As in the wrapper() produced
+            # by llexternal, it is essential that no exception checking occurs
+            # after the call to before().
             return result
-        except Exception, e:
-            if after:
-                after()
-            os.write(2, "Warning: uncaught exception in callback: %s %s\n" % (str(callable), str(e)))
-            return errorcode
-    return wrapper
+    """ % (args, args))
+    miniglobals = locals().copy()
+    miniglobals['Exception'] = Exception
+    miniglobals['os'] = os
+    exec source.compile() in miniglobals
+    return miniglobals['wrapper']
 _make_wrapper_for._annspecialcase_ = 'specialize:memo'
 
 AroundFnPtr = lltype.Ptr(lltype.FuncType([], lltype.Void))



More information about the Pypy-commit mailing list