Skip to content

Commit ae9e289

Browse files
committed
[GR-18769] When starred expression is used in wrong way, syntax error has to be raised.
1 parent 6e46abb commit ae9e289

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
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
ModuleRootNode Name: <module 'starExpr08'> SourceSection: [0,52]`for a, *b in [(1, 2,...`
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,52]`for a, *b in [(1, 2,...`
8+
ExpressionWithSideEffect SourceSection: [0,52]`for a, *b in [(1, 2,...`
9+
Expression:
10+
EmptyNode SourceSection: None
11+
SideEffect:
12+
ElseNode SourceSection: [0,52]`for a, *b in [(1, 2,...`
13+
ForNode SourceSection: None
14+
ForRepeatingNode SourceSection: None
15+
ForNextElementNodeGen SourceSection: None
16+
DestructuringAssignmentNodeGen SourceSection: None
17+
WriteLocalVariableNodeGen SourceSection: None
18+
Identifier: <>temp0
19+
WriteLocalFrameSlotNodeGen SourceSection: None
20+
Frame: [0,<>temp0,Illegal]
21+
WriteLocalVariableNodeGen SourceSection: None
22+
Identifier: <>temp1
23+
WriteLocalFrameSlotNodeGen SourceSection: None
24+
Frame: [1,<>temp1,Illegal]
25+
WriteNameNodeGen SourceSection: None
26+
Identifier: a
27+
ReadLocalVariableNode SourceSection: None
28+
Frame: [0,<>temp0,Illegal]
29+
ReadVariableFromFrameNodeGen SourceSection: None
30+
WriteNameNodeGen SourceSection: None
31+
Identifier: b
32+
ReadLocalVariableNode SourceSection: None
33+
Frame: [1,<>temp1,Illegal]
34+
ReadVariableFromFrameNodeGen SourceSection: None
35+
ExpressionStatementNode SourceSection: [44,52]`print(b)`
36+
PythonCallNodeGen SourceSection: [44,52]`print(b)`
37+
CachedCallNodeGen SourceSection: None
38+
CreateArgumentsNodeGen SourceSection: None
39+
CallDispatchNodeGen SourceSection: None
40+
ReadNameNodeGen SourceSection: [50,51]`b`
41+
Identifier: b
42+
ReadNameNodeGen SourceSection: None
43+
Identifier: print
44+
GetIteratorExpressionNodeGen SourceSection: [13,38]`[(1, 2, 3), (4, 5, 6...`
45+
GetIteratorNodeGen SourceSection: None
46+
ListLiteralNode SourceSection: [13,38]`[(1, 2, 3), (4, 5, 6...`
47+
PythonObjectFactoryNodeGen SourceSection: None
48+
TupleLiteralNode SourceSection: [14,23]`(1, 2, 3)`
49+
PythonObjectFactoryNodeGen SourceSection: None
50+
IntegerLiteralNode SourceSection: [15,16]`1`
51+
Value: 1
52+
IntegerLiteralNode SourceSection: [18,19]`2`
53+
Value: 2
54+
IntegerLiteralNode SourceSection: [21,22]`3`
55+
Value: 3
56+
TupleLiteralNode SourceSection: [25,37]`(4, 5, 6, 7)`
57+
PythonObjectFactoryNodeGen SourceSection: None
58+
IntegerLiteralNode SourceSection: [26,27]`4`
59+
Value: 4
60+
IntegerLiteralNode SourceSection: [29,30]`5`
61+
Value: 5
62+
IntegerLiteralNode SourceSection: [32,33]`6`
63+
Value: 6
64+
IntegerLiteralNode SourceSection: [35,36]`7`
65+
Value: 7
66+
BlockNode SourceSection: None

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
import com.oracle.graal.python.builtins.modules.WeakRefModuleBuiltins;
9090
import com.oracle.graal.python.builtins.modules.ZLibModuleBuiltins;
9191
import com.oracle.graal.python.builtins.modules.ZipImportModuleBuiltins;
92+
import com.oracle.graal.python.builtins.objects.PNone;
9293
import com.oracle.graal.python.builtins.objects.array.ArrayBuiltins;
9394
import com.oracle.graal.python.builtins.objects.bool.BoolBuiltins;
9495
import com.oracle.graal.python.builtins.objects.bytes.AbstractBytesBuiltins;
@@ -658,6 +659,7 @@ public PFloat getNaN() {
658659
return pyNaN;
659660
}
660661

662+
@Override
661663
public RuntimeException raiseInvalidSyntax(Source source, SourceSection section, String message, Object... arguments) {
662664
Node location = new Node() {
663665
@Override
@@ -677,7 +679,10 @@ public RuntimeException raiseInvalidSyntax(Node location, String message, Object
677679
Source source = section.getSource();
678680
String path = source.getPath();
679681
instance.setAttribute("filename", path != null ? path : source.getName() != null ? source.getName() : "<string>");
680-
instance.setAttribute("text", section.isAvailable() ? source.getCharacters(section.getStartLine()) : "");
682+
// Not very nice. This counts on the implementation in traceback.py where if the value of
683+
// text attribute
684+
// is NONE, then the line is not printed
685+
instance.setAttribute("text", section.isAvailable() ? source.getCharacters(section.getStartLine()) : PNone.NONE);
681686
instance.setAttribute("lineno", section.getStartLine());
682687
instance.setAttribute("offset", section.getStartColumn());
683688
String msg;

0 commit comments

Comments
 (0)