[pypy-commit] pypy gc-del: In-progress: start on framework.py.

arigo noreply at buildbot.pypy.org
Mon Mar 25 19:55:20 CET 2013


Author: Armin Rigo <arigo at tunes.org>
Branch: gc-del
Changeset: r62752:df1b2661e5c8
Date: 2013-03-25 19:55 +0100
http://bitbucket.org/pypy/pypy/changeset/df1b2661e5c8/

Log:	In-progress: start on framework.py.

diff --git a/rpython/memory/gctransform/framework.py b/rpython/memory/gctransform/framework.py
--- a/rpython/memory/gctransform/framework.py
+++ b/rpython/memory/gctransform/framework.py
@@ -241,7 +241,6 @@
             [s_gc, s_typeid16,
              annmodel.SomeInteger(nonneg=True),
              annmodel.SomeBool(),
-             annmodel.SomeBool(),
              annmodel.SomeBool()], s_gcref,
             inline = False)
         if hasattr(GCClass, 'malloc_fixedsize'):
@@ -251,7 +250,6 @@
                 [s_gc, s_typeid16,
                  annmodel.SomeInteger(nonneg=True),
                  annmodel.SomeBool(),
-                 annmodel.SomeBool(),
                  annmodel.SomeBool()], s_gcref,
                 inline = False)
         else:
@@ -322,7 +320,7 @@
                 malloc_fast,
                 [s_gc, s_typeid16,
                  annmodel.SomeInteger(nonneg=True),
-                 s_False, s_False, s_False], s_gcref,
+                 s_False, s_False], s_gcref,
                 inline = True)
         else:
             self.malloc_fast_ptr = None
@@ -892,7 +890,7 @@
         c_false = rmodel.inputconst(lltype.Bool, False)
         c_has_weakptr = rmodel.inputconst(lltype.Bool, True)
         args = [self.c_const_gc, c_type_id, c_size,
-                c_false, c_false, c_has_weakptr]
+                c_false, c_has_weakptr]
 
         # push and pop the current live variables *including* the argument
         # to the weakref_create operation, which must be kept alive and
diff --git a/rpython/memory/gctypelayout.py b/rpython/memory/gctypelayout.py
--- a/rpython/memory/gctypelayout.py
+++ b/rpython/memory/gctypelayout.py
@@ -1,13 +1,14 @@
 from rpython.rtyper.lltypesystem import lltype, llmemory, llarena, llgroup
 from rpython.rtyper.lltypesystem import rclass
 from rpython.rtyper.lltypesystem.lloperation import llop
-from rpython.rlib.debug import ll_assert
+from rpython.rlib.debug import ll_assert, debug_print
 from rpython.rlib.rarithmetic import intmask
 from rpython.rlib import rgc
 from rpython.rlib.objectmodel import we_are_translated
 from rpython.tool.identity_dict import identity_dict
 
 
