[pypy-commit] pypy gc-hooks: turn gc_*_enabled from attributes into overrideable methods. This is needed because the normal rpython code CANNOT reference the global gchooks, else we have annotations issues: by using methods, we can write GcHooks in a way which stores the actual *_enabled flags on e.g. the AppLevelGcHooks object

antocuni pypy.commits at gmail.com
Sat Mar 31 06:07:44 EDT 2018


Author: Antonio Cuni <anto.cuni at gmail.com>
Branch: gc-hooks
Changeset: r94195:e854ddff70bd
Date: 2018-03-31 12:02 +0200
http://bitbucket.org/pypy/pypy/changeset/e854ddff70bd/

Log:	turn gc_*_enabled from attributes into overrideable methods. This is
	needed because the normal rpython code CANNOT reference the global
	gchooks, else we have annotations issues: by using methods, we can
	write GcHooks in a way which stores the actual *_enabled flags on
	e.g. the AppLevelGcHooks object

diff --git a/rpython/memory/gc/hook.py b/rpython/memory/gc/hook.py
--- a/rpython/memory/gc/hook.py
+++ b/rpython/memory/gc/hook.py
@@ -10,10 +10,14 @@
     trigger a GC collection.
     """
 
-    def __init__(self):
-        self.gc_minor_enabled = False
-        self.gc_collect_step_enabled = False
-        self.gc_collect_enabled = False
+    def is_gc_minor_enabled(self):
+        return False
+
+    def is_gc_collect_step_enabled(self):
+        return False
+
+    def is_gc_collect_enabled(self):
+        return False
 
     def on_gc_minor(self, total_memory_used, pinned_objects):
         """
@@ -43,19 +47,19 @@
 
     @rgc.no_collect
     def fire_gc_minor(self, total_memory_used, pinned_objects):
-        if self.gc_minor_enabled:
+        if self.is_gc_minor_enabled():
             self.on_gc_minor(total_memory_used, pinned_objects)
 
     @rgc.no_collect
     def fire_gc_collect_step(self, oldstate, newstate):
-        if self.gc_collect_step_enabled:
+        if self.is_gc_collect_step_enabled():
             self.on_gc_collect_step(oldstate, newstate)
 
     @rgc.no_collect
     def fire_gc_collect(self, count, arenas_count_before, arenas_count_after,
                         arenas_bytes, rawmalloc_bytes_before,
                         rawmalloc_bytes_after):
-        if self.gc_collect_enabled:
+        if self.is_gc_collect_enabled():
             self.on_gc_collect(count, arenas_count_before, arenas_count_after,
                                arenas_bytes, rawmalloc_bytes_before,
                                rawmalloc_bytes_after)
diff --git a/rpython/memory/gc/test/test_hook.py b/rpython/memory/gc/test/test_hook.py
--- a/rpython/memory/gc/test/test_hook.py
+++ b/rpython/memory/gc/test/test_hook.py
@@ -7,8 +7,20 @@
 
     def __init__(self):
         GcHooks.__init__(self)
+        self._gc_minor_enabled = False
+        self._gc_collect_step_enabled = False
+        self._gc_collect_enabled = False
         self.reset()
 
+    def is_gc_minor_enabled(self):
+        return self._gc_minor_enabled
+
+    def is_gc_collect_step_enabled(self):
+        return self._gc_collect_step_enabled
+
+    def is_gc_collect_enabled(self):
+        return self._gc_collect_enabled
+
     def reset(self):
         self.minors = []
         self.steps = []
@@ -48,7 +60,7 @@
         self.size_of_S = llmemory.raw_malloc_usage(size)
 
     def test_on_gc_minor(self):
-        self.gc.hooks.gc_minor_enabled = True
+        self.gc.hooks._gc_minor_enabled = True
         self.malloc(S)
         self.gc._minor_collection()
         assert self.gc.hooks.minors == [
@@ -66,8 +78,8 @@
 
     def test_on_gc_collect(self):
         from rpython.memory.gc import incminimark as m
-        self.gc.hooks.gc_collect_step_enabled = True
-        self.gc.hooks.gc_collect_enabled = True
+        self.gc.hooks._gc_collect_step_enabled = True
+        self.gc.hooks._gc_collect_enabled = True
         self.malloc(S)
         self.gc.collect()
         assert self.gc.hooks.steps == [
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
@@ -1410,11 +1410,17 @@
 class MyGcHooks(GcHooks):
 
     def __init__(self):
-        self.gc_minor_enabled = True
-        self.gc_collect_step_enabled = True
-        self.gc_collect_enabled = True
         self.stats = GcHooksStats()
 
+    def is_gc_minor_enabled(self):
+        return True
+
+    def is_gc_collect_step_enabled(self):
+        return True
+
+    def is_gc_collect_enabled(self):
+        return True
+
     def on_gc_minor(self, total_memory_used, pinned_objects):
         self.stats.minors += 1
 


More information about the pypy-commit mailing list