Skip to content

Commit bbf2609

Browse files
committed
[GR-18769] When starred expression is used in wrong way, syntax error has to be raised.
PullRequest: graalpython/681
2 parents 6e46abb + ae9e289 commit bbf2609

File tree

15 files changed

+358
-30
lines changed

15 files changed

+358
-30
lines changed

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/parser/BasicTests.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -522,11 +522,6 @@ public void not01() throws Exception {
522522
checkTreeResult("not a");
523523
}
524524

525-
@Test
526-
public void star01() throws Exception {
527-
checkTreeResult("*a");
528-
}
529-
530525
@Test
531526
public void nonlocal02() throws Exception {
532527
checkTreeResult("nonlocal x");

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/parser/ListAndSlicingTests.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,56 @@ public void slice04() throws Exception {
9494
public void slice05() throws Exception {
9595
checkTreeResult("a()[b():c():d()]");
9696
}
97+
98+
@Test
99+
public void starExpr01() throws Exception {
100+
checkTreeResult("[*[1,2,3]]");
101+
}
102+
103+
@Test
104+
public void starExpr02() throws Exception {
105+
checkSyntaxErrorMessageContains("*[1,2,3]", "can't use starred expression here");
106+
}
107+
108+
@Test
109+
public void starExpr03() throws Exception {
110+
checkSyntaxErrorMessageContains("*a = range(5)", "starred assignment target must be in a list or tuple");
111+
}
112+
113+
@Test
114+
public void starExpr04() throws Exception {
115+
checkTreeResult("*a, = range(5)");
116+
}
117+
118+
@Test
119+
public void starExpr05() throws Exception {
120+
checkTreeResult("a, *b, c = range(5)");
121+
}
122+
123+
@Test
124+
public void starExpr06() throws Exception {
125+
checkTreeResult("first, *rest = seq");
126+
}
127+
128+
@Test
129+
public void starExpr07() throws Exception {
130+
checkTreeResult("[a, *b, c] = seq");
131+
}
132+
133+
@Test
134+
public void starExpr08() throws Exception {
135+
checkTreeResult("for a, *b in [(1, 2, 3), (4, 5, 6, 7)]:\n" +
136+
" print(b)");
137+
}
138+
139+
@Test
140+
public void starExpr09() throws Exception {
141+
checkSyntaxErrorMessageContains("b = *a", "can't use starred expression here");
142+
}
143+
144+
@Test
145+
public void starExpr10() throws Exception {
146+
checkSyntaxErrorMessageContains("[*item for item in l]", "iterable unpacking cannot be used in comprehension");
147+
}
148+
97149
}

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/parser/ParserTestBase.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,18 @@ public void checkSyntaxError(String source) throws Exception {
150150
assertTrue("Expected SyntaxError was not thrown.", thrown);
151151
}
152152

153+
public void checkSyntaxErrorMessageContains(String source, String expectedMessage) throws Exception {
154+
boolean thrown = false;
155+
try {
156+
parseNew(source, name.getMethodName(), PythonParser.ParserMode.File);
157+
} catch (PException e) {
158+
thrown = e.isSyntaxError();
159+
Assert.assertTrue("The expected message:\n\"" + expectedMessage + "\"\nwas not found in\n\"" + e.getMessage() + "\"", e.getMessage().contains(expectedMessage));
160+
}
161+
162+
assertTrue("Expected SyntaxError was not thrown.", thrown);
163+
}
164+
153165
public void checkSyntaxErrorMessage(String source, String expectedMessage) throws Exception {
154166
boolean thrown = false;
155167
try {

graalpython/com.oracle.graal.python.test/testData/goldenFiles/BasicTests/star01.tast

Lines changed: 0 additions & 11 deletions
This file was deleted.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
ModuleRootNode Name: <module 'gr18737'> SourceSection: [0,10]`[*[1,2,3]]`
2+
Signature: varArgs=False, varKeywordArgs=False, noArguments=True, positionalOnly=True, requiresKeywordArgs=False
3+
FreeVars: None
4+
NeedsCellFrame: False
5+
FrameDescriptor: Empty
6+
Documentation: None
7+
InnerRootNode SourceSection: [0,10]`[*[1,2,3]]`
8+
ListLiteralNode SourceSection: [0,10]`[*[1,2,3]]`
9+
PythonObjectFactoryNodeGen SourceSection: None
10+
StarredExpressionNode SourceSection: None
11+
CastToListExpressionNodeGen SourceSection: None
12+
ListLiteralNode SourceSection: [2,9]`[1,2,3]`
13+
PythonObjectFactoryNodeGen SourceSection: None
14+
IntegerLiteralNode SourceSection: [3,4]`1`
15+
Value: 1
16+
IntegerLiteralNode SourceSection: [5,6]`2`
17+
Value: 2
18+
IntegerLiteralNode SourceSection: [7,8]`3`
19+
Value: 3
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
ModuleRootNode Name: <module 'starExpr01'> SourceSection: [0,10]`[*[1,2,3]]`
2+
Signature: varArgs=False, varKeywordArgs=False, noArguments=True, positionalOnly=True, requiresKeywordArgs=False
3+
FreeVars: None
4+
NeedsCellFrame: False
5+
FrameDescriptor: Empty
6+
Documentation: None
7+
InnerRootNode SourceSection: [0,10]`[*[1,2,3]]`
8+
ListLiteralNode SourceSection: [0,10]`[*[1,2,3]]`
9+
PythonObjectFactoryNodeGen SourceSection: None
10+
StarredExpressionNode SourceSection: None
11+
CastToListExpressionNodeGen SourceSection: None
12+
ListLiteralNode SourceSection: [2,9]`[1,2,3]`
13+
PythonObjectFactoryNodeGen SourceSection: None
14+
IntegerLiteralNode SourceSection: [3,4]`1`
15+
Value: 1
16+
IntegerLiteralNode SourceSection: [5,6]`2`
17+
Value: 2
18+
IntegerLiteralNode SourceSection: [7,8]`3`
19+
Value: 3
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
ModuleRootNode Name: <module 'starExpr04'> SourceSection: [0,14]`*a, = range(5)`
2+
Signature: varArgs=False, varKeywordArgs=False, noArguments=True, positionalOnly=True, requiresKeywordArgs=False
3+
FreeVars: None
4+
NeedsCellFrame: False
5+
FrameDescriptor: 1 slots [<>temp0]
6+
Documentation: None
7+
InnerRootNode SourceSection: [0,14]`*a, = range(5)`
8+
ExpressionWithSideEffect SourceSection: [0,14]`*a, = range(5)`
9+
Expression:
10+
EmptyNode SourceSection: None
11+
SideEffect:
12+
DestructuringAssignmentNodeGen SourceSection: [0,14]`*a, = range(5)`
13+
PythonCallNodeGen SourceSection: [6,14]`range(5)`
14+
CachedCallNodeGen SourceSection: None
15+
CreateArgumentsNodeGen SourceSection: None
16+
CallDispatchNodeGen SourceSection: None
17+
IntegerLiteralNode SourceSection: [12,13]`5`
18+
Value: 5
19+
ReadNameNodeGen SourceSection: None
20+
Identifier: range
21+
WriteLocalVariableNodeGen SourceSection: None
22+
Identifier: <>temp0
23+
WriteLocalFrameSlotNodeGen SourceSection: None
24+
Frame: [0,<>temp0,Illegal]
25+
WriteNameNodeGen SourceSection: None
26+
Identifier: a
27+
ReadLocalVariableNode SourceSection: None
28+
Frame: [0,<>temp0,Illegal]
29+
ReadVariableFromFrameNodeGen SourceSection: None
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
ModuleRootNode Name: <module 'starExpr05'> SourceSection: [0,19]`a, *b, c = range(5)`
2+
Signature: varArgs=False, varKeywordArgs=False, noArguments=True, positionalOnly=True, requiresKeywordArgs=False
3+
FreeVars: None
4+
NeedsCellFrame: False
5+
FrameDescriptor: 3 slots [<>temp0, <>temp1, <>temp2]
6+
Documentation: None
7+
InnerRootNode SourceSection: [0,19]`a, *b, c = range(5)`
8+
ExpressionWithSideEffect SourceSection: [0,19]`a, *b, c = range(5)`
9+
Expression:
10+
EmptyNode SourceSection: None
11+
SideEffect:
12+
DestructuringAssignmentNodeGen SourceSection: [0,19]`a, *b, c = range(5)`
13+
PythonCallNodeGen SourceSection: [11,19]`range(5)`
14+
CachedCallNodeGen SourceSection: None
15+
CreateArgumentsNodeGen SourceSection: None
16+
CallDispatchNodeGen SourceSection: None
17+
IntegerLiteralNode SourceSection: [17,18]`5`
18+
Value: 5
19+
ReadNameNodeGen SourceSection: None
20+
Identifier: range
21+
WriteLocalVariableNodeGen SourceSection: None
22+
Identifier: <>temp0
23+
WriteLocalFrameSlotNodeGen SourceSection: None
24+
Frame: [0,<>temp0,Illegal]
25+
WriteLocalVariableNodeGen SourceSection: None
26+
Identifier: <>temp1
27+
WriteLocalFrameSlotNodeGen SourceSection: None
28+
Frame: [1,<>temp1,Illegal]
29+
WriteLocalVariableNodeGen SourceSection: None
30+
Identifier: <>temp2
31+
WriteLocalFrameSlotNodeGen SourceSection: None
32+
Frame: [2,<>temp2,Illegal]
33+
WriteNameNodeGen SourceSection: None
34+
Identifier: a
35+
ReadLocalVariableNode SourceSection: None
36+
Frame: [0,<>temp0,Illegal]
37+
ReadVariableFromFrameNodeGen SourceSection: None
38+
WriteNameNodeGen SourceSection: None
39+
Identifier: b
40+
ReadLocalVariableNode SourceSection: None
41+
Frame: [1,<>temp1,Illegal]
42+
ReadVariableFromFrameNodeGen SourceSection: None
43+
WriteNameNodeGen SourceSection: None
44+
Identifier: c
45+
ReadLocalVariableNode SourceSection: None
46+
Frame: [2,<>temp2,Illegal]
47+
ReadVariableFromFrameNodeGen SourceSection: None
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
ModuleRootNode Name: <module 'starExpr06'> SourceSection: [0,18]`first, *rest = seq`
2+
Signature: varArgs=False, varKeywordArgs=False, noArguments=True, positionalOnly=True, requiresKeywordArgs=False
3+
FreeVars: None
4+
NeedsCellFrame: False
5+
FrameDescriptor: 2 slots [<>temp0, <>temp1]
6+
Documentation: None
7+
InnerRootNode SourceSection: [0,18]`first, *rest = seq`
8+
ExpressionWithSideEffect SourceSection: [0,18]`first, *rest = seq`
9+
Expression:
10+
EmptyNode SourceSection: None
11+
SideEffect:
12+
DestructuringAssignmentNodeGen SourceSection: [0,18]`first, *rest = seq`
13+
ReadNameNodeGen SourceSection: [15,18]`seq`
14+
Identifier: seq
15+
WriteLocalVariableNodeGen SourceSection: None
16+
Identifier: <>temp0
17+
WriteLocalFrameSlotNodeGen SourceSection: None
18+
Frame: [0,<>temp0,Illegal]
19+
WriteLocalVariableNodeGen SourceSection: None
20+
Identifier: <>temp1
21+
WriteLocalFrameSlotNodeGen SourceSection: None
22+
Frame: [1,<>temp1,Illegal]
23+
WriteNameNodeGen SourceSection: None
24+
Identifier: first
25+
ReadLocalVariableNode SourceSection: None
26+
Frame: [0,<>temp0,Illegal]
27+
ReadVariableFromFrameNodeGen SourceSection: None
28+
WriteNameNodeGen SourceSection: None
29+
Identifier: rest
30+
ReadLocalVariableNode SourceSection: None
31+
Frame: [1,<>temp1,Illegal]
32+
ReadVariableFromFrameNodeGen SourceSection: None
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
ModuleRootNode Name: <module 'starExpr07'> SourceSection: [0,16]`[a, *b, c] = seq`
2+
Signature: varArgs=False, varKeywordArgs=False, noArguments=True, positionalOnly=True, requiresKeywordArgs=False
3+
FreeVars: None
4+
NeedsCellFrame: False
5+
FrameDescriptor: 3 slots [<>temp0, <>temp1, <>temp2]
6+
Documentation: None
7+
InnerRootNode SourceSection: [0,16]`[a, *b, c] = seq`
8+
ExpressionWithSideEffect SourceSection: [0,16]`[a, *b, c] = seq`
9+
Expression:
10+
EmptyNode SourceSection: None
11+
SideEffect:
12+
DestructuringAssignmentNodeGen SourceSection: [0,16]`[a, *b, c] = seq`
13+
ReadNameNodeGen SourceSection: [13,16]`seq`
14+
Identifier: seq
15+
WriteLocalVariableNodeGen SourceSection: None
16+
Identifier: <>temp0
17+
WriteLocalFrameSlotNodeGen SourceSection: None
18+
Frame: [0,<>temp0,Illegal]
19+
WriteLocalVariableNodeGen SourceSection: None
20+
Identifier: <>temp1
21+
WriteLocalFrameSlotNodeGen SourceSection: None
22+
Frame: [1,<>temp1,Illegal]
23+
WriteLocalVariableNodeGen SourceSection: None
24+
Identifier: <>temp2
25+
WriteLocalFrameSlotNodeGen SourceSection: None
26+
Frame: [2,<>temp2,Illegal]
27+
WriteNameNodeGen SourceSection: None
28+
Identifier: a
29+
ReadLocalVariableNode SourceSection: None
30+
Frame: [0,<>temp0,Illegal]
31+
ReadVariableFromFrameNodeGen SourceSection: None
32+
WriteNameNodeGen SourceSection: None
33+
Identifier: b
34+
ReadLocalVariableNode SourceSection: None
35+
Frame: [1,<>temp1,Illegal]
36+
ReadVariableFromFrameNodeGen SourceSection: None
37+
WriteNameNodeGen SourceSection: None
38+
Identifier: c
39+
ReadLocalVariableNode SourceSection: None
40+
Frame: [2,<>temp2,Illegal]
41+
ReadVariableFromFrameNodeGen SourceSection: None

0 commit comments

Comments
 (0)