[pypy-svn] r50560 - pypy/branch/applevel-ctypes2/pypy/lib/_ctypes

fijal at codespeak.net fijal at codespeak.net
Sun Jan 13 17:03:15 CET 2008


Author: fijal
Date: Sun Jan 13 17:03:14 2008
New Revision: 50560

Modified:
   pypy/branch/applevel-ctypes2/pypy/lib/_ctypes/__init__.py
   pypy/branch/applevel-ctypes2/pypy/lib/_ctypes/basics.py
   pypy/branch/applevel-ctypes2/pypy/lib/_ctypes/dummy.py
   pypy/branch/applevel-ctypes2/pypy/lib/_ctypes/function.py
   pypy/branch/applevel-ctypes2/pypy/lib/_ctypes/pointer.py
   pypy/branch/applevel-ctypes2/pypy/lib/_ctypes/primitive.py
Log:
(arigo, fijal) Sort out ctypes a bit. Fix pointer situation, start arrays.


Modified: pypy/branch/applevel-ctypes2/pypy/lib/_ctypes/__init__.py
==============================================================================
--- pypy/branch/applevel-ctypes2/pypy/lib/_ctypes/__init__.py	(original)
+++ pypy/branch/applevel-ctypes2/pypy/lib/_ctypes/__init__.py	Sun Jan 13 17:03:14 2008
@@ -1,4 +1,4 @@
-from _ctypes.dummy import Union, Array
+from _ctypes.dummy import Union
 from _ctypes.dummy import ArgumentError, addressof
 from _ctypes.dummy import resize
 from _ctypes.dummy import _memmove_addr, _memset_addr, _string_at_addr
@@ -10,7 +10,7 @@
 from _ctypes.function import CFuncPtr
 from _ctypes.dll import dlopen
 from _ctypes.structure import Structure
-
+from _ctypes.array import Array
 
 __version__ = '1.0.2'
 #XXX platform dependant?

Modified: pypy/branch/applevel-ctypes2/pypy/lib/_ctypes/basics.py
==============================================================================
--- pypy/branch/applevel-ctypes2/pypy/lib/_ctypes/basics.py	(original)
+++ pypy/branch/applevel-ctypes2/pypy/lib/_ctypes/basics.py	Sun Jan 13 17:03:14 2008
@@ -4,7 +4,8 @@
 class _CData(object):
     """ The most basic object for all ctypes types
     """
-    pass
+    def __ctypes_from_outparam__(self):
+        return self
 
 class CArgObject(object):
     def __init__(self, letter, raw_value, _type):
@@ -36,3 +37,7 @@
         raise TypeError("expected CData instance")
     return pointer(cdata)._as_ffi()
 
+def cdata_from_address(self, address):
+    instance = self.__new__(self)
+    instance._array = self._ffiarray.fromaddress(address, 1)
+    return instance

Modified: pypy/branch/applevel-ctypes2/pypy/lib/_ctypes/dummy.py
==============================================================================
--- pypy/branch/applevel-ctypes2/pypy/lib/_ctypes/dummy.py	(original)
+++ pypy/branch/applevel-ctypes2/pypy/lib/_ctypes/dummy.py	Sun Jan 13 17:03:14 2008
@@ -4,9 +4,6 @@
 class Union(object):
     __metaclass__ = UnionType
 
-class Array(type):
-    pass
-
 class ArgumentError(Exception):
     pass
 

Modified: pypy/branch/applevel-ctypes2/pypy/lib/_ctypes/function.py
==============================================================================
--- pypy/branch/applevel-ctypes2/pypy/lib/_ctypes/function.py	(original)
+++ pypy/branch/applevel-ctypes2/pypy/lib/_ctypes/function.py	Sun Jan 13 17:03:14 2008
@@ -13,7 +13,6 @@
     _restype = None
     def __init__(self, stuff):
         if isinstance(stuff, tuple):
-            print stuff
             name, dll = stuff
             self.name = name
             self.dll = dll
@@ -38,7 +37,7 @@
             return res
         else:
             # XXX pointers
-            return self.restype(address=res)
+            return self.restype(res)
 
     def _getfuncptr(self, args):
         if self._funcptr is not None:

Modified: pypy/branch/applevel-ctypes2/pypy/lib/_ctypes/pointer.py
==============================================================================
--- pypy/branch/applevel-ctypes2/pypy/lib/_ctypes/pointer.py	(original)
+++ pypy/branch/applevel-ctypes2/pypy/lib/_ctypes/pointer.py	Sun Jan 13 17:03:14 2008
@@ -1,6 +1,6 @@
 
 import _ffi
-from _ctypes.basics import _CData, CArgObject
+from _ctypes.basics import _CData, CArgObject, cdata_from_address
 
 DEFAULT_VALUE = object()
 
