[pypy-commit] pypy default: implement a simple code callback

fijal noreply at buildbot.pypy.org
Wed Nov 18 10:48:40 EST 2015


Author: fijal
Branch: 
Changeset: r80759:88e614bb406f
Date: 2015-11-18 17:48 +0200
http://bitbucket.org/pypy/pypy/changeset/88e614bb406f/

Log:	implement a simple code callback

diff --git a/pypy/interpreter/baseobjspace.py b/pypy/interpreter/baseobjspace.py
--- a/pypy/interpreter/baseobjspace.py
+++ b/pypy/interpreter/baseobjspace.py
@@ -391,6 +391,7 @@
         self.check_signal_action = None   # changed by the signal module
         self.user_del_action = UserDelAction(self)
         self._code_of_sys_exc_info = None
+        self._code_hook = None
 
         # can be overridden to a subclass
         self.initialize()
@@ -1241,6 +1242,13 @@
             self.setitem(w_globals, w_key, self.wrap(self.builtin))
         return statement.exec_code(self, w_globals, w_locals)
 
+    def new_code_hook(self, w_code):
+        if self._code_hook is not None:
+            try:
+                self.call_function(self._code_hook, w_code)
+            except OperationError, e:
+                e.write_unraisable(self, "new_code_hook()")
+
     def appexec(self, posargs_w, source):
         """ return value from executing given source at applevel.
             EXPERIMENTAL. The source must look like
diff --git a/pypy/interpreter/pycode.py b/pypy/interpreter/pycode.py
--- a/pypy/interpreter/pycode.py
+++ b/pypy/interpreter/pycode.py
@@ -86,6 +86,7 @@
         self._signature = cpython_code_signature(self)
         self._initialize()
         self._init_ready()
+        self.space.new_code_hook(self)
 
     def _initialize(self):
         if self.co_cellvars:
diff --git a/pypy/module/__pypy__/__init__.py b/pypy/module/__pypy__/__init__.py
--- a/pypy/module/__pypy__/__init__.py
+++ b/pypy/module/__pypy__/__init__.py
@@ -86,6 +86,7 @@
         'specialized_zip_2_lists'   : 'interp_magic.specialized_zip_2_lists',
         'set_debug'                 : 'interp_magic.set_debug',
         'locals_to_fast'            : 'interp_magic.locals_to_fast',
+        'set_code_callback'         : 'interp_magic.set_code_callback',
         'save_module_content_for_future_reload':
                           'interp_magic.save_module_content_for_future_reload',
     }
diff --git a/pypy/module/__pypy__/interp_magic.py b/pypy/module/__pypy__/interp_magic.py
--- a/pypy/module/__pypy__/interp_magic.py
+++ b/pypy/module/__pypy__/interp_magic.py
@@ -151,3 +151,9 @@
 def specialized_zip_2_lists(space, w_list1, w_list2):
     from pypy.objspace.std.specialisedtupleobject import specialized_zip_2_lists
     return specialized_zip_2_lists(space, w_list1, w_list2)
+
+def set_code_callback(space, w_callable):
+    if space.is_none(w_callable):
+        space._code_hook = None
+    else:
+        space._code_hook = w_callable
\ No newline at end of file
diff --git a/pypy/module/__pypy__/test/test_magic.py b/pypy/module/__pypy__/test/test_magic.py
--- a/pypy/module/__pypy__/test/test_magic.py
+++ b/pypy/module/__pypy__/test/test_magic.py
@@ -13,3 +13,21 @@
         #
         sys.dont_write_bytecode = d
         __pypy__.save_module_content_for_future_reload(sys)
+
+    def test_new_code_hook(self):
+        l = []
+
+        def callable(code):
+            l.append(code)
+
+        import __pypy__
+        __pypy__.set_code_callback(callable)
+        d = {}
+        try:
+            exec """
+def f():
+    pass
+""" in d
+        finally:
+            __pypy__.set_code_callback(None)
+        assert d['f'].__code__ in l
\ No newline at end of file


More information about the pypy-commit mailing list