Here's a trivial little Python session which attempts to use compiler.walk
(mostly unsuccessfully) :
% python
Python 2.4.2 (#1, Feb 23 2006, 12:48:31)
[GCC 3.4.1] on sunos5
Type "help", "copyright" , "credits" or "license" for more information.
Module(None, Stmt([Assign([AssName('RED', 'OP_ASSIGN')], Const('red')), Assign([AssName('BLUE', 'OP_ASSIGN')], Const('blue'))]))
<compiler.visit or.ExampleASTVi sitor instance at 0x81f0eac>
I did manage to get a verbose print the first time I executed this:
which emitted nice verbose stuff like
dispatch Module
<compiler.visit or.ExampleASTVi sitor instance at 0x835074c>
compiler.ast.Mo dule
asList <bound method Module.asList of Module(None, Stmt([Assign([AssName('RED', 'OP_ASSIGN')], Const('red')), Assign([AssName('BLUE', 'OP_ASSIGN')], Const('blue'))]))>
doc None
getChildNode <bound method Module.getChild Nodes of Module(None, Stmt([Assign([AssName('RED', 'OP_ASSIGN')], Const('red')), Assign([AssName('BLUE', 'OP_ASSIGN')], Const('blue'))]))>
getChildren <bound method Module.getChild ren of Module(None, Stmt([Assign([AssName('RED', 'OP_ASSIGN')], Const('red')), Assign([AssName('BLUE', 'OP_ASSIGN')], Const('blue'))]))>
lineno None
...
but successive calls just yielded the more electron-conserving:
dispatch Module
dispatch Stmt
dispatch Assign
dispatch AssName
dispatch Const
dispatch Assign
dispatch AssName
dispatch Const
What am I doing wrong?
Thanks,
Skip
(mostly unsuccessfully) :
% python
Python 2.4.2 (#1, Feb 23 2006, 12:48:31)
[GCC 3.4.1] on sunos5
Type "help", "copyright" , "credits" or "license" for more information.
>>import compiler
>>ast = compiler.parse( open("a.py").re ad())
>>ast
>>ast = compiler.parse( open("a.py").re ad())
>>ast
>>compiler.walk (ast, compiler.visito r.ExampleASTVis itor, verbose=10)
>><class compiler.visito r.ExampleASTVis itor at 0x831ce3c>
>>compiler.walk (ast, compiler.visito r.ExampleASTVis itor(), verbose=10)
>><class compiler.visito r.ExampleASTVis itor at 0x831ce3c>
>>compiler.walk (ast, compiler.visito r.ExampleASTVis itor(), verbose=10)
I did manage to get a verbose print the first time I executed this:
>>compiler.walk (ast, compiler.visito r.ExampleASTVis itor(), walker=compiler .visitor.Exampl eASTVisitor(), verbose=10)
dispatch Module
<compiler.visit or.ExampleASTVi sitor instance at 0x835074c>
compiler.ast.Mo dule
asList <bound method Module.asList of Module(None, Stmt([Assign([AssName('RED', 'OP_ASSIGN')], Const('red')), Assign([AssName('BLUE', 'OP_ASSIGN')], Const('blue'))]))>
doc None
getChildNode <bound method Module.getChild Nodes of Module(None, Stmt([Assign([AssName('RED', 'OP_ASSIGN')], Const('red')), Assign([AssName('BLUE', 'OP_ASSIGN')], Const('blue'))]))>
getChildren <bound method Module.getChild ren of Module(None, Stmt([Assign([AssName('RED', 'OP_ASSIGN')], Const('red')), Assign([AssName('BLUE', 'OP_ASSIGN')], Const('blue'))]))>
lineno None
...
but successive calls just yielded the more electron-conserving:
dispatch Module
dispatch Stmt
dispatch Assign
dispatch AssName
dispatch Const
dispatch Assign
dispatch AssName
dispatch Const
What am I doing wrong?
Thanks,
Skip