8000 bpo-41796: Make _ast module state per interpreter (GH-23024) · python/cpython@5cf4782 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5cf4782

Browse files
authored
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
1 parent 34637a0 commit 5cf4782

File tree

8 files changed

+549
-202
lines changed

8 files changed

+549
-202
lines changed

Include/Python-ast.h

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/internal/pycore_ast.h

Lines changed: 234 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
1+
// File automatically generated by Parser/asdl_c.py.
2+
3+
#ifndef Py_INTERNAL_AST_H
4+
#define Py_INTERNAL_AST_H
5+
#ifdef __cplusplus
6+
extern "C" {
7+
#endif
8+
9+
#ifndef Py_BUILD_CORE
10+
# error "this header requires Py_BUILD_CORE define"
11+
#endif
12+
13+
struct ast_state {
14+
int initialized;
15+
PyObject *AST_type;
16+
PyObject *Add_singleton;
17+
PyObject *Add_type;
18+
PyObject *And_singleton;
19+
PyObject *And_type;
20+
PyObject *AnnAssign_type;
21+
PyObject *Assert_type;
22+
PyObject *Assign_type;
23+
PyObject *AsyncFor_type;
24+
PyObject *AsyncFunctionDef_type;
25+
PyObject *AsyncWith_type;
26+
PyObject *Attribute_type;
27+
PyObject *AugAssign_type;
28+
PyObject *Await_type;
29+
PyObject *BinOp_type;
30+
PyObject *BitAnd_singleton;
31+
PyObject *BitAnd_type;
32+
PyObject *BitOr_singleton;
33+
PyObject *BitOr_type;
34+
PyObject *BitXor_singleton;
35+
PyObject *BitXor_type;
36+
PyObject *BoolOp_type;
37+
PyObject *Break_type;
38+
PyObject *Call_type;
39+
PyObject *ClassDef_type;
40+
PyObject *Compare_type;
41+
PyObject *Constant_type;
42+
PyObject *Continue_type;
43+
PyObject *Del_singleton;
44+
PyObject *Del_type;
45+
PyObject *Delete_type;
46+
PyObject *DictComp_type;
47+
PyObject *Dict_type;
48+
PyObject *Div_singleton;
49+
PyObject *Div_type;
50+
PyObject *Eq_singleton;
51+
PyObject *Eq_type;
52+
PyObject *ExceptHandler_type;
53+
PyObject *Expr_type;
54+
PyObject *Expression_type;
55+
PyObject *FloorDiv_singleton;
56+
PyObject *FloorDiv_type;
57+
PyObject *For_type;
58+
PyObject *FormattedValue_type;
59+
PyObject *FunctionDef_type;
60+
PyObject *FunctionType_type;
61+
PyObject *GeneratorExp_type;
62+
PyObject *Global_type;
63+
PyObject *GtE_singleton;
64+
PyObject *GtE_type;
65+
PyObject *Gt_singleton;
66+
PyObject *Gt_type;
67+
PyObject *IfExp_type;
68+
PyObject *If_type;
69+
PyObject *ImportFrom_type;
70+
PyObject *Import_type;
71+
PyObject *In_singleton;
72+
PyObject *In_type;
73+
PyObject *Interactive_type;
74+
PyObject *Invert_singleton;
75+
PyObject *Invert_type;
76+
PyObject *IsNot_singleton;
77+
PyObject *IsNot_type;
78+
PyObject *Is_singleton;
79+
PyObject *Is_type;
80+
PyObject *JoinedStr_type;
81+
PyObject *LShift_singleton;
82+
PyObject *LShift_type;
83+
PyObject *Lambda_type;
84+
PyObject *ListComp_type;
85+
PyObject *List_type;
86+
PyObject *Load_singleton;
87+
PyObject *Load_type;
88+
PyObject *LtE_singleton;
89+
PyObject *LtE_type;
90+
PyObject *Lt_singleton;
91+
PyObject *Lt_type;
92+
PyObject *MatMult_singleton;
93+
PyObject *MatMult_type;
94+
PyObject *Mod_singleton;
95+
PyObject *Mod_type;
96+
PyObject *Module_type;
97+
PyObject *Mult_singleton;
98+
PyObject *Mult_type;
99+
PyObject *Name_type;
100+
PyObject *NamedExpr_type;
101+
PyObject *Nonlocal_type;
102+
PyObject *NotEq_singleton;
103+
PyObject *NotEq_type;
104+
PyObject *NotIn_singleton;
105+
PyObject *NotIn_type;
106+
PyObject *Not_singleton;
107+
PyObject *Not_type;
108+
PyObject *Or_singleton;
109+
PyObject *Or_type;
110+
PyObject *Pass_type;
111+
PyObject *Pow_singleton;
112+
PyObject *Pow_type;
113+
PyObject *RShift_singleton;
114+
PyObject *RShift_type;
115+
PyObject *Raise_type;
116+
PyObject *Return_type;
117+
PyObject *SetComp_type;
118+
PyObject *Set_type;
119+
PyObject *Slice_type;
120+
PyObject *Starred_type;
121+
PyObject *Store_singleton;
122+
PyObject *Store_type;
123+
PyObject *Sub_singleton;
124+
PyObject *Sub_type;
125+
PyObject *Subscript_type;
126+
PyObject *Try_type;
127+
PyObject *Tuple_type;
128+
PyObject *TypeIgnore_type;
129+
PyObject *UAdd_singleton;
130+
PyObject *UAdd_type;
131+
PyObject *USub_singleton;
132+
PyObject *USub_type;
133+
PyObject *UnaryOp_type;
134+
PyObject *While_type;
135+
PyObject *With_type;
136+
PyObject *YieldFrom_type;
137+
PyObject *Yield_type;
138+
PyObject *__dict__;
139+
PyObject *__doc__;
140+
PyObject *__module__;
141+
PyObject *_attributes;
142+
PyObject *_fields;
143+
PyObject *alias_type;
144+
PyObject *annotation;
145+
PyObject *arg;
146+
PyObject *arg_type;
147+
PyObject *args;
148+
PyObject *argtypes;
149+
PyObject *arguments_type;
150+
PyObject *asname;
151+
PyObject *ast;
152+
PyObject *attr;
153+
PyObject *bases;
154+
PyObject *body;
155+
PyObject *boolop_type;
156+
PyObject *cause;
157+
PyObject *cmpop_type;
158+
PyObject *col_offset;
159+
PyObject *comparators;
160+
PyObject *comprehension_type;
161+
PyObject *context_expr;
162+
PyObject *conversion;
163+
PyObject *ctx;
164+
PyObject *decorator_list;
165+
PyObject *defaults;
166+
PyObject *elt;
167+
PyObject *elts;
168+
PyObject *end_col_offset;
169+
PyObject *end_lineno;
170+
PyObject *exc;
171+
PyObject *excepthandler_type;
172+
PyObject *expr_context_type;
173+
PyObject *expr_type;
174+
PyObject *finalbody;
175+
PyObject *format_spec;
176+
PyObject *func;
177+
PyObject *generators;
178+
PyObject *handlers;
179+
PyObject *id;
180+
PyObject *ifs;
181+
PyObject *is_async;
182+
PyObject *items;
183+
PyObject *iter;
184+
PyObject *key;
185+
PyObject *keys;
186+
PyObject *keyword_type;
187+
PyObject *keywords;
188+
PyObject *kind;
189+
PyObject *kw_defaults;
190+
PyObject *kwarg;
191+
PyObject *kwonlyargs;
192+
PyObject *left;
193+
PyObject *level;
194+
PyObject *lineno;
195+
PyObject *lower;
196+
PyObject *mod_type;
197+
PyObject *module;
198+
PyObject *msg;
199+
PyObject *name;
200+
PyObject *names;
201+
PyObject *op;
202+
PyObject *operand;
203+
PyObject *operator_type;
204+
PyObject *ops;
205+
PyObject *optional_vars;
206+
PyObject *orelse;
207+
PyObject *posonlyargs;
208+
PyObject *returns;
209+
PyObject *right;
210+
PyObject *simple;
211+
PyObject *slice;
212+
PyObject *step;
213+
PyObject *stmt_type;
214+
PyObject *tag;
215+
PyObject *target;
216+
PyObject *targets;
217+
PyObject *test;
218+
PyObject *type;
219+
PyObject *type_comment;
220+
PyObject *type_ignore_type;
221+
PyObject *type_ignores;
222+
PyObject *unaryop_type;
223+
PyObject *upper;
224+
PyObject *value;
225+
PyObject *values;
226+
PyObject *vararg;
227+
PyObject *withitem_type;
228+
};
229+
230+
#ifdef __cplusplus
231+
}
232+
#endif
233+
#endif /* !Py_INTERNAL_AST_H */
234+

