Skip to content

Commit d05c426

Browse files
committed
allow setting the sys.executable via an expert option
1 parent 014eb9a commit d05c426

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,11 @@ public void initialize(PythonCore core) {
112112
builtinConstants.put("byteorder", ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDIAN ? "little" : "big");
113113
builtinConstants.put("copyright", LICENSE);
114114
builtinConstants.put("dont_write_bytecode", true);
115-
if (TruffleOptions.AOT || !core.getContext().isExecutableAccessAllowed()) {
115+
116+
String executable = PythonOptions.getOption(core.getContext(), PythonOptions.ExecutablePath);
117+
if (!executable.isEmpty() && core.getContext().isExecutableAccessAllowed()) {
118+
builtinConstants.put("executable", executable);
119+
} else if (TruffleOptions.AOT || !core.getContext().isExecutableAccessAllowed()) {
116120
// cannot set the path at this time since the binary is not yet known; will be patched
117121
// in the context
118122
builtinConstants.put("executable", PNone.NONE);
@@ -140,6 +144,7 @@ public void initialize(PythonCore core) {
140144
builtinConstants.put("executable", sb.toString());
141145
builtinConstants.put("executable_list", core.factory().createList(exec_list.toArray()));
142146
}
147+
143148
builtinConstants.put("modules", core.factory().createDict());
144149
builtinConstants.put("path", core.factory().createList());
145150
builtinConstants.put("builtin_module_names", core.factory().createTuple(core.builtinModuleNames()));

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import org.graalvm.options.OptionValues;
4242

4343
import com.oracle.graal.python.PythonLanguage;
44+
import com.oracle.graal.python.builtins.objects.PNone;
4445
import com.oracle.graal.python.builtins.objects.bytes.OpaqueBytes;
4546
import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.PThreadState;
4647
import com.oracle.graal.python.builtins.objects.common.HashingStorage;
@@ -237,7 +238,9 @@ public void patch(Env newEnv) {
237238
private void setupRuntimeInformation() {
238239
PythonModule sysModule = core.initializeSysModule();
239240
if (ImageInfo.inImageRuntimeCode() && isExecutableAccessAllowed()) {
240-
sysModule.setAttribute("executable", ProcessProperties.getExecutableName());
241+
if (sysModule.getAttribute("executable") == PNone.NONE) {
242+
sysModule.setAttribute("executable", ProcessProperties.getExecutableName());
243+
}
241244
}
242245
sysModules = (PDict) sysModule.getAttribute("modules");
243246
builtinsModule = (PythonModule) sysModules.getItem("builtins");

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ private PythonOptions() {
128128
@Option(category = OptionCategory.EXPERT, help = "Set by the launcher to the terminal height.") //
129129
public static final OptionKey<Integer> TerminalHeight = new OptionKey<>(25);
130130

131+
@Option(category = OptionCategory.EXPERT, help = "The sys.executable path") //
132+
public static final OptionKey<String> ExecutablePath = new OptionKey<>("");
133+
131134
public static OptionDescriptors createDescriptors() {
132135
return new PythonOptionsOptionDescriptors();
133136
}

0 commit comments

Comments
 (0)