[pypy-svn] r54615 - in pypy/branch/io-improvements/pypy/rpython/lltypesystem: . test

arigo at codespeak.net arigo at codespeak.net
Sat May 10 12:43:58 CEST 2008


Author: arigo
Date: Sat May 10 12:43:56 2008
New Revision: 54615

Modified:
   pypy/branch/io-improvements/pypy/rpython/lltypesystem/llmemory.py
   pypy/branch/io-improvements/pypy/rpython/lltypesystem/lltype.py
   pypy/branch/io-improvements/pypy/rpython/lltypesystem/test/test_llmemory.py
   pypy/branch/io-improvements/pypy/rpython/lltypesystem/test/test_lltype.py
Log:
Test and fix for _subarray not keeping their array alive,
which is wrong for raw-malloced objects.


Modified: pypy/branch/io-improvements/pypy/rpython/lltypesystem/llmemory.py
==============================================================================
--- pypy/branch/io-improvements/pypy/rpython/lltypesystem/llmemory.py	(original)
+++ pypy/branch/io-improvements/pypy/rpython/lltypesystem/llmemory.py	Sat May 10 12:43:56 2008
@@ -224,6 +224,7 @@
         assert array_type_match(lltype.typeOf(arrayptr).TO, self.TYPE)
         if isinstance(self.TYPE.OF, lltype.ContainerType):
             # XXX this doesn't support empty arrays
+            # XXX it's also missing 'solid' support, probably
             o = arrayptr._obj.getitem(0)
             return o._as_ptr()
         else:

Modified: pypy/branch/io-improvements/pypy/rpython/lltypesystem/lltype.py
==============================================================================
--- pypy/branch/io-improvements/pypy/rpython/lltypesystem/lltype.py	(original)
+++ pypy/branch/io-improvements/pypy/rpython/lltypesystem/lltype.py	Sat May 10 12:43:56 2008
@@ -1512,6 +1512,11 @@
     def __init__(self, TYPE, parent, baseoffset_or_fieldname):
         _parentable.__init__(self, TYPE)
         self._setparentstructure(parent, baseoffset_or_fieldname)
+        # Keep the parent array alive, we share the same allocation.
+        # Don't do it if we are inside a GC object, though -- it's someone
+        # else's job to keep the GC object alive
+        if typeOf(top_container(parent))._gckind == 'raw':
+            self._keepparent = parent
 
     def __repr__(self):
         parent = self._wrparent()

Modified: pypy/branch/io-improvements/pypy/rpython/lltypesystem/test/test_llmemory.py
==============================================================================
--- pypy/branch/io-improvements/pypy/rpython/lltypesystem/test/test_llmemory.py	(original)
+++ pypy/branch/io-improvements/pypy/rpython/lltypesystem/test/test_llmemory.py	Sat May 10 12:43:56 2008
@@ -589,4 +589,13 @@
     a = lltype.malloc(A, flavor='raw')
     src = cast_ptr_to_adr(a) + itemoffsetof(A, 0)
     raw_memclear(src, sizeof(lltype.Signed) * 0)
-    
+
+def test_addr_keeps_object_alive():
+    A = lltype.Array(Address)
+    ptr = lltype.malloc(A, 10, immortal=True)
+    adr = cast_ptr_to_adr(ptr) + ArrayItemsOffset(A)
+    del ptr
+    import gc; gc.collect(); gc.collect()
+    # the following line crashes if the array is dead
+    ptr1 = cast_adr_to_ptr(adr, lltype.Ptr(lltype.FixedSizeArray(Address, 1)))
+    ptr1[0] = NULL

Modified: pypy/branch/io-improvements/pypy/rpython/lltypesystem/test/test_lltype.py
==============================================================================
--- pypy/branch/io-improvements/pypy/rpython/lltypesystem/test/test_lltype.py	(original)
+++ pypy/branch/io-improvements/pypy/rpython/lltypesystem/test/test_lltype.py	Sat May 10 12:43:56 2008
@@ -727,3 +727,11 @@
         setattr(s, word, i)
     for i, word in enumerate(words):
         assert getattr(s, word) == i
+
+def test_subarray_keeps_array_alive():
+    A = Array(Signed)
+    ptr = malloc(A, 10, immortal=True)
+    ptr2 = direct_arrayitems(ptr)
+    del ptr
+    import gc; gc.collect(); gc.collect()
+    ptr2[0] = 5    # crashes if the array was deallocated



More information about the Pypy-commit mailing list