Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions core/src/main/java/org/python/util/Repl.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ void run() throws IOException {
out.print(prompt);
out.flush();
String line = in.readLine();
if (line == null) {
if (line == null || isEndOfInput(line)) {
// End of input: finish any statement in progress.
if (!buffer.isEmpty()) { push(""); }
out.println();
Expand All @@ -83,6 +83,20 @@ void run() throws IOException {
}
}

/**
* Whether a line begins with an end-of-input control character that
* nothing upstream has turned into a real end of file. On Windows,
* Ctrl-D arrives as the character U+0004, and Ctrl-Z (U+001A) may
* arrive literally when input is piped (e.g. through Gradle). Like
* CPython on Windows, we honour these only at the start of a line.
*
* @param line as read
* @return {@code true} if the line means end of input
*/
private static boolean isEndOfInput(String line) {
return !line.isEmpty() && (line.charAt(0) == '\u0004' || line.charAt(0) == '\u001a');
}

/**
* Add a line to the statement being entered and, if it is then
* complete, execute it (or report the error).
Expand Down Expand Up @@ -142,7 +156,7 @@ public static void main(String[] args) throws IOException {
try (ReplCompiler compiler = new ReplCompiler()) {
System.out.printf("Jython 3 (prototype) using CPython compiler %s%n",
compiler.executable);
System.out.println("Ctrl-D to exit.");
System.out.println("Ctrl-D (or Ctrl-Z, Enter) to exit.");
new Repl(in, System.out, System.err, compiler).run();
}
}
Expand Down
10 changes: 10 additions & 0 deletions core/src/test/java/org/python/util/ReplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,16 @@ void finishesAtEof() throws IOException {
assertTrue(s.out().startsWith(">>> ... ... "), s.out());
}

@Test
@DisplayName("treats Ctrl-D or Ctrl-Z at the start of a line as end of input")
void controlCharEndsInput() throws IOException {
for (String eof : new String[] {"\u0004", "\u001a"}) {
Session s = session("x = 6 * 7", eof + "junk", "x");
assertEquals(">>> >>> \n", s.out());
assertEquals("", s.err());
}
}

@Test
@DisplayName("reports a syntax error and carries on")
void reportsSyntaxError() throws IOException {
Expand Down
Loading