summaryrefslogtreecommitdiff
path: root/Lib/test/test_syntax.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/test/test_syntax.py')
-rw-r--r--Lib/test/test_syntax.py152
1 files changed, 105 insertions, 47 deletions
diff --git a/Lib/test/test_syntax.py b/Lib/test/test_syntax.py
index 2c27bf1e52..7994fe67e7 100644
--- a/Lib/test/test_syntax.py
+++ b/Lib/test/test_syntax.py
@@ -27,15 +27,15 @@ In ast.c, syntax errors are raised by calling ast_error().
Errors from set_context():
-TODO(jhylton): "assignment to None" is inconsistent with other messages
-
>>> obj.None = 1
Traceback (most recent call last):
-SyntaxError: assignment to None (<doctest test.test_syntax[1]>, line 1)
+ File "<doctest test.test_syntax[1]>", line 1
+SyntaxError: cannot assign to None
>>> None = 1
Traceback (most recent call last):
-SyntaxError: assignment to None (<doctest test.test_syntax[2]>, line 1)
+ File "<doctest test.test_syntax[2]>", line 1
+SyntaxError: cannot assign to None
It's a syntax error to assign to the empty tuple. Why isn't it an
error to assign to the empty list? It will always raise some error at
@@ -43,35 +43,43 @@ runtime.
>>> () = 1
Traceback (most recent call last):
-SyntaxError: can't assign to () (<doctest test.test_syntax[3]>, line 1)
+ File "<doctest test.test_syntax[3]>", line 1
+SyntaxError: can't assign to ()
>>> f() = 1
Traceback (most recent call last):
-SyntaxError: can't assign to function call (<doctest test.test_syntax[4]>, line 1)
+ File "<doctest test.test_syntax[4]>", line 1
+SyntaxError: can't assign to function call
>>> del f()
Traceback (most recent call last):
-SyntaxError: can't delete function call (<doctest test.test_syntax[5]>, line 1)
+ File "<doctest test.test_syntax[5]>", line 1
+SyntaxError: can't delete function call
>>> a + 1 = 2
Traceback (most recent call last):
-SyntaxError: can't assign to operator (<doctest test.test_syntax[6]>, line 1)
+ File "<doctest test.test_syntax[6]>", line 1
+SyntaxError: can't assign to operator
>>> (x for x in x) = 1
Traceback (most recent call last):
-SyntaxError: can't assign to generator expression (<doctest test.test_syntax[7]>, line 1)
+ File "<doctest test.test_syntax[7]>", line 1
+SyntaxError: can't assign to generator expression
>>> 1 = 1
Traceback (most recent call last):
-SyntaxError: can't assign to literal (<doctest test.test_syntax[8]>, line 1)
+ File "<doctest test.test_syntax[8]>", line 1
+SyntaxError: can't assign to literal
>>> "abc" = 1
Traceback (most recent call last):
-SyntaxError: can't assign to literal (<doctest test.test_syntax[9]>, line 1)
+ File "<doctest test.test_syntax[8]>", line 1
+SyntaxError: can't assign to literal
>>> `1` = 1
Traceback (most recent call last):
-SyntaxError: can't assign to repr (<doctest test.test_syntax[10]>, line 1)
+ File "<doctest test.test_syntax[10]>", line 1
+SyntaxError: can't assign to repr
If the left-hand side of an assignment is a list or tuple, an illegal
expression inside that contain should still cause a syntax error.
@@ -80,22 +88,26 @@ them.
>>> (a, "b", c) = (1, 2, 3)
Traceback (most recent call last):
-SyntaxError: can't assign to literal (<doctest test.test_syntax[11]>, line 1)
+ File "<doctest test.test_syntax[11]>", line 1
+SyntaxError: can't assign to literal
>>> [a, b, c + 1] = [1, 2, 3]
Traceback (most recent call last):
-SyntaxError: can't assign to operator (<doctest test.test_syntax[12]>, line 1)
+ File "<doctest test.test_syntax[12]>", line 1
+SyntaxError: can't assign to operator
>>> a if 1 else b = 1
Traceback (most recent call last):
-SyntaxError: can't assign to conditional expression (<doctest test.test_syntax[13]>, line 1)
+ File "<doctest test.test_syntax[13]>", line 1
+SyntaxError: can't assign to conditional expression
From compiler_complex_args():
>>> def f(None=1):
... pass
Traceback (most recent call last):
-SyntaxError: assignment to None (<doctest test.test_syntax[14]>, line 1)
+ File "<doctest test.test_syntax[14]>", line 1
+SyntaxError: cannot assign to None
From ast_for_arguments():
@@ -103,22 +115,26 @@ From ast_for_arguments():
>>> def f(x, y=1, z):
... pass
Traceback (most recent call last):
-SyntaxError: non-default argument follows default argument (<doctest test.test_syntax[15]>, line 1)
+ File "<doctest test.test_syntax[15]>", line 1
+SyntaxError: non-default argument follows default argument
>>> def f(x, None):
... pass
Traceback (most recent call last):
-SyntaxError: assignment to None (<doctest test.test_syntax[16]>, line 1)
+ File "<doctest test.test_syntax[16]>", line 1
+SyntaxError: cannot assign to None
>>> def f(*None):
... pass
Traceback (most recent call last):
-SyntaxError: assignment to None (<doctest test.test_syntax[17]>, line 1)
+ File "<doctest test.test_syntax[17]>", line 1
+SyntaxError: cannot assign to None
>>> def f(**None):
... pass
Traceback (most recent call last):
-SyntaxError: assignment to None (<doctest test.test_syntax[18]>, line 1)
+ File "<doctest test.test_syntax[18]>", line 1
+SyntaxError: cannot assign to None
From ast_for_funcdef():
@@ -126,7 +142,8 @@ From ast_for_funcdef():
>>> def None(x):
... pass
Traceback (most recent call last):
-SyntaxError: assignment to None (<doctest test.test_syntax[19]>, line 1)
+ File "<doctest test.test_syntax[19]>", line 1
+SyntaxError: cannot assign to None
From ast_for_call():
@@ -138,7 +155,8 @@ From ast_for_call():
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> f(x for x in L, 1)
Traceback (most recent call last):
-SyntaxError: Generator expression must be parenthesized if not sole argument (<doctest test.test_syntax[23]>, line 1)
+ File "<doctest test.test_syntax[23]>", line 1
+SyntaxError: Generator expression must be parenthesized if not sole argument
>>> f((x for x in L), 1)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
@@ -170,7 +188,8 @@ SyntaxError: Generator expression must be parenthesized if not sole argument (<d
... i244, i245, i246, i247, i248, i249, i250, i251, i252,
... i253, i254, i255)
Traceback (most recent call last):
-SyntaxError: more than 255 arguments (<doctest test.test_syntax[25]>, line 1)
+ File "<doctest test.test_syntax[25]>", line 1
+SyntaxError: more than 255 arguments
The actual error cases counts positional arguments, keyword arguments,
and generator expression arguments separately. This test combines the
@@ -204,42 +223,50 @@ three.
... (x for x in i244), i245, i246, i247, i248, i249, i250, i251,
... i252=1, i253=1, i254=1, i255=1)
Traceback (most recent call last):
-SyntaxError: more than 255 arguments (<doctest test.test_syntax[26]>, line 1)
+ File "<doctest test.test_syntax[26]>", line 1
+SyntaxError: more than 255 arguments
>>> f(lambda x: x[0] = 3)
Traceback (most recent call last):
-SyntaxError: lambda cannot contain assignment (<doctest test.test_syntax[27]>, line 1)
+ File "<doctest test.test_syntax[27]>", line 1
+SyntaxError: lambda cannot contain assignment
The grammar accepts any test (basically, any expression) in the
keyword slot of a call site. Test a few different options.
>>> f(x()=2)
Traceback (most recent call last):
-SyntaxError: keyword can't be an expression (<doctest test.test_syntax[28]>, line 1)
+ File "<doctest test.test_syntax[28]>", line 1
+SyntaxError: keyword can't be an expression
>>> f(a or b=1)
Traceback (most recent call last):
-SyntaxError: keyword can't be an expression (<doctest test.test_syntax[29]>, line 1)
+ File "<doctest test.test_syntax[29]>", line 1
+SyntaxError: keyword can't be an expression
>>> f(x.y=1)
Traceback (most recent call last):
-SyntaxError: keyword can't be an expression (<doctest test.test_syntax[30]>, line 1)
+ File "<doctest test.test_syntax[30]>", line 1
+SyntaxError: keyword can't be an expression
-From ast_for_expr_stmt():
+More set_context():
>>> (x for x in x) += 1
Traceback (most recent call last):
-SyntaxError: augmented assignment to generator expression not possible (<doctest test.test_syntax[31]>, line 1)
+ File "<doctest test.test_syntax[31]>", line 1
+SyntaxError: can't assign to generator expression
>>> None += 1
Traceback (most recent call last):
-SyntaxError: assignment to None (<doctest test.test_syntax[32]>, line 1)
+ File "<doctest test.test_syntax[32]>", line 1
+SyntaxError: cannot assign to None
>>> f() += 1
Traceback (most recent call last):
-SyntaxError: illegal expression for augmented assignment (<doctest test.test_syntax[33]>, line 1)
+ File "<doctest test.test_syntax[33]>", line 1
+SyntaxError: can't assign to function call
Test continue in finally in weird combinations.
-continue in for loop under finally shouuld be ok.
+continue in for loop under finally should be ok.
>>> def test():
... try:
@@ -261,7 +288,8 @@ Start simple, a continue in a finally should not be allowed.
... continue
Traceback (most recent call last):
...
- SyntaxError: 'continue' not supported inside 'finally' clause (<doctest test.test_syntax[36]>, line 6)
+ File "<doctest test.test_syntax[36]>", line 6
+ SyntaxError: 'continue' not supported inside 'finally' clause
This is essentially a continue in a finally which should not be allowed.
@@ -276,7 +304,8 @@ This is essentially a continue in a finally which should not be allowed.
... pass
Traceback (most recent call last):
...
- SyntaxError: 'continue' not supported inside 'finally' clause (<doctest test.test_syntax[37]>, line 7)
+ File "<doctest test.test_syntax[37]>", line 6
+ SyntaxError: 'continue' not supported inside 'finally' clause
>>> def foo():
... try:
@@ -285,7 +314,8 @@ This is essentially a continue in a finally which should not be allowed.
... continue
Traceback (most recent call last):
...
- SyntaxError: 'continue' not supported inside 'finally' clause (<doctest test.test_syntax[38]>, line 5)
+ File "<doctest test.test_syntax[38]>", line 5
+ SyntaxError: 'continue' not supported inside 'finally' clause
>>> def foo():
... for a in ():
@@ -295,7 +325,8 @@ This is essentially a continue in a finally which should not be allowed.
... continue
Traceback (most recent call last):
...
- SyntaxError: 'continue' not supported inside 'finally' clause (<doctest test.test_syntax[39]>, line 6)
+ File "<doctest test.test_syntax[39]>", line 6
+ SyntaxError: 'continue' not supported inside 'finally' clause
>>> def foo():
... for a in ():
@@ -308,7 +339,8 @@ This is essentially a continue in a finally which should not be allowed.
... pass
Traceback (most recent call last):
...
- SyntaxError: 'continue' not supported inside 'finally' clause (<doctest test.test_syntax[40]>, line 7)
+ File "<doctest test.test_syntax[40]>", line 7
+ SyntaxError: 'continue' not supported inside 'finally' clause
>>> def foo():
... for a in ():
@@ -320,7 +352,8 @@ This is essentially a continue in a finally which should not be allowed.
... continue
Traceback (most recent call last):
...
- SyntaxError: 'continue' not supported inside 'finally' clause (<doctest test.test_syntax[41]>, line 8)
+ File "<doctest test.test_syntax[41]>", line 8
+ SyntaxError: 'continue' not supported inside 'finally' clause
There is one test for a break that is not in a loop. The compiler
uses a single data structure to keep track of try-finally and loops,
@@ -335,7 +368,8 @@ isn't, there should be a syntax error.
... print 3
Traceback (most recent call last):
...
- SyntaxError: 'break' outside loop (<doctest test.test_syntax[42]>, line 3)
+ File "<doctest test.test_syntax[42]>", line 3
+ SyntaxError: 'break' outside loop
This should probably raise a better error than a SystemError (or none at all).
In 2.5 there was a missing exception and an assert was triggered in a debug
@@ -377,7 +411,8 @@ leading to spurious errors.
... pass
Traceback (most recent call last):
...
- SyntaxError: can't assign to function call (<doctest test.test_syntax[44]>, line 2)
+ File "<doctest test.test_syntax[44]>", line 2
+ SyntaxError: can't assign to function call
>>> if 1:
... pass
@@ -385,7 +420,8 @@ leading to spurious errors.
... x() = 1
Traceback (most recent call last):
...
- SyntaxError: can't assign to function call (<doctest test.test_syntax[45]>, line 4)
+ File "<doctest test.test_syntax[45]>", line 4
+ SyntaxError: can't assign to function call
>>> if 1:
... x() = 1
@@ -395,7 +431,8 @@ leading to spurious errors.
... pass
Traceback (most recent call last):
...
- SyntaxError: can't assign to function call (<doctest test.test_syntax[46]>, line 2)
+ File "<doctest test.test_syntax[46]>", line 2
+ SyntaxError: can't assign to function call
>>> if 1:
... pass
@@ -405,7 +442,8 @@ leading to spurious errors.
... pass
Traceback (most recent call last):
...
- SyntaxError: can't assign to function call (<doctest test.test_syntax[47]>, line 4)
+ File "<doctest test.test_syntax[47]>", line 4
+ SyntaxError: can't assign to function call
>>> if 1:
... pass
@@ -415,12 +453,32 @@ leading to spurious errors.
... x() = 1
Traceback (most recent call last):
...
- SyntaxError: can't assign to function call (<doctest test.test_syntax[48]>, line 6)
+ File "<doctest test.test_syntax[48]>", line 6
+ SyntaxError: can't assign to function call
>>> f(a=23, a=234)
Traceback (most recent call last):
...
-SyntaxError: keyword argument repeated (<doctest test.test_syntax[49]>, line 1)
+ File "<doctest test.test_syntax[49]>", line 1
+SyntaxError: keyword argument repeated
+
+>>> del ()
+Traceback (most recent call last):
+ ...
+ File "<doctest test.test_syntax[50]>", line 1
+SyntaxError: can't delete ()
+
+>>> {1, 2, 3} = 42
+Traceback (most recent call last):
+ ...
+ File "<doctest test.test_syntax[50]>", line 1
+SyntaxError: can't assign to literal
+
+Corner-case that used to crash:
+
+ >>> def f(*xx, **__debug__): pass
+ Traceback (most recent call last):
+ SyntaxError: cannot assign to __debug__
"""
@@ -506,7 +564,7 @@ class SyntaxTestCase(unittest.TestCase):
def test_main():
test_support.run_unittest(SyntaxTestCase)
from test import test_syntax
- with test_support._check_py3k_warnings(("backquote not supported",
+ with test_support.check_py3k_warnings(("backquote not supported",
SyntaxWarning)):
test_support.run_doctest(test_syntax, verbosity=True)