Skip to content

Commit dec5fb5

Browse files
committed
if we're connected to an interactive terminal, cmds through os.system will just inherit the terminal I/O
1 parent 76d0241 commit dec5fb5

File tree

1 file changed

+26
-23
lines changed

1 file changed

+26
-23
lines changed

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

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1270,20 +1270,19 @@ static class PipePump extends Thread {
12701270
private final OutputStream out;
12711271
private final byte[] buffer;
12721272
private volatile boolean finish;
1273-
private volatile boolean flush;
12741273

1275-
public PipePump(InputStream in, OutputStream out) {
1274+
public PipePump(String name, InputStream in, OutputStream out) {
1275+
this.setName(name);
12761276
this.in = in;
12771277
this.out = out;
12781278
this.buffer = new byte[MAX_READ];
12791279
this.finish = false;
1280-
this.flush = false;
12811280
}
12821281

12831282
@Override
12841283
public void run() {
12851284
try {
1286-
while (!finish || (flush && in.available() > 0)) {
1285+
while (!finish || in.available() > 0) {
12871286
if (Thread.interrupted()) {
12881287
finish = true;
12891288
}
@@ -1297,14 +1296,10 @@ public void run() {
12971296
}
12981297
}
12991298

1300-
public void finish(boolean force_flush) {
1299+
public void finish() {
13011300
finish = true;
1302-
flush = force_flush;
1303-
if (flush) {
1304-
// If we need to flush, make ourselves max priority to pump data out as quickly
1305-
// as possible
1306-
setPriority(Thread.MAX_PRIORITY);
1307-
}
1301+
// Make ourselves max priority to flush data out as quickly as possible
1302+
setPriority(Thread.MAX_PRIORITY);
13081303
Thread.yield();
13091304
}
13101305
}
@@ -1320,20 +1315,28 @@ int system(String cmd) {
13201315
Env env = context.getEnv();
13211316
try {
13221317
ProcessBuilder pb = new ProcessBuilder(command);
1323-
pb.redirectInput(Redirect.PIPE);
1324-
pb.redirectOutput(Redirect.PIPE);
1325-
pb.redirectError(Redirect.PIPE);
1318+
PipePump stdout = null, stderr = null;
1319+
boolean stdsArePipes = !terminalIsInteractive(context);
1320+
if (stdsArePipes) {
1321+
pb.redirectInput(Redirect.PIPE);
1322+
pb.redirectOutput(Redirect.PIPE);
1323+
pb.redirectError(Redirect.PIPE);
1324+
} else {
1325+
pb.inheritIO();
1326+
}
13261327
Process proc = pb.start();
1327-
PipePump stdin = new PipePump(env.in(), proc.getOutputStream());
1328-
PipePump stdout = new PipePump(proc.getInputStream(), env.out());
1329-
PipePump stderr = new PipePump(proc.getErrorStream(), env.err());
1330-
stdin.start();
1331-
stdout.start();
1332-
stderr.start();
1328+
if (stdsArePipes) {
1329+
proc.getOutputStream().close(); // stdin will be closed
1330+
stdout = new PipePump(cmd + " [stdout]", proc.getInputStream(), env.out());
1331+
stderr = new PipePump(cmd + " [stderr]", proc.getErrorStream(), env.err());
1332+
stdout.start();
1333+
stderr.start();
1334+
}
13331335
int exitStatus = proc.waitFor();
1334-
stdin.finish(false);
1335-
stdout.finish(true);
1336-
stderr.finish(true);
1336+
if (stdsArePipes) {
1337+
stdout.finish();
1338+
stderr.finish();
1339+
}
13371340
return exitStatus;
13381341
} catch (IOException | InterruptedException e) {
13391342
return -1;

0 commit comments

Comments
 (0)