Skip to content

Commit 893b9fd

Browse files
committed
Update CHANGELOG, produce a warning when using native backend without launcher, handle options without the "python" prefix, untag test_repl.test_close_stdin, handle truffle cwd
1 parent 3eaa2cd commit 893b9fd

File tree

6 files changed

+47
-29
lines changed

6 files changed

+47
-29
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ language runtime. The main focus is on user-observable behavior of the engine.
66
## Version 21.1.0
77

88
* Added subclassing of Java classes in JVM mode
9+
* Use native posix functions in the GraalPython Launcher
910

1011
## Version 21.0.0
1112

graalpython/com.oracle.graal.python.shell/src/com/oracle/graal/python/shell/GraalPythonMain.java

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ protected List<String> preprocessArguments(List<String> givenArgs, Map<String, S
116116
List<String> arguments = new ArrayList<>(inputArgs);
117117
List<String> subprocessArgs = new ArrayList<>();
118118
programArgs = new ArrayList<>();
119+
boolean posixBackendSpecified = false;
119120
for (int i = 0; i < arguments.size(); i++) {
120121
String arg = arguments.get(i);
121122
switch (arg) {
@@ -285,12 +286,15 @@ protected List<String> preprocessArguments(List<String> givenArgs, Map<String, S
285286
}
286287
} else {
287288
if (arg.startsWith("--llvm.") ||
288-
arg.startsWith("--python.CoreHome") ||
289-
arg.startsWith("--python.StdLibHome") ||
290-
arg.startsWith("--python.CAPI") ||
291-
arg.startsWith("--PosixModuleBackend")) {
289+
matchesPythonOption(arg, "CoreHome") ||
290+
matchesPythonOption(arg, "StdLibHome") ||
291+
matchesPythonOption(arg, "CAPI") ||
292+
matchesPythonOption(arg, "PosixModuleBackend")) {
292293
addRelaunchArg(arg);
293294
}
295+
if (matchesPythonOption(arg, "PosixModuleBackend")) {
296+
posixBackendSpecified = true;
297+
}
294298
// possibly a polyglot argument
295299
unrecognized.add(arg);
296300
}
@@ -313,7 +317,9 @@ protected List<String> preprocessArguments(List<String> givenArgs, Map<String, S
313317
if (!subprocessArgs.isEmpty()) {
314318
subExec(inputArgs, subprocessArgs);
315319
}
316-
320+
if (!posixBackendSpecified) {
321+
polyglotOptions.put("python.PosixModuleBackend", "native");
322+
}
317323
return unrecognized;
318324
}
319325

@@ -524,7 +530,7 @@ protected void launch(Builder contextBuilder) {
524530
if (warnOptions == null || warnOptions.isEmpty()) {
525531
warnOptions = "";
526532
}
527-
String executable = getContextOptionIfSetViaCommandLine("python.Executable");
533+
String executable = getContextOptionIfSetViaCommandLine("Executable");
528534
if (executable != null) {
529535
contextBuilder.option("python.ExecutableList", executable);
530536
} else {
@@ -562,13 +568,7 @@ protected void launch(Builder contextBuilder) {
562568
contextBuilder.option("python.TerminalHeight", Integer.toString(consoleHandler.getTerminalHeight()));
563569

564570
contextBuilder.option("python.CheckHashPycsMode", checkHashPycsMode);
565-
566-
if (getContextOptionIfSetViaCommandLine("python.PosixModuleBackend") == null && getContextOptionIfSetViaCommandLine("PosixModuleBackend") == null) {
567-
// TODO find a proper way to force emulated backend in llvm.managed mode
568-
if (getClass() == GraalPythonMain.class) {
569-
contextBuilder.option("python.PosixModuleBackend", "native");
570-
}
571-
}
571+
contextBuilder.option("python.RunViaLauncher", "true");
572572

573573
if (multiContext) {
574574
contextBuilder.engine(Engine.newBuilder().allowExperimentalOptions(true).options(enginePolyglotOptions).build());
@@ -617,12 +617,17 @@ protected void launch(Builder contextBuilder) {
617617
System.exit(rc);
618618
}
619619

620+
private static boolean matchesPythonOption(String arg, String key) {
621+
assert !key.startsWith("python.");
622+
return arg.startsWith("--python." + key) || arg.startsWith("--" + key);
623+
}
624+
620625
private String getContextOptionIfSetViaCommandLine(String key) {
621-
if (System.getProperty("polyglot." + key) != null) {
622-
return System.getProperty("polyglot." + key);
626+
if (System.getProperty("polyglot.python." + key) != null) {
627+
return System.getProperty("polyglot.python." + key);
623628
}
624629
for (String f : givenArguments) {
625-
if (f.startsWith("--" + key)) {
630+
if (matchesPythonOption(f, key)) {
626631
String[] splits = f.split("=", 2);
627632
if (splits.length > 1) {
628633
return splits[1];
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
*graalpython.lib-python.3.test.test_repl.TestInteractiveInterpreter.test_close_stdin
21
*graalpython.lib-python.3.test.test_repl.TestInteractiveInterpreter.test_multiline_string_parsing
32
*graalpython.lib-python.3.test.test_repl.TestInteractiveInterpreter.test_no_memory

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

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -305,16 +305,29 @@ public NFIPosixSupport(PythonContext context, String nfiBackend) {
305305
this.cachedFunctions = new AtomicReferenceArray<>(PosixNativeFunction.values().length);
306306
}
307307

308+
@Override
309+
public void setEnv(Env env) {
310+
// Java NIO (and TruffleFile) do not expect/support changing native working directory since
311+
// it is inherently thread-unsafe operation. It is not defined how NIO behaves when native
312+
// cwd changes, thus we need to prevent TruffleFile from resolving relative paths using
313+
// NIO by setting Truffle cwd to a know value. This cannot be done lazily in chdir() because
314+
// native cwd is global, but Truffle cwd is per context.
315+
// TruffleFile will be unaware of the real working directory and keep resolving against the
316+
// original working directory. This should not matter since we do not use TruffleFile for
317+
// ordinary I/O when using NFI backend.
318+
try {
319+
TruffleFile truffleFile = context.getEnv().getInternalTruffleFile(".").getAbsoluteFile();
320+
context.getEnv().setCurrentWorkingDirectory(truffleFile);
321+
} catch (Exception e) {
322+
LOGGER.log(Level.WARNING, "Unable to change Truffle working directory", e);
323+
}
324+
}
325+
308326
@ExportMessage
309327
public String getBackend() {
310328
return nfiBackend;
311329
}
312330

313-
@Override
314-
public void setEnv(Env env) {
315-
// TODO check that env does not attempt to redirect std streams
316-
}
317-
318331
@ExportMessage
319332
public String strerror(int errorCode,
320333
@Shared("invoke") @Cached InvokeNativeFunction invokeNode) {
@@ -645,13 +658,6 @@ public void chdir(Object path,
645658
if (result != 0) {
646659
throw newPosixException(invokeNode, getErrno(invokeNode));
647660
}
648-
// TODO we don;t need to do this more than once
649-
try {
650-
TruffleFile truffleFile = context.getEnv().getInternalTruffleFile(".").getAbsoluteFile();
651-
context.getEnv().setCurrentWorkingDirectory(truffleFile);
652-
} catch (Exception e) {
653-
LOGGER.log(Level.WARNING, "Unable to change Truffle working directory", e);
654-
}
655661
}
656662

657663
@ExportMessage

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,10 @@ private void initalizePosixSupport() {
586586
resources = new EmulatedPosixSupport(this);
587587
resources.setEnv(env);
588588
} else {
589+
if (!getOption(PythonOptions.RunViaLauncher)) {
590+
writeWarning("Native Posix backend is not fully supported when embedding. For example, standard I/O always uses file " +
591+
"descriptors 0, 1 and 2 regardless of stream redirection specified in Truffle environment");
592+
}
589593
result = new NFIPosixSupport(this, option);
590594
resources = new EmulatedPosixSupport(this);
591595
resources.setEnv(env);

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
@@ -241,6 +241,9 @@ private PythonOptions() {
241241
@Option(category = OptionCategory.EXPERT, help = "Max native memory heap size (default: 2 GB).") //
242242
public static final OptionKey<Long> MaxNativeMemory = new OptionKey<>(1L << 31);
243243

244+
@Option(category = OptionCategory.EXPERT, help = "Set by the launcher to true (false means that GraalPython is being embedded in an application).") //
245+
public static final OptionKey<Boolean> RunViaLauncher = new OptionKey<>(false);
246+
244247
public static final OptionDescriptors DESCRIPTORS = new PythonOptionsOptionDescriptors();
245248

246249
@CompilationFinal(dimensions = 1) private static final OptionKey<?>[] ENGINE_OPTION_KEYS;

0 commit comments

Comments
 (0)