Skip to content

Commit e0bf70d

Browse files
authored
bpo-43244: Fix test_peg_generator for PyAST_Validate() (GH-24912)
test_peg_generator now defines _Py_TEST_PEGEN macro when building C code to not call PyAST_Validate() in Parser/pegen.c. Moreover, it defines Py_BUILD_CORE_MODULE macro to get access to the internal C API. Remove "global_ast_state" from Python-ast.c when it's built by test_peg_generator: always get the AST state from the current interpreter.
1 parent 08fb8ac commit e0bf70d

File tree

4 files changed

+14
-282
lines changed

4 files changed

+14
-282
lines changed

Parser/asdl_c.py

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1373,17 +1373,13 @@ def generate_ast_fini(module_state, f):
13731373
f.write(textwrap.dedent("""
13741374
void _PyAST_Fini(PyInterpreterState *interp)
13751375
{
1376-
#ifdef Py_BUILD_CORE
13771376
struct ast_state *state = &interp->ast;
1378-
#else
1379-
struct ast_state *state = &global_ast_state;
1380-
#endif
13811377
13821378
"""))
13831379
for s in module_state:
13841380
f.write(" Py_CLEAR(state->" + s + ');\n')
13851381
f.write(textwrap.dedent("""
1386-
#if defined(Py_BUILD_CORE) && !defined(NDEBUG)
1382+
#if !defined(NDEBUG)
13871383
state->initialized = -1;
13881384
#else
13891385
state->initialized = 0;
@@ -1428,24 +1424,15 @@ def generate_module_def(mod, f, internal_h):
14281424
generate_ast_state(module_state, internal_h)
14291425

14301426
print(textwrap.dedent(f"""
1431-
#ifdef Py_BUILD_CORE
1432-
# include "pycore_ast_state.h" // struct ast_state
1433-
# include "pycore_interp.h" // _PyInterpreterState.ast
1434-
# include "pycore_pystate.h" // _PyInterpreterState_GET()
1435-
#else
1436-
""").strip(), file=f)
1437-
1438-
generate_ast_state(module_state, f)
1439-
1440-
print(textwrap.dedent(f"""
1441-
#endif // Py_BUILD_CORE
1427+
#include "pycore_ast_state.h" // struct ast_state
1428+
#include "pycore_interp.h" // _PyInterpreterState.ast
1429+
#include "pycore_pystate.h" // _PyInterpreterState_GET()
14421430
""").rstrip(), file=f)
14431431

14441432
f.write("""
14451433
// Forward declaration
14461434
static int init_types(struct ast_state *state);
14471435
1448-
#ifdef Py_BUILD_CORE
14491436
static struct ast_state*
14501437
get_ast_state(void)
14511438
{
@@ -1456,19 +1443,6 @@ def generate_module_def(mod, f, internal_h):
14561443
}
14571444
return state;
14581445
}
1459-
#else
1460-
static struct ast_state global_ast_state;
1461-
1462-
static struct ast_state*
1463-
get_ast_state(void)
1464-
{
1465-
struct ast_state *state = &global_ast_state;
1466-
if (!init_types(state)) {
1467-
return NULL;
1468-
}
1469-
return state;
1470-
}
1471-
#endif // Py_BUILD_CORE
14721446
""")
14731447

14741448
# f-string for {mod.name}

Parser/pegen.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1265,7 +1265,8 @@ _PyPegen_run_parser(Parser *p)
12651265
return RAISE_SYNTAX_ERROR("multiple statements found while compiling a single statement");
12661266
}
12671267

1268-
#if defined(Py_DEBUG) && defined(Py_BUILD_CORE)
1268+
// test_peg_generator defines _Py_TEST_PEGEN to not call PyAST_Validate()
1269+
#if defined(Py_DEBUG) && !defined(_Py_TEST_PEGEN)
12691270
if (p->start_rule == Py_single_input ||
12701271
p->start_rule == Py_file_input ||
12711272
p->start_rule == Py_eval_input)

Python/Python-ast.c

Lines changed: 5 additions & 251 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Tools/peg_generator/pegen/build.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ def compile_c_extension(
5656
source_file_path = pathlib.Path(generated_source_path)
5757
extension_name = source_file_path.stem
5858
extra_compile_args = get_extra_flags("CFLAGS", "PY_CFLAGS_NODIST")
59+
extra_compile_args.append("-DPy_BUILD_CORE_MODULE")
60+
# Define _Py_TEST_PEGEN to not call PyAST_Validate() in Parser/pegen.c
61+
extra_compile_args.append('-D_Py_TEST_PEGEN')
5962
extra_link_args = get_extra_flags("LDFLAGS", "PY_LDFLAGS_NODIST")
6063
if keep_asserts:
6164
extra_compile_args.append("-UNDEBUG")

0 commit comments

Comments
 (0)