[pypy-commit] pypy gc-hooks: add two convenience methods: hooks.set to pass an instance which contains the appropriate on_gc_* methods, and hooks.reset() to set all the hooks to None

antocuni pypy.commits at gmail.com
Thu Apr 5 08:53:25 EDT 2018


Author: Antonio Cuni <anto.cuni at gmail.com>
Branch: gc-hooks
Changeset: r94253:db58a930e0f9
Date: 2018-04-05 14:52 +0200
http://bitbucket.org/pypy/pypy/changeset/db58a930e0f9/

Log:	add two convenience methods: hooks.set to pass an instance which
	contains the appropriate on_gc_* methods, and hooks.reset() to set
	all the hooks to None

diff --git a/pypy/module/gc/hook.py b/pypy/module/gc/hook.py
--- a/pypy/module/gc/hook.py
+++ b/pypy/module/gc/hook.py
@@ -91,6 +91,19 @@
         self.gc_collect.w_callable = w_obj
         self.gc_collect.fix_annotation()
 
+    def descr_set(self, space, w_obj):
+        w_a = space.getattr(w_obj, space.newtext('on_gc_minor'))
+        w_b = space.getattr(w_obj, space.newtext('on_gc_collect_step'))
+        w_c = space.getattr(w_obj, space.newtext('on_gc_collect'))
+        self.descr_set_on_gc_minor(space, w_a)
+        self.descr_set_on_gc_collect_step(space, w_b)
+        self.descr_set_on_gc_collect(space, w_c)
+
+    def descr_reset(self, space):
+        self.descr_set_on_gc_minor(space, space.w_None)
+        self.descr_set_on_gc_collect_step(space, space.w_None)
+        self.descr_set_on_gc_collect(space, space.w_None)
+
 
 class GcMinorHookAction(AsyncAction):
     total_memory_used = 0
@@ -218,6 +231,9 @@
     on_gc_collect = GetSetProperty(
         W_AppLevelHooks.descr_get_on_gc_collect,
         W_AppLevelHooks.descr_set_on_gc_collect),
+
+    set = interp2app(W_AppLevelHooks.descr_set),
+    reset = interp2app(W_AppLevelHooks.descr_reset),
     )
 
 W_GcMinorStats.typedef = TypeDef(
diff --git a/pypy/module/gc/test/test_hook.py b/pypy/module/gc/test/test_hook.py
--- a/pypy/module/gc/test/test_hook.py
+++ b/pypy/module/gc/test/test_hook.py
@@ -114,16 +114,28 @@
 
     def test_clear_queue(self):
         import gc
-        lst = []
-        def on_gc_minor(stats):        lst.append('minor')
-        def on_gc_collect_step(stats): lst.append('step')
-        def on_gc_collect(stats):      lst.append('collect')
-        gc.hooks.on_gc_minor = on_gc_minor
-        gc.hooks.on_gc_collect_step = on_gc_collect_step
-        gc.hooks.on_gc_collect = on_gc_collect
-        #
+        class MyHooks(object):
+
+            def __init__(self):
+                self.lst = []
+
+            def on_gc_minor(self, stats):
+                self.lst.append('minor')
+
+            def on_gc_collect_step(self, stats):
+                self.lst.append('step')
+
+            def on_gc_collect(self, stats):
+                self.lst.append('collect')
+
+        myhooks = MyHooks()
+        gc.hooks.set(myhooks)
         self.fire_many()
-        assert lst == ['minor', 'step', 'collect']
-        lst[:] = []
+        assert myhooks.lst == ['minor', 'step', 'collect']
+        myhooks.lst[:] = []
         self.fire_gc_minor(0, 0)
-        assert lst == ['minor']
+        assert myhooks.lst == ['minor']
+        gc.hooks.reset()
+        assert gc.hooks.on_gc_minor is None
+        assert gc.hooks.on_gc_collect_step is None
+        assert gc.hooks.on_gc_collect is None


More information about the pypy-commit mailing list