[pypy-svn] r61966 - pypy/trunk/pypy/lib/_ctypes

afa at codespeak.net afa at codespeak.net
Tue Feb 17 13:10:25 CET 2009


Author: afa
Date: Tue Feb 17 13:10:24 2009
New Revision: 61966

Modified:
   pypy/trunk/pypy/lib/_ctypes/__init__.py
   pypy/trunk/pypy/lib/_ctypes/array.py
   pypy/trunk/pypy/lib/_ctypes/basics.py
   pypy/trunk/pypy/lib/_ctypes/function.py
   pypy/trunk/pypy/lib/_ctypes/primitive.py
   pypy/trunk/pypy/lib/_ctypes/structure.py
   pypy/trunk/pypy/lib/_ctypes/union.py
Log:
ctypes: More correct support for OUT parameters:
- __ctypes_from_outparam__ is used only for out parameters, not for return values
- pass the byref() to the function, but returns the base object at the end.


Modified: pypy/trunk/pypy/lib/_ctypes/__init__.py
==============================================================================
--- pypy/trunk/pypy/lib/_ctypes/__init__.py	(original)
+++ pypy/trunk/pypy/lib/_ctypes/__init__.py	Tue Feb 17 13:10:24 2009
@@ -16,6 +16,7 @@
 if _os.name in ("nt", "ce"):
     from _rawffi import FormatError
     from _rawffi import check_HRESULT as _check_HRESULT
+    CopyComPointer = None # XXX
 
 from _rawffi import FUNCFLAG_STDCALL, FUNCFLAG_CDECL, FUNCFLAG_PYTHONAPI
 

Modified: pypy/trunk/pypy/lib/_ctypes/array.py
==============================================================================
--- pypy/trunk/pypy/lib/_ctypes/array.py	(original)
+++ pypy/trunk/pypy/lib/_ctypes/array.py	Tue Feb 17 13:10:24 2009
@@ -85,7 +85,7 @@
         res._buffer = ffiarray
         res._base = base
         res._index = index
-        return res.__ctypes_from_outparam__()
+        return res
 
     def _CData_retval(self, resbuffer):
         raise NotImplementedError

Modified: pypy/trunk/pypy/lib/_ctypes/basics.py
==============================================================================
--- pypy/trunk/pypy/lib/_ctypes/basics.py	(original)
+++ pypy/trunk/pypy/lib/_ctypes/basics.py	Tue Feb 17 13:10:24 2009
@@ -71,7 +71,7 @@
         res.__dict__['_buffer'] = resbuffer
         res.__dict__['_base'] = base
         res.__dict__['_index'] = index
-        return res.__ctypes_from_outparam__()
+        return res
 
     def _CData_retval(self, resbuffer):
         return self._CData_output(resbuffer)

Modified: pypy/trunk/pypy/lib/_ctypes/function.py
==============================================================================
--- pypy/trunk/pypy/lib/_ctypes/function.py	(original)
+++ pypy/trunk/pypy/lib/_ctypes/function.py	Tue Feb 17 13:10:24 2009
@@ -230,11 +230,17 @@
         for i, argtype in enumerate(argtypes):
             if i > 0 and self._paramflags is not None:
                 idlflag, name = self._paramflags[i-1]
-                if idlflag == 2: # OUT
+                if idlflag == 1: # IN
+                    pass
+                elif idlflag == 2: # OUT
                     import ctypes
-                    arg = ctypes.byref(argtype._type_())
-                    wrapped_args.append((arg, arg))
+                    val = argtype._type_()
+                    wrapped = (val, ctypes.byref(val))
+                    wrapped_args.append(wrapped)
                     continue
+                else:
+                    raise NotImplementedError(
+                        "paramflags = %s" % (self._paramflags[i-1],))
 
             if consumed == len(args):
                 raise TypeError("Not enough arguments")
@@ -282,18 +288,17 @@
         
         results = []
         if self._paramflags:
-            for argtype, (_, obj), paramflag in zip(argtypes[1:], argsandobjs[1:],
+            for argtype, (obj, _), paramflag in zip(argtypes[1:], argsandobjs[1:],
                                                     self._paramflags):
                 idlflag, name = paramflag
-                if idlflag == 2: # OUT
-                    val = obj.contents
-
-                    # XXX find a better way to detect pointers to pointers
-                    basetype = argtype._type_._type_
-                    if isinstance(basetype, str) and basetype != 'P':
-                        val = val.value
-                        
+                if idlflag == 1: # IN
+                    pass
+                elif idlflag == 2: # OUT
+                    val = obj.__ctypes_from_outparam__()
                     results.append(val)
+                else:
+                    raise NotImplementedError(
+                        "paramflags = %s" % (paramflag,))
 
         if results:
             if len(results) == 1:

Modified: pypy/trunk/pypy/lib/_ctypes/primitive.py
==============================================================================
--- pypy/trunk/pypy/lib/_ctypes/primitive.py	(original)
+++ pypy/trunk/pypy/lib/_ctypes/primitive.py	Tue Feb 17 13:10:24 2009
@@ -257,6 +257,10 @@
         except (TypeError, ValueError):
             return super(SimpleType, self).from_param(value)
 
+    def _CData_output(self, resbuffer, base=None, index=-1):
+        output = super(SimpleType, self)._CData_output(resbuffer, base, index)
+        return output.value
+    
     def _sizeofinstances(self):
         return _rawffi.sizeof(self._type_)
 
@@ -287,6 +291,10 @@
     del _getvalue, _setvalue
 
     def __ctypes_from_outparam__(self):
+        meta = type(type(self))
+        if issubclass(meta, SimpleType) and meta != SimpleType:
+            return self
+
         return self.value
 
     def __repr__(self):

Modified: pypy/trunk/pypy/lib/_ctypes/structure.py
==============================================================================
--- pypy/trunk/pypy/lib/_ctypes/structure.py	(original)
+++ pypy/trunk/pypy/lib/_ctypes/structure.py	Tue Feb 17 13:10:24 2009
@@ -140,14 +140,14 @@
         res.__dict__['_buffer'] = ffistruct
         res.__dict__['_base'] = base
         res.__dict__['_index'] = index
-        return res.__ctypes_from_outparam__()
+        return res
     
     def _CData_retval(self, resbuffer):
         res = self.__new__(self)
         res.__dict__['_buffer'] = resbuffer
         res.__dict__['_base'] = None
         res.__dict__['_index'] = -1
-        return res.__ctypes_from_outparam__()
+        return res
 
 class Structure(_CData):
     __metaclass__ = StructureMeta

Modified: pypy/trunk/pypy/lib/_ctypes/union.py
==============================================================================
--- pypy/trunk/pypy/lib/_ctypes/union.py	(original)
+++ pypy/trunk/pypy/lib/_ctypes/union.py	Tue Feb 17 13:10:24 2009
@@ -72,14 +72,14 @@
         res.__dict__['_buffer'] = ffiopaque
         res.__dict__['_base'] = base
         res.__dict__['_index'] = index
-        return res.__ctypes_from_outparam__()
+        return res
     
     def _CData_retval(self, resbuffer):
         res = self.__new__(self)
         res.__dict__['_buffer'] = resbuffer
         res.__dict__['_base'] = None
         res.__dict__['_index'] = -1
-        return res.__ctypes_from_outparam__()
+        return res
 
 
 class Union(_CData):



More information about the Pypy-commit mailing list