Skip to content

Commit 1332226

Browse files
authored
Validate the AST produced by the parser in debug mode (GH-21643)
This will improve the debug experience if something fails in the produced AST. Previously, errors in the produced AST can be felt much later like in the garbage collector or the compiler, making debugging them much more difficult.
1 parent 13efaec commit 1332226

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

Parser/pegen.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "pegen.h"
66
#include "string_parser.h"
7+
#include "ast.h"
78

89
PyObject *
910
_PyPegen_new_type_comment(Parser *p, char *s)
@@ -1137,6 +1138,14 @@ _PyPegen_run_parser(Parser *p)
11371138
return RAISE_SYNTAX_ERROR("multiple statements found while compiling a single statement");
11381139
}
11391140

1141+
#if defined(Py_DEBUG) && defined(Py_BUILD_CORE)
1142+
if (p->start_rule == Py_single_input ||
1143+
p->start_rule == Py_file_input ||
1144+
p->start_rule == Py_eval_input)
1145+
{
1146+
assert(PyAST_Validate(res));
1147+
}
1148+
#endif
11401149
return res;
11411150
}
11421151

0 commit comments

Comments
 (0)