+
 class GCData(object):
     """The GC information tables, and the query functions that the GC
     calls to decode their content.  The encoding of this information
@@ -28,6 +29,7 @@
                                             llmemory.Address],
                                            llmemory.Address)
     DESTRUCTOR_OR_CT = lltype.Ptr(DESTRUCTOR_OR_CT_FUNC)
+    FINALIZER = lltype.Ptr(lltype.FuncType([llmemory.Address], lltype.Void))
 
     # structure describing the layout of a typeid
     TYPE_INFO = lltype.Struct("type_info",
@@ -138,15 +140,14 @@
         return infobits & T_ANY_SLOW_FLAG == 0
 
     def q_call_finalizer(self, finalizer, obj):
-        XXX
-        FINALIZER = lltype.Ptr(lltype.FuncType([llmemory.Address],
-                                               lltype.Void))
-        finalizer = llmemory.cast_adr_to_ptr(finalizer, FINALIZER)
-        finalizer(obj)
-##                except rgc.FinalizeLater:
-##                    xxx
-##                        except Exception, e:
-##                    XXX
+        finalizer = llmemory.cast_adr_to_ptr(finalizer, self.FINALIZER)
+        try:
+            finalizer(obj)
+        except rgc.FinalizeLater:
+            return False
+        except Exception:
+            debug_print("exception from finalizer", finalizer, "of", obj)
+            llop.debug_fatalerror(lltype.Void, "exception from finalizer!")
         return True
 
 
diff --git a/rpython/memory/test/test_transformed_gc.py b/rpython/memory/test/test_transformed_gc.py
--- a/rpython/memory/test/test_transformed_gc.py
+++ b/rpython/memory/test/test_transformed_gc.py
@@ -287,7 +287,7 @@
         res = run([])
         assert res == 42
 
-    def define_finalizer(cls):
+    def define_destructor(cls):
         class B(object):
             pass
         b = B()
@@ -312,42 +312,10 @@
 
     def test_finalizer(self):
         run = self.runner("finalizer")
-        res = run([5, 42]) #XXX pure lazyness here too
+        res = run([5, 42])
         assert res == 6
 
-    def define_finalizer_calls_malloc(cls):
-        class B(object):
-            pass
-        b = B()
-        b.nextid = 0
-        b.num_deleted = 0
-        class AAA(object):
-            def __init__(self):
-                self.id = b.nextid
-                b.nextid += 1
-            def __del__(self):
-                b.num_deleted += 1
-                C()
-        class C(AAA):
-            def __del__(self):
-                b.num_deleted += 1
-        def f(x, y):
-            a = AAA()
-            i = 0
-            while i < x:
-                i += 1
-                a = AAA()
-            llop.gc__collect(lltype.Void)
-            llop.gc__collect(lltype.Void)
-            return b.num_deleted
-        return f
-
-    def test_finalizer_calls_malloc(self):
-        run = self.runner("finalizer_calls_malloc")
-        res = run([5, 42]) #XXX pure lazyness here too
-        assert res == 12
-
-    def define_finalizer_resurrects(cls):
+    def define_finalizer(cls):
         class B(object):
             pass
         b = B()
@@ -357,9 +325,9 @@
             def __init__(self):
                 self.id = b.nextid
                 b.nextid += 1
-            def __del__(self):
+                rgc.register_finalizer(self.finalizer)
+            def finalizer(self):
                 b.num_deleted += 1
-                b.a = self
         def f(x, y):
             a = A()
             i = 0
@@ -368,18 +336,13 @@
                 a = A()
             llop.gc__collect(lltype.Void)
             llop.gc__collect(lltype.Void)
-            aid = b.a.id
-            b.a = None
-            # check that __del__ is not called again
-            llop.gc__collect(lltype.Void)
-            llop.gc__collect(lltype.Void)
-            return b.num_deleted * 10 + aid + 100 * (b.a is None)
+            return b.num_deleted
         return f
 
-    def test_finalizer_resurrects(self):
-        run = self.runner("finalizer_resurrects")
-        res = run([5, 42]) #XXX pure lazyness here too
-        assert 160 <= res <= 165
+    def test_finalizer(self):
+        run = self.runner("finalizer")
+        res = run([5, 42])
+        assert res == 6
 
     def define_custom_trace(cls):
         from rpython.rtyper.annlowlevel import llhelper
@@ -475,13 +438,16 @@
             def __init__(self):
                 self.id = b.nextid
                 b.nextid += 1
-            def __del__(self):
+                rgc.register_finalizer(self.finalizer)
+            def finalizer(self):
+                self.finalizer1()    # possibly-overriden method
+            def finalizer1(self):
                 llop.gc__collect(lltype.Void)
                 b.num_deleted += 1
                 C()
                 C()
         class C(A):
-            def __del__(self):
+            def finalizer1(self):
                 b.num_deleted += 1
                 b.num_deleted_c += 1
         def f(x, y):
@@ -504,7 +470,7 @@
     def test_collect_during_collect(self):
         run = self.runner("collect_during_collect")
         # runs collect recursively 4 times
-        res = run([4, 42]) #XXX pure lazyness here too
+        res = run([4, 42])
         assert res == 12
 
     def define_collect_0(cls):
@@ -782,8 +748,7 @@
                 if op.opname == 'do_malloc_fixedsize_clear':
                     op.args = [Constant(type_id, llgroup.HALFWORD),
                                Constant(llmemory.sizeof(P), lltype.Signed),
-                               Constant(False, lltype.Bool), # has_finalizer
-                               Constant(False, lltype.Bool), # is_finalizer_light
+                               Constant(False, lltype.Bool), # has_destructor
                                Constant(False, lltype.Bool)] # contains_weakptr
                     break
             else:
@@ -819,8 +784,7 @@
                 if op.opname == 'do_malloc_fixedsize_clear':
                     op.args = [Constant(type_id, llgroup.HALFWORD),
                                Constant(llmemory.sizeof(P), lltype.Signed),
-                               Constant(False, lltype.Bool), # has_finalizer
-                               Constant(False, lltype.Bool), # is_finalizer_light
+                               Constant(False, lltype.Bool), # has_destructor
                                Constant(False, lltype.Bool)] # contains_weakptr
                     break
             else:


More information about the pypy-commit mailing list