Python/ast.c
cpython 3.14 @ ab2d84fe1023/Python/ast.c
AST validation and docstring extraction. _PyAST_Validate is called
in debug builds after a successful parse to check invariants that the
PEG grammar cannot express: legal constant types, valid expression
contexts, unique keyword names in match patterns, and structural
constraints on comprehensions, walrus targets, and starred expressions.
_PyAST_GetDocString extracts the leading string literal from a
function, class, or module body.
The bulk of the file is a set of validate_* functions, one per AST
node family. They recurse through the tree and return 0 on the first
violation. This file has no effect on correct parses; it exists to
catch manually-constructed or malformed AST trees early, with a clear
error message instead of a crash in the compiler.
Map
| Lines | Symbol | Role | gopy |
|---|---|---|---|
| 14-51 | recursion guard macro, forward declarations | validate_stmts, validate_exprs, validate_patterns, validate_type_params forward decls plus recursion depth check. | compile/ast_validate.go |
| 52-156 | validate_name, validate_comprehension, validate_keywords, validate_args, validate_arguments | Structural validators for sub-nodes shared by multiple parent kinds. | compile/ast_validate.go |
| 157-208 | validate_constant | Verify that Constant.value is one of the legal Python constant types. | compile/ast_validate.go:validateConstant |
| 210-416 | validate_expr | Switch over all 37 expression kinds; checks context, targets, and structural invariants. | compile/ast_validate.go:validateExpr |
| 417-538 | ensure_literal_* helpers | Validate that match-case constant patterns contain only literal values. | compile/ast_validate.go |
| 539-701 | validate_capture, validate_pattern, validate_pattern_match_value | Match-statement pattern tree validation, including star_ok threading. | compile/ast_validate.go:validatePattern |
| 702-953 | validate_assignlist, validate_body, validate_stmt | Statement validation: switch over all statement kinds, check targets, bodies, and handlers. | compile/ast_validate.go:validateStmt |
| 954-1045 | validate_stmts, validate_exprs, validate_patterns, validate_typeparam, validate_type_params | Sequence validators and PEP 695 type parameter validation. | compile/ast_validate.go |
| 1047-1075 | _PyAST_Validate | Public entry. Dispatches on mod kind and drives the recursive walk. | compile/ast_validate.go:Validate |
| 1077-1091 | _PyAST_GetDocString | Extract body[0] if it is Expr(Constant(str)). | compile/codegen_stmt.go:getDocString |
Reading
validate_constant (lines 157 to 208)
cpython 3.14 @ ab2d84fe1023/Python/ast.c#L157-208
static int
validate_constant(struct validator *state, PyObject *value)
{
if (value == Py_None || value == Py_Ellipsis) {
return 1;
}
if (PyBool_Check(value)) {
return 1;
}
if (PyLong_CheckExact(value) || PyFloat_CheckExact(value) ||
PyComplex_CheckExact(value) || PyUnicode_CheckExact(value) ||
PyBytes_CheckExact(value)) {
return 1;
}
if (PyTuple_CheckExact(value)) {
Py_ssize_t i;
for (i = 0; i < PyTuple_GET_SIZE(value); i++) {
if (!validate_constant(state, PyTuple_GET_ITEM(value, i))) {
return 0;
}
}
return 1;
}
if (PyFrozenSet_Check(value)) {
...
return 1;
}
PyErr_Format(PyExc_SystemError,
"invalid constant value %R", value);
return 0;
}
The legal constant types are None, Ellipsis, bool (checked
before int because bool is a subclass of int), int, float,
complex, str, bytes, tuple (validated recursively), and
frozenset. Any other type raises SystemError with the repr of the
value. In practice this fires only when code constructs an ast.Constant
node by hand with an illegal value; the PEG parser never emits a bad
constant.