Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1 | """This module tests SyntaxErrors. |
| 2 | |
| 3 | Here's an example of the sort of thing that is tested. |
| 4 | |
| 5 | >>> def f(x): |
| 6 | ... global x |
| 7 | Traceback (most recent call last): |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 8 | SyntaxError: name 'x' is parameter and global |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 9 | |
| 10 | The tests are all raise SyntaxErrors. They were created by checking |
| 11 | each C call that raises SyntaxError. There are several modules that |
| 12 | raise these exceptions-- ast.c, compile.c, future.c, pythonrun.c, and |
| 13 | symtable.c. |
| 14 | |
| 15 | The parser itself outlaws a lot of invalid syntax. None of these |
| 16 | errors are tested here at the moment. We should add some tests; since |
| 17 | there are infinitely many programs with invalid syntax, we would need |
| 18 | to be judicious in selecting some. |
| 19 | |
| 20 | The compiler generates a synthetic module name for code executed by |
| 21 | doctest. Since all the code comes from the same module, a suffix like |
| 22 | [1] is appended to the module name, As a consequence, changing the |
| 23 | order of tests in this module means renumbering all the errors after |
| 24 | it. (Maybe we should enable the ellipsis option for these tests.) |
| 25 | |
| 26 | In ast.c, syntax errors are raised by calling ast_error(). |
| 27 | |
| 28 | Errors from set_context(): |
| 29 | |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 30 | >>> obj.None = 1 |
| 31 | Traceback (most recent call last): |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 32 | SyntaxError: invalid syntax |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 33 | |
| 34 | >>> None = 1 |
| 35 | Traceback (most recent call last): |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 36 | SyntaxError: cannot assign to None |
| 37 | |
| 38 | >>> obj.True = 1 |
| 39 | Traceback (most recent call last): |
| 40 | SyntaxError: invalid syntax |
| 41 | |
| 42 | >>> True = 1 |
| 43 | Traceback (most recent call last): |
| 44 | SyntaxError: cannot assign to True |
| 45 | |
Serhiy Storchaka | d8b3a98 | 2019-03-05 20:42:06 +0200 | [diff] [blame] | 46 | >>> (True := 1) |
| 47 | Traceback (most recent call last): |
Ned Batchelder | 37143a8 | 2019-12-31 21:40:58 -0500 | [diff] [blame] | 48 | SyntaxError: cannot use assignment expressions with True |
Serhiy Storchaka | d8b3a98 | 2019-03-05 20:42:06 +0200 | [diff] [blame] | 49 | |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 50 | >>> obj.__debug__ = 1 |
| 51 | Traceback (most recent call last): |
| 52 | SyntaxError: cannot assign to __debug__ |
| 53 | |
| 54 | >>> __debug__ = 1 |
| 55 | Traceback (most recent call last): |
| 56 | SyntaxError: cannot assign to __debug__ |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 57 | |
Stéphane Wirtel | 3ad9167 | 2019-02-21 11:11:53 +0100 | [diff] [blame] | 58 | >>> (__debug__ := 1) |
| 59 | Traceback (most recent call last): |
| 60 | SyntaxError: cannot assign to __debug__ |
| 61 | |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 62 | >>> f() = 1 |
| 63 | Traceback (most recent call last): |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 64 | SyntaxError: cannot assign to function call |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 65 | |
Shantanu | 27c0d9b | 2020-05-11 14:53:58 -0700 | [diff] [blame] | 66 | >>> del f() |
| 67 | Traceback (most recent call last): |
| 68 | SyntaxError: cannot delete function call |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 69 | |
| 70 | >>> a + 1 = 2 |
| 71 | Traceback (most recent call last): |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 72 | SyntaxError: cannot assign to operator |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 73 | |
| 74 | >>> (x for x in x) = 1 |
| 75 | Traceback (most recent call last): |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 76 | SyntaxError: cannot assign to generator expression |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 77 | |
| 78 | >>> 1 = 1 |
| 79 | Traceback (most recent call last): |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 80 | SyntaxError: cannot assign to literal |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 81 | |
| 82 | >>> "abc" = 1 |
| 83 | Traceback (most recent call last): |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 84 | SyntaxError: cannot assign to literal |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 85 | |
Benjamin Peterson | bd3e362 | 2011-04-12 18:33:28 -0500 | [diff] [blame] | 86 | >>> b"" = 1 |
| 87 | Traceback (most recent call last): |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 88 | SyntaxError: cannot assign to literal |
| 89 | |
| 90 | >>> ... = 1 |
| 91 | Traceback (most recent call last): |
| 92 | SyntaxError: cannot assign to Ellipsis |
Benjamin Peterson | bd3e362 | 2011-04-12 18:33:28 -0500 | [diff] [blame] | 93 | |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 94 | >>> `1` = 1 |
| 95 | Traceback (most recent call last): |
Brett Cannon | 8933cb4 | 2006-08-25 04:12:10 +0000 | [diff] [blame] | 96 | SyntaxError: invalid syntax |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 97 | |
| 98 | If the left-hand side of an assignment is a list or tuple, an illegal |
| 99 | expression inside that contain should still cause a syntax error. |
| 100 | This test just checks a couple of cases rather than enumerating all of |
| 101 | them. |
| 102 | |
Pablo Galindo | 16ab070 | 2020-05-15 02:04:52 +0100 | [diff] [blame] | 103 | >>> (a, "b", c) = (1, 2, 3) |
| 104 | Traceback (most recent call last): |
| 105 | SyntaxError: cannot assign to literal |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 106 | |
Pablo Galindo | 16ab070 | 2020-05-15 02:04:52 +0100 | [diff] [blame] | 107 | >>> (a, True, c) = (1, 2, 3) |
| 108 | Traceback (most recent call last): |
| 109 | SyntaxError: cannot assign to True |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 110 | |
| 111 | >>> (a, __debug__, c) = (1, 2, 3) |
| 112 | Traceback (most recent call last): |
| 113 | SyntaxError: cannot assign to __debug__ |
| 114 | |
Pablo Galindo | 16ab070 | 2020-05-15 02:04:52 +0100 | [diff] [blame] | 115 | >>> (a, *True, c) = (1, 2, 3) |
| 116 | Traceback (most recent call last): |
| 117 | SyntaxError: cannot assign to True |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 118 | |
| 119 | >>> (a, *__debug__, c) = (1, 2, 3) |
| 120 | Traceback (most recent call last): |
| 121 | SyntaxError: cannot assign to __debug__ |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 122 | |
Pablo Galindo | 16ab070 | 2020-05-15 02:04:52 +0100 | [diff] [blame] | 123 | >>> [a, b, c + 1] = [1, 2, 3] |
| 124 | Traceback (most recent call last): |
| 125 | SyntaxError: cannot assign to operator |
| 126 | |
| 127 | >>> [a, b[1], c + 1] = [1, 2, 3] |
| 128 | Traceback (most recent call last): |
| 129 | SyntaxError: cannot assign to operator |
| 130 | |
| 131 | >>> [a, b.c.d, c + 1] = [1, 2, 3] |
| 132 | Traceback (most recent call last): |
| 133 | SyntaxError: cannot assign to operator |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 134 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 135 | >>> a if 1 else b = 1 |
| 136 | Traceback (most recent call last): |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 137 | SyntaxError: cannot assign to conditional expression |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 138 | |
Miss Islington (bot) | 8df4f39 | 2020-06-08 02:22:06 -0700 | [diff] [blame] | 139 | >>> True = True = 3 |
| 140 | Traceback (most recent call last): |
| 141 | SyntaxError: cannot assign to True |
| 142 | |
| 143 | >>> x = y = True = z = 3 |
| 144 | Traceback (most recent call last): |
| 145 | SyntaxError: cannot assign to True |
| 146 | |
| 147 | >>> x = y = yield = 1 |
| 148 | Traceback (most recent call last): |
| 149 | SyntaxError: assignment to yield expression not possible |
| 150 | |
Lysandros Nikolaou | ce21cfc | 2020-05-14 23:13:50 +0300 | [diff] [blame] | 151 | >>> a, b += 1, 2 |
| 152 | Traceback (most recent call last): |
Pablo Galindo | 16ab070 | 2020-05-15 02:04:52 +0100 | [diff] [blame] | 153 | SyntaxError: 'tuple' is an illegal expression for augmented assignment |
Lysandros Nikolaou | ce21cfc | 2020-05-14 23:13:50 +0300 | [diff] [blame] | 154 | |
| 155 | >>> (a, b) += 1, 2 |
| 156 | Traceback (most recent call last): |
Pablo Galindo | 16ab070 | 2020-05-15 02:04:52 +0100 | [diff] [blame] | 157 | SyntaxError: 'tuple' is an illegal expression for augmented assignment |
Lysandros Nikolaou | ce21cfc | 2020-05-14 23:13:50 +0300 | [diff] [blame] | 158 | |
| 159 | >>> [a, b] += 1, 2 |
| 160 | Traceback (most recent call last): |
Pablo Galindo | 16ab070 | 2020-05-15 02:04:52 +0100 | [diff] [blame] | 161 | SyntaxError: 'list' is an illegal expression for augmented assignment |
Lysandros Nikolaou | ce21cfc | 2020-05-14 23:13:50 +0300 | [diff] [blame] | 162 | |
Lysandros Nikolaou | a5442b2 | 2020-06-19 03:03:58 +0300 | [diff] [blame^] | 163 | Invalid targets in `for` loops and `with` statements should also |
| 164 | produce a specialized error message |
| 165 | |
| 166 | >>> for a() in b: pass |
| 167 | Traceback (most recent call last): |
| 168 | SyntaxError: cannot assign to function call |
| 169 | |
| 170 | >>> for (a, b()) in b: pass |
| 171 | Traceback (most recent call last): |
| 172 | SyntaxError: cannot assign to function call |
| 173 | |
| 174 | >>> for [a, b()] in b: pass |
| 175 | Traceback (most recent call last): |
| 176 | SyntaxError: cannot assign to function call |
| 177 | |
| 178 | >>> for (*a, b, c+1) in b: pass |
| 179 | Traceback (most recent call last): |
| 180 | SyntaxError: cannot assign to operator |
| 181 | |
| 182 | >>> for (x, *(y, z.d())) in b: pass |
| 183 | Traceback (most recent call last): |
| 184 | SyntaxError: cannot assign to function call |
| 185 | |
| 186 | >>> for a, b() in c: pass |
| 187 | Traceback (most recent call last): |
| 188 | SyntaxError: cannot assign to function call |
| 189 | |
| 190 | >>> for i < (): pass |
| 191 | Traceback (most recent call last): |
| 192 | SyntaxError: invalid syntax |
| 193 | |
| 194 | >>> with a as b(): pass |
| 195 | Traceback (most recent call last): |
| 196 | SyntaxError: cannot assign to function call |
| 197 | |
| 198 | >>> with a as (b, c()): pass |
| 199 | Traceback (most recent call last): |
| 200 | SyntaxError: cannot assign to function call |
| 201 | |
| 202 | >>> with a as [b, c()]: pass |
| 203 | Traceback (most recent call last): |
| 204 | SyntaxError: cannot assign to function call |
| 205 | |
| 206 | >>> with a as (*b, c, d+1): pass |
| 207 | Traceback (most recent call last): |
| 208 | SyntaxError: cannot assign to operator |
| 209 | |
| 210 | >>> with a as (x, *(y, z.d())): pass |
| 211 | Traceback (most recent call last): |
| 212 | SyntaxError: cannot assign to function call |
| 213 | |
| 214 | >>> with a as b, c as d(): pass |
| 215 | Traceback (most recent call last): |
| 216 | SyntaxError: cannot assign to function call |
| 217 | |
Miss Islington (bot) | 8df4f39 | 2020-06-08 02:22:06 -0700 | [diff] [blame] | 218 | >>> p = p = |
| 219 | Traceback (most recent call last): |
| 220 | SyntaxError: invalid syntax |
| 221 | |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 222 | From compiler_complex_args(): |
| 223 | |
| 224 | >>> def f(None=1): |
| 225 | ... pass |
| 226 | Traceback (most recent call last): |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 227 | SyntaxError: invalid syntax |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 228 | |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 229 | From ast_for_arguments(): |
| 230 | |
| 231 | >>> def f(x, y=1, z): |
| 232 | ... pass |
| 233 | Traceback (most recent call last): |
Guido van Rossum | 33d2689 | 2007-08-05 15:29:28 +0000 | [diff] [blame] | 234 | SyntaxError: non-default argument follows default argument |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 235 | |
| 236 | >>> def f(x, None): |
| 237 | ... pass |
| 238 | Traceback (most recent call last): |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 239 | SyntaxError: invalid syntax |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 240 | |
| 241 | >>> def f(*None): |
| 242 | ... pass |
| 243 | Traceback (most recent call last): |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 244 | SyntaxError: invalid syntax |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 245 | |
| 246 | >>> def f(**None): |
| 247 | ... pass |
| 248 | Traceback (most recent call last): |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 249 | SyntaxError: invalid syntax |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 250 | |
Lysandros Nikolaou | 75b863a | 2020-05-18 22:14:47 +0300 | [diff] [blame] | 251 | >>> import ast; ast.parse(''' |
| 252 | ... def f( |
| 253 | ... *, # type: int |
| 254 | ... a, # type: int |
| 255 | ... ): |
| 256 | ... pass |
| 257 | ... ''', type_comments=True) |
| 258 | Traceback (most recent call last): |
| 259 | SyntaxError: bare * has associated type comment |
| 260 | |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 261 | |
| 262 | From ast_for_funcdef(): |
| 263 | |
| 264 | >>> def None(x): |
| 265 | ... pass |
| 266 | Traceback (most recent call last): |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 267 | SyntaxError: invalid syntax |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 268 | |
| 269 | |
| 270 | From ast_for_call(): |
| 271 | |
Serhiy Storchaka | 9165f77 | 2017-11-15 08:49:40 +0200 | [diff] [blame] | 272 | >>> def f(it, *varargs, **kwargs): |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 273 | ... return list(it) |
| 274 | >>> L = range(10) |
| 275 | >>> f(x for x in L) |
| 276 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
| 277 | >>> f(x for x in L, 1) |
| 278 | Traceback (most recent call last): |
Serhiy Storchaka | 9165f77 | 2017-11-15 08:49:40 +0200 | [diff] [blame] | 279 | SyntaxError: Generator expression must be parenthesized |
| 280 | >>> f(x for x in L, y=1) |
| 281 | Traceback (most recent call last): |
| 282 | SyntaxError: Generator expression must be parenthesized |
| 283 | >>> f(x for x in L, *[]) |
| 284 | Traceback (most recent call last): |
| 285 | SyntaxError: Generator expression must be parenthesized |
| 286 | >>> f(x for x in L, **{}) |
| 287 | Traceback (most recent call last): |
| 288 | SyntaxError: Generator expression must be parenthesized |
Miss Islington (bot) | 55c8923 | 2020-05-21 18:14:55 -0700 | [diff] [blame] | 289 | >>> f(L, x for x in L) |
| 290 | Traceback (most recent call last): |
| 291 | SyntaxError: Generator expression must be parenthesized |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 292 | >>> f(x for x in L, y for y in L) |
| 293 | Traceback (most recent call last): |
Serhiy Storchaka | 9165f77 | 2017-11-15 08:49:40 +0200 | [diff] [blame] | 294 | SyntaxError: Generator expression must be parenthesized |
| 295 | >>> f(x for x in L,) |
| 296 | Traceback (most recent call last): |
| 297 | SyntaxError: Generator expression must be parenthesized |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 298 | >>> f((x for x in L), 1) |
| 299 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
Serhiy Storchaka | ddbce13 | 2017-11-15 17:39:37 +0200 | [diff] [blame] | 300 | >>> class C(x for x in L): |
| 301 | ... pass |
| 302 | Traceback (most recent call last): |
| 303 | SyntaxError: invalid syntax |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 304 | |
Serhiy Storchaka | 214678e | 2016-11-28 10:52:05 +0200 | [diff] [blame] | 305 | >>> def g(*args, **kwargs): |
| 306 | ... print(args, sorted(kwargs.items())) |
| 307 | >>> g(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, |
| 308 | ... 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, |
| 309 | ... 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, |
| 310 | ... 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, |
| 311 | ... 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, |
| 312 | ... 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, |
| 313 | ... 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, |
| 314 | ... 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, |
| 315 | ... 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, |
| 316 | ... 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, |
| 317 | ... 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, |
| 318 | ... 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, |
| 319 | ... 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, |
| 320 | ... 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, |
| 321 | ... 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, |
| 322 | ... 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, |
| 323 | ... 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261, |
| 324 | ... 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275, |
| 325 | ... 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289, |
| 326 | ... 290, 291, 292, 293, 294, 295, 296, 297, 298, 299) # doctest: +ELLIPSIS |
| 327 | (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ..., 297, 298, 299) [] |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 328 | |
Serhiy Storchaka | 214678e | 2016-11-28 10:52:05 +0200 | [diff] [blame] | 329 | >>> g(a000=0, a001=1, a002=2, a003=3, a004=4, a005=5, a006=6, a007=7, a008=8, |
| 330 | ... a009=9, a010=10, a011=11, a012=12, a013=13, a014=14, a015=15, a016=16, |
| 331 | ... a017=17, a018=18, a019=19, a020=20, a021=21, a022=22, a023=23, a024=24, |
| 332 | ... a025=25, a026=26, a027=27, a028=28, a029=29, a030=30, a031=31, a032=32, |
| 333 | ... a033=33, a034=34, a035=35, a036=36, a037=37, a038=38, a039=39, a040=40, |
| 334 | ... a041=41, a042=42, a043=43, a044=44, a045=45, a046=46, a047=47, a048=48, |
| 335 | ... a049=49, a050=50, a051=51, a052=52, a053=53, a054=54, a055=55, a056=56, |
| 336 | ... a057=57, a058=58, a059=59, a060=60, a061=61, a062=62, a063=63, a064=64, |
| 337 | ... a065=65, a066=66, a067=67, a068=68, a069=69, a070=70, a071=71, a072=72, |
| 338 | ... a073=73, a074=74, a075=75, a076=76, a077=77, a078=78, a079=79, a080=80, |
| 339 | ... a081=81, a082=82, a083=83, a084=84, a085=85, a086=86, a087=87, a088=88, |
| 340 | ... a089=89, a090=90, a091=91, a092=92, a093=93, a094=94, a095=95, a096=96, |
| 341 | ... a097=97, a098=98, a099=99, a100=100, a101=101, a102=102, a103=103, |
| 342 | ... a104=104, a105=105, a106=106, a107=107, a108=108, a109=109, a110=110, |
| 343 | ... a111=111, a112=112, a113=113, a114=114, a115=115, a116=116, a117=117, |
| 344 | ... a118=118, a119=119, a120=120, a121=121, a122=122, a123=123, a124=124, |
| 345 | ... a125=125, a126=126, a127=127, a128=128, a129=129, a130=130, a131=131, |
| 346 | ... a132=132, a133=133, a134=134, a135=135, a136=136, a137=137, a138=138, |
| 347 | ... a139=139, a140=140, a141=141, a142=142, a143=143, a144=144, a145=145, |
| 348 | ... a146=146, a147=147, a148=148, a149=149, a150=150, a151=151, a152=152, |
| 349 | ... a153=153, a154=154, a155=155, a156=156, a157=157, a158=158, a159=159, |
| 350 | ... a160=160, a161=161, a162=162, a163=163, a164=164, a165=165, a166=166, |
| 351 | ... a167=167, a168=168, a169=169, a170=170, a171=171, a172=172, a173=173, |
| 352 | ... a174=174, a175=175, a176=176, a177=177, a178=178, a179=179, a180=180, |
| 353 | ... a181=181, a182=182, a183=183, a184=184, a185=185, a186=186, a187=187, |
| 354 | ... a188=188, a189=189, a190=190, a191=191, a192=192, a193=193, a194=194, |
| 355 | ... a195=195, a196=196, a197=197, a198=198, a199=199, a200=200, a201=201, |
| 356 | ... a202=202, a203=203, a204=204, a205=205, a206=206, a207=207, a208=208, |
| 357 | ... a209=209, a210=210, a211=211, a212=212, a213=213, a214=214, a215=215, |
| 358 | ... a216=216, a217=217, a218=218, a219=219, a220=220, a221=221, a222=222, |
| 359 | ... a223=223, a224=224, a225=225, a226=226, a227=227, a228=228, a229=229, |
| 360 | ... a230=230, a231=231, a232=232, a233=233, a234=234, a235=235, a236=236, |
| 361 | ... a237=237, a238=238, a239=239, a240=240, a241=241, a242=242, a243=243, |
| 362 | ... a244=244, a245=245, a246=246, a247=247, a248=248, a249=249, a250=250, |
| 363 | ... a251=251, a252=252, a253=253, a254=254, a255=255, a256=256, a257=257, |
| 364 | ... a258=258, a259=259, a260=260, a261=261, a262=262, a263=263, a264=264, |
| 365 | ... a265=265, a266=266, a267=267, a268=268, a269=269, a270=270, a271=271, |
| 366 | ... a272=272, a273=273, a274=274, a275=275, a276=276, a277=277, a278=278, |
| 367 | ... a279=279, a280=280, a281=281, a282=282, a283=283, a284=284, a285=285, |
| 368 | ... a286=286, a287=287, a288=288, a289=289, a290=290, a291=291, a292=292, |
| 369 | ... a293=293, a294=294, a295=295, a296=296, a297=297, a298=298, a299=299) |
| 370 | ... # doctest: +ELLIPSIS |
| 371 | () [('a000', 0), ('a001', 1), ('a002', 2), ..., ('a298', 298), ('a299', 299)] |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 372 | |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 373 | >>> class C: |
| 374 | ... def meth(self, *args): |
| 375 | ... return args |
| 376 | >>> obj = C() |
| 377 | >>> obj.meth( |
| 378 | ... 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, |
| 379 | ... 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, |
| 380 | ... 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, |
| 381 | ... 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, |
| 382 | ... 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, |
| 383 | ... 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, |
| 384 | ... 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, |
| 385 | ... 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, |
| 386 | ... 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, |
| 387 | ... 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, |
| 388 | ... 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, |
| 389 | ... 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, |
| 390 | ... 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, |
| 391 | ... 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, |
| 392 | ... 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, |
| 393 | ... 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, |
| 394 | ... 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261, |
| 395 | ... 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275, |
| 396 | ... 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289, |
| 397 | ... 290, 291, 292, 293, 294, 295, 296, 297, 298, 299) # doctest: +ELLIPSIS |
| 398 | (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ..., 297, 298, 299) |
| 399 | |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 400 | # >>> f(lambda x: x[0] = 3) |
| 401 | # Traceback (most recent call last): |
| 402 | # SyntaxError: expression cannot contain assignment, perhaps you meant "=="? |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 403 | |
| 404 | The grammar accepts any test (basically, any expression) in the |
| 405 | keyword slot of a call site. Test a few different options. |
| 406 | |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 407 | # >>> f(x()=2) |
| 408 | # Traceback (most recent call last): |
| 409 | # SyntaxError: expression cannot contain assignment, perhaps you meant "=="? |
| 410 | # >>> f(a or b=1) |
| 411 | # Traceback (most recent call last): |
| 412 | # SyntaxError: expression cannot contain assignment, perhaps you meant "=="? |
| 413 | # >>> f(x.y=1) |
| 414 | # Traceback (most recent call last): |
| 415 | # SyntaxError: expression cannot contain assignment, perhaps you meant "=="? |
| 416 | # >>> f((x)=2) |
| 417 | # Traceback (most recent call last): |
| 418 | # SyntaxError: expression cannot contain assignment, perhaps you meant "=="? |
| 419 | # >>> f(True=2) |
| 420 | # Traceback (most recent call last): |
| 421 | # SyntaxError: cannot assign to True |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 422 | >>> f(__debug__=1) |
| 423 | Traceback (most recent call last): |
| 424 | SyntaxError: cannot assign to __debug__ |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 425 | >>> __debug__: int |
| 426 | Traceback (most recent call last): |
| 427 | SyntaxError: cannot assign to __debug__ |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 428 | |
| 429 | |
Benjamin Peterson | 87c8d87 | 2009-06-11 22:54:11 +0000 | [diff] [blame] | 430 | More set_context(): |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 431 | |
| 432 | >>> (x for x in x) += 1 |
| 433 | Traceback (most recent call last): |
Pablo Galindo | 16ab070 | 2020-05-15 02:04:52 +0100 | [diff] [blame] | 434 | SyntaxError: 'generator expression' is an illegal expression for augmented assignment |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 435 | >>> None += 1 |
| 436 | Traceback (most recent call last): |
Pablo Galindo | 16ab070 | 2020-05-15 02:04:52 +0100 | [diff] [blame] | 437 | SyntaxError: 'None' is an illegal expression for augmented assignment |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 438 | >>> __debug__ += 1 |
| 439 | Traceback (most recent call last): |
| 440 | SyntaxError: cannot assign to __debug__ |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 441 | >>> f() += 1 |
| 442 | Traceback (most recent call last): |
Pablo Galindo | 16ab070 | 2020-05-15 02:04:52 +0100 | [diff] [blame] | 443 | SyntaxError: 'function call' is an illegal expression for augmented assignment |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 444 | |
| 445 | |
| 446 | Test continue in finally in weird combinations. |
| 447 | |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 448 | continue in for loop under finally should be ok. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 449 | |
| 450 | >>> def test(): |
| 451 | ... try: |
| 452 | ... pass |
| 453 | ... finally: |
| 454 | ... for abc in range(10): |
| 455 | ... continue |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 456 | ... print(abc) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 457 | >>> test() |
| 458 | 9 |
| 459 | |
Serhiy Storchaka | fe2bbb1 | 2018-03-18 09:56:52 +0200 | [diff] [blame] | 460 | continue in a finally should be ok. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 461 | |
| 462 | >>> def test(): |
| 463 | ... for abc in range(10): |
| 464 | ... try: |
| 465 | ... pass |
| 466 | ... finally: |
| 467 | ... continue |
Serhiy Storchaka | fe2bbb1 | 2018-03-18 09:56:52 +0200 | [diff] [blame] | 468 | ... print(abc) |
| 469 | >>> test() |
| 470 | 9 |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 471 | |
| 472 | >>> def test(): |
| 473 | ... for abc in range(10): |
| 474 | ... try: |
| 475 | ... pass |
| 476 | ... finally: |
| 477 | ... try: |
| 478 | ... continue |
| 479 | ... except: |
| 480 | ... pass |
Serhiy Storchaka | fe2bbb1 | 2018-03-18 09:56:52 +0200 | [diff] [blame] | 481 | ... print(abc) |
| 482 | >>> test() |
| 483 | 9 |
| 484 | |
| 485 | >>> def test(): |
| 486 | ... for abc in range(10): |
| 487 | ... try: |
| 488 | ... pass |
| 489 | ... finally: |
| 490 | ... try: |
| 491 | ... pass |
| 492 | ... except: |
| 493 | ... continue |
| 494 | ... print(abc) |
| 495 | >>> test() |
| 496 | 9 |
| 497 | |
| 498 | A continue outside loop should not be allowed. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 499 | |
| 500 | >>> def foo(): |
| 501 | ... try: |
| 502 | ... pass |
| 503 | ... finally: |
| 504 | ... continue |
| 505 | Traceback (most recent call last): |
| 506 | ... |
Serhiy Storchaka | fe2bbb1 | 2018-03-18 09:56:52 +0200 | [diff] [blame] | 507 | SyntaxError: 'continue' not properly in loop |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 508 | |
| 509 | There is one test for a break that is not in a loop. The compiler |
| 510 | uses a single data structure to keep track of try-finally and loops, |
| 511 | so we need to be sure that a break is actually inside a loop. If it |
| 512 | isn't, there should be a syntax error. |
| 513 | |
| 514 | >>> try: |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 515 | ... print(1) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 516 | ... break |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 517 | ... print(2) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 518 | ... finally: |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 519 | ... print(3) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 520 | Traceback (most recent call last): |
| 521 | ... |
Guido van Rossum | 33d2689 | 2007-08-05 15:29:28 +0000 | [diff] [blame] | 522 | SyntaxError: 'break' outside loop |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 523 | |
Benjamin Peterson | e09ed54 | 2016-07-14 22:00:03 -0700 | [diff] [blame] | 524 | This raises a SyntaxError, it used to raise a SystemError. |
| 525 | Context for this change can be found on issue #27514 |
| 526 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 527 | In 2.5 there was a missing exception and an assert was triggered in a debug |
| 528 | build. The number of blocks must be greater than CO_MAXBLOCKS. SF #1565514 |
| 529 | |
| 530 | >>> while 1: |
| 531 | ... while 2: |
| 532 | ... while 3: |
| 533 | ... while 4: |
| 534 | ... while 5: |
| 535 | ... while 6: |
| 536 | ... while 8: |
| 537 | ... while 9: |
| 538 | ... while 10: |
| 539 | ... while 11: |
| 540 | ... while 12: |
| 541 | ... while 13: |
| 542 | ... while 14: |
| 543 | ... while 15: |
| 544 | ... while 16: |
| 545 | ... while 17: |
| 546 | ... while 18: |
| 547 | ... while 19: |
| 548 | ... while 20: |
| 549 | ... while 21: |
| 550 | ... while 22: |
| 551 | ... break |
| 552 | Traceback (most recent call last): |
| 553 | ... |
Benjamin Peterson | e09ed54 | 2016-07-14 22:00:03 -0700 | [diff] [blame] | 554 | SyntaxError: too many statically nested blocks |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 555 | |
Guido van Rossum | 6cff874 | 2016-09-09 09:36:26 -0700 | [diff] [blame] | 556 | Misuse of the nonlocal and global statement can lead to a few unique syntax errors. |
| 557 | |
| 558 | >>> def f(): |
Ivan Levkivskyi | 8c83c23 | 2017-10-26 23:28:35 +0200 | [diff] [blame] | 559 | ... print(x) |
| 560 | ... global x |
| 561 | Traceback (most recent call last): |
| 562 | ... |
| 563 | SyntaxError: name 'x' is used prior to global declaration |
| 564 | |
| 565 | >>> def f(): |
Guido van Rossum | 6cff874 | 2016-09-09 09:36:26 -0700 | [diff] [blame] | 566 | ... x = 1 |
| 567 | ... global x |
| 568 | Traceback (most recent call last): |
| 569 | ... |
| 570 | SyntaxError: name 'x' is assigned to before global declaration |
| 571 | |
Ivan Levkivskyi | 8c83c23 | 2017-10-26 23:28:35 +0200 | [diff] [blame] | 572 | >>> def f(x): |
| 573 | ... global x |
| 574 | Traceback (most recent call last): |
| 575 | ... |
| 576 | SyntaxError: name 'x' is parameter and global |
| 577 | |
Guido van Rossum | 6cff874 | 2016-09-09 09:36:26 -0700 | [diff] [blame] | 578 | >>> def f(): |
| 579 | ... x = 1 |
| 580 | ... def g(): |
| 581 | ... print(x) |
| 582 | ... nonlocal x |
| 583 | Traceback (most recent call last): |
| 584 | ... |
| 585 | SyntaxError: name 'x' is used prior to nonlocal declaration |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 586 | |
Serhiy Storchaka | 3b66ebe | 2017-10-24 00:27:14 +0300 | [diff] [blame] | 587 | >>> def f(): |
| 588 | ... x = 1 |
| 589 | ... def g(): |
| 590 | ... x = 2 |
| 591 | ... nonlocal x |
| 592 | Traceback (most recent call last): |
| 593 | ... |
| 594 | SyntaxError: name 'x' is assigned to before nonlocal declaration |
| 595 | |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 596 | >>> def f(x): |
| 597 | ... nonlocal x |
| 598 | Traceback (most recent call last): |
| 599 | ... |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 600 | SyntaxError: name 'x' is parameter and nonlocal |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 601 | |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 602 | >>> def f(): |
| 603 | ... global x |
| 604 | ... nonlocal x |
| 605 | Traceback (most recent call last): |
| 606 | ... |
| 607 | SyntaxError: name 'x' is nonlocal and global |
| 608 | |
| 609 | >>> def f(): |
| 610 | ... nonlocal x |
| 611 | Traceback (most recent call last): |
| 612 | ... |
| 613 | SyntaxError: no binding for nonlocal 'x' found |
| 614 | |
Nick Coghlan | 4138bfe | 2007-04-23 10:14:27 +0000 | [diff] [blame] | 615 | From SF bug #1705365 |
| 616 | >>> nonlocal x |
| 617 | Traceback (most recent call last): |
| 618 | ... |
| 619 | SyntaxError: nonlocal declaration not allowed at module level |
| 620 | |
Serhiy Storchaka | 3b66ebe | 2017-10-24 00:27:14 +0300 | [diff] [blame] | 621 | From https://bugs.python.org/issue25973 |
Benjamin Peterson | 3cc8f4b | 2015-12-29 10:08:34 -0600 | [diff] [blame] | 622 | >>> class A: |
| 623 | ... def f(self): |
| 624 | ... nonlocal __x |
| 625 | Traceback (most recent call last): |
| 626 | ... |
| 627 | SyntaxError: no binding for nonlocal '_A__x' found |
| 628 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 629 | |
| 630 | This tests assignment-context; there was a bug in Python 2.5 where compiling |
| 631 | a complex 'if' (one with 'elif') would fail to notice an invalid suite, |
| 632 | leading to spurious errors. |
| 633 | |
| 634 | >>> if 1: |
| 635 | ... x() = 1 |
| 636 | ... elif 1: |
| 637 | ... pass |
| 638 | Traceback (most recent call last): |
| 639 | ... |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 640 | SyntaxError: cannot assign to function call |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 641 | |
| 642 | >>> if 1: |
| 643 | ... pass |
| 644 | ... elif 1: |
| 645 | ... x() = 1 |
| 646 | Traceback (most recent call last): |
| 647 | ... |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 648 | SyntaxError: cannot assign to function call |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 649 | |
| 650 | >>> if 1: |
| 651 | ... x() = 1 |
| 652 | ... elif 1: |
| 653 | ... pass |
| 654 | ... else: |
| 655 | ... pass |
| 656 | Traceback (most recent call last): |
| 657 | ... |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 658 | SyntaxError: cannot assign to function call |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 659 | |
| 660 | >>> if 1: |
| 661 | ... pass |
| 662 | ... elif 1: |
| 663 | ... x() = 1 |
| 664 | ... else: |
| 665 | ... pass |
| 666 | Traceback (most recent call last): |
| 667 | ... |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 668 | SyntaxError: cannot assign to function call |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 669 | |
| 670 | >>> if 1: |
| 671 | ... pass |
| 672 | ... elif 1: |
| 673 | ... pass |
| 674 | ... else: |
| 675 | ... x() = 1 |
| 676 | Traceback (most recent call last): |
| 677 | ... |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 678 | SyntaxError: cannot assign to function call |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 679 | |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 680 | Make sure that the old "raise X, Y[, Z]" form is gone: |
| 681 | >>> raise X, Y |
| 682 | Traceback (most recent call last): |
| 683 | ... |
| 684 | SyntaxError: invalid syntax |
| 685 | >>> raise X, Y, Z |
| 686 | Traceback (most recent call last): |
| 687 | ... |
| 688 | SyntaxError: invalid syntax |
| 689 | |
Benjamin Peterson | 07a1f94 | 2008-07-01 20:03:27 +0000 | [diff] [blame] | 690 | |
| 691 | >>> f(a=23, a=234) |
| 692 | Traceback (most recent call last): |
| 693 | ... |
Pablo Galindo | 254ec78 | 2020-04-03 20:37:13 +0100 | [diff] [blame] | 694 | SyntaxError: keyword argument repeated: a |
Benjamin Peterson | 07a1f94 | 2008-07-01 20:03:27 +0000 | [diff] [blame] | 695 | |
Benjamin Peterson | 1b1a1a4 | 2010-06-24 00:17:03 +0000 | [diff] [blame] | 696 | >>> {1, 2, 3} = 42 |
| 697 | Traceback (most recent call last): |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 698 | SyntaxError: cannot assign to set display |
| 699 | |
| 700 | >>> {1: 2, 3: 4} = 42 |
| 701 | Traceback (most recent call last): |
| 702 | SyntaxError: cannot assign to dict display |
| 703 | |
| 704 | >>> f'{x}' = 42 |
| 705 | Traceback (most recent call last): |
| 706 | SyntaxError: cannot assign to f-string expression |
| 707 | |
| 708 | >>> f'{x}-{y}' = 42 |
| 709 | Traceback (most recent call last): |
| 710 | SyntaxError: cannot assign to f-string expression |
Benjamin Peterson | 1b1a1a4 | 2010-06-24 00:17:03 +0000 | [diff] [blame] | 711 | |
Pablo Galindo | 275d7e1 | 2020-05-21 22:04:54 +0100 | [diff] [blame] | 712 | >>> from t import x, |
| 713 | Traceback (most recent call last): |
| 714 | SyntaxError: trailing comma not allowed without surrounding parentheses |
| 715 | |
| 716 | >>> from t import x,y, |
| 717 | Traceback (most recent call last): |
| 718 | SyntaxError: trailing comma not allowed without surrounding parentheses |
| 719 | |
Amaury Forgeot d'Arc | 12844e6 | 2010-08-19 21:32:38 +0000 | [diff] [blame] | 720 | Corner-cases that used to fail to raise the correct error: |
| 721 | |
| 722 | >>> def f(*, x=lambda __debug__:0): pass |
| 723 | Traceback (most recent call last): |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 724 | SyntaxError: cannot assign to __debug__ |
Amaury Forgeot d'Arc | 12844e6 | 2010-08-19 21:32:38 +0000 | [diff] [blame] | 725 | |
| 726 | >>> def f(*args:(lambda __debug__:0)): pass |
| 727 | Traceback (most recent call last): |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 728 | SyntaxError: cannot assign to __debug__ |
Amaury Forgeot d'Arc | 12844e6 | 2010-08-19 21:32:38 +0000 | [diff] [blame] | 729 | |
| 730 | >>> def f(**kwargs:(lambda __debug__:0)): pass |
| 731 | Traceback (most recent call last): |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 732 | SyntaxError: cannot assign to __debug__ |
Amaury Forgeot d'Arc | 12844e6 | 2010-08-19 21:32:38 +0000 | [diff] [blame] | 733 | |
Lysandros Nikolaou | e10e7c7 | 2020-05-04 13:58:31 +0300 | [diff] [blame] | 734 | >>> with (lambda *:0): pass |
| 735 | Traceback (most recent call last): |
| 736 | SyntaxError: named arguments must follow bare * |
Amaury Forgeot d'Arc | 12844e6 | 2010-08-19 21:32:38 +0000 | [diff] [blame] | 737 | |
| 738 | Corner-cases that used to crash: |
| 739 | |
| 740 | >>> def f(**__debug__): pass |
| 741 | Traceback (most recent call last): |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 742 | SyntaxError: cannot assign to __debug__ |
Amaury Forgeot d'Arc | 12844e6 | 2010-08-19 21:32:38 +0000 | [diff] [blame] | 743 | |
| 744 | >>> def f(*xx, __debug__): pass |
| 745 | Traceback (most recent call last): |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 746 | SyntaxError: cannot assign to __debug__ |
Amaury Forgeot d'Arc | 12844e6 | 2010-08-19 21:32:38 +0000 | [diff] [blame] | 747 | |
Lysandros Nikolaou | 7b7a21b | 2020-05-18 20:32:03 +0300 | [diff] [blame] | 748 | >>> import ä £ |
| 749 | Traceback (most recent call last): |
| 750 | SyntaxError: invalid character '£' (U+00A3) |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 751 | """ |
| 752 | |
Jeremy Hylton | 05ab2e6 | 2002-05-31 14:08:29 +0000 | [diff] [blame] | 753 | import re |
| 754 | import unittest |
| 755 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 756 | from test import support |
Jeremy Hylton | 05ab2e6 | 2002-05-31 14:08:29 +0000 | [diff] [blame] | 757 | |
| 758 | class SyntaxTestCase(unittest.TestCase): |
| 759 | |
| 760 | def _check_error(self, code, errtext, |
Serhiy Storchaka | 8b58339 | 2016-12-11 14:39:01 +0200 | [diff] [blame] | 761 | filename="<testcase>", mode="exec", subclass=None, lineno=None, offset=None): |
Jeremy Hylton | 05ab2e6 | 2002-05-31 14:08:29 +0000 | [diff] [blame] | 762 | """Check that compiling code raises SyntaxError with errtext. |
| 763 | |
| 764 | errtest is a regular expression that must be present in the |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 765 | test of the exception raised. If subclass is specified it |
| 766 | is the expected subclass of SyntaxError (e.g. IndentationError). |
Jeremy Hylton | 05ab2e6 | 2002-05-31 14:08:29 +0000 | [diff] [blame] | 767 | """ |
| 768 | try: |
| 769 | compile(code, filename, mode) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 770 | except SyntaxError as err: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 771 | if subclass and not isinstance(err, subclass): |
| 772 | self.fail("SyntaxError is not a %s" % subclass.__name__) |
Jeremy Hylton | 05ab2e6 | 2002-05-31 14:08:29 +0000 | [diff] [blame] | 773 | mo = re.search(errtext, str(err)) |
| 774 | if mo is None: |
Shantanu | 27c0d9b | 2020-05-11 14:53:58 -0700 | [diff] [blame] | 775 | self.fail("SyntaxError did not contain %r" % (errtext,)) |
Serhiy Storchaka | 8b58339 | 2016-12-11 14:39:01 +0200 | [diff] [blame] | 776 | self.assertEqual(err.filename, filename) |
| 777 | if lineno is not None: |
| 778 | self.assertEqual(err.lineno, lineno) |
| 779 | if offset is not None: |
| 780 | self.assertEqual(err.offset, offset) |
Jeremy Hylton | 05ab2e6 | 2002-05-31 14:08:29 +0000 | [diff] [blame] | 781 | else: |
| 782 | self.fail("compile() did not raise SyntaxError") |
| 783 | |
| 784 | def test_assign_call(self): |
| 785 | self._check_error("f() = 1", "assign") |
| 786 | |
Pablo Galindo | 16ab070 | 2020-05-15 02:04:52 +0100 | [diff] [blame] | 787 | @unittest.skipIf(support.use_old_parser(), "The old parser cannot generate these error messages") |
Jeremy Hylton | 05ab2e6 | 2002-05-31 14:08:29 +0000 | [diff] [blame] | 788 | def test_assign_del(self): |
Shantanu | 27c0d9b | 2020-05-11 14:53:58 -0700 | [diff] [blame] | 789 | self._check_error("del (,)", "invalid syntax") |
| 790 | self._check_error("del 1", "delete literal") |
| 791 | self._check_error("del (1, 2)", "delete literal") |
| 792 | self._check_error("del None", "delete None") |
| 793 | self._check_error("del *x", "delete starred") |
Lysandros Nikolaou | a5442b2 | 2020-06-19 03:03:58 +0300 | [diff] [blame^] | 794 | self._check_error("del (*x)", "use starred expression") |
Shantanu | 27c0d9b | 2020-05-11 14:53:58 -0700 | [diff] [blame] | 795 | self._check_error("del (*x,)", "delete starred") |
| 796 | self._check_error("del [*x,]", "delete starred") |
| 797 | self._check_error("del f()", "delete function call") |
| 798 | self._check_error("del f(a, b)", "delete function call") |
| 799 | self._check_error("del o.f()", "delete function call") |
| 800 | self._check_error("del a[0]()", "delete function call") |
| 801 | self._check_error("del x, f()", "delete function call") |
| 802 | self._check_error("del f(), x", "delete function call") |
| 803 | self._check_error("del [a, b, ((c), (d,), e.f())]", "delete function call") |
| 804 | self._check_error("del (a if True else b)", "delete conditional") |
| 805 | self._check_error("del +a", "delete operator") |
| 806 | self._check_error("del a, +b", "delete operator") |
| 807 | self._check_error("del a + b", "delete operator") |
| 808 | self._check_error("del (a + b, c)", "delete operator") |
| 809 | self._check_error("del (c[0], a + b)", "delete operator") |
| 810 | self._check_error("del a.b.c + 2", "delete operator") |
| 811 | self._check_error("del a.b.c[0] + 2", "delete operator") |
| 812 | self._check_error("del (a, b, (c, d.e.f + 2))", "delete operator") |
| 813 | self._check_error("del [a, b, (c, d.e.f[0] + 2)]", "delete operator") |
| 814 | self._check_error("del (a := 5)", "delete named expression") |
| 815 | # We don't have a special message for this, but make sure we don't |
| 816 | # report "cannot delete name" |
| 817 | self._check_error("del a += b", "invalid syntax") |
Jeremy Hylton | 05ab2e6 | 2002-05-31 14:08:29 +0000 | [diff] [blame] | 818 | |
Ivan Levkivskyi | 8c83c23 | 2017-10-26 23:28:35 +0200 | [diff] [blame] | 819 | def test_global_param_err_first(self): |
Amaury Forgeot d'Arc | fe7b405 | 2010-09-10 19:47:43 +0000 | [diff] [blame] | 820 | source = """if 1: |
| 821 | def error(a): |
| 822 | global a # SyntaxError |
Ivan Levkivskyi | 8c83c23 | 2017-10-26 23:28:35 +0200 | [diff] [blame] | 823 | def error2(): |
Amaury Forgeot d'Arc | fe7b405 | 2010-09-10 19:47:43 +0000 | [diff] [blame] | 824 | b = 1 |
Ivan Levkivskyi | 8c83c23 | 2017-10-26 23:28:35 +0200 | [diff] [blame] | 825 | global b # SyntaxError |
Amaury Forgeot d'Arc | fe7b405 | 2010-09-10 19:47:43 +0000 | [diff] [blame] | 826 | """ |
Ivan Levkivskyi | 8c83c23 | 2017-10-26 23:28:35 +0200 | [diff] [blame] | 827 | self._check_error(source, "parameter and global", lineno=3) |
| 828 | |
| 829 | def test_nonlocal_param_err_first(self): |
| 830 | source = """if 1: |
| 831 | def error(a): |
| 832 | nonlocal a # SyntaxError |
| 833 | def error2(): |
| 834 | b = 1 |
| 835 | global b # SyntaxError |
| 836 | """ |
| 837 | self._check_error(source, "parameter and nonlocal", lineno=3) |
Jeremy Hylton | 42d9016 | 2003-07-15 20:24:27 +0000 | [diff] [blame] | 838 | |
Neal Norwitz | fcf4435 | 2005-11-27 20:37:43 +0000 | [diff] [blame] | 839 | def test_break_outside_loop(self): |
| 840 | self._check_error("break", "outside loop") |
| 841 | |
Pablo Galindo | af8646c | 2019-05-17 11:37:08 +0100 | [diff] [blame] | 842 | def test_yield_outside_function(self): |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 843 | self._check_error("if 0: yield", "outside function") |
| 844 | self._check_error("if 0: yield\nelse: x=1", "outside function") |
| 845 | self._check_error("if 1: pass\nelse: yield", "outside function") |
| 846 | self._check_error("while 0: yield", "outside function") |
| 847 | self._check_error("while 0: yield\nelse: x=1", "outside function") |
| 848 | self._check_error("class C:\n if 0: yield", "outside function") |
| 849 | self._check_error("class C:\n if 1: pass\n else: yield", |
| 850 | "outside function") |
| 851 | self._check_error("class C:\n while 0: yield", "outside function") |
| 852 | self._check_error("class C:\n while 0: yield\n else: x = 1", |
| 853 | "outside function") |
Pablo Galindo | af8646c | 2019-05-17 11:37:08 +0100 | [diff] [blame] | 854 | |
| 855 | def test_return_outside_function(self): |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 856 | self._check_error("if 0: return", "outside function") |
| 857 | self._check_error("if 0: return\nelse: x=1", "outside function") |
| 858 | self._check_error("if 1: pass\nelse: return", "outside function") |
| 859 | self._check_error("while 0: return", "outside function") |
| 860 | self._check_error("class C:\n if 0: return", "outside function") |
| 861 | self._check_error("class C:\n while 0: return", "outside function") |
| 862 | self._check_error("class C:\n while 0: return\n else: x=1", |
| 863 | "outside function") |
| 864 | self._check_error("class C:\n if 0: return\n else: x= 1", |
| 865 | "outside function") |
| 866 | self._check_error("class C:\n if 1: pass\n else: return", |
| 867 | "outside function") |
Pablo Galindo | af8646c | 2019-05-17 11:37:08 +0100 | [diff] [blame] | 868 | |
| 869 | def test_break_outside_loop(self): |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 870 | self._check_error("if 0: break", "outside loop") |
| 871 | self._check_error("if 0: break\nelse: x=1", "outside loop") |
| 872 | self._check_error("if 1: pass\nelse: break", "outside loop") |
| 873 | self._check_error("class C:\n if 0: break", "outside loop") |
| 874 | self._check_error("class C:\n if 1: pass\n else: break", |
| 875 | "outside loop") |
Pablo Galindo | af8646c | 2019-05-17 11:37:08 +0100 | [diff] [blame] | 876 | |
| 877 | def test_continue_outside_loop(self): |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 878 | self._check_error("if 0: continue", "not properly in loop") |
| 879 | self._check_error("if 0: continue\nelse: x=1", "not properly in loop") |
| 880 | self._check_error("if 1: pass\nelse: continue", "not properly in loop") |
| 881 | self._check_error("class C:\n if 0: continue", "not properly in loop") |
| 882 | self._check_error("class C:\n if 1: pass\n else: continue", |
| 883 | "not properly in loop") |
Pablo Galindo | af8646c | 2019-05-17 11:37:08 +0100 | [diff] [blame] | 884 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 885 | def test_unexpected_indent(self): |
| 886 | self._check_error("foo()\n bar()\n", "unexpected indent", |
| 887 | subclass=IndentationError) |
| 888 | |
| 889 | def test_no_indent(self): |
| 890 | self._check_error("if 1:\nfoo()", "expected an indented block", |
| 891 | subclass=IndentationError) |
| 892 | |
| 893 | def test_bad_outdent(self): |
| 894 | self._check_error("if 1:\n foo()\n bar()", |
| 895 | "unindent does not match .* level", |
| 896 | subclass=IndentationError) |
| 897 | |
| 898 | def test_kwargs_last(self): |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 899 | self._check_error("int(base=10, '2')", |
| 900 | "positional argument follows keyword argument") |
| 901 | |
| 902 | def test_kwargs_last2(self): |
Serhiy Storchaka | 0cc99c8 | 2018-01-04 10:36:35 +0200 | [diff] [blame] | 903 | self._check_error("int(**{'base': 10}, '2')", |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 904 | "positional argument follows " |
| 905 | "keyword argument unpacking") |
| 906 | |
| 907 | def test_kwargs_last3(self): |
Serhiy Storchaka | 0cc99c8 | 2018-01-04 10:36:35 +0200 | [diff] [blame] | 908 | self._check_error("int(**{'base': 10}, *['2'])", |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 909 | "iterable argument unpacking follows " |
| 910 | "keyword argument unpacking") |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 911 | |
Miss Islington (bot) | e3ce3bb | 2020-06-10 17:14:16 -0700 | [diff] [blame] | 912 | def test_empty_line_after_linecont(self): |
| 913 | # See issue-40847 |
| 914 | s = r"""\ |
| 915 | pass |
| 916 | \ |
| 917 | |
| 918 | pass |
| 919 | """ |
| 920 | try: |
| 921 | compile(s, '<string>', 'exec') |
| 922 | except SyntaxError: |
| 923 | self.fail("Empty line after a line continuation character is valid.") |
| 924 | |
| 925 | |
Jeremy Hylton | 05ab2e6 | 2002-05-31 14:08:29 +0000 | [diff] [blame] | 926 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 927 | support.run_unittest(SyntaxTestCase) |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 928 | from test import test_syntax |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 929 | support.run_doctest(test_syntax, verbosity=True) |
Jeremy Hylton | 05ab2e6 | 2002-05-31 14:08:29 +0000 | [diff] [blame] | 930 | |
| 931 | if __name__ == "__main__": |
| 932 | test_main() |