Skip to content

Commit effdb6e

Browse files
committed
Disallow unparenthesized for in calls with multiple arguments
1 parent c4d3af1 commit effdb6e

File tree

4 files changed

+23
-1
lines changed

4 files changed

+23
-1
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/ErrorMessages.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ public abstract class ErrorMessages {
216216
public static final String FUNC_CONSTRUCTION_NOT_SUPPORTED = "function construction not supported for (%p, %p, %p, %p, %p, %p)";
217217
public static final String FUNC_TAKES_AT_LEAST_D_ARGS = "function takes at least %d arguments (%d given)";
218218
public static final String FUNC_TAKES_EXACTLY_D_ARGS = "function takes exaclty %d arguments (%d given)";
219+
public static final String GENERATOR_EXPR_MUST_BE_PARENTHESIZED = "Generator expression must be parenthesized";
219220
public static final String GENERATOR_IGNORED_EXIT = "generator ignored GeneratorExit";
220221
public static final String GENERATOR_RAISED_STOPITER = "generator raised StopIteration";
221222
public static final String GENERATOR_ALREADY_EXECUTING = "generator already executing";

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/parser/antlr/Python3.g4

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1622,7 +1622,7 @@ argument [ArgListBuilder args] returns [SSTNode result]
16221622
}
16231623
test comp_for[$test.result, null, PythonBuiltinClassType.PGenerator, 0]
16241624
{
1625-
args.addArg($comp_for.result);
1625+
args.addNakedForComp($comp_for.result);
16261626
scopeEnvironment.popScope();
16271627
}
16281628
|

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public final class ArgListBuilder {
6161
private List<String> nameArgNames;
6262
private List<SSTNode> starArg;
6363
private List<SSTNode> kwArg;
64+
private SSTNode nakedForComp;
6465

6566
public ArgListBuilder() {
6667
// default
@@ -188,4 +189,16 @@ public ExpressionNode getStarArgs(SSTreeVisitor<PNode> visitor) {
188189
return result;
189190
}
190191

192+
public boolean hasNakedForComp() {
193+
return nakedForComp != null;
194+
}
195+
196+
public SSTNode getNakedForComp() {
197+
return nakedForComp;
198+
}
199+
200+
public void addNakedForComp(SSTNode node) {
201+
this.nakedForComp = node;
202+
addArg(node);
203+
}
191204
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,10 @@ public PNode visit(CallSSTNode node) {
347347
ExpressionNode target = (ExpressionNode) node.target.accept(this);
348348

349349
ArgListBuilder argBuilder = node.parameters;
350+
if (argBuilder.hasNakedForComp() && argBuilder.getArgs().length != 1) {
351+
SSTNode forComp = argBuilder.getNakedForComp();
352+
throw errors.raiseInvalidSyntax(source, createSourceSection(forComp.getStartOffset(), forComp.getEndOffset()), ErrorMessages.GENERATOR_EXPR_MUST_BE_PARENTHESIZED);
353+
}
350354
ExpressionNode callNode = PythonCallNode.create(target, argBuilder.getArgs(this), argBuilder.getNameArgs(this), argBuilder.getStarArgs(this), argBuilder.getKwArgs(this));
351355
callNode.assignSourceSection(createSourceSection(node.startOffset, node.endOffset));
352356
return callNode;
@@ -457,6 +461,10 @@ public PNode visit(ClassSSTNode node) {
457461
ExpressionNode starArg = null;
458462
ExpressionNode kwArg = null;
459463
if (node.baseClasses != null) {
464+
if (node.baseClasses.hasNakedForComp()) {
465+
SSTNode forComp = node.baseClasses.getNakedForComp();
466+
throw errors.raiseInvalidSyntax(source, createSourceSection(forComp.getStartOffset(), forComp.getEndOffset()));
467+
}
460468
ExpressionNode[] sstArgs = node.baseClasses.getArgs(this);
461469
args = new ExpressionNode[sstArgs.length + 2];
462470
for (int i = 0; i < sstArgs.length; i++) {

0 commit comments

Comments
 (0)