Skip to content

Commit 469090a

Browse files
committed
[GR-27247] Wrong Syntax Error message, when augmenting assignment is called after a binary operation.
PullRequest: graalpython/1379
2 parents 7d331c6 + 3c2c2fc commit 469090a

File tree

11 files changed

+331
-204
lines changed

11 files changed

+331
-204
lines changed

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

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -193,29 +193,4 @@ public void annotationType02() throws Exception {
193193
public void annotationType03() throws Exception {
194194
checkTreeResult("j = 1\n" + "ahoj.__annotations__['j'] = float");
195195
}
196-
197-
@Test
198-
public void annotationType04() throws Exception {
199-
checkSyntaxErrorMessage("lambda: x:x", "SyntaxError: illegal target for annotation");
200-
}
201-
202-
@Test
203-
public void annotationType05() throws Exception {
204-
checkSyntaxErrorMessage("{}: int", "SyntaxError: illegal target for annotation");
205-
}
206-
207-
@Test
208-
public void annotationType06() throws Exception {
209-
checkSyntaxErrorMessage("(1,2): int", "SyntaxError: only single target (not tuple) can be annotated");
210-
}
211-
212-
@Test
213-
public void annotationType07() throws Exception {
214-
checkSyntaxErrorMessage("[1,2]: int", "SyntaxError: only single target (not list) can be annotated");
215-
}
216-
217-
@Test
218-
public void annotationType08() throws Exception {
219-
checkSyntaxErrorMessage("list(): int", "SyntaxError: illegal target for annotation");
220-
}
221196
}

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

Lines changed: 113 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -129,41 +129,120 @@ def foo(object: object):
129129
assert foo.__annotations__['object'] == object
130130

131131

132+
import sys
133+
134+
def assert_raise_syntax_error(source, msg):
135+
try:
136+
compile(source, "", "single")
137+
except SyntaxError as e:
138+
assert msg in str(e), "\nCode:\n----\n%s\n----\n Expected message: %s\n Actual message: %s" % (source, msg, str(e))
139+
else:
140+
assert False , "Syntax Error was not raised.\nCode:\n----\n%s\n----\nhas to raise Syntax Error: %s" % (source,msg)
141+
132142
def test_cannot_assign():
133-
import sys
134143
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")
161-
144+
def check(s, msg):
145+
# test simple assignment
146+
assert_raise_syntax_error("%s = 1" % s, msg)
147+
# testing aug assignment
148+
assert_raise_syntax_error("%s += 1" % s, msg)
149+
# test with statement
150+
assert_raise_syntax_error("with foo as %s:\n pass" % s, msg)
151+
# test for statement
152+
assert_raise_syntax_error("for %s in range(1,10):\n pass" % s, msg)
153+
# test for comprehension statement
154+
assert_raise_syntax_error("[1 for %s in range(1,10)]" % s, msg)
155+
check("1", "cannot assign to literal")
156+
check("1.1", "cannot assign to literal")
157+
check("{1}", "cannot assign to set display")
158+
check("{}", "cannot assign to dict display")
159+
check("{1: 2}", "cannot assign to dict display")
160+
check("[1,2, 3]", "cannot assign to literal")
161+
check("(1,2, 3)", "cannot assign to literal")
162+
check("1.2j", "cannot assign to literal")
163+
check("None", "cannot assign to None")
164+
check("...", "cannot assign to Ellipsis")
165+
check("True", "cannot assign to True")
166+
check("False", "cannot assign to False")
167+
check("b''", "cannot assign to literal")
168+
check("''", "cannot assign to literal")
169+
check("f''", "cannot assign to f-string expression")
170+
check("(a,None, b)", "cannot assign to None")
171+
check("(a,True, b)", "cannot assign to True")
172+
check("(a,False, b)", "cannot assign to False")
173+
check("a+b", "cannot assign to operator")
174+
check("fn()", "cannot assign to function call")
175+
check("{letter for letter in 'ahoj'}", "cannot assign to set comprehension")
176+
check("[letter for letter in 'ahoj']", "cannot assign to list comprehension")
177+
check("(letter for letter in 'ahoj')", "cannot assign to generator expression")
178+
check("obj.True", "invalid syntax")
179+
check("(a, *True, b)", "cannot assign to True")
180+
check("(a, *False, b)", "cannot assign to False")
181+
check("(a, *None, b)", "cannot assign to None")
182+
check("(a, *..., b)", "cannot assign to Ellipsis")
183+
check("__debug__", "cannot assign to __debug__")
184+
check("a.__debug__", "cannot assign to __debug__")
185+
check("a.b.__debug__", "cannot assign to __debug__")
186+
187+
def test_cannot_assign_without_with():
188+
if sys.implementation.version.minor >= 8:
189+
def check(s, msg):
190+
# test simple assignment
191+
assert_raise_syntax_error("%s = 1" % s, msg)
192+
# testing aug assignment
193+
assert_raise_syntax_error("%s += 1" % s, msg)
194+
# test for statement
195+
assert_raise_syntax_error("for %s in range(1,10):\n pass" % s, msg)
196+
# test for comprehension statement
197+
assert_raise_syntax_error("[1 for %s in range(1,10)]" % s, msg)
198+
check("*True", "cannot assign to True")
199+
check("*False", "cannot assign to False")
200+
check("*None", "cannot assign to None")
201+
check("*...", "cannot assign to Ellipsis")
202+
check("[a, b, c + 1],", "cannot assign to operator")
203+
204+
def test_cannot_assign_other():
205+
if sys.implementation.version.minor >= 8:
206+
assert_raise_syntax_error("a if 1 else b = 1", "cannot assign to conditional expression")
207+
assert_raise_syntax_error("a if 1 else b -= 1", "cannot assign to conditional expression")
208+
assert_raise_syntax_error("f(True=2)", "cannot assign to True")
209+
assert_raise_syntax_error("f(__debug__=2)", "cannot assign to __debug__")
210+
assert_raise_syntax_error("def f(__debug__): pass\n", "cannot assign to __debug__")
211+
assert_raise_syntax_error("def f(*, x=lambda __debug__:0): pass\n", "cannot assign to __debug__")
212+
assert_raise_syntax_error("def f(*args:(lambda __debug__:0)): pass\n", "cannot assign to __debug__")
213+
assert_raise_syntax_error("def f(**kwargs:(lambda __debug__:0)): pass\n", "cannot assign to __debug__")
214+
assert_raise_syntax_error("def f(**__debug__): pass\n", "cannot assign to __debug__")
215+
assert_raise_syntax_error("def f(*xx, __debug__): pass\n", "cannot assign to __debug__")
216+
217+
def test_invalid_assignmetn_to_yield_expression():
218+
if sys.implementation.version.minor >= 8:
219+
assert_raise_syntax_error("def fn():\n (yield 10) = 1", "cannot assign to yield expression")
220+
assert_raise_syntax_error("def fn():\n (yield 10) += 1", "cannot assign to yield expression")
221+
assert_raise_syntax_error("def fn():\n with foo as (yield 10) : pass", "cannot assign to yield expression")
222+
assert_raise_syntax_error("def fn():\n for (yield 10) in range(1,10): pass", "cannot assign to yield expression")
162223

