pycc.asttools package

Submodules

pycc.asttools.compiler module

pycc.asttools.name module

Utilities for working with ast.Name nodes.

class pycc.asttools.name.NameVisitorMixin

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.

visit_ClassDef(node)

Get a surrogate name from a function or class definition.

visit_FunctionDef(node)

Get a surrogate name from a function or class definition.

visit_Global(node)

Get a surrogate name from a global or nonlocal statement.

visit_Name(node)

Return ast.Name values unaltered.

visit_Nonlocal(node)

Get a surrogate name from a global or nonlocal statement.

visit_alias(node)

Get a surrogate name using an alias.

visit_arg(node)

Get a surrogate name from an argument specification.

visit_arguments(node)

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.

pycc.asttools.name.declaration(node, 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.

pycc.asttools.name.name_source(node, 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.

pycc.asttools.references.add_parent_references(node)

Add a parent backref to all child nodes.

pycc.asttools.references.add_sibling_references(node)

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.

pycc.asttools.references.get_top_node(node)

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

pycc.asttools.scope module

Containers for variable scope rules.

pycc.asttools.scope.child_scopes(node)

Generate child AST nodes that represent lexical scopes.

pycc.asttools.scope.is_scope(node)

True if the ast node is a scope else False.

pycc.asttools.scope.parent_scope(node)

Find the AST node that represents the first enclosing scope.

pycc.asttools.scope.scope_type(node)

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.

modified

Determine whether or not the source was altered.

visit()

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.

generic_visit(node)

Queue up all child nodes for visiting.

visit()

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()

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.