Skip to main content

Lib/ast.py

cpython 3.14 @ ab2d84fe1023/Lib/ast.py

Lib/ast.py re-exports the AST node classes from _ast (C extension) and adds parse(), literal_eval(), NodeVisitor, NodeTransformer, fix_missing_locations, and dump().

Map

LinesSymbolRole
1-80Re-exports from _astAll AST node classes
81-160parseCompile source to an AST with mode/type_comments options
161-280literal_evalSafe eval of literal expressions
281-480NodeVisitor, NodeTransformerVisitor pattern for AST traversal and rewriting
481-600fix_missing_locationsPropagate line/col from parent to child nodes
601-950dump, unparseAST to string conversion

Reading

parse

ast.parse(source, mode='exec') calls compile(source, filename, mode, ast.PyCF_ONLY_AST) which returns a parsed AST node without compiling to bytecode.

# CPython: Lib/ast.py:42 parse
def parse(source, filename="<unknown>", mode="exec", *,
type_comments=False, feature_version=None, optimize=-1):
...
return compile(source, filename, mode, flags,
dont_inherit=True, optimize=optimize)

literal_eval

literal_eval(node_or_string) evaluates a string containing only Python literals (strings, bytes, numbers, tuples, lists, dicts, sets, booleans, None). It raises ValueError for any expression that is not a literal. This is safe against code injection because it never evaluates arbitrary expressions.

# CPython: Lib/ast.py:175 literal_eval
def literal_eval(node_or_string):
...
def _convert(node):
if isinstance(node, Constant):
return node.value
elif isinstance(node, Tuple):
return tuple(map(_convert, node.elts))
...
raise ValueError('malformed node or string: ' + repr(node))
return _convert(node)

NodeVisitor

NodeVisitor.visit(node) dispatches to visit_ClassName if defined, or generic_visit otherwise. generic_visit calls visit on all child nodes. This is the standard way to traverse an AST without recursion boilerplate.

unparse

ast.unparse(node) reconstructs Python source from an AST. It is a NodeVisitor that builds a string buffer, writing parentheses where needed for operator precedence.

gopy notes

compile/ uses the AST from parser/pegen. ast.parse() is available via pythonrun.RunString with Py_file_input. literal_eval is not yet ported. NodeVisitor requires the AST node classes to be Python-visible objects, which is handled through module/ast/.