Skip to content

Commit 9025176

Browse files
committed
[GR-12795] Fix destructuring assignment to single element
PullRequest: graalpython/319
2 parents fa83811 + d036cf4 commit 9025176

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/grammar/TestParserTranslator.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@ public void parseAssignments() {
330330
parseAs = parseAs("a = 1,2", WriteNameNode.class);
331331
assert parseAs.getRhs() instanceof TupleLiteralNode;
332332

333+
parseAs("a, = 1,", DestructuringAssignmentNode.class);
333334
parseAs("a,b = 1,2", DestructuringAssignmentNode.class);
334335
parseAs("a,*b,c = 1,2", DestructuringAssignmentNode.class);
335336
parseAs("[[a],*b],c = 1,2", DestructuringAssignmentNode.class);

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ private PNode makeAugmentedAssignment(ExpressionNode lhs, String text, Expressio
173173
}
174174

175175
private PNode visitTargetlist(ParserRuleContext ctx, int starSize) {
176+
boolean endsWithComma = false;
176177
if (starSize > 0) {
177178
if (starSize > 1) {
178179
throw errors.raise(SyntaxError, "%d starred expressions in assigment", starSize);
@@ -181,7 +182,11 @@ private PNode visitTargetlist(ParserRuleContext ctx, int starSize) {
181182
List<ExpressionNode> targets = new ArrayList<>();
182183
for (int i = 0; i < ctx.getChildCount(); i++) {
183184
ParseTree child = ctx.getChild(i);
185+
endsWithComma = false;
184186
if (child instanceof TerminalNode) {
187+
if (child.getText().equals(",")) {
188+
endsWithComma = true;
189+
}
185190
continue;
186191
} else if (child instanceof Python3Parser.TestContext ||
187192
child instanceof Python3Parser.Star_exprContext ||
@@ -191,7 +196,7 @@ private PNode visitTargetlist(ParserRuleContext ctx, int starSize) {
191196
assert false;
192197
}
193198
}
194-
if (targets.size() == 1) {
199+
if (targets.size() == 1 && !endsWithComma) {
195200
PNode pNode = targets.get(0);
196201
if (pNode instanceof ReadNode) {
197202
return pNode;

0 commit comments

Comments
 (0)