Skip to content

Commit c3dd538

Browse files
committed
correctly raise SyntaxError when trying to assign to None
1 parent 7827c29 commit c3dd538

File tree

8 files changed

+1394
-1377
lines changed

8 files changed

+1394
-1377
lines changed

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

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -167,36 +167,6 @@ 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-
}
199-
200170
@Test
201171
public void nonLocal01() throws Exception {
202172
checkSyntaxErrorMessage(

graalpython/com.oracle.graal.python.test/src/tests/test_parser.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,32 @@ def test_annotation_scope():
127127
def foo(object: object):
128128
pass
129129
assert foo.__annotations__['object'] == object
130+
131+
132+
def test_cannot_assign():
133+
def assert_raise(s, msg):
134+
try:
135+
compile("%s = 1" % s, "", "single")
136+
except SyntaxError as e:
137+
assert msg in str(e), str(e)
138+
else:
139+
assert False
140+
try:
141+
compile("with foo as %s:\n pass" % s, "", "single")
142+
except SyntaxError as e:
143+
assert msg in str(e), str(e)
144+
else:
145+
assert False
146+
assert_raise("None", "cannot assign to None")
147+
assert_raise("1", "cannot assign to literal")
148+
assert_raise("1.1", "cannot assign to literal")
149+
assert_raise("{1}", "cannot assign to set display")
150+
assert_raise("{1: 2}", "cannot assign to dict display")
151+
assert_raise("1.2j", "cannot assign to literal")
152+
assert_raise("...", "cannot assign to Ellipsis")
153+
assert_raise("True", "cannot assign to True")
154+
assert_raise("False", "cannot assign to False")
155+
assert_raise("b''", "cannot assign to literal")
156+
assert_raise("''", "cannot assign to literal")
157+
assert_raise("f''", "cannot assign to f-string expression")
158+
assert_raise("(None,)", "cannot assign to None")

graalpython/com.oracle.graal.python.test/src/tests/unittest_tags/test_with.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,6 @@
4444
*NonexceptionalTestCase.testInlineGeneratorSyntax
4545
*NonexceptionalTestCase.testNestedSingleStatements
4646
*NonexceptionalTestCase.testUnboundGenerator
47+
testAssignmentToNoneError
48+
testAssignmentToTupleOnlyContainingNoneError
49+
testAssignmentToTupleContainingNoneError

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
@@ -330,7 +330,7 @@ locals
330330
(
331331
NEWLINE
332332
| simple_stmt
333-
| compound_stmt NEWLINE
333+
| compound_stmt
334334
)
335335
{ $result = new BlockSSTNode(getArray(start, SSTNode[].class), getStartIndex($ctx), getLastIndex($ctx)); }
336336
{

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

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)