@@ -151,21 +151,18 @@ def double(x):
151151 self .assertEqual (counts ['double' ], 4 )
152152
153153 def test_errors (self ):
154- # Test syntax restrictions - these are all compile-time errors:
155- #
156- for expr in [ "1+2" , "x[3]" , "(1, 2)" ]:
157- # Sanity check: is expr is a valid expression by itself?
158- compile (expr , "testexpr" , "exec" )
159-
160- codestr = "@%s\n def f(): pass" % expr
161- self .assertRaises (SyntaxError , compile , codestr , "test" , "exec" )
162154
163- # You can't put multiple decorators on a single line:
164- #
165- self .assertRaises (SyntaxError , compile ,
166- "@f1 @f2\n def f(): pass" , "test" , "exec" )
155+ # Test SyntaxErrors:
156+ for stmt in ("x," , "x, y" , "x = y" , "pass" , "import sys" ):
157+ compile (stmt , "test" , "exec" ) # Sanity check.
158+ with self .assertRaises (SyntaxError ):
159+ compile (f"@{ stmt } \n def f(): pass" , "test" , "exec" )
167160
168- # Test runtime errors
161+ # Test TypeErrors that used to be SyntaxErrors:
162+ for expr in ("1.+2j" , "[1, 2][-1]" , "(1, 2)" , "True" , "..." , "None" ):
163+ compile (expr , "test" , "eval" ) # Sanity check.
164+ with self .assertRaises (TypeError ):
165+ exec (f"@{ expr } \n def f(): pass" )
169166
170167 def unimp (func ):
171168 raise NotImplementedError
@@ -179,6 +176,13 @@ def unimp(func):
179176 code = compile (codestr , "test" , "exec" )
180177 self .assertRaises (exc , eval , code , context )
181178
179+ def test_expressions (self ):
180+ for expr in (
181+ "(x,)" , "(x, y)" , "x := y" , "(x := y)" , "x @y" , "(x @ y)" , "x[0]" ,
182+ "w[x].y.z" , "w + x - (y + z)" , "x(y)()(z)" , "[w, x, y][z]" , "x.y" ,
183+ ):
184+ compile (f"@{ expr } \n def f(): pass" , "test" , "exec" )
185+
182186 def test_double (self ):
183187 class C (object ):
184188 @funcattrs (abc = 1 , xyz = "haha" )
0 commit comments