[pypy-svn] r45028 - in pypy/dist/pypy/rpython/lltypesystem: . test

arigo at codespeak.net arigo at codespeak.net
Fri Jul 13 16:48:22 CEST 2007


Author: arigo
Date: Fri Jul 13 16:48:22 2007
New Revision: 45028

Modified:
   pypy/dist/pypy/rpython/lltypesystem/compactlltype.py
   pypy/dist/pypy/rpython/lltypesystem/test/test_compactlltype.py
Log:
(lac, arigo)
Some progress on compactlltype, which is again going to be changed anyway :-/


Modified: pypy/dist/pypy/rpython/lltypesystem/compactlltype.py
==============================================================================
--- pypy/dist/pypy/rpython/lltypesystem/compactlltype.py	(original)
+++ pypy/dist/pypy/rpython/lltypesystem/compactlltype.py	Fri Jul 13 16:48:22 2007
@@ -37,15 +37,13 @@
                 _allocated.append(storage)
                 return _ctypes_struct(S, storage)
             else:
-                XXX
                 if n is None:
                     raise TypeError("%r is variable-sized" % (S,))
-                smallercls = build_ctypes_struct(S, n)
-                smallstruct = smallercls()
-                getattr(smallstruct, S._arrayfld).length = n
-                structptr = ctypes.cast(ctypes.pointer(smallstruct),
-                                        ctypes.POINTER(cls))
-                return structptr
+                biggercls = build_ctypes_struct(S, n)
+                bigstruct = biggercls()
+                _allocated.append(bigstruct)
+                getattr(bigstruct, S._arrayfld).length = n
+                return _ctypes_struct(S, bigstruct)
         malloc = classmethod(malloc)
 
     CStruct.__name__ = 'ctypes_%s' % (S,)
@@ -130,11 +128,24 @@
             raise RuntimeError("lltype.free() on a pointer that was not "
                                "obtained by lltype.malloc()")
 
+    def _convert_to_ctypes_pointer(self):
+        if self._TYPE._is_varsize():
+            PtrType = ctypes.POINTER(get_ctypes_type(self._TYPE))
+            return ctypes.cast(ctypes.pointer(self._ctypes_storage), PtrType)
+        else:
+            return ctypes.pointer(self._ctypes_storage)
+
 
 class _ctypes_struct(_ctypes_parentable):
     _kind = "structure"
     __slots__ = ()
 
+    def __init__(self, TYPE, ctypes_storage):
+        _ctypes_parentable.__init__(self, TYPE, ctypes_storage)
+        if TYPE._arrayfld is not None:
+            array = getattr(ctypes_storage, TYPE._arrayfld)
+            assert array.length == len(array.items)
+
     def __repr__(self):
         return '<ctypes struct %s at 0x%x>' % (
             self._TYPE._name,
@@ -156,9 +167,6 @@
             return ctypes2lltype(getattr(self._ctypes_storage, field_name),
                                  getattr(self._TYPE, field_name))
 
-    def _convert_to_ctypes_pointer(self):
-        return ctypes.pointer(self._ctypes_storage)
-
 class _ctypes_array(_ctypes_parentable):
     _kind = "array"
     __slots__ = ()
@@ -183,10 +191,6 @@
         return ctypes2lltype(self._ctypes_storage.items[index],
                              self._TYPE.OF)
 
-    def _convert_to_ctypes_pointer(self):
-        PtrType = ctypes.POINTER(get_ctypes_type(self._TYPE))
-        return ctypes.cast(ctypes.pointer(self._ctypes_storage), PtrType)
-
 # ____________________________________________________________
 
 def malloc(T, n=None, flavor='gc', immortal=False):

Modified: pypy/dist/pypy/rpython/lltypesystem/test/test_compactlltype.py
==============================================================================
--- pypy/dist/pypy/rpython/lltypesystem/test/test_compactlltype.py	(original)
+++ pypy/dist/pypy/rpython/lltypesystem/test/test_compactlltype.py	Fri Jul 13 16:48:22 2007
@@ -120,14 +120,12 @@
     iwfreelist(l)
 
 def test_varsizestruct():
-    S1 = GcStruct("s1", ('a', Signed), ('rest', Array(('v', Signed))))
-    py.test.raises(TypeError, "malloc(S1)")
-    s1 = malloc(S1, 4)
+    S1 = Struct("s1", ('a', Signed), ('rest', Array(('v', Signed))))
+    py.test.raises(TypeError, "malloc(S1, flavor='raw')")
+    s1 = malloc(S1, 4, flavor='raw')
     s1.a = 0
     assert s1.a == 0
-    assert isweak(s1.rest, S1.rest)
     assert len(s1.rest) == 4
-    assert isweak(s1.rest[0], S1.rest.OF)
     s1.rest[0].v = 0
     assert typeOf(s1.rest[0].v) == Signed
     assert s1.rest[0].v == 0
@@ -138,35 +136,31 @@
     s1.rest[3].v = 5
     assert s1.a == 17
     assert s1.rest[3].v == 5
-
-    py.test.raises(TypeError, "Struct('invalid', ('rest', Array(('v', Signed))), ('a', Signed))")
-    py.test.raises(TypeError, "Struct('invalid', ('rest', GcArray(('v', Signed))), ('a', Signed))")
-    py.test.raises(TypeError, "Struct('invalid', ('x', Struct('s1', ('a', Signed), ('rest', Array(('v', Signed))))))")
-    py.test.raises(TypeError, "Struct('invalid', ('x', S1))")
+    free(s1, flavor='raw')
 
 def test_substructure_ptr():
+    objcount = getobjcount()
     S3 = Struct("s3", ('a', Signed))
     S2 = Struct("s2", ('s3', S3))
-    S1 = GcStruct("s1", ('sub1', S2), ('sub2', S2))
-    p1 = malloc(S1)
-    assert isweak(p1.sub1, S2)
-    assert isweak(p1.sub2, S2)
-    assert isweak(p1.sub1.s3, S3)
-    p2 = p1.sub1
-    assert isweak(p2.s3, S3)
-
-def test_gc_substructure_ptr():
-    S1 = GcStruct("s2", ('a', Signed))
-    S2 = Struct("s3", ('a', Signed))
-    S0 = GcStruct("s1", ('sub1', S1), ('sub2', S2))
-    p1 = malloc(S0)
-    assert typeOf(p1.sub1) == Ptr(S1)
-    assert isweak(p1.sub2, S2)
+    S1 = Struct("s1", ('sub1', S2), ('sub2', S2))
+    p1 = malloc(S1, flavor='raw')
+    p1.sub1.s3.a = 123
+    p1.sub2.s3.a = 456
+    assert p1.sub1.s3.a == 123
+    assert p1.sub2.s3.a == 456
+    p2 = p1.sub2
+    py.test.raises(RuntimeError, "free(p2, flavor='raw')")
+    free(p1, flavor='raw')
+    py.test.raises(RuntimeError, "free(p1, flavor='raw')")
+
+    p2 = malloc(S1, flavor='raw')
+    free(p2.sub1, flavor='raw')
+    assert objcount == getobjcount()    # check for leaks
 
 def test_cast_simple_widening():
     S2 = Struct("s2", ('a', Signed))
     S1 = Struct("s1", ('sub1', S2), ('sub2', S2))
-    p1 = malloc(S1, immortal=True)
+    p1 = malloc(S1, flavor='raw')
     p2 = p1.sub1
     p3 = p2
     assert typeOf(p3) == Ptr(S2)



More information about the Pypy-commit mailing list