Include/internal/pycore_interp.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ extern "C" {
88
# error "this header requires Py_BUILD_CORE define"
99
#endif
1010

11-
#include "pycore_atomic.h" /* _Py_atomic_address */
12-
#include "pycore_gil.h" /* struct _gil_runtime_state */
13-
#include "pycore_gc.h" /* struct _gc_runtime_state */
14-
#include "pycore_warnings.h" /* struct _warnings_runtime_state */
11+
#include "pycore_atomic.h" // _Py_atomic_address
12+
#include "pycore_ast.h" // struct ast_state
13+
#include "pycore_gil.h" // struct _gil_runtime_state
14+
#include "pycore_gc.h" // struct _gc_runtime_state
15+
#include "pycore_warnings.h" // struct _warnings_runtime_state
1516

1617
struct _pending_calls {
1718
PyThread_type_lock lock;
@@ -258,6 +259,8 @@ struct _is {
258259
struct _Py_async_gen_state async_gen;
259260
struct _Py_context_state context;
260261
struct _Py_exc_state exc_state;
262+
263+
struct ast_state ast;
261264
};
262265

263266
/* Used by _PyImport_Cleanup() */

Makefile.pre.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -840,9 +840,11 @@ regen-ast:
840840
$(PYTHON_FOR_REGEN) $(srcdir)/Parser/asdl_c.py \
841841
$(srcdir)/Parser/Python.asdl \
842842
-H $(srcdir)/Include/Python-ast.h.new \
843+
-I $(srcdir)/Include/internal/pycore_ast.h.new \
843844
-C $(srcdir)/Python/Python-ast.c.new
844845

845846
$(UPDATE_FILE) $(srcdir)/Include/Python-ast.h $(srcdir)/Include/Python-ast.h.new
847+
$(UPDATE_FILE) $(srcdir)/Include/internal/pycore_ast.h $(srcdir)/Include/internal/pycore_ast.h.new
846848
$(UPDATE_FILE) $(srcdir)/Python/Python-ast.c $(srcdir)/Python/Python-ast.c.new
847849

848850
.PHONY: regen-opcode
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
The :mod:`ast` module internal state is now per interpreter. Patch by Victor
2+
Stinner.

PCbuild/regen.vcxproj

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@
137137
</None>
138138
<None Include="..\Include\opcode.h">
139139
</None>
140+
<None Include="..\Include\internal\pycore_ast.h">
141+
</None>
140142
<None Include="..\Include\Python-ast.h">
141143
</None>
142144
<None Include="..\Python\Python-ast.c">
@@ -161,15 +163,18 @@
161163
<Warning Text="Pegen updated. You will need to rebuild pythoncore to see the changes." Condition="'@(_UpdatedParse)' != ''" />
162164
</Target>
163165
<Target Name="_RegenAST_H" AfterTargets="_RegenGrammar">
164-
<!-- Regenerate Include/Python-ast.h and Python/Python-ast.c using Parser/asdl_c.py -h -->
165-
<Exec Command="&quot;$(PythonExe)&quot; &quot;$(PySourcePath)Parser\asdl_c.py&quot; &quot;$(PySourcePath)Parser\Python.asdl&quot; -H &quot;$(IntDir)Python-ast.h&quot; -C &quot;$(IntDir)Python-ast.c&quot;" />
166+
<!-- Regenerate Include/Python-ast.h, Python/Python-ast.c and Include/internal/pycore_ast.h using Parser/asdl_c.py -h -->
167+
<Exec Command="&quot;$(PythonExe)&quot; &quot;$(PySourcePath)Parser\asdl_c.py&quot; &quot;$(PySourcePath)Parser\Python.asdl&quot; -H &quot;$(IntDir)Python-ast.h&quot; -C &quot;$(IntDir)Python-ast.c&quot; -I &quot;$(IntDir)pycore_ast.h&quot;" />
166168
<Copy SourceFiles="$(IntDir)Python-ast.h" DestinationFiles="$(PySourcePath)Include\Python-ast.h">
167169
<Output TaskParameter="CopiedFiles" ItemName="_UpdatedH" />
168170
</Copy>
169171
<Copy SourceFiles="$(IntDir)Python-ast.c" DestinationFiles="$(PySourcePath)Python\Python-ast.c">
170172
<Output TaskParameter="CopiedFiles" ItemName="_UpdatedC" />
171173
</Copy>
172-
<Warning Text="ASDL is updated. You will need to rebuild pythoncore to see the changes." Condition="'@(_UpdatedH)' != '' and '@(_UpdatedC)' != ''" />
174+
<Copy SourceFiles="$(IntDir)pycore_ast.h" DestinationFiles="$(PySourcePath)Include\internal\pycore_ast.h">
175+
<Output TaskParameter="CopiedFiles" ItemName="_UpdatedInternalH" />
176+
</Copy>
177+
<Warning Text="ASDL is updated. You will need to rebuild pythoncore to see the changes." Condition="'@(_UpdatedH)' != '' != '' and '@(_UpdatedC)' != '' and @(_UpdatedInternalH)'" />
173178
</Target>
174179
<Target Name="_RegenOpcodes" AfterTargets="_RegenAST_C">
175180
<!-- Regenerate Include/opcode.h from Lib/opcode.py using Tools/scripts/generate_opcode_h.py-->
@@ -204,6 +209,7 @@
204209
<Clean Include="$(IntDir)opcode.h" />
205210
<Clean Include="$(IntDir)Python-ast.c" />
206211
<Clean Include="$(IntDir)Python-ast.h" />
212+
<Clean Include="$(IntDir)pycore_ast.h" />
207213
</ItemGroup>
208214
</Target>
209215
</Project>

0 commit comments

Comments
 (0)
0