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

arigo at codespeak.net arigo at codespeak.net
Mon Jan 14 18:26:38 CET 2008


Author: arigo
Date: Mon Jan 14 18:26:37 2008
New Revision: 50611

Modified:
   pypy/branch/applevel-ctypes2/pypy/lib/_ctypes/basics.py
   pypy/branch/applevel-ctypes2/pypy/lib/_ctypes/pointer.py
   pypy/branch/applevel-ctypes2/pypy/lib/_ctypes/primitive.py
Log:
* support for sizeof() on non-primitives (incomplete).
* implement _setvalue for primitives.


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	Mon Jan 14 18:26:37 2008
@@ -48,18 +48,14 @@
 #        return "<cparam '%s' %r>" % (self.ffiletter, self._array[0])
 
 
-TP_TO_FFITP = {    # XXX this should die; interp_ffi should just accept them
-        'O': 'P',
-        'z': 's',
-}
-
 def sizeof(tp):
-    ffitp = tp._type_
-    return _rawffi.sizeof(TP_TO_FFITP.get(ffitp, ffitp))
+    if not isinstance(tp, _CDataMeta):
+        raise TypeError("ctypes type expected, got %r" % (type(tp).__name__,))
+    return tp._sizeofinstances()
 
 def alignment(tp):
     ffitp = tp._type_
-    return _rawffi.alignment(TP_TO_FFITP.get(ffitp, ffitp))
+    return _rawffi.alignment(ffitp)
 
 def byref(cdata):
     from ctypes import pointer

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	Mon Jan 14 18:26:37 2008
@@ -44,6 +44,9 @@
                 return value
         return _CDataMeta.from_param(self, value)
 
+    def _sizeofinstances(self):
+        return _rawffi.sizeof('P')
+
     from_address = cdata_from_address
 
 class _Pointer(_CData):

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	Mon Jan 14 18:26:37 2008
@@ -2,7 +2,7 @@
 
 SIMPLE_TYPE_CHARS = "cbBhHiIlLdfuzZqQPXOv"
 
-from _ctypes.basics import _CData, _CDataMeta, cdata_from_address, TP_TO_FFITP
+from _ctypes.basics import _CData, _CDataMeta, cdata_from_address
 from _ctypes.array import create_array_type
 
 class NULL(object):
@@ -39,8 +39,7 @@
             tp not in SIMPLE_TYPE_CHARS):
             raise ValueError('%s is not a type character' % (tp))
         default = TP_TO_DEFAULT[tp]
-        ffitp = TP_TO_FFITP.get(tp, tp)
-        ffiarray = _rawffi.Array(ffitp)
+        ffiarray = _rawffi.Array(tp)
         result = type.__new__(self, name, bases, dct)
         result._ffiletter = tp
         result._ffiarray = ffiarray
@@ -48,18 +47,14 @@
             # c_char_p special cases
             from _ctypes import Array, _Pointer
 
-            def __init__(self, value=DEFAULT_VALUE):
+            def _getvalue(self):
+                return _rawffi.charp2string(self._array[0])
+            def _setvalue(self, value):
                 if isinstance(value, str):
                     array = _rawffi.Array('c')(len(value)+1, value)
                     value = array.buffer
                     # XXX free 'array' later
-                _SimpleCData.__init__(self, value)
-            result.__init__ = __init__
-
-            def _getvalue(self):
-                return _rawffi.charp2string(self._array[0])
-            def _setvalue(self, value):
-                xxx
+                self._array[0] = value
             result.value = property(_getvalue, _setvalue)
 
             def from_param(self, value):
@@ -90,6 +85,9 @@
         except (TypeError, ValueError):
             return super(SimpleType, self).from_param(value)
 
+    def _sizeofinstances(self):
+        return _rawffi.sizeof(self._type_)
+
 class _SimpleCData(_CData):
     __metaclass__ = SimpleType
     _type_ = 'i'
@@ -97,13 +95,13 @@
     def __init__(self, value=DEFAULT_VALUE):
         self._array = self._ffiarray(1)
         if value is not DEFAULT_VALUE:
-            self._array[0] = value
+            self.value = value
 
     def _getvalue(self):
         return self._array[0]
 
     def _setvalue(self, value):
-        xxx
+        self._array[0] = value
     value = property(_getvalue, _setvalue)
     del _getvalue, _setvalue
 



More information about the Pypy-commit mailing list