@@ -18,15 +18,10 @@
         for k, v in d.iteritems():
             setattr(obj, k, v)
         if '_type_' in typedict:
-            ffiarray = _ffi.Array(typedict['_type_']._ffiletter)
-            def __init__(self, value=0, address=DEFAULT_VALUE):
-                if address is not DEFAULT_VALUE:
-                    self._array = ffiarray.fromaddress(address, 1)
-                elif value == 0:
-                    # null pointer
-                    self._array = ffiarray.fromaddress(0, 1)
-                else:
-                    self._array = ffiarray.fromaddress(value._array.buffer, 1)
+            ffiarray = _ffi.Array('P')
+            def __init__(self, value=0):
+                self._array = ffiarray(1)
+                self.contents = value
             obj._ffiarray = ffiarray
         else:
             def __init__(self, value=0):
@@ -34,12 +29,14 @@
         obj.__init__ = __init__
         return obj
 
+    from_address = cdata_from_address
+
     def from_param(self, param):
         # XXX think deeper about that
         if isinstance(param, CArgObject):
             return param
         else:
-            return self(address=param._array.buffer)._as_ffi()
+            return self.from_address(param._array.buffer)._as_ffi()
 
 class _Pointer(_CData):
     __metaclass__ = PointerType
@@ -50,18 +47,25 @@
     value = property(getvalue)
 
     def getcontents(self):
-        return self._type_.from_address(self._array.buffer)
+        return self._type_.from_address(self._array[0])
 
     def setcontents(self, value):
-        self._array = self._ffiarray.fromaddress(value._array.buffer, 1)
+        if isinstance(value, int):
+            self._array[0] = value
+        else:
+            self._array[0] = value._array
 
     def _as_ffi(self):
-        return CArgObject('P', self._array, type(self))
+        # XXX performance
+        return CArgObject('P', self._type_.from_address(self._array[0])._array, type(self))
 
     def __getitem__(self, item):
-        return self._array[item]
+        assert item == 0
+        return self._type_.from_address(self._array[0]).__ctypes_from_outparam__()
 
     def __setitem__(self, item, value):
-        self._array[item] = value
+        if item != 0:
+            raise IndexError
+        self._type_.from_address(self._array[item]).value = value
 
     contents = property(getcontents, setcontents)

Modified: pypy/branch/applevel-ctypes2/pypy/lib/_ctypes/primitive.py
==============================================================================
--- pypy/branch/applevel-ctypes2/pypy/lib/_ctypes/primitive.py	(original)
+++ pypy/branch/applevel-ctypes2/pypy/lib/_ctypes/primitive.py	Sun Jan 13 17:03:14 2008
@@ -2,7 +2,8 @@
 
 SIMPLE_TYPE_CHARS = "cbBhHiIlLdfuzZqQPXOv"
 
-from _ctypes.basics import _CData, CArgObject, TP_TO_FFITP
+from _ctypes.basics import _CData, CArgObject, cdata_from_address, TP_TO_FFITP
+from _ctypes.array import create_array_type
 
 class NULL(object):
     pass
@@ -40,24 +41,15 @@
         default = TP_TO_DEFAULT[tp]
         ffitp = TP_TO_FFITP.get(tp, tp)
         ffiarray = _ffi.Array(ffitp)
-        def __init__(self, value=DEFAULT_VALUE, address=DEFAULT_VALUE):
-            if address is not DEFAULT_VALUE:
-                self._array = ffiarray.fromaddress(address, 1)
-            else:
-                self._array = ffiarray(1)
-                if value is not DEFAULT_VALUE:
-                    self._array[0] = value
-        dct['__init__'] = __init__
         result = type.__new__(self, name, bases, dct)
         result._ffiletter = tp
         result._ffiarray = ffiarray
         return result
 
-    def from_address(self, address):
-        return self(address=address)        
+    from_address = cdata_from_address
 
     def __mul__(self, other):
-        pass
+        return create_array_type(self, other)
 
     def from_param(self, value):
         return self(value)._as_ffi()
@@ -66,13 +58,24 @@
     __metaclass__ = SimpleType
     _type_ = 'i'
 
+    def __init__(self, value=DEFAULT_VALUE):
+        #if address is not DEFAULT_VALUE:
+        #    self._array = ffiarray.fromaddress(address, 1)
+        #else:
+        self._array = self._ffiarray(1)
+        if value is not DEFAULT_VALUE:
+            self._array[0] = value
+
     def _getvalue(self):
         return self._array[0]
 
-    def _setvalue(self, val):
+    def _setvalue(self, value):
         self._array[0] = value
     value = property(_getvalue, _setvalue)
 
+    def __ctypes_from_outparam__(self):
+        return self._array[0]
+
     def _as_ffi(self):        
         return CArgObject(self._type_, self.value, type(self))
 



More information about the Pypy-commit mailing list