Skip to content

Commit 7962b0d

Browse files
committed
Move AST class creation to postInitialize
1 parent 71b65ed commit 7962b0d

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1207,7 +1207,7 @@ Object generic(VirtualFrame frame, Object wSource, Object wFilename, Object wMod
12071207
}
12081208
checkOptimize(optimize, kwOptimize);
12091209
}
1210-
if (getCore().isCoreInitialized() && AstModuleBuiltins.isAst(getContext(), wSource)) {
1210+
if (AstModuleBuiltins.isAst(getContext(), wSource)) {
12111211
ModTy mod = AstModuleBuiltins.obj2sst(getContext(), wSource);
12121212
// TODO _PyAST_Validate
12131213
Source source = createFakeSource();

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/ast/AstModuleBuiltins.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ public void initialize(Python3Core core) {
9898
clsAst.setAttribute(T__FIELDS, emptyTuple);
9999
clsAst.setAttribute(T__ATTRIBUTES, emptyTuple);
100100
// TODO clsAst.setAttribute('__match_args__', emptyTuple);
101+
}
102+
103+
@Override
104+
public void postInitialize(Python3Core core) {
105+
super.postInitialize(core);
101106
PythonModule astModule = core.lookupBuiltinModule(T__AST);
102107
AstTypeFactory astTypeFactory = new AstTypeFactory(core.getLanguage(), core.factory(), astModule);
103108
AstState state = new AstState(astTypeFactory, core.lookupType(PythonBuiltinClassType.AST));
@@ -108,7 +113,8 @@ public void initialize(Python3Core core) {
108113

109114
private void createBackwardCompatibilityClasses(Python3Core core, PythonModule astModule, AstState state) {
110115
// ast.py from cpython 3.10.5 defines classes for backwards compatibility
111-
// As long as we are still using ast.py from 3.8.6, we need to provide these classes somehow.
116+
// As long as we are still using ast.py from 3.8.6, we need to provide these classes
117+
// somehow.
112118
PythonLanguage language = core.getLanguage();
113119
PythonObjectFactory factory = core.factory();
114120

@@ -146,6 +152,9 @@ public static ModTy obj2sst(PythonContext context, Object obj) {
146152

147153
@TruffleBoundary
148154
public static boolean isAst(PythonContext context, Object obj) {
149-
return Obj2SstBase.isInstanceOf(obj, getAstState(context).clsAst);
155+
// We need to look up the ast.AST class in the context and cannot rely on the cached value
156+
// in AstState.clsAst because this method is called from compile() which may be called
157+
// before postInitialize().
158+
return Obj2SstBase.isInstanceOf(obj, context.lookupType(PythonBuiltinClassType.AST));
150159
}
151160
}

0 commit comments

Comments
 (0)