[pypy-svn] r22570 - pypy/branch/ast-experiments/pypy/module/recparser/hooksamples

adim at codespeak.net adim at codespeak.net
Tue Jan 24 10:55:57 CET 2006


Author: adim
Date: Tue Jan 24 10:55:56 2006
New Revision: 22570

Modified:
   pypy/branch/ast-experiments/pypy/module/recparser/hooksamples/tracer.py
Log:
updated assign tracer example with insert_before / insert_after

Modified: pypy/branch/ast-experiments/pypy/module/recparser/hooksamples/tracer.py
==============================================================================
--- pypy/branch/ast-experiments/pypy/module/recparser/hooksamples/tracer.py	(original)
+++ pypy/branch/ast-experiments/pypy/module/recparser/hooksamples/tracer.py	Tue Jan 24 10:55:56 2006
@@ -6,7 +6,16 @@
 """
 
 from parser import ASTPrintnl, ASTConst, ASTName
-from parser import install_compiler_hook
+from parser import install_compiler_hook, source2ast
+
+BEFORE_LOG_SOURCE = """if '%s' in locals() or '%s' in globals():
+    print '(before) %s <--', locals().get('%s', globals().get('%s', '<XXX>'))
+"""
+AFTER_LOG_SOURCE = "print '(after) %s <--', %s"
+
+def get_statements(source):
+    module = source2ast(source)
+    return module.node.nodes
 
 class Tracer:
     def visitModule(self, module):
@@ -23,11 +32,10 @@
     def visitAssign(self, assign):
         stmt = assign.parent
         varname = assign.nodes[0].name
-        lognode = ASTPrintnl([ASTConst('%s <--' % varname), ASTName(varname)], None)
-        index = stmt.nodes.index(assign)
-        newstmts = stmt.nodes
-        newstmts.insert(index + 1, lognode)
-        stmt.nodes = newstmts
+        before_stmts = get_statements(BEFORE_LOG_SOURCE % ((varname,) * 5))
+        after_stmts = get_statements(AFTER_LOG_SOURCE % (varname, varname))
+        stmt.insert_before(assign, before_stmts)
+        stmt.insert_after(assign, after_stmts)
         return assign
 
     def __getattr__(self, attrname):



More information about the Pypy-commit mailing list