Skip to content

Commit 16102ea

Browse files
committed
Adding PythonEngineOptions.
1 parent a11ed69 commit 16102ea

File tree

10 files changed

+78
-41
lines changed

10 files changed

+78
-41
lines changed

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

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@
6464
import com.oracle.graal.python.nodes.object.GetClassNode;
6565
import com.oracle.graal.python.parser.PythonParserImpl;
6666
import com.oracle.graal.python.runtime.PythonContext;
67-
import com.oracle.graal.python.runtime.PythonContextOptions;
6867
import com.oracle.graal.python.runtime.PythonCore;
68+
import com.oracle.graal.python.runtime.PythonEngineOptions;
6969
import com.oracle.graal.python.runtime.PythonOptions;
7070
import com.oracle.graal.python.runtime.PythonParser.ParserMode;
7171
import com.oracle.graal.python.runtime.exception.PException;
@@ -182,16 +182,7 @@ protected void finalizeContext(PythonContext context) {
182182

183183
@Override
184184
protected boolean areOptionsCompatible(OptionValues firstOptions, OptionValues newOptions) {
185-
// internal sources were marked during context initialization
186-
return (firstOptions.get(PythonOptions.ExposeInternalSources).equals(newOptions.get(PythonOptions.ExposeInternalSources)) &&
187-
// we cache WithThread on the language
188-
firstOptions.get(PythonOptions.WithThread).equals(newOptions.get(PythonOptions.WithThread)) &&
189-
// we cache JythonEmulation on nodes
190-
firstOptions.get(PythonOptions.EmulateJython).equals(newOptions.get(PythonOptions.EmulateJython)) &&
191-
// we cache CatchAllExceptions hard on TryExceptNode
192-
firstOptions.get(PythonOptions.CatchAllExceptions).equals(newOptions.get(PythonOptions.CatchAllExceptions)) &&
193-
// we cache BuiltinsInliningMaxCallerSize on the language
194-
firstOptions.get(PythonOptions.BuiltinsInliningMaxCallerSize).equals(newOptions.get(PythonOptions.BuiltinsInliningMaxCallerSize)));
185+
return PythonEngineOptions.fromOptionValues(firstOptions).equals(PythonEngineOptions.fromOptionValues(newOptions));
195186
}
196187

197188
private boolean areOptionsCompatibleWithPreinitializedContext(OptionValues firstOptions, OptionValues newOptions) {
@@ -218,7 +209,7 @@ protected PythonContext createContext(Env env) {
218209
assert this.isWithThread == null || this.isWithThread == PythonOptions.isWithThread(env) : "conflicting thread options in the same language!";
219210
this.isWithThread = PythonOptions.isWithThread(env);
220211
Python3Core newCore = new Python3Core(new PythonParserImpl(env));
221-
final PythonContext context = new PythonContext(this, env, newCore, PythonContextOptions.fromOptionValues(env.getOptions()));
212+
final PythonContext context = new PythonContext(this, env, newCore);
222213
context.initializeHomeAndPrefixPaths(env, getLanguageHome());
223214
return context;
224215
}
@@ -551,7 +542,7 @@ public static Source newSource(PythonContext ctxt, TruffleFile src, String name)
551542

552543
private static Source newSource(PythonContext ctxt, SourceBuilder srcBuilder) throws IOException {
553544
boolean coreIsInitialized = ctxt.getCore().isInitialized();
554-
boolean internal = !coreIsInitialized && !PythonOptions.getOption(ctxt, PythonOptions.ExposeInternalSources);
545+
boolean internal = !coreIsInitialized && !ctxt.areInternalSourcesExposed();
555546
if (internal) {
556547
srcBuilder.internal(true);
557548
}

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
@@ -1107,7 +1107,7 @@ boolean isInstance(VirtualFrame frame, Object instance, PTuple clsTuple,
11071107
protected boolean emulateJython() {
11081108
if (emulateJython == null) {
11091109
CompilerDirectives.transferToInterpreterAndInvalidate();
1110-
emulateJython = PythonOptions.getFlag(getContext(), PythonOptions.EmulateJython);
1110+
emulateJython = getContext().isJythonEmulated();
11111111
}
11121112
return emulateJython;
11131113
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2017, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -267,7 +267,7 @@ private void ensureCapiWasLoaded() {
267267
Object capi = null;
268268
try {
269269
SourceBuilder capiSrcBuilder = Source.newBuilder(LLVM_LANGUAGE, capiFile);
270-
if (!PythonOptions.getOption(context, PythonOptions.ExposeInternalSources)) {
270+
if (!context.areInternalSourcesExposed()) {
271271
capiSrcBuilder.internal(true);
272272
}
273273
capi = context.getEnv().parseInternal(capiSrcBuilder.build()).call();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ public void postInitialize(PythonCore core) {
196196
sys.setAttribute("executable", PythonOptions.getOption(context, PythonOptions.Executable));
197197
sys.setAttribute("graal_python_home", context.getLanguage().getHome());
198198
}
199-
sys.setAttribute("graal_python_jython_emulation_enabled", PythonOptions.getOption(context, PythonOptions.EmulateJython));
199+
sys.setAttribute("graal_python_jython_emulation_enabled", context.isJythonEmulated());
200200
sys.setAttribute("graal_python_host_import_enabled", context.getEnv().isHostLookupAllowed());
201201
sys.setAttribute("graal_python_core_home", coreHome);
202202
sys.setAttribute("graal_python_stdlib_home", stdlibHome);

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
@@ -111,7 +111,7 @@ private <T extends PythonBuiltinBaseNode> boolean callerExceedsMaxSize(T builtin
111111
int n = root instanceof PRootNode ? ((PRootNode) root).getNodeCount() : NodeUtil.countNodes(root);
112112
// nb: option 'BuiltinsInliningMaxCallerSize' is defined as a compatible option, i.e.,
113113
// ASTs will only we shared between contexts that have the same value for this option.
114-
int maxSize = PythonOptions.getOption(lookupContextReference(PythonLanguage.class).get(), PythonOptions.BuiltinsInliningMaxCallerSize);
114+
int maxSize = lookupContextReference(PythonLanguage.class).get().getBuiltinsInliningMaxCallerSize();
115115
if (n >= maxSize || n + NodeUtil.countNodes(builtinNode) >= maxSize) {
116116
maxSizeExceeded = true;
117117
return true;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/statement/AbstractImportNode.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -52,9 +52,7 @@
5252
import com.oracle.graal.python.nodes.BuiltinNames;
5353
import com.oracle.graal.python.nodes.call.CallNode;
5454
import com.oracle.graal.python.nodes.object.GetDictNode;
55-
import com.oracle.graal.python.nodes.util.ExceptionStateNodes.PassCaughtExceptionNode;
5655
import com.oracle.graal.python.runtime.PythonContext;
57-
import com.oracle.graal.python.runtime.PythonOptions;
5856
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
5957
import com.oracle.truffle.api.CompilerDirectives;
6058
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
@@ -69,7 +67,6 @@ public abstract class AbstractImportNode extends StatementNode {
6967

7068
@Child private CallNode callNode;
7169
@Child private GetDictNode getDictNode;
72-
@Child private PassCaughtExceptionNode passExceptionNode;
7370
@CompilationFinal private Boolean emulateJython;
7471

7572
public AbstractImportNode() {
@@ -156,7 +153,7 @@ Object __import__(VirtualFrame frame, String name, Object globals, String[] from
156153
protected boolean emulateJython() {
157154
if (emulateJython == null) {
158155
CompilerDirectives.transferToInterpreterAndInvalidate();
159-
emulateJython = PythonOptions.getFlag(getContext(), PythonOptions.EmulateJython);
156+
emulateJython = getContext().isJythonEmulated();
160157
}
161158
return emulateJython;
162159
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/statement/AssertNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public void executeVoid(VirtualFrame frame) {
6666
if (assertionsEnabled == null) {
6767
CompilerDirectives.transferToInterpreterAndInvalidate();
6868
PythonContext context = getContext();
69-
javaExceptionsFailAssertions = PythonOptions.getOption(context, PythonOptions.CatchAllExceptions);
69+
javaExceptionsFailAssertions = context.isCatchingAllExcetptionsEnabled();
7070
assertionsEnabled = !PythonOptions.getOption(context, PythonOptions.PythonOptimizeFlag);
7171
}
7272
if (assertionsEnabled) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/statement/TryExceptNode.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2019, Oracle and/or its affiliates.
2+
* Copyright (c) 2017, 2020, Oracle and/or its affiliates.
33
* Copyright (c) 2013, Regents of the University of California
44
*
55
* All rights reserved.
@@ -25,9 +25,6 @@
2525
*/
2626
package com.oracle.graal.python.nodes.statement;
2727

28-
import static com.oracle.graal.python.runtime.PythonOptions.CatchAllExceptions;
29-
import static com.oracle.graal.python.runtime.PythonOptions.EmulateJython;
30-
3128
import java.util.ArrayList;
3229

3330
import com.oracle.graal.python.PythonLanguage;
@@ -47,7 +44,6 @@
4744
import com.oracle.graal.python.nodes.util.ExceptionStateNodes.RestoreExceptionStateNode;
4845
import com.oracle.graal.python.nodes.util.ExceptionStateNodes.SaveExceptionStateNode;
4946
import com.oracle.graal.python.runtime.PythonContext;
50-
import com.oracle.graal.python.runtime.PythonOptions;
5147
import com.oracle.graal.python.runtime.exception.ExceptionHandledException;
5248
import com.oracle.graal.python.runtime.exception.PException;
5349
import com.oracle.graal.python.runtime.exception.PythonErrorType;
@@ -130,7 +126,7 @@ public void executeVoid(VirtualFrame frame) {
130126
} catch (Exception e) {
131127
if (shouldCatchJavaExceptions == null) {
132128
CompilerDirectives.transferToInterpreterAndInvalidate();
133-
shouldCatchJavaExceptions = PythonOptions.getOption(getContext(), EmulateJython);
129+
shouldCatchJavaExceptions = getContext().isJythonEmulated();
134130
}
135131
if (shouldCatchJavaExceptions && getContext().getEnv().isHostException(e)) {
136132
if (catchException(frame, (TruffleException) e, exceptionState)) {
@@ -139,7 +135,7 @@ public void executeVoid(VirtualFrame frame) {
139135
}
140136
if (shouldCatchAll == null) {
141137
CompilerDirectives.transferToInterpreterAndInvalidate();
142-
shouldCatchAll = PythonOptions.getOption(getContext(), CatchAllExceptions);
138+
shouldCatchAll = getContext().isCatchingAllExcetptionsEnabled();
143139
}
144140
if (shouldCatchAll) {
145141
if (e instanceof ControlFlowException) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/PythonContext.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,15 +223,15 @@ List<WeakReference<Thread>> getOwners() {
223223
// compat
224224
private final ThreadLocal<ArrayDeque<String>> currentImport = new ThreadLocal<>();
225225

226-
private final PythonContextOptions options;
226+
private final PythonEngineOptions engineOptions;
227227

228-
public PythonContext(PythonLanguage language, TruffleLanguage.Env env, PythonCore core, PythonContextOptions options) {
228+
public PythonContext(PythonLanguage language, TruffleLanguage.Env env, PythonCore core) {
229229
this.language = language;
230230
this.core = core;
231231
this.env = env;
232232
this.resources = new PosixResources();
233233
this.handler = new AsyncHandler(language);
234-
this.options = options;
234+
this.engineOptions = PythonEngineOptions.fromOptionValues(env.getOptions());
235235
if (env == null) {
236236
this.in = System.in;
237237
this.out = System.out;
@@ -380,8 +380,20 @@ public void patch(Env newEnv) {
380380
core.postInitialize();
381381
}
382382

383+
public boolean isCatchingAllExcetptionsEnabled() {
384+
return engineOptions.isCatchingAllExcetptionsEnabled();
385+
}
386+
387+
public boolean areInternalSourcesExposed() {
388+
return engineOptions.areInternalSourcesExposed();
389+
}
390+
383391
public boolean isJythonEmulated() {
384-
return options.isJythonEmulated();
392+
return engineOptions.isJythonEmulated();
393+
}
394+
395+
public int getBuiltinsInliningMaxCallerSize() {
396+
return engineOptions.getBuiltinsInliningMaxCallerSize();
385397
}
386398

387399
/**

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/PythonContextOptions.java renamed to graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/PythonEngineOptions.java

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,42 +45,74 @@
4545
import org.graalvm.options.OptionKey;
4646
import org.graalvm.options.OptionValues;
4747

48-
public class PythonContextOptions {
48+
public class PythonEngineOptions {
4949
@CompilationFinal private OptionValues optionValues;
5050

51+
@CompilationFinal private int builtinsInliningMaxCallerSize;
52+
@CompilationFinal private boolean catchAllExceptions;
5153
@CompilationFinal private boolean emulatedJython;
54+
@CompilationFinal private boolean exposeInternalSources;
55+
@CompilationFinal private boolean withThread;
5256

53-
PythonContextOptions(OptionValues optionValues) {
57+
PythonEngineOptions(OptionValues optionValues) {
5458
this.optionValues = optionValues;
5559
setOptionValues(optionValues);
5660
}
5761

58-
public static PythonContextOptions fromOptionValues(OptionValues optionValues) {
59-
return new PythonContextOptions(optionValues);
62+
public static PythonEngineOptions fromOptionValues(OptionValues optionValues) {
63+
return new PythonEngineOptions(optionValues);
6064
}
6165

6266
private boolean readBooleanOption(OptionKey<Boolean> key) {
6367
return key.getValue(optionValues);
6468
}
6569

70+
private int readIntegerOption(OptionKey<Integer> key) {
71+
return key.getValue(optionValues);
72+
}
73+
6674
public void setOptionValues(OptionValues newOptions) {
6775
CompilerAsserts.neverPartOfCompilation();
6876
optionValues = newOptions;
6977
cacheOptions();
7078
}
7179

7280
private void cacheOptions() {
81+
this.builtinsInliningMaxCallerSize = readIntegerOption(PythonOptions.BuiltinsInliningMaxCallerSize);
82+
this.catchAllExceptions = readBooleanOption(PythonOptions.CatchAllExceptions);
7383
this.emulatedJython = readBooleanOption(PythonOptions.EmulateJython);
84+
this.exposeInternalSources = readBooleanOption(PythonOptions.ExposeInternalSources);
85+
this.withThread = readBooleanOption(PythonOptions.WithThread);
86+
}
87+
88+
public boolean areInternalSourcesExposed() {
89+
return this.exposeInternalSources;
90+
}
91+
92+
public boolean isCatchingAllExcetptionsEnabled() {
93+
return this.catchAllExceptions;
7494
}
7595

7696
public boolean isJythonEmulated() {
7797
return this.emulatedJython;
7898
}
7999

100+
public boolean isWithThread() {
101+
return this.withThread;
102+
}
103+
104+
public int getBuiltinsInliningMaxCallerSize() {
105+
return this.builtinsInliningMaxCallerSize;
106+
}
107+
80108
@Override
81109
public int hashCode() {
82110
int hash = 7;
111+
hash = 53 * hash + this.builtinsInliningMaxCallerSize;
112+
hash = 53 * hash + (this.catchAllExceptions ? 1 : 0);
83113
hash = 53 * hash + (this.emulatedJython ? 1 : 0);
114+
hash = 53 * hash + (this.exposeInternalSources ? 1 : 0);
115+
hash = 53 * hash + (this.withThread ? 1 : 0);
84116
return hash;
85117
}
86118

@@ -95,10 +127,19 @@ public boolean equals(Object obj) {
95127
if (getClass() != obj.getClass()) {
96128
return false;
97129
}
98-
final PythonContextOptions other = (PythonContextOptions) obj;
130+
final PythonEngineOptions other = (PythonEngineOptions) obj;
131+
if (this.builtinsInliningMaxCallerSize != other.builtinsInliningMaxCallerSize) {
132+
return false;
133+
}
134+
if (this.catchAllExceptions != other.catchAllExceptions) {
135+
return false;
136+
}
99137
if (this.emulatedJython != other.emulatedJython) {
100138
return false;
101139
}
102-
return true;
140+
if (this.exposeInternalSources != other.exposeInternalSources) {
141+
return false;
142+
}
143+
return this.withThread == other.withThread;
103144
}
104145
}

0 commit comments

Comments
 (0)