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

arigo at codespeak.net arigo at codespeak.net
Tue May 31 23:32:20 CEST 2005


Author: arigo
Date: Tue May 31 23:32:20 2005
New Revision: 12953

Modified:
   pypy/dist/pypy/rpython/lltype.py
   pypy/dist/pypy/rpython/test/test_lltype.py
Log:
- split ForwardReference into a Gc and a non-gc version
- malloc(..., immortal=True) to get some non-gc'ed static storage



Modified: pypy/dist/pypy/rpython/lltype.py
==============================================================================
--- pypy/dist/pypy/rpython/lltype.py	(original)
+++ pypy/dist/pypy/rpython/lltype.py	Tue May 31 23:32:20 2005
@@ -160,13 +160,21 @@
 
 class ForwardReference(ContainerType):
     def become(self, realcontainertype):
+        if not isinstance(realcontainertype, ContainerType):
+            raise TypeError("ForwardReference can only be to a container, "
+                            "not %r" % (realcontainertype,))
+        self.__class__ = realcontainertype.__class__
+        self.__dict__ = realcontainertype.__dict__
+
+class GcForwardReference(ForwardReference):
+    def become(self, realcontainertype):
         if not isinstance(realcontainertype, GC_CONTAINER):
-            raise TypeError("ForwardReference can only be to GcStruct or "
+            raise TypeError("GcForwardReference can only be to GcStruct or "
                             "GcArray, not %r" % (realcontainertype,))
         self.__class__ = realcontainertype.__class__
         self.__dict__ = realcontainertype.__dict__
 
-GC_CONTAINER = (GcStruct, GcArray, PyObjectType, ForwardReference)
+GC_CONTAINER = (GcStruct, GcArray, PyObjectType, GcForwardReference)
 
 
 class Primitive(LowLevelType):
@@ -573,14 +581,18 @@
         return "pyobject %s" % (super(_pyobject, self).__str__(),)
 
 
-def malloc(T, n=None):
+def malloc(T, n=None, immortal=False):
     if isinstance(T, Struct):
         o = _struct(T, n)
     elif isinstance(T, Array):
         o = _array(T, n)
     else:
         raise TypeError, "malloc for Structs and Arrays only"
-    return _ptr(GcPtr(T), o)
+    if immortal:
+        T = NonGcPtr(T)
+    else:
+        T = GcPtr(T)
+    return _ptr(T, o)
 
 def functionptr(TYPE, name, **attrs):
     if not isinstance(TYPE, FuncType):

Modified: pypy/dist/pypy/rpython/test/test_lltype.py
==============================================================================
--- pypy/dist/pypy/rpython/test/test_lltype.py	(original)
+++ pypy/dist/pypy/rpython/test/test_lltype.py	Tue May 31 23:32:20 2005
@@ -252,11 +252,12 @@
     py.test.raises(TypeError, "GcStruct('a', ('x', A))")
 
 def test_forward_reference():
-    F = ForwardReference()
+    F = GcForwardReference()
     S = GcStruct('abc', ('x', GcPtr(F)))
     F.become(S)
     assert S.x == GcPtr(S)
-    py.test.raises(TypeError, "ForwardReference().become(Struct('abc'))")
+    py.test.raises(TypeError, "GcForwardReference().become(Struct('abc'))")
+    ForwardReference().become(Struct('abc'))
 
 
 def test_nullptr():



More information about the Pypy-commit mailing list