Skip to content

Commit 66f7ceb

Browse files
committed
Print SyntaxError details in interactive mode too
CPython does it.
1 parent 8a9c58c commit 66f7ceb

File tree

1 file changed

+9
-21
lines changed

1 file changed

+9
-21
lines changed

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

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2019, Oracle and/or its affiliates.
2+
* Copyright (c) 2017, 2020, Oracle and/or its affiliates.
33
* Copyright (c) 2013, Regents of the University of California
44
*
55
* All rights reserved.
@@ -25,10 +25,9 @@
2525
*/
2626
package com.oracle.graal.python.parser;
2727

28-
import com.oracle.graal.python.builtins.objects.PNone;
29-
import com.oracle.graal.python.builtins.objects.exception.PBaseException;
3028
import org.antlr.v4.runtime.CharStreams;
3129
import org.antlr.v4.runtime.CommonTokenStream;
30+
import org.antlr.v4.runtime.Token;
3231

3332
import com.oracle.graal.python.parser.antlr.DescriptiveBailErrorListener;
3433
import com.oracle.graal.python.parser.antlr.Python3Lexer;
@@ -40,14 +39,12 @@
4039
import com.oracle.graal.python.runtime.PythonParser;
4140
import com.oracle.graal.python.runtime.exception.PException;
4241
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
43-
import com.oracle.truffle.api.TruffleException;
4442
import com.oracle.truffle.api.TruffleLanguage.Env;
4543
import com.oracle.truffle.api.frame.Frame;
4644
import com.oracle.truffle.api.frame.FrameDescriptor;
4745
import com.oracle.truffle.api.nodes.Node;
4846
import com.oracle.truffle.api.source.Source;
4947
import com.oracle.truffle.api.source.SourceSection;
50-
import org.antlr.v4.runtime.Token;
5148

5249
public final class PythonParserImpl implements PythonParser {
5350

@@ -152,18 +149,18 @@ public Node parseN(ParserMode mode, ParserErrorCallback errors, Source source, F
152149
parser.reset();
153150
parserSSTResult = parser.eval_input().result;
154151
} catch (Exception e2) {
155-
throw handleParserError(errors, source, e, !(mode == ParserMode.InteractiveStatement || mode == ParserMode.Statement));
152+
throw handleParserError(errors, source, e);
156153
}
157154
} else {
158-
throw handleParserError(errors, source, e, !(mode == ParserMode.InteractiveStatement || mode == ParserMode.Statement));
155+
throw handleParserError(errors, source, e);
159156
}
160157
}
161158

162159
lastGlobalScope = sstFactory.getScopeEnvironment().getGlobalScope();
163160
try {
164161
return sstFactory.createParserResult(parserSSTResult, mode, currentFrame);
165162
} catch (Exception e) {
166-
throw handleParserError(errors, source, e, !(mode == ParserMode.InteractiveStatement || mode == ParserMode.Statement));
163+
throw handleParserError(errors, source, e);
167164
}
168165
}
169166

@@ -197,20 +194,11 @@ public String unescapeJavaString(String str) {
197194
return StringUtils.unescapeJavaString(str);
198195
}
199196

200-
private static PException handleParserError(ParserErrorCallback errors, Source source, Exception e, boolean showBadLine) {
201-
if (e instanceof TruffleException && ((TruffleException) e).isSyntaxError() && e instanceof PException) {
202-
if (!showBadLine) {
203-
PBaseException instance = ((PException) e).getExceptionObject();
204-
// In cpython shell the line with the error is not displayed, so we should do it in
205-
// the same way.
206-
// This rely on implementation in traceback.py file. See comment in
207-
// Python3Core.raiseInvalidSyntax method
208-
instance.setAttribute("text", PNone.NONE);
209-
}
210-
return (PException) e;
197+
private static PException handleParserError(ParserErrorCallback errors, Source source, Exception e) {
198+
if (e instanceof PException && ((PException) e).isSyntaxError()) {
199+
throw (PException) e;
211200
}
212-
213-
SourceSection section = showBadLine ? PythonErrorStrategy.getPosition(source, e) : source.createUnavailableSection();
201+
SourceSection section = PythonErrorStrategy.getPosition(source, e);
214202
// from parser we are getting RuntimeExceptions
215203
String message = e instanceof RuntimeException && e.getMessage() != null ? e.getMessage() : "invalid syntax";
216204
throw errors.raiseInvalidSyntax(source, section, message);

0 commit comments

Comments
 (0)