[pypy-svn] r45031 - in pypy/dist/pypy/rpython/lltypesystem: . test
arigo at codespeak.net
arigo at codespeak.net
Fri Jul 13 17:20:59 CEST 2007
Author: arigo
Date: Fri Jul 13 17:20:58 2007
New Revision: 45031
Modified:
pypy/dist/pypy/rpython/lltypesystem/lltype.py
pypy/dist/pypy/rpython/lltypesystem/lltype2ctypes.py
pypy/dist/pypy/rpython/lltypesystem/test/test_lltype2ctypes.py
Log:
(lac, arigo)
Support for arrays.
Modified: pypy/dist/pypy/rpython/lltypesystem/lltype.py
==============================================================================
--- pypy/dist/pypy/rpython/lltypesystem/lltype.py (original)
+++ pypy/dist/pypy/rpython/lltypesystem/lltype.py Fri Jul 13 17:20:58 2007
@@ -1411,6 +1411,8 @@
return 0, stop
def getitem(self, index, uninitialized_ok=False):
+ if self._ctypes_storage is not None:
+ return self._ctypes_storage._getitem(index)
try:
v = self.items[index]
if isinstance(v, _uninitialized) and not uninitialized_ok:
@@ -1425,6 +1427,9 @@
raise
def setitem(self, index, value):
+ if self._ctypes_storage is not None:
+ self._ctypes_storage._setitem(index, value)
+ return
try:
self.items[index] = value
except IndexError:
Modified: pypy/dist/pypy/rpython/lltypesystem/lltype2ctypes.py
==============================================================================
--- pypy/dist/pypy/rpython/lltypesystem/lltype2ctypes.py (original)
+++ pypy/dist/pypy/rpython/lltypesystem/lltype2ctypes.py Fri Jul 13 17:20:58 2007
@@ -63,6 +63,14 @@
return bigarray
_malloc = classmethod(_malloc)
+ def _getitem(self, index):
+ cobj = self.items[index]
+ return ctypes2lltype(cobj)
+
+ def _setitem(self, index, value):
+ cobj = lltype2ctypes(value)
+ self.items[index] = cobj
+
CArray.__name__ = 'ctypes_%s*%d' % (A, max_n)
return CArray
@@ -90,7 +98,19 @@
for field_name in STRUCT._names:
field_value = getattr(container, field_name)
delattr(container, field_name)
- setattr(cstruct, field_name, lltype2ctypes(field_value))
+ if not isinstance(field_value, lltype._uninitialized):
+ setattr(cstruct, field_name, lltype2ctypes(field_value))
+
+def convert_array(container):
+ ARRAY = container._TYPE
+ cls = get_ctypes_type(ARRAY)
+ carray = cls._malloc(container.getlength())
+ container._ctypes_storage = carray
+ for i in range(container.getlength()):
+ item_value = container.items[i] # fish fish
+ container.items[i] = None
+ if not isinstance(item_value, lltype._uninitialized):
+ carray.items[i] = lltype2ctypes(item_value)
def lltype2ctypes(llobj):
T = lltype.typeOf(llobj)
@@ -99,6 +119,8 @@
if container._ctypes_storage is None:
if isinstance(T.TO, lltype.Struct):
convert_struct(container)
+ elif isinstance(T.TO, lltype.Array):
+ convert_array(container)
else:
raise NotImplementedError(T)
return ctypes.pointer(container._ctypes_storage)
Modified: pypy/dist/pypy/rpython/lltypesystem/test/test_lltype2ctypes.py
==============================================================================
--- pypy/dist/pypy/rpython/lltypesystem/test/test_lltype2ctypes.py (original)
+++ pypy/dist/pypy/rpython/lltypesystem/test/test_lltype2ctypes.py Fri Jul 13 17:20:58 2007
@@ -8,7 +8,7 @@
assert lltype2ctypes('?') == '?'
def test_simple_struct():
- S = lltype.Struct('S', ('x', lltype.Signed))
+ S = lltype.Struct('S', ('x', lltype.Signed), ('y', lltype.Signed))
s = lltype.malloc(S, flavor='raw')
s.x = 123
sc = lltype2ctypes(s)
@@ -18,4 +18,22 @@
assert s.x == 456
s.x = 789
assert sc.contents.x == 789
+ s.y = 52
+ assert sc.contents.y == 52
lltype.free(s, flavor='raw')
+
+def test_simple_array():
+ A = lltype.Array(lltype.Signed)
+ a = lltype.malloc(A, 10, flavor='raw')
+ a[0] = 100
+ a[1] = 101
+ a[2] = 102
+ ac = lltype2ctypes(a)
+ assert isinstance(ac.contents, ctypes.Structure)
+ assert ac.contents.length == 10
+ assert ac.contents.items[1] == 101
+ ac.contents.items[2] = 456
+ assert a[2] == 456
+ a[3] = 789
+ assert ac.contents.items[3] == 789
+ lltype.free(a, flavor='raw')
More information about the Pypy-commit
mailing list