Skip to content

Commit 82977ce

Browse files
committed
[GR-18775] Error from sys.excepthook.
PullRequest: graalpython/684
2 parents a9a5123 + ff793c3 commit 82977ce

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
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;
2830
import org.antlr.v4.runtime.CharStreams;
2931
import org.antlr.v4.runtime.CommonTokenStream;
3032
import org.antlr.v4.runtime.ParserRuleContext;
@@ -39,6 +41,7 @@
3941
import com.oracle.graal.python.runtime.PythonParser;
4042
import com.oracle.graal.python.runtime.exception.PException;
4143
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
44+
import com.oracle.truffle.api.TruffleException;
4245
import com.oracle.truffle.api.TruffleLanguage.Env;
4346
import com.oracle.truffle.api.frame.Frame;
4447
import com.oracle.truffle.api.frame.FrameDescriptor;
@@ -164,6 +167,7 @@ public Node parseN(ParserMode mode, ParserErrorCallback errors, Source source, F
164167
default:
165168
throw new RuntimeException("unexpected mode: " + mode);
166169
}
170+
167171
} catch (Exception e) {
168172
if ((mode == ParserMode.InteractiveStatement || mode == ParserMode.Statement) && e instanceof PIncompleteSourceException) {
169173
((PIncompleteSourceException) e).setSource(source);
@@ -186,7 +190,6 @@ public Node parseN(ParserMode mode, ParserErrorCallback errors, Source source, F
186190
} catch (Exception e) {
187191
throw handleParserError(errors, source, e, !(mode == ParserMode.InteractiveStatement || mode == ParserMode.Statement));
188192
}
189-
190193
}
191194

192195
// @Override
@@ -262,13 +265,18 @@ public String unescapeJavaString(String str) {
262265
}
263266

264267
private static PException handleParserError(ParserErrorCallback errors, Source source, Exception e, boolean showBadLine) {
265-
if (e instanceof PException) {
268+
if (e instanceof TruffleException && ((TruffleException) e).isSyntaxError() && e instanceof PException) {
269+
if (!showBadLine) {
270+
PBaseException instance = ((PException) e).getExceptionObject();
271+
// In cpython shell the line with the error is not displayed, so we should do it in
272+
// the same way.
273+
// This rely on implementation in traceback.py file. See comment in
274+
// Python3Core.raiseInvalidSyntax method
275+
instance.setAttribute("text", PNone.NONE);
276+
}
266277
return (PException) e;
267278
}
268-
// In cpython shell the line with the error is not displayed, so we should do it in the same
269-
// way
270-
// This rely on implementation in traceback.py file. See comment in
271-
// Python3Core.raiseInvalidSyntax method
279+
272280
SourceSection section = showBadLine ? PythonErrorStrategy.getPosition(source, e) : source.createUnavailableSection();
273281
// from parser we are getting RuntimeExceptions
274282
String message = e instanceof RuntimeException && e.getMessage() != null ? e.getMessage() : "invalid syntax";

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/parser/sst/FactorySSTVisitor.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@
101101
import com.oracle.graal.python.nodes.subscript.SliceLiteralNode;
102102
import com.oracle.graal.python.parser.ScopeInfo.ScopeKind;
103103
import com.oracle.graal.python.runtime.PythonParser;
104-
import static com.oracle.graal.python.runtime.exception.PythonErrorType.SyntaxError;
105104
import com.oracle.truffle.api.RootCallTarget;
106105
import com.oracle.truffle.api.Truffle;
107106
import com.oracle.truffle.api.frame.FrameDescriptor;
@@ -250,7 +249,7 @@ public PNode visit(AssignmentSSTNode node) {
250249
public PNode visit(AugAssignmentSSTNode node) {
251250
ExpressionNode lhs = (ExpressionNode) node.lhs.accept(this);
252251
if (!(lhs instanceof ReadNode)) {
253-
throw errors.raise(SyntaxError, "illegal expression for augmented assignment");
252+
throw errors.raiseInvalidSyntax(source, createSourceSection(node.startOffset, node.endOffset), "illegal expression for augmented assignment");
254253
}
255254
ExpressionNode rhs = (ExpressionNode) node.rhs.accept(this);
256255
ExpressionNode binOp = nodeFactory.createInplaceOperation(node.operation, lhs, rhs);
@@ -625,15 +624,15 @@ public PNode visit(ForSSTNode node) {
625624
target = targets[0];
626625
if (!(target instanceof ReadNode || target instanceof TupleLiteralNode || target instanceof ListLiteralNode)) {
627626
if (target instanceof StarredExpressionNode) {
628-
throw errors.raise(SyntaxError, "starred assignment target must be in a list or tuple");
627+
throw errors.raiseInvalidSyntax(source, createSourceSection(node.startOffset, node.endOffset), "starred assignment target must be in a list or tuple");
629628
} else {
630629
// TODO handle this???
631630
// String text = ctx.getText();
632631
// if (environment.isNonlocal(text)) {
633632
// throw errors.raise(SyntaxError, "no binding for nonlocal variable \"%s\"
634633
// found", text);
635634
// }
636-
throw errors.raise(SyntaxError, "Cannot assign to %s", target);
635+
throw errors.raiseInvalidSyntax(source, createSourceSection(node.startOffset, node.endOffset), "Cannot assign to %s", target);
637636
}
638637
}
639638
} else {
@@ -1101,9 +1100,9 @@ public PNode visit(VarLookupSSTNode node) {
11011100
PNode result = (PNode) scopeEnvironment.findVariable(node.name);
11021101
if (result == null) {
11031102
if (scopeEnvironment.isNonlocal(node.name)) {
1104-
throw errors.raise(SyntaxError, "no binding for nonlocal variable \"%s\" found", node.name);
1103+
throw errors.raiseInvalidSyntax(source, createSourceSection(node.startOffset, node.endOffset), "no binding for nonlocal variable \"%s\" found", node.name);
11051104
}
1106-
throw errors.raise(SyntaxError, "Cannot assign to %s", node.name);
1105+
throw errors.raiseInvalidSyntax(source, createSourceSection(node.startOffset, node.endOffset), "Cannot assign to %s", node.name);
11071106
}
11081107
result.assignSourceSection(createSourceSection(node.startOffset, node.endOffset));
11091108
// scopeEnvironment.setCurrentScope(oldScope);
@@ -1213,7 +1212,11 @@ private StatementNode createDestructuringAssignment(ExpressionNode[] leftHandSid
12131212
temps[i] = tempRead;
12141213
if (leftHandSides[i] instanceof StarredExpressionNode) {
12151214
if (starredIndex != -1) {
1216-
throw errors.raise(SyntaxError, "two starred expressions in assignment");
1215+
SourceSection section = leftHandSides[0].getSourceSection();
1216+
if (section == null) {
1217+
section = ((StarredExpressionNode) leftHandSides[i]).getValue().getSourceSection();
1218+
}
1219+
throw errors.raiseInvalidSyntax(source, section, "two starred expressions in assignment");
12171220
}
12181221
starredIndex = i;
12191222
statements[i] = createAssignment(((StarredExpressionNode) leftHandSides[i]).getValue(), (ExpressionNode) tempRead);

0 commit comments

Comments
 (0)