Skip to content

Commit b0e08fe

Browse files
committed
raise more complete SyntaxErrors in more cases
1 parent 5ffe3d7 commit b0e08fe

File tree

4 files changed

+27
-23
lines changed

4 files changed

+27
-23
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -694,15 +694,15 @@ public PInt getFalse() {
694694
return pyFalse;
695695
}
696696

697-
public RuntimeException raiseInvalidSyntax(Source source, SourceSection section) {
697+
public RuntimeException raiseInvalidSyntax(Source source, SourceSection section, String message, Object... arguments) {
698698
Node location = new Node() {
699699
@Override
700700
public SourceSection getSourceSection() {
701701
return section;
702702
}
703703
};
704704
PBaseException instance;
705-
instance = factory().createBaseException(SyntaxError, "invalid syntax", new Object[0]);
705+
instance = factory().createBaseException(SyntaxError, message, arguments);
706706
String path = source.getPath();
707707
instance.setAttribute("filename", path != null ? path : source.getName() != null ? source.getName() : "<string>");
708708
instance.setAttribute("text", section.isAvailable() ? source.getCharacters(section.getStartLine()) : "");

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/frame/DestructuringAssignmentNode.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,10 @@
2525
*/
2626
package com.oracle.graal.python.nodes.frame;
2727

28-
import static com.oracle.graal.python.runtime.exception.PythonErrorType.IndexError;
29-
import static com.oracle.graal.python.runtime.exception.PythonErrorType.SyntaxError;
30-
import static com.oracle.graal.python.runtime.exception.PythonErrorType.ValueError;
3128
import static com.oracle.graal.python.builtins.objects.PNone.NO_VALUE;
3229
import static com.oracle.graal.python.nodes.SpecialMethodNames.__GETITEM__;
30+
import static com.oracle.graal.python.runtime.exception.PythonErrorType.IndexError;
31+
import static com.oracle.graal.python.runtime.exception.PythonErrorType.ValueError;
3332

3433
import java.util.Arrays;
3534

@@ -182,7 +181,7 @@ public void doWrite(VirtualFrame frame, Object rhsValue) {
182181
try {
183182
getNonExistingItem.execute(rhsValue, nonExistingItem);
184183
tooManyValuesProfile.enter();
185-
throw raise(SyntaxError, "too many values to unpack (expected %d)", nonExistingItem);
184+
throw getCore().raiseInvalidSyntax(getEncapsulatingSourceSection().getSource(), getEncapsulatingSourceSection(), "too many values to unpack (expected %d)", nonExistingItem);
186185
} catch (PException e) {
187186
if (tooManyValuesErrorProfile.profileException(e, IndexError)) {
188187
// expected, fall through

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

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ private void visitCallArglist(Python3Parser.ArglistContext arglist, List<Express
302302
}
303303
} else if (isStararg(argctx)) {
304304
if (kwargs != null) {
305-
throw errors.raise(SyntaxError, "iterable argument unpacking follows keyword argument unpacking");
305+
throw errors.raiseInvalidSyntax(source, deriveSourceSection(argctx), "iterable argument unpacking follows keyword argument unpacking");
306306
}
307307
if (starargs != null) {
308308
starargs = factory.createBinaryOperation("+", starargs, arg);
@@ -311,10 +311,10 @@ private void visitCallArglist(Python3Parser.ArglistContext arglist, List<Express
311311
}
312312
} else {
313313
if (!keywords.isEmpty()) {
314-
throw errors.raise(SyntaxError, "positional argument follows keyword argument");
314+
throw errors.raiseInvalidSyntax(source, deriveSourceSection(argctx), "positional argument follows keyword argument");
315315
}
316316
if (kwargs != null) {
317-
throw errors.raise(SyntaxError, "positional argument follows keyword argument unpacking");
317+
throw errors.raiseInvalidSyntax(source, deriveSourceSection(argctx), "positional argument follows keyword argument unpacking");
318318
}
319319
argumentNodes.add(arg);
320320
}
@@ -355,7 +355,7 @@ private ExpressionNode getDefaultarg(Python3Parser.ArgumentContext ctx) {
355355
// In CPython, ast.c ensures this
356356
String argName = ctx.test(0).accept(new ExtractNameVisitor());
357357
if (argName == null) {
358-
throw errors.raise(SyntaxError, "Keyword can't be an expression");
358+
throw errors.raiseInvalidSyntax(source, deriveSourceSection(ctx), "Keyword can't be an expression");
359359
}
360360
return factory.createKeywordLiteral((ExpressionNode) ctx.test(1).accept(this), argName);
361361
} else {
@@ -428,7 +428,7 @@ public Object visitAtom(Python3Parser.AtomContext ctx) {
428428
} else if (ctx.NAME() != null) {
429429
return environment.findVariable(ctx.NAME().getText());
430430
} else if (ctx.getChildCount() == 1) {
431-
return parseSpecialLiteral(ctx.getText());
431+
return parseSpecialLiteral(ctx);
432432
} else if (ctx.dictorsetmaker() != null) {
433433
return super.visitAtom(ctx);
434434
} else if (ctx.getChild(0).getText().equals("{")) { // empty dict
@@ -540,23 +540,24 @@ private ExpressionNode visitNormalDictmaker(Python3Parser.DictmakerContext ctx)
540540

541541
private PNode visitDictmakerComprehension(Python3Parser.DictmakerContext ctx) {
542542
if (!ctx.expr().isEmpty()) {
543-
throw errors.raise(SyntaxError, "dict unpacking cannot be used in dict comprehension");
543+
throw errors.raiseInvalidSyntax(source, deriveSourceSection(ctx), "dict unpacking cannot be used in dict comprehension");
544544
}
545545
return factory.callBuiltin(DICT,
546546
createComprehensionExpression(ctx, ctx.comp_for(), c -> factory.createTupleLiteral((ExpressionNode) ctx.test(0).accept(this), (ExpressionNode) ctx.test(1).accept(this))));
547547
}
548548

549-
private Object parseSpecialLiteral(String text) {
550-
if (text.equals("...")) {
549+
private Object parseSpecialLiteral(Python3Parser.AtomContext ctx) {
550+
String txt = ctx.getText();
551+
if (txt.equals("...")) {
551552
return factory.createObjectLiteral(PEllipsis.INSTANCE);
552-
} else if (text.equals("None")) {
553+
} else if (txt.equals("None")) {
553554
return factory.createObjectLiteral(PNone.NONE);
554-
} else if (text.equals("True")) {
555+
} else if (txt.equals("True")) {
555556
return factory.createBooleanLiteral(true);
556-
} else if (text.equals("False")) {
557+
} else if (txt.equals("False")) {
557558
return factory.createBooleanLiteral(false);
558559
} else {
559-
throw errors.raise(SyntaxError, "Unknown literal %s", text);
560+
throw errors.raiseInvalidSyntax(source, deriveSourceSection(ctx), "Unknown literal %s", txt);
560561
}
561562
}
562563

@@ -975,7 +976,7 @@ public Object visitImport_from(Python3Parser.Import_fromContext ctx) {
975976
return factory.createImportFrom(sb.toString(), fromlist.toArray(new String[0]), asNodes.toArray(new WriteNode[0]), level);
976977
} else {
977978
if (!environment.atModuleLevel()) {
978-
throw errors.raise(SyntaxError, "import * only allowed at module level");
979+
throw errors.raiseInvalidSyntax(source, deriveSourceSection(ctx), "import * only allowed at module level");
979980
}
980981
return factory.createImportStar(sb.toString(), level);
981982
}
@@ -1097,7 +1098,7 @@ private void delTarget(List<StatementNode> blockList, PNode target) {
10971098
delTarget(blockList, targetValue);
10981099
}
10991100
} else {
1100-
throw errors.raise(SyntaxError, "can't delete '%s'", target.getSourceSection().getCharacters());
1101+
throw errors.raiseInvalidSyntax(target.getSourceSection().getSource(), target.getSourceSection(), "can't delete '%s'", target.getSourceSection().getCharacters());
11011102
}
11021103
}
11031104

@@ -1156,7 +1157,7 @@ public Object visitReturn_stmt(Python3Parser.Return_stmtContext ctx) {
11561157
return factory.createFrameReturn(factory.createWriteLocal((ExpressionNode) ctx.testlist().accept(this), environment.getReturnSlot()));
11571158
}
11581159
}
1159-
throw errors.raise(SyntaxError, "'return' outside function");
1160+
throw errors.raiseInvalidSyntax(source, deriveSourceSection(ctx), "'return' outside function");
11601161
}
11611162

11621163
private static boolean lastChildIsComma(ParserRuleContext ctx) {
@@ -1369,7 +1370,7 @@ public Object visitTry_stmt(Python3Parser.Try_stmtContext ctx) {
13691370
WriteNode exceptName = null;
13701371
if (excctx.test() != null) {
13711372
if (gotDefaultExcept) {
1372-
throw errors.raise(SyntaxError, "default except: must be last");
1373+
throw errors.raiseInvalidSyntax(source, deriveSourceSection(excctx), "default except: must be last");
13731374
}
13741375
exceptType = (ExpressionNode) excctx.test().accept(this);
13751376
if (excctx.NAME() != null) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/PythonParser.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,11 @@ public enum ParserMode {
7272
public interface ParserErrorCallback {
7373
RuntimeException raise(PythonBuiltinClassType type, String message, Object... args);
7474

75-
RuntimeException raiseInvalidSyntax(Source source, SourceSection section);
75+
RuntimeException raiseInvalidSyntax(Source source, SourceSection section, String message, Object... arguments);
76+
77+
default RuntimeException raiseInvalidSyntax(Source source, SourceSection section) {
78+
return raiseInvalidSyntax(source, section, "invalid syntax", new Object[0]);
79+
}
7680

7781
PythonLanguage getLanguage();
7882
}

0 commit comments

Comments
 (0)