NodeTransformer: how to remove nodes?

Chris Angelico rosuav at gmail.com
Mon Aug 19 07:04:27 EDT 2013


On Mon, Aug 19, 2013 at 10:19 AM, Tobias Müller <to.mueller13 at gmail.com> wrote:
> Hi
>
> I'm facing an issue with NodeTransformer, a tool used for Python AST
> manipulations.
>
> Last week I posted on stackoverflow.com, but there are no responses yet.
> Maybe someone reading the mailing list can have a look and leave me a
> response here or over there?
>
> http://stackoverflow.com/questions/18275662/python-nodetransformer-how-to-remove-nodes

I'm not familiar with NodeTransformer, but by the look of things, your
visit_For is being called (indirectly) by your own iterate_children,
which then ignores the return value. To apply changes like this,
either remove your definition of generic_visit, or have it pass
through to super():

    def generic_visit(self, node):
        """
        default behaviour
        """
        print("visiting: "+node.__class__.__name__)
        return super().generic_visit(node)

It's the code for NodeTransformer.generic_visit that does the
replacements (check out Lib/ast.py - it's down the very bottom in the
3.3 that I have), so if you bypass that, no replacement happens.

ChrisA



More information about the Python-list mailing list