50
50
import static com .oracle .graal .python .runtime .exception .PythonErrorType .TypeError ;
51
51
52
52
import java .io .IOException ;
53
+ import java .io .InputStreamReader ;
53
54
import java .io .PrintWriter ;
54
55
import java .nio .charset .StandardCharsets ;
55
56
import java .util .List ;
@@ -246,7 +247,7 @@ PNone run() {
246
247
PythonContext context = getContext ();
247
248
String inputFilePath = context .getOption (PythonOptions .InputFilePath );
248
249
PythonModule sysModule = context .getSysModule ();
249
- boolean needsMainImporter = getImporter (sysModule , inputFilePath );
250
+ boolean needsMainImporter = ! inputFilePath . isEmpty () && getImporter (sysModule , inputFilePath );
250
251
if (needsMainImporter ) {
251
252
Object sysPath = sysModule .getAttribute ("path" );
252
253
PyObjectCallMethodObjArgs .getUncached ().execute (null , sysPath , "insert" , 0 , inputFilePath );
@@ -264,11 +265,19 @@ PNone run() {
264
265
return PNone .NONE ;
265
266
}
266
267
267
- // Equivalent of CPython's pymain_run_file
268
+ // Equivalent of CPython's pymain_run_file and pymain_run_stdin
268
269
private void runFile (PythonContext context , String inputFilePath ) {
269
270
Source source ;
270
271
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 ();
272
281
// TODO we should handle non-IO errors better
273
282
} catch (IOException e ) {
274
283
ErrorAndMessagePair error = OSErrorEnum .fromException (e );
0 commit comments