[pypy-svn] pypy fast-forward: ctypes: implement the new "use_errno" protocol.

amauryfa commits-noreply at bitbucket.org
Fri Jan 14 18:24:55 CET 2011


Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: fast-forward
Changeset: r40688:964669bb3f60
Date: 2011-01-14 16:05 +0100
http://bitbucket.org/pypy/pypy/changeset/964669bb3f60/

Log:	ctypes: implement the new "use_errno" protocol.

diff --git a/lib_pypy/_ctypes/builtin.py b/lib_pypy/_ctypes/builtin.py
--- a/lib_pypy/_ctypes/builtin.py
+++ b/lib_pypy/_ctypes/builtin.py
@@ -1,5 +1,6 @@
 
 import _rawffi, sys
+import threading
 
 class ConvMode:
     encoding = 'ascii'
@@ -26,3 +27,25 @@
     cobj = ctypes.c_void_p.from_param(addr)
     arg = cobj._get_buffer_value()
     return _rawffi.wcharp2rawunicode(arg, lgt)
+
+class ErrorObject(threading.local):
+    def __init__(self):
+        self.errno = 0
+        self.winerror = 0
+_error_object = ErrorObject()
+
+def get_errno():
+    return _error_object.errno
+
+def set_errno(errno):
+    old_errno = _error_object.errno
+    _error_object.errno = errno
+    return old_errno
+
+def get_last_error():
+    return _error_object.winerror
+
+def set_last_error(winerror):
+    old_winerror = _error_object.winerror
+    _error_object.winerror = winerror
+    return old_winerror

diff --git a/lib_pypy/_ctypes/__init__.py b/lib_pypy/_ctypes/__init__.py
--- a/lib_pypy/_ctypes/__init__.py
+++ b/lib_pypy/_ctypes/__init__.py
@@ -23,10 +23,10 @@
 
 from _rawffi import FUNCFLAG_STDCALL, FUNCFLAG_CDECL, FUNCFLAG_PYTHONAPI
 from _rawffi import FUNCFLAG_USE_ERRNO, FUNCFLAG_USE_LASTERROR
-from _rawffi import get_errno, set_errno
 
+from _ctypes.builtin import get_errno, set_errno
 if _os.name in ("nt", "ce"):
-    from _rawffi import get_last_error, set_last_error
+    from _ctypes.builtin import get_last_error, set_last_error
 
 __version__ = '1.1.0'
 #XXX platform dependant?

diff --git a/lib_pypy/_ctypes/function.py b/lib_pypy/_ctypes/function.py
--- a/lib_pypy/_ctypes/function.py
+++ b/lib_pypy/_ctypes/function.py
@@ -1,6 +1,7 @@
 
 from _ctypes.basics import _CData, _CDataMeta, cdata_from_address
 from _ctypes.basics import ArgumentError, keepalive_key
+from _ctypes.builtin import set_errno, set_last_error
 import _rawffi
 import sys
 import traceback
@@ -210,8 +211,18 @@
         
         restype = self._restype_
         funcptr = self._getfuncptr(argtypes, restype, thisarg)
-        resbuffer = funcptr(*[arg._get_buffer_for_param()._buffer
-                              for arg in args])
+        if self._flags_ & _rawffi.FUNCFLAG_USE_ERRNO:
+            set_errno(_rawffi.get_errno())
+        if self._flags_ & _rawffi.FUNCFLAG_USE_LASTERROR:
+            set_last_error(_rawffi.get_last_error())
+        try:
+            resbuffer = funcptr(*[arg._get_buffer_for_param()._buffer
+                                  for arg in args])
+        finally:
+            if self._flags_ & _rawffi.FUNCFLAG_USE_ERRNO:
+                set_errno(_rawffi.get_errno())
+            if self._flags_ & _rawffi.FUNCFLAG_USE_LASTERROR:
+                set_last_error(_rawffi.get_last_error())
         result = self._build_result(restype, resbuffer, argtypes, args)
 
         # The 'errcheck' protocol


More information about the Pypy-commit mailing list