Skip to content

Commit 6456c83

Browse files
committed
[GR-12976] _thread builtin support controllable via expert mode level "python.WithThread" option
PullRequest: graalpython/325
2 parents 2fd3d5c + 00a0fdb commit 6456c83

File tree

4 files changed

+20
-11
lines changed

4 files changed

+20
-11
lines changed

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,6 @@ public final class PythonLanguage extends TruffleLanguage<PythonContext> {
103103
public static final int MICRO = 0;
104104
public static final String VERSION = MAJOR + "." + MINOR + "." + MICRO;
105105

106-
public static boolean WITH_THREADS = false;
107-
108106
public static final String MIME_TYPE = "text/x-python";
109107
public static final String EXTENSION = ".py";
110108

@@ -141,7 +139,7 @@ protected boolean patchContext(PythonContext context, Env newEnv) {
141139
@Override
142140
protected PythonContext createContext(Env env) {
143141
ensureHomeInOptions(env);
144-
Python3Core newCore = new Python3Core(new PythonParserImpl());
142+
Python3Core newCore = new Python3Core(new PythonParserImpl(), env);
145143
return new PythonContext(this, env, newCore);
146144
}
147145

@@ -491,7 +489,7 @@ protected boolean isThreadAccessAllowed(Thread thread, boolean singleThreaded) {
491489
if (singleThreaded) {
492490
return super.isThreadAccessAllowed(thread, singleThreaded);
493491
}
494-
return WITH_THREADS;
492+
return PythonOptions.isWithThread();
495493
}
496494

497495
@Override

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/Python3Core.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ private static final String[] initializeCoreFiles() {
227227

228228
private final PythonBuiltins[] builtins;
229229

230-
private static final PythonBuiltins[] initializeBuiltins() {
230+
private static final PythonBuiltins[] initializeBuiltins(Env env) {
231231
List<PythonBuiltins> builtins = new ArrayList<>(Arrays.asList(
232232
new BuiltinConstructors(),
233233
new BuiltinFunctions(),
@@ -330,7 +330,7 @@ private static final PythonBuiltins[] initializeBuiltins() {
330330
}
331331
}
332332
// threads
333-
if (PythonLanguage.WITH_THREADS) {
333+
if (env.getOptions().get(PythonOptions.WithThread)) {
334334
builtins.addAll(new ArrayList<>(Arrays.asList(
335335
new ThreadModuleBuiltins(),
336336
new ThreadBuiltins(),
@@ -361,9 +361,9 @@ private static final PythonBuiltins[] initializeBuiltins() {
361361

362362
private final PythonObjectFactory factory = PythonObjectFactory.create();
363363

364-
public Python3Core(PythonParser parser) {
364+
public Python3Core(PythonParser parser, Env env) {
365365
this.parser = parser;
366-
this.builtins = initializeBuiltins();
366+
this.builtins = initializeBuiltins(env);
367367
this.coreFiles = initializeCoreFiles();
368368
}
369369

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,14 @@
4444
import java.util.List;
4545
import java.util.Map;
4646

47-
import com.oracle.graal.python.PythonLanguage;
4847
import com.oracle.graal.python.builtins.Builtin;
4948
import com.oracle.graal.python.builtins.CoreFunctions;
5049
import com.oracle.graal.python.builtins.PythonBuiltins;
5150
import com.oracle.graal.python.builtins.objects.dict.PDict;
5251
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
5352
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
53+
import com.oracle.graal.python.runtime.PythonCore;
54+
import com.oracle.graal.python.runtime.PythonOptions;
5455
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
5556
import com.oracle.truffle.api.dsl.NodeFactory;
5657
import com.oracle.truffle.api.dsl.Specialization;
@@ -62,8 +63,11 @@
6263
@CoreFunctions(defineModule = "_sysconfig")
6364
public class SysConfigModuleBuiltins extends PythonBuiltins {
6465
private final static Map<Object, Object> STATIC_CONFIG_OPTIONS = new HashMap<>();
65-
static {
66-
STATIC_CONFIG_OPTIONS.put("WITH_THREAD", PythonLanguage.WITH_THREADS ? 1 : 0);
66+
67+
@Override
68+
public void initialize(PythonCore core) {
69+
super.initialize(core);
70+
STATIC_CONFIG_OPTIONS.put("WITH_THREAD", PythonOptions.isWithThread() ? 1 : 0);
6771
}
6872

6973
@Override

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ private PythonOptions() {
8282
@Option(category = OptionCategory.EXPERT, help = "This option is set by the Python launcher to tell the language it can print exceptions directly") //
8383
public static final OptionKey<Boolean> AlwaysRunExcepthook = new OptionKey<>(false);
8484

85+
@Option(category = OptionCategory.EXPERT, help = "This option control builtin _thread module support") //
86+
public static final OptionKey<Boolean> WithThread = new OptionKey<>(false);
87+
8588
@Option(category = OptionCategory.USER, help = "This option makes reading from files return opaque objects. Imports can work with such data, " +
8689
"but all other access to the contents of the file is disabled, so the files are kept secret.") //
8790
public static final OptionKey<Boolean> OpaqueFilesystem = new OptionKey<>(false);
@@ -156,4 +159,8 @@ public static boolean useLazyString() {
156159
public static int getMinLazyStringLength() {
157160
return getOption(PythonLanguage.getContextRef().get(), MinLazyStringLength);
158161
}
162+
163+
public static boolean isWithThread() {
164+
return getOption(PythonLanguage.getContextRef().get(), WithThread);
165+
}
159166
}

0 commit comments

Comments
 (0)