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

fijal at codespeak.net fijal at codespeak.net
Sun Jan 13 18:31:58 CET 2008


Author: fijal
Date: Sun Jan 13 18:31:56 2008
New Revision: 50563

Modified:
   pypy/branch/applevel-ctypes2/pypy/lib/_ctypes/__init__.py
   pypy/branch/applevel-ctypes2/pypy/lib/_ctypes/array.py
   pypy/branch/applevel-ctypes2/pypy/lib/_ctypes/basics.py
Log:
Wack enough to make test_array pass.


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 18:31:56 2008
@@ -1,10 +1,10 @@
 from _ctypes.dummy import Union
-from _ctypes.dummy import ArgumentError, addressof
+from _ctypes.dummy import ArgumentError
 from _ctypes.dummy import resize
 from _ctypes.dummy import _memmove_addr, _memset_addr, _string_at_addr
 from _ctypes.dummy import _cast_addr
 
-from _ctypes.basics import _CData, sizeof, alignment, byref
+from _ctypes.basics import _CData, sizeof, alignment, byref, addressof
 from _ctypes.primitive import _SimpleCData
 from _ctypes.pointer import _Pointer
 from _ctypes.function import CFuncPtr

Modified: pypy/branch/applevel-ctypes2/pypy/lib/_ctypes/array.py
==============================================================================
--- pypy/branch/applevel-ctypes2/pypy/lib/_ctypes/array.py	(original)
+++ pypy/branch/applevel-ctypes2/pypy/lib/_ctypes/array.py	Sun Jan 13 18:31:56 2008
@@ -1,7 +1,7 @@
 
 import _ffi
 
-from _ctypes.basics import _CData
+from _ctypes.basics import _CData, cdata_from_address
 
 class ArrayMeta(type):
     def __new__(self, name, cls, typedict):
@@ -10,10 +10,29 @@
         if '_type_' in typedict:
             ffiarray = _ffi.Array(typedict['_type_']._ffiletter)
             res._ffiarray = ffiarray
+            if typedict['_type_']._type_ == 'c':
+                def getvalue(self):
+                    res = []
+                    i = 0
+                    while i < self._length_ and self[i] != '\x00':
+                        res.append(self[i])
+                        i += 1
+                    return "".join(res)
+                def setvalue(self, val):
+                    for i in range(min(len(val), self._length_)):
+                        self[i] = val[i]
+                    self[len(val)] = '\x00'
+                res.value = property(getvalue, setvalue)
+
+                def getraw(self):
+                    return "".join([self[i] for i in range(self._length_)])
+                res.raw = property(getraw)
         else:
             res._ffiarray = None
         return res
 
+    from_address = cdata_from_address
+
 class Array(_CData):
     __metaclass__ = ArrayMeta
 
@@ -22,11 +41,49 @@
         for i, arg in enumerate(args):
             self[i] = arg
 
+    def _fix_item(self, item):
+        if item >= self._length_:
+            raise IndexError
+        if item < 0:
+            return self._length_ + item
+        return item
+
+    def _get_slice_params(self, item):
+        if item.step is not None:
+            raise TypeError("3 arg slices not supported (for no reason)")
+        start = item.start or 0
+        stop = item.stop or self._length_
+        return start, stop
+    
+    def _slice_setitem(self, item, value):
+        start, stop = self._get_slice_params(item)
+        for i in range(start, stop):
+            self[i] = value[i - start]
+
+    def _slice_getitem(self, item):
+        start, stop = self._get_slice_params(item)
+        return "".join([self[i] for i in range(start, stop)])
+    
     def __setitem__(self, item, value):
-        xxx
+        if isinstance(item, slice):
+            self._slice_setitem(item, value)
+        # XXX
+        from ctypes import _SimpleCData
+        if isinstance(value, _SimpleCData):
+            value = value.value
+        item = self._fix_item(item)
+        if self._type_._ffiletter == 'c' and len(value) > 1:
+            raise TypeError("Expected strings of length 1")
+        self._array[item] = value
 
     def __getitem__(self, item):
-        xxx
+        if isinstance(item, slice):
+            return self._slice_getitem(item)
+        item = self._fix_item(item)
+        return self._array[item]
+
+    def __len__(self):
+        return self._length_
 
 ARRAY_CACHE = {}
 

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 18:31:56 2008
@@ -39,5 +39,10 @@
 
 def cdata_from_address(self, address):
     instance = self.__new__(self)
-    instance._array = self._ffiarray.fromaddress(address, 1)
+    lgt = getattr(self, '_length_', 1)
+    instance._array = self._ffiarray.fromaddress(address, lgt)
     return instance
+
+def addressof(tp):
+    # XXX we should have a method on each..
+    return tp._array.buffer



More information about the Pypy-commit mailing list