Skip to content

Commit fb39788

Browse files
committed
[GR-24424] Correctly query engine options.
PullRequest: graalpython/1071
2 parents b0531d7 + 77d194f commit fb39788

17 files changed

+147
-108
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/PythonLanguage.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,7 @@ public static Source newSource(PythonContext ctxt, TruffleFile src, String name)
559559

560560
private static Source newSource(PythonContext ctxt, SourceBuilder srcBuilder) throws IOException {
561561
boolean coreIsInitialized = ctxt.getCore().isInitialized();
562-
boolean internal = !coreIsInitialized && !ctxt.getOption(PythonOptions.ExposeInternalSources);
562+
boolean internal = !coreIsInitialized && !ctxt.getLanguage().getEngineOption(PythonOptions.ExposeInternalSources);
563563
if (internal) {
564564
srcBuilder.internal(true);
565565
}

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

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@
170170
import com.oracle.truffle.api.RootCallTarget;
171171
import com.oracle.truffle.api.Truffle;
172172
import com.oracle.truffle.api.TruffleLanguage;
173+
import com.oracle.truffle.api.TruffleLanguage.LanguageReference;
173174
import com.oracle.truffle.api.debug.Debugger;
174175
import com.oracle.truffle.api.dsl.Cached;
175176
import com.oracle.truffle.api.dsl.Cached.Shared;
@@ -1094,7 +1095,7 @@ public abstract static class IsInstanceNode extends PythonBinaryBuiltinNode {
10941095
@Child private SequenceStorageNodes.LenNode lenNode;
10951096
@Child private GetObjectArrayNode getObjectArrayNode;
10961097

1097-
@CompilationFinal private Boolean emulateJython;
1098+
@CompilationFinal private LanguageReference<PythonLanguage> languageRef;
10981099

10991100
public static IsInstanceNode create() {
11001101
return BuiltinFunctionsFactory.IsInstanceNodeFactory.create();
@@ -1109,8 +1110,8 @@ private boolean isInstanceCheckInternal(VirtualFrame frame, Object instance, Obj
11091110

11101111
@Specialization
11111112
boolean isInstance(VirtualFrame frame, Object instance, PythonAbstractClass cls,
1112-
@Cached("create()") TypeNodes.IsSameTypeNode isSameTypeNode,
1113-
@Cached("create()") IsSubtypeNode isSubtypeNode) {
1113+
@Cached TypeNodes.IsSameTypeNode isSameTypeNode,
1114+
@Cached IsSubtypeNode isSubtypeNode) {
11141115
Object instanceClass = getClassNode.execute(instance);
11151116
return isSameTypeNode.execute(instanceClass, cls) || isSubtypeNode.execute(frame, instanceClass, cls) || isInstanceCheckInternal(frame, instance, cls);
11161117
}
@@ -1119,7 +1120,7 @@ boolean isInstance(VirtualFrame frame, Object instance, PythonAbstractClass cls,
11191120
@ExplodeLoop(kind = LoopExplosionKind.FULL_UNROLL_UNTIL_RETURN)
11201121
boolean isInstanceTupleConstantLen(VirtualFrame frame, Object instance, PTuple clsTuple,
11211122
@Cached("getLength(clsTuple)") int cachedLen,
1122-
@Cached("create()") IsInstanceNode isInstanceNode) {
1123+
@Cached IsInstanceNode isInstanceNode) {
11231124
Object[] array = getArray(clsTuple);
11241125
for (int i = 0; i < cachedLen; i++) {
11251126
Object cls = array[i];
@@ -1132,7 +1133,7 @@ boolean isInstanceTupleConstantLen(VirtualFrame frame, Object instance, PTuple c
11321133

11331134
@Specialization(replaces = "isInstanceTupleConstantLen")
11341135
boolean isInstance(VirtualFrame frame, Object instance, PTuple clsTuple,
1135-
@Cached("create()") IsInstanceNode instanceNode) {
1136+
@Cached IsInstanceNode instanceNode) {
11361137
for (Object cls : getArray(clsTuple)) {
11371138
if (instanceNode.executeWith(frame, instance, cls)) {
11381139
return true;
@@ -1143,12 +1144,13 @@ boolean isInstance(VirtualFrame frame, Object instance, PTuple clsTuple,
11431144

11441145
@Fallback
11451146
boolean isInstance(VirtualFrame frame, Object instance, Object cls) {
1146-
PythonContext context = getContext();
1147-
TruffleLanguage.Env env = context.getEnv();
1148-
if (context.getOption(PythonOptions.EmulateJython) && env.isHostObject(cls)) {
1149-
Object hostCls = env.asHostObject(cls);
1150-
Object hostInstance = env.isHostObject(instance) ? env.asHostObject(instance) : instance;
1151-
return hostCls instanceof Class && ((Class<?>) hostCls).isAssignableFrom(hostInstance.getClass());
1147+
if (getPythonLanguage().getEngineOption(PythonOptions.EmulateJython)) {
1148+
TruffleLanguage.Env env = getContext().getEnv();
1149+
if (env.isHostObject(cls)) {
1150+
Object hostCls = env.asHostObject(cls);
1151+
Object hostInstance = env.isHostObject(instance) ? env.asHostObject(instance) : instance;
1152+
return hostCls instanceof Class && ((Class<?>) hostCls).isAssignableFrom(hostInstance.getClass());
1153+
}
11521154
}
11531155
return isInstanceCheckInternal(frame, instance, cls) || typeInstanceCheckNode.executeWith(frame, cls, instance);
11541156
}
@@ -1168,6 +1170,14 @@ private Object[] getArray(PTuple tuple) {
11681170
}
11691171
return getObjectArrayNode.execute(tuple);
11701172
}
1173+
1174+
private PythonLanguage getPythonLanguage() {
1175+
if (languageRef == null) {
1176+
CompilerDirectives.transferToInterpreterAndInvalidate();
1177+
languageRef = lookupLanguageReference(PythonLanguage.class);
1178+
}
1179+
return languageRef.get();
1180+
}
11711181
}
11721182

11731183
// issubclass(class, classinfo)

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,16 +129,17 @@ public void postInitialize(PythonCore core) {
129129
super.postInitialize(core);
130130
PythonContext context = core.getContext();
131131
PythonModule mod = core.lookupBuiltinModule(__GRAALPYTHON__);
132+
PythonLanguage language = context.getLanguage();
132133
if (!ImageInfo.inImageBuildtimeCode()) {
133-
mod.setAttribute("home", context.getLanguage().getHome());
134+
mod.setAttribute("home", language.getHome());
134135
}
135136
String coreHome = context.getCoreHome();
136137
String stdlibHome = context.getStdlibHome();
137138
String capiHome = context.getCAPIHome();
138139
Env env = context.getEnv();
139140
LanguageInfo llvmInfo = env.getInternalLanguages().get(LLVM_LANGUAGE);
140141
Toolchain toolchain = env.lookup(llvmInfo, Toolchain.class);
141-
mod.setAttribute("jython_emulation_enabled", context.getOption(PythonOptions.EmulateJython));
142+
mod.setAttribute("jython_emulation_enabled", language.getEngineOption(PythonOptions.EmulateJython));
142143
mod.setAttribute("host_import_enabled", context.getEnv().isHostLookupAllowed());
143144
mod.setAttribute("core_home", coreHome);
144145
mod.setAttribute("stdlib_home", stdlibHome);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ private void ensureCapiWasLoaded() {
281281
Object capi = null;
282282
try {
283283
SourceBuilder capiSrcBuilder = Source.newBuilder(LLVM_LANGUAGE, capiFile);
284-
if (!context.getOption(PythonOptions.ExposeInternalSources)) {
284+
if (!context.getLanguage().getEngineOption(PythonOptions.ExposeInternalSources)) {
285285
capiSrcBuilder.internal(true);
286286
}
287287
capi = context.getEnv().parseInternal(capiSrcBuilder.build()).call();

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

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
import static com.oracle.graal.python.runtime.exception.PythonErrorType.TypeError;
4444
import static com.oracle.graal.python.runtime.exception.PythonErrorType.ValueError;
4545

46-
import java.io.UnsupportedEncodingException;
46+
import java.nio.charset.StandardCharsets;
4747
import java.util.List;
4848

4949
import com.oracle.graal.python.PythonLanguage;
@@ -72,7 +72,9 @@
7272
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
7373
import com.oracle.truffle.api.TruffleException;
7474
import com.oracle.truffle.api.dsl.Cached;
75+
import com.oracle.truffle.api.dsl.Cached.Shared;
7576
import com.oracle.truffle.api.dsl.CachedContext;
77+
import com.oracle.truffle.api.dsl.CachedLanguage;
7678
import com.oracle.truffle.api.dsl.Fallback;
7779
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
7880
import com.oracle.truffle.api.dsl.NodeFactory;
@@ -97,20 +99,20 @@ protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFa
9799
@Builtin(name = "_build_regex_engine", minNumOfPositionalArgs = 1)
98100
@GenerateNodeFactory
99101
abstract static class BuildRegexEngine extends PythonUnaryBuiltinNode {
100-
protected static boolean withTRegex(PythonContext context) {
101-
return context.getOption(PythonOptions.WithTRegex);
102+
protected static boolean withTRegex(PythonLanguage language) {
103+
return language.getEngineOption(PythonOptions.WithTRegex);
102104
}
103105

104-
@Specialization(guards = "!withTRegex(context)")
105-
Object useSRE(@SuppressWarnings("unused") String code,
106-
@SuppressWarnings("unused") @CachedContext(PythonLanguage.class) PythonContext context) {
106+
@Specialization(guards = "!withTRegex(language)")
107+
static Object useSRE(@SuppressWarnings("unused") String code,
108+
@Shared("language") @CachedLanguage @SuppressWarnings("unused") PythonLanguage language) {
107109
return PNone.NONE;
108110
}
109111

110-
@Specialization(guards = "withTRegex(context)")
112+
@Specialization(guards = "withTRegex(language)")
111113
@TruffleBoundary
112114
Object run(String code,
113-
@SuppressWarnings("unused") @CachedContext(PythonLanguage.class) PythonContext context) {
115+
@Shared("language") @CachedLanguage @SuppressWarnings("unused") PythonLanguage language) {
114116
return getContext().getEnv().parseInternal(Source.newBuilder("regex", code, "build-regex-engine").build()).call();
115117
}
116118
}
@@ -144,29 +146,19 @@ Object run(String str) {
144146
@Specialization
145147
Object run(PIBytesLike str) {
146148
byte[] bytes = doBytes(getToByteArrayNode().execute(str.getSequenceStorage()));
147-
if (bytes != null) {
148-
return factory().createByteArray(bytes);
149-
}
150-
return str;
149+
return factory().createByteArray(bytes);
151150
}
152151

153152
@Specialization
154153
Object run(VirtualFrame frame, PMemoryView memoryView) {
155154
byte[] bytes = doBytes(getToBytesNode().execute(frame, memoryView));
156-
if (bytes != null) {
157-
return factory().createByteArray(bytes);
158-
}
159-
return memoryView;
155+
return factory().createByteArray(bytes);
160156
}
161157

162158
@TruffleBoundary(transferToInterpreterOnException = false, allowInlining = true)
163159
private byte[] doBytes(byte[] str) {
164-
try {
165-
StringBuilder sb = BytesUtils.decodeEscapes(getCore(), new String(str, "ascii"), true);
166-
return sb.toString().getBytes("ascii");
167-
} catch (UnsupportedEncodingException e) {
168-
}
169-
return null;
160+
StringBuilder sb = BytesUtils.decodeEscapes(getCore(), new String(str, StandardCharsets.US_ASCII), true);
161+
return sb.toString().getBytes(StandardCharsets.US_ASCII);
170162
}
171163

172164
private static boolean containsBackslash(String str) {

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,13 @@
4242

4343
import java.util.List;
4444

45+
import org.graalvm.collections.EconomicMap;
46+
4547
import com.oracle.graal.python.builtins.Builtin;
4648
import com.oracle.graal.python.builtins.CoreFunctions;
4749
import com.oracle.graal.python.builtins.PythonBuiltins;
4850
import com.oracle.graal.python.builtins.objects.dict.PDict;
51+
import com.oracle.graal.python.builtins.objects.ints.PInt;
4952
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
5053
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
5154
import com.oracle.graal.python.runtime.PythonCore;
@@ -54,8 +57,6 @@
5457
import com.oracle.truffle.api.dsl.NodeFactory;
5558
import com.oracle.truffle.api.dsl.Specialization;
5659

57-
import org.graalvm.collections.EconomicMap;
58-
5960
/**
6061
* this builtin module is used to fill in truffle land config options into the sysconfig python
6162
* module
@@ -66,8 +67,8 @@ public class SysConfigModuleBuiltins extends PythonBuiltins {
6667

6768
@Override
6869
public void initialize(PythonCore core) {
70+
STATIC_CONFIG_OPTIONS.put("WITH_THREAD", PInt.intValue(core.getLanguage().getEngineOption(PythonOptions.WithThread)));
6971
super.initialize(core);
70-
STATIC_CONFIG_OPTIONS.put("WITH_THREAD", core.getContext().getOption(PythonOptions.WithThread) ? 1 : 0);
7172
}
7273

7374
@Override

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/argument/ReadVarKeywordsNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ protected int getLimit() {
7373
protected int getAndCheckKwargLen(VirtualFrame frame) {
7474
CompilerAsserts.neverPartOfCompilation("caching the kwarg len should never be compiled");
7575
int length = getKwargLen(frame);
76-
if (length >= PythonLanguage.getContext().getOption(PythonOptions.VariableArgumentReadUnrollingLimit)) {
76+
if (length >= PythonLanguage.getCurrent().getEngineOption(PythonOptions.VariableArgumentReadUnrollingLimit)) {
7777
return -1;
7878
}
7979
return length;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/call/InvokeNode.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import com.oracle.graal.python.runtime.PythonOptions;
3636
import com.oracle.truffle.api.Assumption;
3737
import com.oracle.truffle.api.CallTarget;
38+
import com.oracle.truffle.api.CompilerAsserts;
3839
import com.oracle.truffle.api.CompilerDirectives;
3940
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
4041
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
@@ -46,11 +47,13 @@
4647

4748
public abstract class InvokeNode extends Node implements IndirectCallNode {
4849
protected static boolean shouldInlineGenerators() {
49-
return PythonLanguage.getContext().getOption(PythonOptions.ForceInlineGeneratorCalls);
50+
CompilerAsserts.neverPartOfCompilation();
51+
return PythonLanguage.getCurrent().getEngineOption(PythonOptions.ForceInlineGeneratorCalls);
5052
}
5153

5254
protected static boolean forceSplitBuiltins() {
53-
return PythonLanguage.getContext().getOption(PythonOptions.EnableForcedSplits);
55+
CompilerAsserts.neverPartOfCompilation();
56+
return PythonLanguage.getCurrent().getEngineOption(PythonOptions.EnableForcedSplits);
5457
}
5558

5659
@TruffleBoundary

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/call/special/CallSpecialMethodNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ private <T extends PythonBuiltinBaseNode> boolean callerExceedsMaxSize(T builtin
115115
int n = root instanceof PRootNode ? ((PRootNode) root).getNodeCount() : NodeUtil.countNodes(root);
116116
// nb: option 'BuiltinsInliningMaxCallerSize' is defined as a compatible option, i.e.,
117117
// ASTs will only we shared between contexts that have the same value for this option.
118-
int maxSize = lookupContextReference(PythonLanguage.class).get().getOption(PythonOptions.BuiltinsInliningMaxCallerSize);
118+
int maxSize = lookupLanguageReference(PythonLanguage.class).get().getEngineOption(PythonOptions.BuiltinsInliningMaxCallerSize);
119119
if (n >= maxSize || n + NodeUtil.countNodes(builtinNode) >= maxSize) {
120120
setMaxSizeExceeded(true);
121121
return true;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/control/TopLevelExceptionHandler.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,12 @@ private static void printJavaStackTrace(PException e) {
264264
traceback = traceback.getNextChain();
265265
}
266266
if (traceback != null) {
267-
traceback.getException().printStackTrace();
267+
PException exception = traceback.getException();
268+
if (exception.getCause() != null && exception.getCause().getStackTrace().length != 0) {
269+
exception.getCause().printStackTrace();
270+
} else {
271+
exception.printStackTrace();
272+
}
268273
}
269274
}
270275

0 commit comments

Comments
 (0)