1
"""this one logs simple assignments and somewhat clearly shows
2
that we need a nice API to define "joinpoints". Maybe a SAX-like
3
(i.e. event-based) API ?
5
XXX: crashes on everything else than simple assignment (AssAttr, etc.)
8
from parser import ASTPrintnl, ASTConst, ASTName, ASTAssign, ASTMutator
9
from parser import install_compiler_hook, source2ast
11
BEFORE_LOG_SOURCE = """if '%s' in locals() or '%s' in globals():
12
print '(before) %s <--', locals().get('%s', globals().get('%s', '<XXX>'))
14
AFTER_LOG_SOURCE = "print '(after) %s <--', %s"
16
def get_statements(source):
17
module = source2ast(source)
18
return module.node.nodes
20
class Tracer(ASTMutator):
21
def visitAssName(self, assname):
23
while not isinstance(assign, ASTAssign):
24
assign = assign.parent
26
varname = assname.name
27
before_stmts = get_statements(BEFORE_LOG_SOURCE % ((varname,) * 5))
28
after_stmts = get_statements(AFTER_LOG_SOURCE % (varname, varname))
29
stmt.insert_before(assign, before_stmts)
30
stmt.insert_after(assign, after_stmts)
34
def _trace(ast, enc, filename):
35
return ast.accept(Tracer())
37
install_compiler_hook(_trace)