Skip to content

Commit c3d3b06

Browse files
committed
[GH-29] Various fixes to get further running prompt_toolkit
PullRequest: graalpython/338
2 parents b76b045 + 8aff8cf commit c3d3b06

File tree

11 files changed

+734
-47
lines changed

11 files changed

+734
-47
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2018, Oracle and/or its affiliates.
2+
* Copyright (c) 2017, 2019, Oracle and/or its affiliates.
33
* Copyright (c) 2013, Regents of the University of California
44
*
55
* All rights reserved.
@@ -247,6 +247,7 @@ protected void launch(Builder contextBuilder) {
247247

248248
ConsoleHandler consoleHandler = createConsoleHandler(System.in, System.out);
249249
contextBuilder.arguments(getLanguageId(), programArgs.toArray(new String[0])).in(consoleHandler.createInputStream());
250+
contextBuilder.option("python.TerminalIsInteractive", Boolean.toString(stdinIsInteractive));
250251
contextBuilder.option("python.TerminalWidth", Integer.toString(consoleHandler.getTerminalWidth()));
251252
contextBuilder.option("python.TerminalHeight", Integer.toString(consoleHandler.getTerminalHeight()));
252253

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/grammar/TestParserTranslator.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -71,6 +71,7 @@
7171
import com.oracle.graal.python.nodes.expression.BinaryComparisonNode;
7272
import com.oracle.graal.python.nodes.expression.CastToBooleanNode;
7373
import com.oracle.graal.python.nodes.expression.CastToBooleanNode.NotNode;
74+
import com.oracle.graal.python.nodes.expression.ContainsNode;
7475
import com.oracle.graal.python.nodes.expression.ExpressionNode;
7576
import com.oracle.graal.python.nodes.expression.IsNode;
7677
import com.oracle.graal.python.nodes.expression.OrNode;
@@ -358,9 +359,9 @@ public void parseComparisons() {
358359
parseAs("x <= y", BinaryComparisonNode.class);
359360
parseAs("x <> y", BinaryComparisonNode.class);
360361
parseAs("x != y", BinaryComparisonNode.class);
361-
parseAs("x in y", BinaryComparisonNode.class);
362+
parseAs("x in y", ContainsNode.class);
362363
CastToBooleanNode notNode = parseAs("x not in y", CastToBooleanNode.NotNode.class);
363-
getChild(notNode, 0, BinaryComparisonNode.class);
364+
getChild(notNode, 0, ContainsNode.class);
364365
parseAs("x is y", IsNode.class);
365366
notNode = parseAs("x is not y", CastToBooleanNode.NotNode.class);
366367
getChild(notNode, 0, IsNode.class);

graalpython/com.oracle.graal.python.test/src/tests/test_iterator.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
1+
# Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
22
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
33
#
44
# The Universal Permissive License (UPL), Version 1.0
@@ -115,3 +115,10 @@ def test_iter_try_except():
115115
break
116116

117117
assert exit_via_break
118+
119+
120+
def test_iterator_in():
121+
assert 1 not in (i for i in range(1))
122+
assert 1 in (i for i in range(2))
123+
assert 1 in iter(range(2))
124+
assert 1 not in iter(range(1))

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/Python3Core.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2018, Oracle and/or its affiliates.
2+
* Copyright (c) 2017, 2019, Oracle and/or its affiliates.
33
* Copyright (c) 2013, Regents of the University of California
44
*
55
* All rights reserved.
@@ -221,6 +221,7 @@ private static final String[] initializeCoreFiles() {
221221
"_thread",
222222
"ctypes",
223223
"zlib",
224+
"termios",
224225
"zipimport"));
225226

226227
return coreFiles.toArray(new String[coreFiles.size()]);

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

Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, Oracle and/or its affiliates.
2+
* Copyright (c) 2018, 2019, Oracle and/or its affiliates.
33
* Copyright (c) 2014, Regents of the University of California
44
*
55
* All rights reserved.
@@ -184,6 +184,10 @@ public class PosixModuleBuiltins extends PythonBuiltins {
184184
new PosixFilePermission[]{PosixFilePermission.OWNER_READ, PosixFilePermission.OWNER_WRITE, PosixFilePermission.OWNER_EXECUTE},
185185
};
186186

187+
private static boolean terminalIsInteractive(PythonContext context) {
188+
return PythonOptions.getFlag(context, PythonOptions.TerminalIsInteractive);
189+
}
190+
187191
@Override
188192
protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFactories() {
189193
return PosixModuleBuiltinsFactory.getFactories();
@@ -1032,21 +1036,11 @@ boolean isATTY(int fd) {
10321036
case 0:
10331037
case 1:
10341038
case 2:
1035-
// TODO: XXX: actually check
1036-
// TODO: We can only return true here once we
1037-
// have at least basic subprocess module support,
1038-
// because otherwise we break the REPL help
1039-
// return consoleCheck();
1040-
return false;
1039+
return terminalIsInteractive(getContext());
10411040
default:
10421041
return false;
10431042
}
10441043
}
1045-
1046-
@TruffleBoundary(allowInlining = true)
1047-
private static boolean consoleCheck() {
1048-
return System.console() != null;
1049-
}
10501044
}
10511045

