summaryrefslogtreecommitdiffstats
path: root/Python/Python-ast.c
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@python.org>2020-11-02 21:03:28 (GMT)
committerGitHub <noreply@github.com>2020-11-02 21:03:28 (GMT)
commit5cf4782a2630629d0978bf4cf6b6340365f449b2 (patch)
treee54caf1ea68b6c0ded1d72d72a35db3ab7224138 /Python/Python-ast.c
parent34637a0ce21e7261b952fbd9d006474cc29b681f (diff)
downloadcpython-5cf4782a2630629d0978bf4cf6b6340365f449b2.zip
cpython-5cf4782a2630629d0978bf4cf6b6340365f449b2.tar.gz
cpython-5cf4782a2630629d0978bf4cf6b6340365f449b2.tar.bz2
bpo-41796: Make _ast module state per interpreter (GH-23024)
The ast module internal state is now per interpreter. * Rename "astmodulestate" to "struct ast_state" * Add pycore_ast.h internal header: the ast_state structure is now declared in pycore_ast.h. * Add PyInterpreterState.ast (struct ast_state) * Remove get_ast_state() * Rename get_global_ast_state() to get_ast_state() * PyAST_obj2mod() now handles get_ast_state() failures
Diffstat (limited to 'Python/Python-ast.c')
-rw-r--r--Python/Python-ast.c245
1 files changed, 133 insertions, 112 deletions
diff --git a/Python/Python-ast.c b/Python/Python-ast.c
index 13657a6..f04addb 100644
--- a/Python/Python-ast.c
+++ b/Python/Python-ast.c
@@ -1,12 +1,16 @@
-/* File automatically generated by Parser/asdl_c.py. */
+// File automatically generated by Parser/asdl_c.py.
+
#include <stddef.h>
#include "Python.h"
-#include "Python-ast.h"
-#include "structmember.h" // PyMemberDef
-typedef struct {
+#ifdef Py_BUILD_CORE
+# include "pycore_ast.h" // struct ast_state
+# include "pycore_interp.h" // _PyInterpreterState.ast
+# include "pycore_pystate.h" // _PyInterpreterState_GET()
+#else
+struct ast_state {
int initialized;
PyObject *AST_type;
PyObject *Add_singleton;
@@ -221,38 +225,50 @@ typedef struct {
PyObject *values;
PyObject *vararg;
PyObject *withitem_type;
-} astmodulestate;
-
+};
+#endif // Py_BUILD_CORE
// Forward declaration
-static int init_types(astmodulestate *state);
+static int init_types(struct ast_state *state);
-// bpo-41194, bpo-41261, bpo-41631: The _ast module uses a global state.
-static astmodulestate global_ast_state = {0};
-
-static astmodulestate*
-get_global_ast_state(void)
+#ifdef Py_BUILD_CORE
+static struct ast_state*
+get_ast_state(void)
{
- astmodulestate* state = &global_ast_state;
+ PyInterpreterState *interp = _PyInterpreterState_GET();
+ struct ast_state *state = &interp->ast;
if (!init_types(state)) {
return NULL;
}
return state;
}
+#else
+static struct ast_state global_ast_state;
-static astmodulestate*
-get_ast_state(PyObject* Py_UNUSED(module))
+static struct ast_state*
+get_ast_state(void)
{
- astmodulestate* state = get_global_ast_state();
- // get_ast_state() must only be called after _ast module is imported,
- // and astmodule_exec() calls init_types()
- assert(state != NULL);
+ struct ast_state *state = &global_ast_state;
+ if (!init_types(state)) {
+ return NULL;
+ }
return state;
}
+#endif // Py_BUILD_CORE
+
+// Include Python-ast.h after pycore_interp.h to avoid conflicts
+// with the Yield macro redefined by <winbase.h>
+#include "Python-ast.h"
+#include "structmember.h"
void _PyAST_Fini(PyThreadState *tstate)
{
- astmodulestate* state = &global_ast_state;
+#ifdef Py_BUILD_CORE
+ struct ast_state *state = &tstate->interp->ast;
+#else
+ struct ast_state *state = &global_ast_state;
+#endif
+
Py_CLEAR(state->AST_type);
Py_CLEAR(state->Add_singleton);
Py_CLEAR(state->Add_type);
@@ -470,7 +486,7 @@ void _PyAST_Fini(PyThreadState *tstate)
state->initialized = 0;
}
-static int init_identifiers(astmodulestate *state)
+static int init_identifiers(struct ast_state *state)
{
if ((state->__dict__ = PyUnicode_InternFromString("__dict__")) == NULL) return 0;
if ((state->__doc__ = PyUnicode_InternFromString("__doc__")) == NULL) return 0;
@@ -561,7 +577,7 @@ GENERATE_ASDL_SEQ_CONSTRUCTOR(alias, alias_ty)
GENERATE_ASDL_SEQ_CONSTRUCTOR(withitem, withitem_ty)
GENERATE_ASDL_SEQ_CONSTRUCTOR(type_ignore, type_ignore_ty)
-static PyObject* ast2obj_mod(astmodulestate *state, void*);
+static PyObject* ast2obj_mod(struct ast_state *state, void*);
static const char * const Module_fields[]={
"body",
"type_ignores",
@@ -582,7 +598,7 @@ static const char * const stmt_attributes[] = {
"end_lineno",
"end_col_offset",
};
-static PyObject* ast2obj_stmt(astmodulestate *state, void*);
+static PyObject* ast2obj_stmt(struct ast_state *state, void*);
static const char * const FunctionDef_fields[]={
"name",
"args",
@@ -699,7 +715,7 @@ static const char * const expr_attributes[] = {
"end_lineno",
"end_col_offset",
};
-static PyObject* ast2obj_expr(astmodulestate *state, void*);
+static PyObject* ast2obj_expr(struct ast_state *state, void*);
static const char * const BoolOp_fields[]={
"op",
"values",
@@ -812,12 +828,12 @@ static const char * const Slice_fields[]={
"upper",
"step",
};
-static PyObject* ast2obj_expr_context(astmodulestate *state, expr_context_ty);
-static PyObject* ast2obj_boolop(astmodulestate *state, boolop_ty);
-static PyObject* ast2obj_operator(astmodulestate *state, operator_ty);
-static PyObject* ast2obj_unaryop(astmodulestate *state, unaryop_ty);
-static PyObject* ast2obj_cmpop(astmodulestate *state, cmpop_ty);
-static PyObject* ast2obj_comprehension(astmodulestate *state, void*);
+static PyObject* ast2obj_expr_context(struct ast_state *state, expr_context_ty);
+static PyObject* ast2obj_boolop(struct ast_state *state, boolop_ty);
+static PyObject* ast2obj_operator(struct ast_state *state, operator_ty);
+static PyObject* ast2obj_unaryop(struct ast_state *state, unaryop_ty);
+static PyObject* ast2obj_cmpop(struct ast_state *state, cmpop_ty);
+static PyObject* ast2obj_comprehension(struct ast_state *state, void*);
static const char * const comprehension_fields[]={
"target",
"iter",
@@ -830,13 +846,13 @@ static const char * const excepthandler_attributes[] = {
"end_lineno",
"end_col_offset",
};
-static PyObject* ast2obj_excepthandler(astmodulestate *state, void*);
+static PyObject* ast2obj_excepthandler(struct ast_state *state, void*);
static const char * const ExceptHandler_fields[]={
"type",
"name",
"body",
};
-static PyObject* ast2obj_arguments(astmodulestate *state, void*);
+static PyObject* ast2obj_arguments(struct ast_state *state, void*);
static const char * const arguments_fields[]={
"posonlyargs",
"args",
@@ -846,7 +862,7 @@ static const char * const arguments_fields[]={
"kwarg",
"defaults",
};
-static PyObject* ast2obj_arg(astmodulestate *state, void*);
+static PyObject* ast2obj_arg(struct ast_state *state, void*);
static const char * const arg_attributes[] = {
"lineno",
"col_offset",
@@ -858,7 +874,7 @@ static const char * const arg_fields[]={
"annotation",
"type_comment",
};
-static PyObject* ast2obj_keyword(astmodulestate *state, void*);
+static PyObject* ast2obj_keyword(struct ast_state *state, void*);
static const char * const keyword_attributes[] = {
"lineno",
"col_offset",
@@ -869,17 +885,17 @@ static const char * const keyword_fields[]={
"arg",
"value",
};
-static PyObject* ast2obj_alias(astmodulestate *state, void*);
+static PyObject* ast2obj_alias(struct ast_state *state, void*);
static const char * const alias_fields[]={
"name",
"asname",
};
-static PyObject* ast2obj_withitem(astmodulestate *state, void*);
+static PyObject* ast2obj_withitem(struct ast_state *state, void*);
static const char * const withitem_fields[]={
"context_expr",
"optional_vars",
};
-static PyObject* ast2obj_type_ignore(astmodulestate *state, void*);
+static PyObject* ast2obj_type_ignore(struct ast_state *state, void*);
static const char * const TypeIgnore_fields[]={
"lineno",
"tag",
@@ -923,7 +939,7 @@ ast_clear(AST_object *self)
static int
ast_type_init(PyObject *self, PyObject *args, PyObject *kw)
{
- astmodulestate *state = get_global_ast_state();
+ struct ast_state *state = get_ast_state();
if (state == NULL) {
return -1;
}
@@ -999,7 +1015,7 @@ ast_type_init(PyObject *self, PyObject *args, PyObject *kw)
static PyObject *
ast_type_reduce(PyObject *self, PyObject *unused)
{
- astmodulestate *state = get_global_ast_state();
+ struct ast_state *state = get_ast_state();
if (state == NULL) {
return NULL;
}
@@ -1054,7 +1070,7 @@ static PyType_Spec AST_type_spec = {
};
static PyObject *
-make_type(astmodulestate *state, const char *type, PyObject* base,
+make_type(struct ast_state *state, const char *type, PyObject* base,
const char* const* fields, int num_fields, const char *doc)
{
PyObject *fnames, *result;
@@ -1080,7 +1096,7 @@ make_type(astmodulestate *state, const char *type, PyObject* base,
}
static int
-add_attributes(astmodulestate *state, PyObject *type, const char * const *attrs, int num_fields)
+add_attributes(struct ast_state *state, PyObject *type, const char * const *attrs, int num_fields)
{
int i, result;
PyObject *s, *l = PyTuple_New(num_fields);
@@ -1101,7 +1117,7 @@ add_attributes(astmodulestate *state, PyObject *type, const char * const *attrs,
/* Conversion AST -> Python */
-static PyObject* ast2obj_list(astmodulestate *state, asdl_seq *seq, PyObject* (*func)(astmodulestate *state, void*))
+static PyObject* ast2obj_list(struct ast_state *state, asdl_seq *seq, PyObject* (*func)(struct ast_state *state, void*))
{
Py_ssize_t i, n = asdl_seq_LEN(seq);
PyObject *result = PyList_New(n);
@@ -1119,7 +1135,7 @@ static PyObject* ast2obj_list(astmodulestate *state, asdl_seq *seq, PyObject* (*
return result;
}
-static PyObject* ast2obj_object(astmodulestate *Py_UNUSED(state), void *o)
+static PyObject* ast2obj_object(struct ast_state *Py_UNUSED(state), void *o)
{
if (!o)
o = Py_None;
@@ -1130,14 +1146,14 @@ static PyObject* ast2obj_object(astmodulestate *Py_UNUSED(state), void *o)
#define ast2obj_identifier ast2obj_object
#define ast2obj_string ast2obj_object
-static PyObject* ast2obj_int(astmodulestate *Py_UNUSED(state), long b)
+static PyObject* ast2obj_int(struct ast_state *Py_UNUSED(state), long b)
{
return PyLong_FromLong(b);
}
/* Conversion Python -> AST */
-static int obj2ast_object(astmodulestate *Py_UNUSED(state), PyObject* obj, PyObject** out, PyArena* arena)
+static int obj2ast_object(struct ast_state *Py_UNUSED(state), PyObject* obj, PyObject** out, PyArena* arena)
{
if (obj == Py_None)
obj = NULL;
@@ -1152,7 +1168,7 @@ static int obj2ast_object(astmodulestate *Py_UNUSED(state), PyObject* obj, PyObj
return 0;
}
-static int obj2ast_constant(astmodulestate *Py_UNUSED(state), PyObject* obj, PyObject** out, PyArena* arena)
+static int obj2ast_constant(struct ast_state *Py_UNUSED(state), PyObject* obj, PyObject** out, PyArena* arena)
{
if (PyArena_AddPyObject(arena, obj) < 0) {
*out = NULL;
@@ -1163,7 +1179,7 @@ static int obj2ast_constant(astmodulestate *Py_UNUSED(state), PyObject* obj, PyO
return 0;
}
-static int obj2ast_identifier(astmodulestate *state, PyObject* obj, PyObject** out, PyArena* arena)
+static int obj2ast_identifier(struct ast_state *state, PyObject* obj, PyObject** out, PyArena* arena)
{
if (!PyUnicode_CheckExact(obj) && obj != Py_None) {
PyErr_SetString(PyExc_TypeError, "AST identifier must be of type str");
@@ -1172,7 +1188,7 @@ static int obj2ast_identifier(astmodulestate *state, PyObject* obj, PyObject** o
return obj2ast_object(state, obj, out, arena);
}
-static int obj2ast_string(astmodulestate *state, PyObject* obj, PyObject** out, PyArena* arena)
+static int obj2ast_string(struct ast_state *state, PyObject* obj, PyObject** out, PyArena* arena)
{
if (!PyUnicode_CheckExact(obj) && !PyBytes_CheckExact(obj)) {
PyErr_SetString(PyExc_TypeError, "AST string must be of type str");
@@ -1181,7 +1197,7 @@ static int obj2ast_string(astmodulestate *state, PyObject* obj, PyObject** out,
return obj2ast_object(state, obj, out, arena);
}
-static int obj2ast_int(astmodulestate* Py_UNUSED(state), PyObject* obj, int* out, PyArena* arena)
+static int obj2ast_int(struct ast_state* Py_UNUSED(state), PyObject* obj, int* out, PyArena* arena)
{
int i;
if (!PyLong_Check(obj)) {
@@ -1196,7 +1212,7 @@ static int obj2ast_int(astmodulestate* Py_UNUSED(state), PyObject* obj, int* out
return 0;
}
-static int add_ast_fields(astmodulestate *state)
+static int add_ast_fields(struct ast_state *state)
{
PyObject *empty_tuple;
empty_tuple = PyTuple_New(0);
@@ -1211,7 +1227,7 @@ static int add_ast_fields(astmodulestate *state)
}
-static int init_types(astmodulestate *state)
+static int init_types(struct ast_state *state)
{
if (state->initialized) return 1;
if (init_identifiers(state) < 0) return 0;
@@ -1890,37 +1906,37 @@ static int init_types(astmodulestate *state)
return 1;
}
-static int obj2ast_mod(astmodulestate *state, PyObject* obj, mod_ty* out,
+static int obj2ast_mod(struct ast_state *state, PyObject* obj, mod_ty* out,
PyArena* arena);
-static int obj2ast_stmt(astmodulestate *state, PyObject* obj, stmt_ty* out,
+static int obj2ast_stmt(struct ast_state *state, PyObject* obj, stmt_ty* out,
PyArena* arena);
-static int obj2ast_expr(astmodulestate *state, PyObject* obj, expr_ty* out,
+static int obj2ast_expr(struct ast_state *state, PyObject* obj, expr_ty* out,
PyArena* arena);
-static int obj2ast_expr_context(astmodulestate *state, PyObject* obj,
+static int obj2ast_expr_context(struct ast_state *state, PyObject* obj,
expr_context_ty* out, PyArena* arena);
-static int obj2ast_boolop(astmodulestate *state, PyObject* obj, boolop_ty* out,
- PyArena* arena);
-static int obj2ast_operator(astmodulestate *state, PyObject* obj, operator_ty*
- out, PyArena* arena);
-static int obj2ast_unaryop(astmodulestate *state, PyObject* obj, unaryop_ty*
+static int obj2ast_boolop(struct ast_state *state, PyObject* obj, boolop_ty*
+ out, PyArena* arena);
+static int obj2ast_operator(struct ast_state *state, PyObject* obj,
+ operator_ty* out, PyArena* arena);
+static int obj2ast_unaryop(struct ast_state *state, PyObject* obj, unaryop_ty*
out, PyArena* arena);
-static int obj2ast_cmpop(astmodulestate *state, PyObject* obj, cmpop_ty* out,
+static int obj2ast_cmpop(struct ast_state *state, PyObject* obj, cmpop_ty* out,
PyArena* arena);
-static int obj2ast_comprehension(astmodulestate *state, PyObject* obj,
+static int obj2ast_comprehension(struct ast_state *state, PyObject* obj,
comprehension_ty* out, PyArena* arena);
-static int obj2ast_excepthandler(astmodulestate *state, PyObject* obj,
+static int obj2ast_excepthandler(struct ast_state *state, PyObject* obj,
excepthandler_ty* out, PyArena* arena);
-static int obj2ast_arguments(astmodulestate *state, PyObject* obj,
+static int obj2ast_arguments(struct ast_state *state, PyObject* obj,
arguments_ty* out, PyArena* arena);
-static int obj2ast_arg(astmodulestate *state, PyObject* obj, arg_ty* out,
+static int obj2ast_arg(struct ast_state *state, PyObject* obj, arg_ty* out,
PyArena* arena);
-static int obj2ast_keyword(astmodulestate *state, PyObject* obj, keyword_ty*
+static int obj2ast_keyword(struct ast_state *state, PyObject* obj, keyword_ty*
out, PyArena* arena);
-static int obj2ast_alias(astmodulestate *state, PyObject* obj, alias_ty* out,
+static int obj2ast_alias(struct ast_state *state, PyObject* obj, alias_ty* out,
PyArena* arena);
-static int obj2ast_withitem(astmodulestate *state, PyObject* obj, withitem_ty*
- out, PyArena* arena);
-static int obj2ast_type_ignore(astmodulestate *state, PyObject* obj,
+static int obj2ast_withitem(struct ast_state *state, PyObject* obj,
+ withitem_ty* out, PyArena* arena);
+static int obj2ast_type_ignore(struct ast_state *state, PyObject* obj,
type_ignore_ty* out, PyArena* arena);
mod_ty
@@ -3391,7 +3407,7 @@ TypeIgnore(int lineno, string tag, PyArena *arena)
PyObject*
-ast2obj_mod(astmodulestate *state, void* _o)
+ast2obj_mod(struct ast_state *state, void* _o)
{
mod_ty o = (mod_ty)_o;
PyObject *result = NULL, *value = NULL;
@@ -3462,7 +3478,7 @@ failed:
}
PyObject*
-ast2obj_stmt(astmodulestate *state, void* _o)
+ast2obj_stmt(struct ast_state *state, void* _o)
{
stmt_ty o = (stmt_ty)_o;
PyObject *result = NULL, *value = NULL;
@@ -3977,7 +3993,7 @@ failed:
}
PyObject*
-ast2obj_expr(astmodulestate *state, void* _o)
+ast2obj_expr(struct ast_state *state, void* _o)
{
expr_ty o = (expr_ty)_o;
PyObject *result = NULL, *value = NULL;
@@ -4453,7 +4469,7 @@ failed:
return NULL;
}
-PyObject* ast2obj_expr_context(astmodulestate *state, expr_context_ty o)
+PyObject* ast2obj_expr_context(struct ast_state *state, expr_context_ty o)
{
switch(o) {
case Load:
@@ -4468,7 +4484,7 @@ PyObject* ast2obj_expr_context(astmodulestate *state, expr_context_ty o)
}
Py_UNREACHABLE();
}
-PyObject* ast2obj_boolop(astmodulestate *state, boolop_ty o)
+PyObject* ast2obj_boolop(struct ast_state *state, boolop_ty o)
{
switch(o) {
case And:
@@ -4480,7 +4496,7 @@ PyObject* ast2obj_boolop(astmodulestate *state, boolop_ty o)
}
Py_UNREACHABLE();
}
-PyObject* ast2obj_operator(astmodulestate *state, operator_ty o)
+PyObject* ast2obj_operator(struct ast_state *state, operator_ty o)
{
switch(o) {
case Add:
@@ -4525,7 +4541,7 @@ PyObject* ast2obj_operator(astmodulestate *state, operator_ty o)
}
Py_UNREACHABLE();
}
-PyObject* ast2obj_unaryop(astmodulestate *state, unaryop_ty o)
+PyObject* ast2obj_unaryop(struct ast_state *state, unaryop_ty o)
{
switch(o) {
case Invert:
@@ -4543,7 +4559,7 @@ PyObject* ast2obj_unaryop(astmodulestate *state, unaryop_ty o)
}
Py_UNREACHABLE();
}
-PyObject* ast2obj_cmpop(astmodulestate *state, cmpop_ty o)
+PyObject* ast2obj_cmpop(struct ast_state *state, cmpop_ty o)
{
switch(o) {
case Eq:
@@ -4580,7 +4596,7 @@ PyObject* ast2obj_cmpop(astmodulestate *state, cmpop_ty o)
Py_UNREACHABLE();
}
PyObject*
-ast2obj_comprehension(astmodulestate *state, void* _o)
+ast2obj_comprehension(struct ast_state *state, void* _o)
{
comprehension_ty o = (comprehension_ty)_o;
PyObject *result = NULL, *value = NULL;
@@ -4619,7 +4635,7 @@ failed:
}
PyObject*
-ast2obj_excepthandler(astmodulestate *state, void* _o)
+ast2obj_excepthandler(struct ast_state *state, void* _o)
{
excepthandler_ty o = (excepthandler_ty)_o;
PyObject *result = NULL, *value = NULL;
@@ -4678,7 +4694,7 @@ failed:
}
PyObject*
-ast2obj_arguments(astmodulestate *state, void* _o)
+ast2obj_arguments(struct ast_state *state, void* _o)
{
arguments_ty o = (arguments_ty)_o;
PyObject *result = NULL, *value = NULL;
@@ -4732,7 +4748,7 @@ failed:
}
PyObject*
-ast2obj_arg(astmodulestate *state, void* _o)
+ast2obj_arg(struct ast_state *state, void* _o)
{
arg_ty o = (arg_ty)_o;
PyObject *result = NULL, *value = NULL;
@@ -4786,7 +4802,7 @@ failed:
}
PyObject*
-ast2obj_keyword(astmodulestate *state, void* _o)
+ast2obj_keyword(struct ast_state *state, void* _o)
{
keyword_ty o = (keyword_ty)_o;
PyObject *result = NULL, *value = NULL;
@@ -4835,7 +4851,7 @@ failed:
}
PyObject*
-ast2obj_alias(astmodulestate *state, void* _o)
+ast2obj_alias(struct ast_state *state, void* _o)
{
alias_ty o = (alias_ty)_o;
PyObject *result = NULL, *value = NULL;
@@ -4864,7 +4880,7 @@ failed:
}
PyObject*
-ast2obj_withitem(astmodulestate *state, void* _o)
+ast2obj_withitem(struct ast_state *state, void* _o)
{
withitem_ty o = (withitem_ty)_o;
PyObject *result = NULL, *value = NULL;
@@ -4893,7 +4909,7 @@ failed:
}
PyObject*
-ast2obj_type_ignore(astmodulestate *state, void* _o)
+ast2obj_type_ignore(struct ast_state *state, void* _o)
{
type_ignore_ty o = (type_ignore_ty)_o;
PyObject *result = NULL, *value = NULL;
@@ -4927,7 +4943,7 @@ failed:
int
-obj2ast_mod(astmodulestate *state, PyObject* obj, mod_ty* out, PyArena* arena)
+obj2ast_mod(struct ast_state *state, PyObject* obj, mod_ty* out, PyArena* arena)
{
int isinstance;
@@ -5154,7 +5170,8 @@ obj2ast_mod(astmodulestate *state, PyObject* obj, mod_ty* out, PyArena* arena)
}
int
-obj2ast_stmt(astmodulestate *state, PyObject* obj, stmt_ty* out, PyArena* arena)
+obj2ast_stmt(struct ast_state *state, PyObject* obj, stmt_ty* out, PyArena*
+ arena)
{
int isinstance;
@@ -7052,7 +7069,8 @@ obj2ast_stmt(astmodulestate *state, PyObject* obj, stmt_ty* out, PyArena* arena)
}
int
-obj2ast_expr(astmodulestate *state, PyObject* obj, expr_ty* out, PyArena* arena)
+obj2ast_expr(struct ast_state *state, PyObject* obj, expr_ty* out, PyArena*
+ arena)
{
int isinstance;
@@ -8562,7 +8580,7 @@ obj2ast_expr(astmodulestate *state, PyObject* obj, expr_ty* out, PyArena* arena)
}
int
-obj2ast_expr_context(astmodulestate *state, PyObject* obj, expr_context_ty*
+obj2ast_expr_context(struct ast_state *state, PyObject* obj, expr_context_ty*
out, PyArena* arena)
{
int isinstance;
@@ -8597,7 +8615,7 @@ obj2ast_expr_context(astmodulestate *state, PyObject* obj, expr_context_ty*
}
int
-obj2ast_boolop(astmodulestate *state, PyObject* obj, boolop_ty* out, PyArena*
+obj2ast_boolop(struct ast_state *state, PyObject* obj, boolop_ty* out, PyArena*
arena)
{
int isinstance;
@@ -8624,7 +8642,7 @@ obj2ast_boolop(astmodulestate *state, PyObject* obj, boolop_ty* out, PyArena*
}
int
-obj2ast_operator(astmodulestate *state, PyObject* obj, operator_ty* out,
+obj2ast_operator(struct ast_state *state, PyObject* obj, operator_ty* out,
PyArena* arena)
{
int isinstance;
@@ -8739,8 +8757,8 @@ obj2ast_operator(astmodulestate *state, PyObject* obj, operator_ty* out,
}
int
-obj2ast_unaryop(astmodulestate *state, PyObject* obj, unaryop_ty* out, PyArena*
- arena)
+obj2ast_unaryop(struct ast_state *state, PyObject* obj, unaryop_ty* out,
+ PyArena* arena)
{
int isinstance;
@@ -8782,7 +8800,7 @@ obj2ast_unaryop(astmodulestate *state, PyObject* obj, unaryop_ty* out, PyArena*
}
int
-obj2ast_cmpop(astmodulestate *state, PyObject* obj, cmpop_ty* out, PyArena*
+obj2ast_cmpop(struct ast_state *state, PyObject* obj, cmpop_ty* out, PyArena*
arena)
{
int isinstance;
@@ -8873,7 +8891,7 @@ obj2ast_cmpop(astmodulestate *state, PyObject* obj, cmpop_ty* out, PyArena*
}
int
-obj2ast_comprehension(astmodulestate *state, PyObject* obj, comprehension_ty*
+obj2ast_comprehension(struct ast_state *state, PyObject* obj, comprehension_ty*
out, PyArena* arena)
{
PyObject* tmp = NULL;
@@ -8962,7 +8980,7 @@ failed:
}
int
-obj2ast_excepthandler(astmodulestate *state, PyObject* obj, excepthandler_ty*
+obj2ast_excepthandler(struct ast_state *state, PyObject* obj, excepthandler_ty*
out, PyArena* arena)
{
int isinstance;
@@ -9112,7 +9130,7 @@ obj2ast_excepthandler(astmodulestate *state, PyObject* obj, excepthandler_ty*
}
int
-obj2ast_arguments(astmodulestate *state, PyObject* obj, arguments_ty* out,
+obj2ast_arguments(struct ast_state *state, PyObject* obj, arguments_ty* out,
PyArena* arena)
{
PyObject* tmp = NULL;
@@ -9324,7 +9342,7 @@ failed:
}
int
-obj2ast_arg(astmodulestate *state, PyObject* obj, arg_ty* out, PyArena* arena)
+obj2ast_arg(struct ast_state *state, PyObject* obj, arg_ty* out, PyArena* arena)
{
PyObject* tmp = NULL;
identifier arg;
@@ -9435,8 +9453,8 @@ failed:
}
int
-obj2ast_keyword(astmodulestate *state, PyObject* obj, keyword_ty* out, PyArena*
- arena)
+obj2ast_keyword(struct ast_state *state, PyObject* obj, keyword_ty* out,
+ PyArena* arena)
{
PyObject* tmp = NULL;
identifier arg;
@@ -9533,7 +9551,7 @@ failed:
}
int
-obj2ast_alias(astmodulestate *state, PyObject* obj, alias_ty* out, PyArena*
+obj2ast_alias(struct ast_state *state, PyObject* obj, alias_ty* out, PyArena*
arena)
{
PyObject* tmp = NULL;
@@ -9574,7 +9592,7 @@ failed:
}
int
-obj2ast_withitem(astmodulestate *state, PyObject* obj, withitem_ty* out,
+obj2ast_withitem(struct ast_state *state, PyObject* obj, withitem_ty* out,
PyArena* arena)
{
PyObject* tmp = NULL;
@@ -9615,8 +9633,8 @@ failed:
}
int
-obj2ast_type_ignore(astmodulestate *state, PyObject* obj, type_ignore_ty* out,
- PyArena* arena)
+obj2ast_type_ignore(struct ast_state *state, PyObject* obj, type_ignore_ty*
+ out, PyArena* arena)
{
int isinstance;
@@ -9677,9 +9695,8 @@ obj2ast_type_ignore(astmodulestate *state, PyObject* obj, type_ignore_ty* out,
static int
astmodule_exec(PyObject *m)
{
- astmodulestate *state = get_ast_state(m);
-
- if (!init_types(state)) {
+ struct ast_state *state = get_ast_state();
+ if (state == NULL) {
return -1;
}
if (PyModule_AddObject(m, "AST", state->AST_type) < 0) {
@@ -10132,7 +10149,7 @@ static PyModuleDef_Slot astmodule_slots[] = {
static struct PyModuleDef _astmodule = {
PyModuleDef_HEAD_INIT,
.m_name = "_ast",
- // The _ast module uses a global state (global_ast_state).
+ // The _ast module uses a per-interpreter state (PyInterpreterState.ast)
.m_size = 0,
.m_slots = astmodule_slots,
};
@@ -10146,7 +10163,7 @@ PyInit__ast(void)
PyObject* PyAST_mod2obj(mod_ty t)
{
- astmodulestate *state = get_global_ast_state();
+ struct ast_state *state = get_ast_state();
if (state == NULL) {
return NULL;
}
@@ -10163,7 +10180,11 @@ mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode)
return NULL;
}
- astmodulestate *state = get_global_ast_state();
+ struct ast_state *state = get_ast_state();
+ if (state == NULL) {
+ return NULL;
+ }
+
PyObject *req_type[3];
req_type[0] = state->Module_type;
req_type[1] = state->Expression_type;
@@ -10189,7 +10210,7 @@ mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode)
int PyAST_Check(PyObject* obj)
{
- astmodulestate *state = get_global_ast_state();
+ struct ast_state *state = get_ast_state();
if (state == NULL) {
return -1;
}