Skip to content

Commit 71b65ed

Browse files
committed
Provide fake ast classes for backwards compatibility
1 parent fa32768 commit 71b65ed

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,23 +44,27 @@
4444
import static com.oracle.graal.python.builtins.modules.BuiltinFunctions.CompileNode.PyCF_ONLY_AST;
4545
import static com.oracle.graal.python.builtins.modules.BuiltinFunctions.CompileNode.PyCF_TYPE_COMMENTS;
4646
import static com.oracle.graal.python.util.PythonUtils.EMPTY_OBJECT_ARRAY;
47+
import static com.oracle.graal.python.util.PythonUtils.toTruffleStringUncached;
4748
import static com.oracle.graal.python.util.PythonUtils.tsLiteral;
4849

4950
import java.util.ArrayList;
5051
import java.util.List;
5152

53+
import com.oracle.graal.python.PythonLanguage;
5254
import com.oracle.graal.python.builtins.CoreFunctions;
5355
import com.oracle.graal.python.builtins.Python3Core;
5456
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
5557
import com.oracle.graal.python.builtins.PythonBuiltins;
5658
import com.oracle.graal.python.builtins.objects.module.PythonModule;
5759
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
60+
import com.oracle.graal.python.builtins.objects.type.PythonAbstractClass;
5861
import com.oracle.graal.python.builtins.objects.type.PythonBuiltinClass;
5962
import com.oracle.graal.python.builtins.objects.type.PythonClass;
6063
import com.oracle.graal.python.nodes.attributes.ReadAttributeFromObjectNode;
6164
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
6265
import com.oracle.graal.python.pegparser.sst.ModTy;
6366
import com.oracle.graal.python.runtime.PythonContext;
67+
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
6468
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
6569
import com.oracle.truffle.api.dsl.NodeFactory;
6670
import com.oracle.truffle.api.object.HiddenKey;
@@ -98,6 +102,32 @@ public void initialize(Python3Core core) {
98102
AstTypeFactory astTypeFactory = new AstTypeFactory(core.getLanguage(), core.factory(), astModule);
99103
AstState state = new AstState(astTypeFactory, core.lookupType(PythonBuiltinClassType.AST));
100104
astModule.setAttribute(AST_STATE_KEY, state);
105+
106+
createBackwardCompatibilityClasses(core, astModule, state);
107+
}
108+
109+
private void createBackwardCompatibilityClasses(Python3Core core, PythonModule astModule, AstState state) {
110+
// 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.
112+
PythonLanguage language = core.getLanguage();
113+
PythonObjectFactory factory = core.factory();
114+
115+
makeType(language, factory, astModule, "Suite", state.clsModTy);
116+
117+
PythonClass slice_type = makeType(language, factory, astModule, "slice", state.clsAst);
118+
makeType(language, factory, astModule, "ExtSlice", slice_type);
119+
makeType(language, factory, astModule, "Index", slice_type);
120+
121+
makeType(language, factory, astModule, "AugLoad", state.clsExprContextTy);
122+
makeType(language, factory, astModule, "AugStore", state.clsExprContextTy);
123+
makeType(language, factory, astModule, "Param", state.clsExprContextTy);
124+
}
125+
126+
private static PythonClass makeType(PythonLanguage language, PythonObjectFactory factory, PythonModule astModule, String name, PythonAbstractClass base) {
127+
TruffleString tsName = toTruffleStringUncached(name);
128+
PythonClass newType = factory.createPythonClassAndFixupSlots(language, PythonBuiltinClassType.PythonClass, tsName, new PythonAbstractClass[]{base});
129+
astModule.setAttribute(tsName, newType);
130+
return newType;
101131
}
102132

103133
private static AstState getAstState(PythonContext context) {

0 commit comments

Comments
 (0)