Skip to content

Commit 60257b9

Browse files
committed
[hotfix] Incomplete source exception flag not set in REPL
PullRequest: graalpython/410
2 parents fca7296 + 3b92e43 commit 60257b9

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/interop/JavaInteropTest.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,10 @@
4141
package com.oracle.graal.python.test.interop;
4242

4343
import static org.junit.Assert.assertEquals;
44+
import static org.junit.Assert.assertFalse;
4445
import static org.junit.Assert.assertNotNull;
4546
import static org.junit.Assert.assertTrue;
47+
import static org.junit.Assert.fail;
4648

4749
import java.io.ByteArrayOutputStream;
4850
import java.io.IOException;
@@ -52,6 +54,7 @@
5254

5355
import org.graalvm.polyglot.Context;
5456
import org.graalvm.polyglot.Engine;
57+
import org.graalvm.polyglot.PolyglotException;
5558
import org.graalvm.polyglot.Context.Builder;
5659
import org.graalvm.polyglot.Source;
5760
import org.graalvm.polyglot.Value;
@@ -76,6 +79,7 @@
7679
@RunWith(Enclosed.class)
7780
public class JavaInteropTest {
7881
public static class GeneralInterop extends PythonTests {
82+
private static final String INCOMPLETE_SOURCE = "class A:";
7983
private ByteArrayOutputStream out;
8084
private Context context;
8185
private ByteArrayOutputStream err;
@@ -107,6 +111,55 @@ public void evalFailsOnError() {
107111
assertTrue(didFail);
108112
}
109113

114+
@Test
115+
public void evalNonInteractiveThrowsSyntaxError() throws IOException {
116+
try (Context c = Context.newBuilder().allowAllAccess(true).option("python.TerminalIsInteractive", "false").build()) {
117+
c.eval(Source.newBuilder("python", INCOMPLETE_SOURCE, "eval").interactive(false).build());
118+
} catch (PolyglotException t) {
119+
assertTrue(t.isSyntaxError());
120+
assertFalse(t.isIncompleteSource());
121+
return;
122+
}
123+
fail();
124+
}
125+
126+
@Test
127+
public void evalNonInteractiveInInteractiveTerminalThrowsSyntaxError() throws IOException {
128+
try (Context c = Context.newBuilder().allowAllAccess(true).option("python.TerminalIsInteractive", "true").build()) {
129+
c.eval(Source.newBuilder("python", INCOMPLETE_SOURCE, "eval").interactive(false).build());
130+
} catch (PolyglotException t) {
131+
assertTrue(t.isSyntaxError());
132+
assertFalse(t.isIncompleteSource());
133+
return;
134+
}
135+
fail();
136+
}
137+
138+
@Test
139+
public void evalInteractiveInNonInteractiveTerminalThrowsSyntaxError() throws IOException {
140+
try (Context c = Context.newBuilder().allowAllAccess(true).option("python.TerminalIsInteractive", "false").build()) {
141+
c.eval(Source.newBuilder("python", INCOMPLETE_SOURCE, "eval").interactive(true).build());
142+
} catch (PolyglotException t) {
143+
assertTrue(t.isSyntaxError());
144+
assertTrue(t.isIncompleteSource());
145+
return;
146+
}
147+
fail();
148+
}
149+
150+
@Test
151+
public void evalInteractiveInInteractiveTerminalThrowsSyntaxError() throws IOException {
152+
try (Context c = Context.newBuilder().allowAllAccess(true).option("python.TerminalIsInteractive", "true").build()) {
153+
c.eval(Source.newBuilder("python", INCOMPLETE_SOURCE, "eval").interactive(true).build());
154+
} catch (PolyglotException t) {
155+
assertTrue(t.isSyntaxError());
156+
assertTrue(t.isIncompleteSource());
157+
return;
158+
}
159+
160+
fail();
161+
}
162+
110163
@Test
111164
public void truffleMethodExport() {
112165
String source = "import polyglot\n" +

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/parser/PythonParserImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public Node parse(ParserMode mode, ParserErrorCallback errors, Source source, Fr
7171
throw new RuntimeException("unexpected mode: " + mode);
7272
}
7373
} catch (Exception e) {
74-
if (mode == ParserMode.InteractiveStatement && e instanceof PIncompleteSourceException) {
74+
if ((mode == ParserMode.InteractiveStatement || mode == ParserMode.Statement) && e instanceof PIncompleteSourceException) {
7575
((PIncompleteSourceException) e).setSource(source);
7676
throw e;
7777
} else if (mode == ParserMode.InlineEvaluation) {

0 commit comments

Comments
 (0)