[pypy-commit] pypy ffi-backend: hg backout 842e37163ebb: added the missing cerrno.py.

arigo noreply at buildbot.pypy.org
Tue Jul 10 13:03:13 CEST 2012


Author: Armin Rigo <arigo at tunes.org>
Branch: ffi-backend
Changeset: r56012:e7ba61e78967
Date: 2012-07-10 13:02 +0200
http://bitbucket.org/pypy/pypy/changeset/e7ba61e78967/

Log:	hg backout 842e37163ebb: added the missing cerrno.py.

diff --git a/pypy/module/_cffi_backend/__init__.py b/pypy/module/_cffi_backend/__init__.py
--- a/pypy/module/_cffi_backend/__init__.py
+++ b/pypy/module/_cffi_backend/__init__.py
@@ -30,4 +30,7 @@
         'getcname': 'func.getcname',
 
         'buffer': 'cbuffer.buffer',
+
+        'get_errno': 'cerrno.get_errno',
+        'set_errno': 'cerrno.set_errno',
         }
diff --git a/pypy/module/_cffi_backend/ccallback.py b/pypy/module/_cffi_backend/ccallback.py
--- a/pypy/module/_cffi_backend/ccallback.py
+++ b/pypy/module/_cffi_backend/ccallback.py
@@ -9,6 +9,7 @@
 
 from pypy.module._cffi_backend.cdataobj import W_CData, W_CDataApplevelOwning
 from pypy.module._cffi_backend.ctypefunc import SIZE_OF_FFI_ARG
+from pypy.module._cffi_backend import cerrno
 
 # ____________________________________________________________
 
@@ -108,6 +109,7 @@
     ll_userdata - a special structure which holds necessary information
                   (what the real callback is for example), casted to VOIDP
     """
+    cerrno.save_errno()
     ll_res = rffi.cast(rffi.CCHARP, ll_res)
     unique_id = rffi.cast(lltype.Signed, ll_userdata)
     callback = global_callback_mapping.get(unique_id)
@@ -141,3 +143,4 @@
         except OSError:
             pass
         callback.write_error_return_value(ll_res)
+    cerrno.restore_errno()
diff --git a/pypy/module/_cffi_backend/ctypefunc.py b/pypy/module/_cffi_backend/ctypefunc.py
--- a/pypy/module/_cffi_backend/ctypefunc.py
+++ b/pypy/module/_cffi_backend/ctypefunc.py
@@ -13,7 +13,7 @@
 from pypy.module._cffi_backend.ctypevoid import W_CTypeVoid
 from pypy.module._cffi_backend.ctypestruct import W_CTypeStructOrUnion
 from pypy.module._cffi_backend import ctypeprim, ctypestruct, ctypearray
-from pypy.module._cffi_backend import cdataobj
+from pypy.module._cffi_backend import cdataobj, cerrno
 
 
 class W_CTypeFunc(W_CTypePtrBase):
@@ -129,10 +129,12 @@
                 argtype.convert_from_object(data, w_obj)
             resultdata = rffi.ptradd(buffer, cif_descr.exchange_result)
 
+            cerrno.restore_errno()
             clibffi.c_ffi_call(cif_descr.cif,
                                rffi.cast(rffi.VOIDP, funcaddr),
                                resultdata,
                                buffer_array)
+            cerrno.save_errno()
 
             if isinstance(self.ctitem, W_CTypeVoid):
                 w_res = space.w_None
diff --git a/pypy/module/_cffi_backend/test/_backend_test_c.py b/pypy/module/_cffi_backend/test/_backend_test_c.py
--- a/pypy/module/_cffi_backend/test/_backend_test_c.py
+++ b/pypy/module/_cffi_backend/test/_backend_test_c.py
@@ -1378,3 +1378,22 @@
     BUChar = new_primitive_type("unsigned char")
     BArray = new_array_type(new_pointer_type(BUChar), 123)
     assert getcname(BArray, "<-->") == "unsigned char<-->[123]"
+
+def test_errno():
+    BVoid = new_void_type()
+    BFunc5 = new_function_type((), BVoid)
+    f = cast(BFunc5, _testfunc(5))
+    set_errno(50)
+    f()
+    assert get_errno() == 65
+    f(); f()
+    assert get_errno() == 95
+    #
+    def cb():
+        e = get_errno()
+        set_errno(e - 6)
+    f = callback(BFunc5, cb)
+    f()
+    assert get_errno() == 89
+    f(); f()
+    assert get_errno() == 77
diff --git a/pypy/module/_cffi_backend/test/_test_lib.c b/pypy/module/_cffi_backend/test/_test_lib.c
--- a/pypy/module/_cffi_backend/test/_test_lib.c
+++ b/pypy/module/_cffi_backend/test/_test_lib.c
@@ -1,5 +1,6 @@
 #include <stdio.h>
 #include <stdarg.h>
+#include <errno.h>
 
 static char _testfunc0(char a, char b)
 {
@@ -23,6 +24,7 @@
 }
 static void _testfunc5(void)
 {
+    errno = errno + 15;
 }
 static int *_testfunc6(int *x)
 {
diff --git a/pypy/rpython/lltypesystem/ll2ctypes.py b/pypy/rpython/lltypesystem/ll2ctypes.py
--- a/pypy/rpython/lltypesystem/ll2ctypes.py
+++ b/pypy/rpython/lltypesystem/ll2ctypes.py
@@ -331,7 +331,12 @@
                 restype = None
             else:
                 restype = get_ctypes_type(T.TO.RESULT)
-            return ctypes.CFUNCTYPE(restype, *argtypes)
+            try:
+                kwds = {'use_errno': True}
+                return ctypes.CFUNCTYPE(restype, *argtypes, **kwds)
+            except TypeError:
+                # unexpected 'use_errno' argument, old ctypes version
+                return ctypes.CFUNCTYPE(restype, *argtypes)
         elif isinstance(T.TO, lltype.OpaqueType):
             return ctypes.c_void_p
         else:


More information about the pypy-commit mailing list