Skip to content

Commit 14389ef

Browse files
committed
[GR-23271] Correctly raise SyntaxError when trying to assign to None
PullRequest: graalpython/963
2 parents 0b23191 + 9793cc2 commit 14389ef

File tree

8 files changed

+1397
-1378
lines changed

8 files changed

+1397
-1378
lines changed

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

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -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: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,34 @@ 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+
import sys
134+
if sys.implementation.version.minor >= 8:
135+
def assert_raise(s, msg):
136+
try:
137+
compile("%s = 1" % s, "", "single")
138+
except SyntaxError as e:
139+
assert msg in str(e), str(e)
140+
else:
141+
assert False
142+
try:
143+
compile("with foo as %s:\n pass" % s, "", "single")
144+
except SyntaxError as e:
145+
assert msg in str(e), str(e)
146+
else:
147+
assert False
148+
assert_raise("None", "cannot assign to None")
149+
assert_raise("1", "cannot assign to literal")
150+
assert_raise("1.1", "cannot assign to literal")
151+
assert_raise("{1}", "cannot assign to set display")
152+
assert_raise("{1: 2}", "cannot assign to dict display")
153+
assert_raise("1.2j", "cannot assign to literal")
154+
assert_raise("...", "cannot assign to Ellipsis")
155+
assert_raise("True", "cannot assign to True")
156+
assert_raise("False", "cannot assign to False")
157+
assert_raise("b''", "cannot assign to literal")
158+
assert_raise("''", "cannot assign to literal")
159+
assert_raise("f''", "cannot assign to f-string expression")
160+
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)