Skip to main content

Python/Python-ast.c

Source:

cpython 3.14 @ ab2d84fe1023/Python/Python-ast.c

Python/Python-ast.c is a machine-generated file that defines every Python-visible AST node type. It is generated by Parser/asdl_c.py from Parser/Python.asdl. Each node class is a Python heap type with a C struct for its fields, a _fields tuple for introspection, and __match_args__ for structural pattern matching. The file should not be edited directly.

Map

LinesSymbolRole
1-100Module-level init, ast_type_reduceModule init; pickle support for all AST nodes
101-500AST_object, ast_typeBase ast.AST type; _fields, _attributes, __init__
501-2000mod_*, stmt_*Module and statement node types
2001-4000expr_*Expression node types
4001-5500pattern_*, matchcasePattern matching node types (3.10+)
5501-7000comprehension, excepthandler_*, arguments, etc.Auxiliary node types
7001-8000PyAST_mod_Init, init_typesNode type initialization; module-level type table

Reading

Generation from Python.asdl

Parser/Python.asdl is a grammar-like file that lists every node type with its field names and types. Running make regen-ast invokes Parser/asdl_c.py which reads the ASDL grammar and writes Python/Python-ast.c and Include/cpython/Python-ast.h. Do not edit these generated files; edit the .asdl source instead.

// Python/Python-ast.c:501 FunctionDef type (generated)
static PyStructSequence_Field FunctionDef_fields[] = {
{"name", NULL},
{"args", NULL},
{"body", NULL},
{"decorator_list", NULL},
{"returns", NULL},
{"type_comment", NULL},
{"type_params", NULL},
{NULL}
};

AST_object base and _fields

Every node inherits from ast.AST. The _fields tuple lists field names in order; _attributes lists lineno, col_offset, end_lineno, end_col_offset. These tuples power ast.dump(), ast.fix_missing_locations(), and the ast.NodeVisitor pattern.

// Python/Python-ast.c:101 ast_type __init__
static int
ast_type_init(PyObject *self, PyObject *args, PyObject *kw)
{
/* set positional args from _fields order, then keyword args */
Py_ssize_t i, numfields = PySequence_Size(_fields);
for (i = 0; i < PyTuple_GET_SIZE(args) && i < numfields; i++) {
PyObject *fname = PyTuple_GET_ITEM(_fields, i);
if (PyObject_SetAttr(self, fname, PyTuple_GET_ITEM(args, i)) < 0)
return -1;
}
return 0;
}

match_args for structural pattern matching

Each node type sets __match_args__ to the same tuple as _fields, allowing match statements to destructure AST nodes positionally.

# CPython: Python/Python-ast.c (generated accessor)
match node:
case ast.BinOp(left=l, op=op, right=r):
... # works because BinOp.__match_args__ = ('left', 'op', 'right')

pickle support

ast_type_reduce serializes an AST node by returning (type, (), state_dict). The state dict is built from __dict__ plus the _fields and _attributes values. This allows pickle.dumps(tree) and pickle.loads(data) to round-trip the full AST.

gopy notes

Not yet ported. gopy's AST is defined in parser/pegen/ as Go structs rather than Python heap types. The Python-visible ast module (used by tools like ast.dump) would require this generated layer. It is a low-priority port target since gopy's compiler consumes the AST internally in Go rather than exposing it to Python code.