From 70856f126dd415e310e807c82edad24f4d0778ce Mon Sep 17 00:00:00 2001 From: Sam Gross Date: Tue, 7 Jul 2020 13:41:47 -0400 Subject: [PATCH] asdl: use _PyOnceFlag in Python-ast.c --- Include/internal/pycore_ast_state.h | 1 + Parser/asdl_c.py | 9 +++++++-- Python/Python-ast.c | 8 ++++++-- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/Include/internal/pycore_ast_state.h b/Include/internal/pycore_ast_state.h index f15b4905eed..9c4de1d50b2 100644 --- a/Include/internal/pycore_ast_state.h +++ b/Include/internal/pycore_ast_state.h @@ -12,6 +12,7 @@ extern "C" { struct ast_state { int initialized; + _PyOnceFlag once; int recursion_depth; int recursion_limit; PyObject *AST_type; diff --git a/Parser/asdl_c.py b/Parser/asdl_c.py index 3e307610b63..37f01b14d8f 100755 --- a/Parser/asdl_c.py +++ b/Parser/asdl_c.py @@ -1467,6 +1467,7 @@ def visit(self, object): def generate_ast_state(module_state, f): f.write('struct ast_state {\n') f.write(' int initialized;\n') + f.write(' _PyOnceFlag once;\n') f.write(' int recursion_depth;\n') f.write(' int recursion_limit;\n') for s in module_state: @@ -1544,8 +1545,12 @@ def generate_module_def(mod, metadata, f, internal_h): { PyInterpreterState *interp = _PyInterpreterState_GET(); struct ast_state *state = &interp->ast; - if (!init_types(state)) { - return NULL; + if (_PyBeginOnce(&state->once)) { + if (!init_types(state)) { + _PyEndOnceFailed(&state->once); + return NULL; + } + _PyEndOnce(&state->once); } return state; } diff --git a/Python/Python-ast.c b/Python/Python-ast.c index d113c47b953..1e30227a8a0 100644 --- a/Python/Python-ast.c +++ b/Python/Python-ast.c @@ -17,8 +17,12 @@ get_ast_state(void) { PyInterpreterState *interp = _PyInterpreterState_GET(); struct ast_state *state = &interp->ast; - if (!init_types(state)) { - return NULL; + if (_PyBeginOnce(&state->once)) { + if (!init_types(state)) { + _PyEndOnceFailed(&state->once); + return NULL; + } + _PyEndOnce(&state->once); } return state; }