163224
def test_invalid_ann_assignment():
164-
try:
165-
exec("lambda: x:x")
166-
except SyntaxError as e:
167-
assert e.msg == 'illegal target for annotation'
168-
return
169-
assert False, "SyntaxError not raised"
225+
if sys.implementation.version.minor >= 8:
226+
assert_raise_syntax_error("lambda: x:x", 'illegal target for annotation')
227+
assert_raise_syntax_error("list(): int", "illegal target for annotation")
228+
assert_raise_syntax_error("{}: int", "illegal target for annotation")
229+
assert_raise_syntax_error("{1,2}: int", "illegal target for annotation")
230+
assert_raise_syntax_error("{'1':'2'}: int", "illegal target for annotation")
231+
assert_raise_syntax_error("[1,2]: int", "only single target (not list) can be annotated")
232+
assert_raise_syntax_error("(1,2): int", "only single target (not tuple) can be annotated")
233+
234+
def test_invalid_aug_assignment():
235+
if sys.implementation.version.minor >= 8:
236+
assert_raise_syntax_error("[] += 1", 'illegal expression for augmented assignment')
237+
assert_raise_syntax_error("() += 1", 'illegal expression for augmented assignment')
238+
assert_raise_syntax_error("x, y += (1, 2)", 'illegal expression for augmented assignment')
239+
assert_raise_syntax_error("x, y += 1", 'illegal expression for augmented assignment')
240+
241+
def test_invalid_star_import():
242+
assert_raise_syntax_error("def f(): from bla import *\n", 'import * only allowed at module level')
243+
244+
def test_invalid_return_statement():
245+
assert_raise_syntax_error("return 10", "'return' outside function")
246+
assert_raise_syntax_error("class A: return 10\n", "'return' outside function")
247+
248+

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/ErrorMessages.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ public abstract class ErrorMessages {
274274
public static final String ILLEGAL_EXPRESSION_FOR_AUGMENTED_ASSIGNEMNT = "illegal expression for augmented assignment";
275275
public static final String ILLEGAL_IP_STRING_PASSED_TO = "illegal IP address string passed to %s";
276276
public static final String ILLEGAL_SOCKET_ADDR_ARG = "%s: illegal sockaddr argument";
277-
public static final String IMPORT_START_ONLY_ALLOWED_AT_MODULE_LEVEL = "import * only allowed at module level";
277+
public static final String IMPORT_STAR_ONLY_ALLOWED_AT_MODULE_LEVEL = "import * only allowed at module level";
278278
public static final String INCOMPLETE_FORMAT = "incomplete format";
279279
public static final String INDEX_NOT_INT = "%s: index not int";
280280
public static final String INDEX_OUT_OF_BOUNDS = "index out of bounds";

0 commit comments

Comments
 (0)