10521046
@Builtin(name = "_exit", fixedNumOfPositionalArgs = 1)
@@ -1276,20 +1270,19 @@ static class PipePump extends Thread {
12761270
private final OutputStream out;
12771271
private final byte[] buffer;
12781272
private volatile boolean finish;
1279-
private volatile boolean flush;
12801273

1281-
public PipePump(InputStream in, OutputStream out) {
1274+
public PipePump(String name, InputStream in, OutputStream out) {
1275+
this.setName(name);
12821276
this.in = in;
12831277
this.out = out;
12841278
this.buffer = new byte[MAX_READ];
12851279
this.finish = false;
1286-
this.flush = false;
12871280
}
12881281

12891282
@Override
12901283
public void run() {
12911284
try {
1292-
while (!finish || (flush && in.available() > 0)) {
1285+
while (!finish || in.available() > 0) {
12931286
if (Thread.interrupted()) {
12941287
finish = true;
12951288
}
@@ -1303,14 +1296,10 @@ public void run() {
13031296
}
13041297
}
13051298

1306-
public void finish(boolean force_flush) {
1299+
public void finish() {
13071300
finish = true;
1308-
flush = force_flush;
1309-
if (flush) {
1310-
// If we need to flush, make ourselves max priority to pump data out as quickly
1311-
// as possible
1312-
setPriority(Thread.MAX_PRIORITY);
1313-
}
1301+
// Make ourselves max priority to flush data out as quickly as possible
1302+
setPriority(Thread.MAX_PRIORITY);
13141303
Thread.yield();
13151304
}
13161305
}
@@ -1326,20 +1315,28 @@ int system(String cmd) {
13261315
Env env = context.getEnv();
13271316
try {
13281317
ProcessBuilder pb = new ProcessBuilder(command);
1329-
pb.redirectInput(Redirect.PIPE);
1330-
pb.redirectOutput(Redirect.PIPE);
1331-
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+
}
13321327
Process proc = pb.start();
1333-
PipePump stdin = new PipePump(env.in(), proc.getOutputStream());
1334-
PipePump stdout = new PipePump(proc.getInputStream(), env.out());
1335-
PipePump stderr = new PipePump(proc.getErrorStream(), env.err());
1336-
stdin.start();
1337-
stdout.start();
1338-
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+
}
13391335
int exitStatus = proc.waitFor();
1340-
stdin.finish(false);
1341-
stdout.finish(true);
1342-
stderr.finish(true);
1336+
if (stdsArePipes) {
1337+
stdout.finish();
1338+
stderr.finish();
1339+
}
13431340
return exitStatus;
13441341
} catch (IOException | InterruptedException e) {
13451342
return -1;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/NodeFactory.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2018, Oracle and/or its affiliates.
2+
* Copyright (c) 2017, 2019, Oracle and/or its affiliates.
33
* Copyright (c) 2013, Regents of the University of California
44
*
55
* All rights reserved.
@@ -59,6 +59,7 @@
5959
import com.oracle.graal.python.nodes.expression.BinaryArithmetic;
6060
import com.oracle.graal.python.nodes.expression.BinaryComparisonNode;
6161
import com.oracle.graal.python.nodes.expression.CastToBooleanNode;
62+
import com.oracle.graal.python.nodes.expression.ContainsNode;
6263
import com.oracle.graal.python.nodes.expression.ExpressionNode;
6364
import com.oracle.graal.python.nodes.expression.InplaceArithmetic;
6465
import com.oracle.graal.python.nodes.expression.IsNode;
@@ -389,9 +390,9 @@ public ExpressionNode createComparisonOperation(String operator, ExpressionNode
389390
case "!=":
390391
return BinaryComparisonNode.create(SpecialMethodNames.__NE__, SpecialMethodNames.__NE__, operator, left, right);
391392
case "in":
392-
return BinaryComparisonNode.create(SpecialMethodNames.__CONTAINS__, null, operator, right, left);
393+
return ContainsNode.create(right, left);
393394
case "notin":
394-
return CastToBooleanNode.createIfFalseNode(BinaryComparisonNode.create(SpecialMethodNames.__CONTAINS__, null, operator, right, left));
395+
return CastToBooleanNode.createIfFalseNode(ContainsNode.create(right, left));
395396
case "is":
396397
return IsNode.create(left, right);
397398
case "isnot":

0 commit comments

Comments
 (0)