Skip to content

Commit 3e069f9

Browse files
committed
[GR-18449] Assigning to a literal should raise error.
PullRequest: graalpython/670
2 parents 6a8cd7c + 974c0c5 commit 3e069f9

File tree

27 files changed

+292
-1
lines changed

27 files changed

+292
-1
lines changed

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,4 +167,33 @@ public void augassign14() throws Exception {
167167
" cls_or_self, *rest = args");
168168
}
169169

170+
@Test
171+
public void assignToLiteral01() throws Exception {
172+
checkSyntaxErrorMessage("1 = 1", "SyntaxError: can't assign to literal");
173+
}
174+
175+
@Test
176+
public void assignToLiteral02() throws Exception {
177+
checkSyntaxErrorMessage("1.1 = 1", "SyntaxError: can't assign to literal");
178+
}
179+
180+
@Test
181+
public void assignToLiteral03() throws Exception {
182+
checkSyntaxErrorMessage("'' = 1", "SyntaxError: can't assign to literal");
183+
}
184+
185+
@Test
186+
public void assignToLiteral04() throws Exception {
187+
checkSyntaxErrorMessage("f'' = 1", "SyntaxError: can't assign to literal");
188+
}
189+
190+
@Test
191+
public void assignToLiteral05() throws Exception {
192+
checkSyntaxErrorMessage("'' f'' = 1", "SyntaxError: can't assign to literal");
193+
}
194+
195+
@Test
196+
public void assignToKeyword01() throws Exception {
197+
checkSyntaxErrorMessage("True = 1", "SyntaxError: can't assign to keyword");
198+
}
170199
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
import java.nio.CharBuffer;
6060
import java.util.Arrays;
6161
import java.util.List;
62+
import org.junit.Assert;
6263
import static org.junit.Assert.assertTrue;
6364
import org.junit.Rule;
6465
import org.junit.rules.TestName;
@@ -149,6 +150,18 @@ public void checkSyntaxError(String source) throws Exception {
149150
assertTrue("Expected SyntaxError was not thrown.", thrown);
150151
}
151152

153+
public void checkSyntaxErrorMessage(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.assertEquals(expectedMessage, e.getMessage());
160+
}
161+
162+
assertTrue("Expected SyntaxError was not thrown.", thrown);
163+
}
164+
152165
public void saveNewTreeResult(File testFile, boolean goldenFileNextToTestFile) throws Exception {
153166
assertTrue("The test files " + testFile.getAbsolutePath() + " was not found.", testFile.exists());
154167
TruffleFile src = context.getEnv().getInternalTruffleFile(testFile.getAbsolutePath());
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
ModuleRootNode Name: <module 'bin01'> SourceSection: [0,5]`0b101`
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,5]`0b101`
8+
IntegerLiteralNode SourceSection: [0,5]`0b101`
9+
Value: 5
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
ModuleRootNode Name: <module 'bin02'> SourceSection: [0,6]`-0b101`
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,6]`-0b101`
8+
IntegerLiteralNode SourceSection: [0,6]`-0b101`
9+
Value: -5
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
ModuleRootNode Name: <module 'bin03'> SourceSection: [0,66]`0b111111111111111111...`
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,66]`0b111111111111111111...`
8+
PIntLiteralNode SourceSection: [0,66]`0b111111111111111111...`
9+
Value: 18446744073709551615
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
ModuleRootNode Name: <module 'complex01'> SourceSection: [0,4]`0+1j`
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,4]`0+1j`
8+
BinaryArithmeticExpression SourceSection: [0,4]`0+1j`
9+
LookupAndCallBinaryNodeGen SourceSection: None
10+
Op: __add__
11+
IntegerLiteralNode SourceSection: [0,1]`0`
12+
Value: 0
13+
ComplexLiteralNode SourceSection: [2,4]`1j`
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
ModuleRootNode Name: <module 'int01'> SourceSection: [0,1]`1`
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,1]`1`
8+
IntegerLiteralNode SourceSection: [0,1]`1`
9+
Value: 1
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
ModuleRootNode Name: <module 'int02'> SourceSection: [0,2]`-1`
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,2]`-1`
8+
IntegerLiteralNode SourceSection: [0,2]`-1`
9+
Value: -1
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
ModuleRootNode Name: <module 'int03'> SourceSection: [0,2]`-0`
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,2]`-0`
8+
IntegerLiteralNode SourceSection: [0,2]`-0`
9+
Value: 0
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
ModuleRootNode Name: <module 'int04'> SourceSection: [0,7]`h == -1`
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,7]`h == -1`
8+
BinaryComparisonNodeGen SourceSection: [0,7]`h == -1`
9+
LookupAndCallBinaryNodeGen SourceSection: None
10+
Op: __eq__
11+
ReadNameNodeGen SourceSection: [0,1]`h`
12+
Identifier: h
13+
IntegerLiteralNode SourceSection: [5,7]`-1`
14+
Value: -1

0 commit comments

Comments
 (0)