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.
This commit is contained in:
Victor Stinner 2021-03-18 02:46:06 +01:00 committed by GitHub
parent 08fb8ac99a
commit e0bf70d08c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 14 additions and 282 deletions

View file

@ -1373,17 +1373,13 @@ def generate_ast_fini(module_state, f):
f.write(textwrap.dedent("""
void _PyAST_Fini(PyInterpreterState *interp)
{
#ifdef Py_BUILD_CORE
struct ast_state *state = &interp->ast;
#else
struct ast_state *state = &global_ast_state;
#endif
"""))
for s in module_state:
f.write(" Py_CLEAR(state->" + s + ');\n')
f.write(textwrap.dedent("""
#if defined(Py_BUILD_CORE) && !defined(NDEBUG)
#if !defined(NDEBUG)
state->initialized = -1;
#else
state->initialized = 0;
@ -1428,24 +1424,15 @@ def generate_module_def(mod, f, internal_h):
generate_ast_state(module_state, internal_h)
print(textwrap.dedent(f"""
#ifdef Py_BUILD_CORE
# include "pycore_ast_state.h" // struct ast_state
# include "pycore_interp.h" // _PyInterpreterState.ast
# include "pycore_pystate.h" // _PyInterpreterState_GET()
#else
""").strip(), file=f)
generate_ast_state(module_state, f)
print(textwrap.dedent(f"""
#endif // Py_BUILD_CORE
#include "pycore_ast_state.h" // struct ast_state
#include "pycore_interp.h" // _PyInterpreterState.ast
#include "pycore_pystate.h" // _PyInterpreterState_GET()
""").rstrip(), file=f)
f.write("""
// Forward declaration
static int init_types(struct ast_state *state);
#ifdef Py_BUILD_CORE
static struct ast_state*
get_ast_state(void)
{
@ -1456,19 +1443,6 @@ get_ast_state(void)
}
return state;
}
#else
static struct ast_state global_ast_state;
static struct ast_state*
get_ast_state(void)
{
struct ast_state *state = &global_ast_state;
if (!init_types(state)) {
return NULL;
}
return state;
}
#endif // Py_BUILD_CORE
""")
# f-string for {mod.name}