[pypy-svn] r59838 - in pypy/branch/oo-jit/pypy/rpython/lltypesystem: . test

arigo at codespeak.net arigo at codespeak.net
Sun Nov 9 18:32:30 CET 2008


Author: arigo
Date: Sun Nov  9 18:32:28 2008
New Revision: 59838

Modified:
   pypy/branch/oo-jit/pypy/rpython/lltypesystem/ll2ctypes.py
   pypy/branch/oo-jit/pypy/rpython/lltypesystem/test/test_ll2ctypes.py
Log:
Implement another case.


Modified: pypy/branch/oo-jit/pypy/rpython/lltypesystem/ll2ctypes.py
==============================================================================
--- pypy/branch/oo-jit/pypy/rpython/lltypesystem/ll2ctypes.py	(original)
+++ pypy/branch/oo-jit/pypy/rpython/lltypesystem/ll2ctypes.py	Sun Nov  9 18:32:28 2008
@@ -390,6 +390,15 @@
     def setitem(self, index, value):
         self._storage._setitem(index, value, boundscheck=False)
 
+class _array_of_known_length(_array_of_unknown_length):
+    __slots__ = ()
+
+    def getlength(self):
+        return self._storage.length
+
+    def getbounds(self):
+        return 0, self.getlength()
+
 # ____________________________________________________________
 
 # XXX THIS IS A HACK XXX
@@ -487,7 +496,8 @@
                 container = _array_of_unknown_length(T.TO)
                 container._storage = cobj.contents
             else:
-                raise NotImplementedError("array with an explicit length")
+                container = _array_of_known_length(T.TO)
+                container._storage = cobj.contents
         elif isinstance(T.TO, lltype.FuncType):
             _callable = get_ctypes_trampoline(T.TO, cobj)
             return lltype.functionptr(T.TO, getattr(cobj, '__name__', '?'),

Modified: pypy/branch/oo-jit/pypy/rpython/lltypesystem/test/test_ll2ctypes.py
==============================================================================
--- pypy/branch/oo-jit/pypy/rpython/lltypesystem/test/test_ll2ctypes.py	(original)
+++ pypy/branch/oo-jit/pypy/rpython/lltypesystem/test/test_ll2ctypes.py	Sun Nov  9 18:32:28 2008
@@ -789,6 +789,9 @@
         c1 = lltype2ctypes(a1)
         c2 = lltype2ctypes(a2)
         assert type(c1) is type(c2)
+        lltype.free(a1, flavor='raw')
+        lltype.free(a2, flavor='raw')
+        assert not ALLOCATED     # detects memory leaks in the test
 
     def test_varsized_struct(self):
         S = lltype.Struct('S', ('x', lltype.Signed),
@@ -807,3 +810,25 @@
         assert sc.contents.a.items[1] == ord('y')
         lltype.free(s1, flavor='raw')
         assert not ALLOCATED     # detects memory leaks in the test
+
+    def test_with_explicit_length(self):
+        A = lltype.Array(lltype.Signed)
+        a1 = lltype.malloc(A, 5, flavor='raw')
+        a1[0] = 42
+        c1 = lltype2ctypes(a1, normalize=False)
+        assert c1.contents.length == 5
+        assert c1.contents.items[0] == 42
+        res = ctypes2lltype(lltype.Ptr(A), c1)
+        assert len(res) == 5
+        assert res[0] == 42
+        res[0] += 1
+        assert c1.contents.items[0] == 43
+        assert a1[0] == 43
+        a1[0] += 2
+        assert c1.contents.items[0] == 45
+        assert a1[0] == 45
+        c1.contents.items[0] += 3
+        assert res[0] == 48
+        assert a1[0] == 48
+        lltype.free(a1, flavor='raw')
+        assert not ALLOCATED     # detects memory leaks in the test



More information about the Pypy-commit mailing list