pycc.asttools package


pycc.asttools.compiler module module

Utilities for working with ast.Name nodes.


Bases: object

Generates ast.Name nodes for a given tree.

Surrogate ast.Name nodes will be given for names created by other actions such as function/definitions and function parameters.


Get a surrogate name from a function or class definition.


Get a surrogate name from a function or class definition.


Get a surrogate name from a global or nonlocal statement.


Return ast.Name values unaltered.


Get a surrogate name from a global or nonlocal statement.


Get a surrogate name using an alias.


Get a surrogate name from an argument specification.


Get surrogate name nodes from function arguments.

This method only handles the *args and **kwargs variable names. In PY2 the rest of the children for this node names. In PY3+ they are args and handled by another visitor., start=None)

Find the AST node where a name is first declared.

The search begins in the same scope as the given ast.Name node. To search within a custom path pass in an AST node as the ‘start’ parameter., declared=None)

Get the NAME_SOURCE for the given name.

If ‘declared’ is not given it will be derived from the given name.

pycc.asttools.parse module

Utilities for generating AST objects from source.

pycc.asttools.parse.parse(source, filename=u'<unknown>', mode=u'exec')

An ast.parse extension.

This function behaves identically to the standard ast.parse except that it adds parent and sibling references to each node.

pycc.asttools.references module

Utilities for adding node references to AST nodes.


Add a parent backref to all child nodes.


Add sibling references to all child nodes.

pycc.asttools.references.copy_location(new_node, old_node)

An ast.copy_location extension.

This function behaves identically to the standard ast.copy_location except that it also copies parent and sibling references.


Get the top level ast node by backtracking through the parent refs.

pycc.asttools.scope module

Containers for variable scope rules.


Generate child AST nodes that represent lexical scopes.


True if the ast node is a scope else False.


Find the AST node that represents the first enclosing scope.


Get the type of scope represented by a node.

pycc.asttools.visitor module

Utilities for iterating over child AST nodes.

class pycc.asttools.visitor.NodeTransformer(*args, **kwargs)

Bases: pycc.asttools.visitor.NodeVisitor

Alternate implementation of ast.NodeTransformer.

This implementation subclasses the non-recursive NodeVisitor from this module. This implementation should behave identically to that of the standard behaviour in how it replaces nodes.

However this implementation differs in behaviour as layed out in the documentation for the non-recursive NodeVisitor. Another major difference in behaviour is that the visit method does not return the modified AST. Instead you must rely on the fact that the node is modified in place. The standard implementation also modifies the AST in place, it simply returned the resulting value for convenience.

This implementation relies on the parent and sibling references provided by the asttools.references module.


Determine whether or not the source was altered.


Visit the nodes.

class pycc.asttools.visitor.NodeVisitor(node)

Bases: object

Alternate visitor implementation.

This NodeVisitor interface differs slightly from the standard implementation. Primarily, the AST node to visit is given during initialization rather than at visit time. This has the effect of making instances of this NodeVisitor only usable with one AST and only one time. Subsequent calls will have no effect.

This implementation provides a “generic_visit” method which can be used within individual visit methods in order to evaluate all the children of a node. This method may be used but should not be overwritten.

This implementation provides no return value from the visit method. This makes it not well suited for use in a compiler.

The benefit of this implementation is that it does not leverage function recursion. Rough benchmarks show this to be somewhere between two and four times faster than the default implementation.


Queue up all child nodes for visiting.


Visit the nodes.

This method will always start with the node given at initialization.

class pycc.asttools.visitor.NodeVisitorIter(node)

Bases: pycc.asttools.visitor.NodeVisitor

NodeVisitor subclass which produces all visitor return values.

Unlike the base NodeVisitor in this module, this subclass returns an iterable from the visit method which contains all values returned by individual visit methods.


Visit the nodes.

This method returns an iterable of values returned by visitor methods.

If multiple values are returned by a visitor they will all be included in the resulting iterable.

Module contents

Utilities for working with Python AST nodes.