Abstract Syntax Tree (xonsh.ast)

The xonsh abstract syntax tree node.

class xonsh.ast.CtxAwareTransformer(parser)[source]

Transforms a xonsh AST based to use subprocess calls when the first name in an expression statement is not known in the context. This assumes that the expression statement is instead parseable as a subprocess.


A parse instance to try to parse subprocess statements with.

ctxadd(self, value)[source]

Adds a value the most recent context.

ctxremove(self, value)[source]

Removes a value the most recent context.

ctxupdate(self, iterable)[source]

Updated the most recent context.

ctxvisit(self, node, inp, ctx, mode='exec', filename=None, debug_level=0)[source]

Transforms the node in a context-dependent way.


A syntax tree to transform.


The input code in string format.


The root context to use.

filenamestr, optional

File we are to transform.

debug_levelint, optional

Debugging level to use in lexing and parsing.


The transformed node.

generic_visit(self, node)

Called if no explicit visitor function exists for a node.

is_in_scope(self, node)[source]

Determines whether or not the current node is in scope.

try_subproc_toks(self, node, strip_expr=False)[source]

Tries to parse the line of the node as a subprocess.

visit(self, node)

Visit a node.

visit_AnnAssign(self, node)

Handle visiting an assignment statement.

visit_Assign(self, node)[source]

Handle visiting an assignment statement.

visit_BoolOp(self, node)[source]

Handle visiting an boolean operands, like and/or.

visit_ClassDef(self, node)[source]

Handle visiting a class definition.

visit_Delete(self, node)[source]

Handle visiting a del statement.

visit_Expr(self, node)[source]

Handle visiting an expression.

visit_Expression(self, node)[source]

Handle visiting an expression body.

visit_For(self, node)[source]

Handle visiting a for statement.

visit_FunctionDef(self, node)[source]

Handle visiting a function definition.

visit_Global(self, node)[source]

Handle visiting a global statement.

visit_Import(self, node)[source]

Handle visiting a import statement.

visit_ImportFrom(self, node)[source]

Handle visiting a “from … import …” statement.

visit_Try(self, node)[source]

Handle visiting a try statement.

visit_UnaryOp(self, node)[source]

Handle visiting an unary operands, like not.

visit_With(self, node)[source]

Handle visiting a with statement.


Returns the names present in the node’s tree in a set of load nodes and a set of store nodes.


Returns the set of all names present in the node’s tree.

xonsh.ast.get_col(node, default=-1)[source]

Gets the col_offset of a node, or returns the default

xonsh.ast.get_id(node, default=None)[source]

Gets the id attribute of a node, or returns a default.


Gets the id and attribute of a node, or returns a default.

xonsh.ast.get_lineno(node, default=0)[source]

Gets the lineno of a node or returns the default.


Tests if x is an AST node with elements.


Determines whether or not a node is worth visiting. Currently only UnaryOp and BoolOp nodes are visited.

xonsh.ast.isexpression(node, ctx=None, *args, **kwargs)[source]

Determines whether a node (or code string) is an expression, and does not contain any statements. The execution context (ctx) and other args and kwargs are passed down to the parser, as needed.


Attempts to find the first name in the tree.

xonsh.ast.load_attribute_chain(name, lineno=None, col=None)[source]

Creates an AST that loads variable name that may (or may not) have attribute chains. For example, “a.b.c”


Returns the maximum col_offset of the node and all sub-nodes.


Computes the maximum lineno.


Computes the minimum col_offset.


Computes the minimum lineno.


The length of a node as a string

xonsh.ast.pdump(s, **kwargs)[source]

performs a pretty dump of an AST node.

xonsh.ast.pprint_ast(s, *, sep=None, end=None, file=None, flush=False, **kwargs)[source]

Performs a pretty print of the AST nodes.

xonsh.ast.xonsh_call(name, args, lineno=None, col=None)[source]

Creates the AST node for calling a function of a given name. Functions names may contain attribute access, e.g. __xonsh__.env.