Skip to content

Commit 4f983af

Browse files
committed
Fix executing from stdin
1 parent 10b3cde commit 4f983af

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,7 @@ protected void launch(Builder contextBuilder) {
621621
}
622622
consoleHandler.setContext(context);
623623

624-
if (commandString != null || inputFile != null) {
624+
if (commandString != null || inputFile != null || !stdinIsInteractive) {
625625
try {
626626
evalNonInteractive(context, consoleHandler);
627627
rc = 0;
@@ -722,8 +722,7 @@ private void evalNonInteractive(Context context, ConsoleHandler consoleHandler)
722722
if (commandString != null) {
723723
src = Source.newBuilder(getLanguageId(), commandString, "<string>").build();
724724
} else {
725-
assert inputFile != null;
726-
// the path is passed through a context option
725+
// the path is passed through a context option, may be empty when running from stdin
727726
src = Source.newBuilder(getLanguageId(), "__graalpython__.run_path()", "<internal>").internal(true).build();
728727
}
729728
context.eval(src);

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import static com.oracle.graal.python.runtime.exception.PythonErrorType.TypeError;
5151

5252
import java.io.IOException;
53+
import java.io.InputStreamReader;
5354
import java.io.PrintWriter;
5455
import java.nio.charset.StandardCharsets;
5556
import java.util.List;
@@ -246,7 +247,7 @@ PNone run() {
246247
PythonContext context = getContext();
247248
String inputFilePath = context.getOption(PythonOptions.InputFilePath);
248249
PythonModule sysModule = context.getSysModule();
249-
boolean needsMainImporter = getImporter(sysModule, inputFilePath);
250+
boolean needsMainImporter = !inputFilePath.isEmpty() && getImporter(sysModule, inputFilePath);
250251
if (needsMainImporter) {
251252
Object sysPath = sysModule.getAttribute("path");
252253
PyObjectCallMethodObjArgs.getUncached().execute(null, sysPath, "insert", 0, inputFilePath);
@@ -264,11 +265,19 @@ PNone run() {
264265
return PNone.NONE;
265266
}
266267

267-
// Equivalent of CPython's pymain_run_file
268+
// Equivalent of CPython's pymain_run_file and pymain_run_stdin
268269
private void runFile(PythonContext context, String inputFilePath) {
269270
Source source;
270271
try {
271-
source = Source.newBuilder(PythonLanguage.ID, context.getPublicTruffleFileRelaxed(inputFilePath)).mimeType(PythonLanguage.MIME_TYPE).build();
272+
Source.SourceBuilder builder;
273+
if (inputFilePath.isEmpty()) {
274+
// Reading from stdin
275+
builder = Source.newBuilder(PythonLanguage.ID, new InputStreamReader(context.getStandardIn()), "<stdin>");
276+
} else {
277+
TruffleFile file = context.getPublicTruffleFileRelaxed(inputFilePath);
278+
builder = Source.newBuilder(PythonLanguage.ID, file);
279+
}
280+
source = builder.mimeType(PythonLanguage.MIME_TYPE).build();
272281
// TODO we should handle non-IO errors better
273282
} catch (IOException e) {
274283
ErrorAndMessagePair error = OSErrorEnum.fromException(e);

0 commit comments

Comments
 (0)