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): |
Pablo Galindo | b86ed8e | 2021-04-12 16:59:30 +0100 | [diff] [blame] | 64 | SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='? |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 65 | |
Pablo Galindo | 9f49590 | 2020-06-08 02:57:00 +0100 | [diff] [blame] | 66 | >>> yield = 1 |
| 67 | Traceback (most recent call last): |
| 68 | SyntaxError: assignment to yield expression not possible |
| 69 | |
Shantanu | 27c0d9b | 2020-05-11 14:53:58 -0700 | [diff] [blame] | 70 | >>> del f() |
| 71 | Traceback (most recent call last): |
| 72 | SyntaxError: cannot delete function call |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 73 | |
| 74 | >>> a + 1 = 2 |
| 75 | Traceback (most recent call last): |
Pablo Galindo | b86ed8e | 2021-04-12 16:59:30 +0100 | [diff] [blame] | 76 | SyntaxError: cannot assign to expression here. Maybe you meant '==' instead of '='? |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 77 | |
| 78 | >>> (x for x in x) = 1 |
| 79 | Traceback (most recent call last): |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 80 | SyntaxError: cannot assign to generator expression |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 81 | |
| 82 | >>> 1 = 1 |
| 83 | Traceback (most recent call last): |
Pablo Galindo | b86ed8e | 2021-04-12 16:59:30 +0100 | [diff] [blame] | 84 | SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='? |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 85 | |
| 86 | >>> "abc" = 1 |
| 87 | Traceback (most recent call last): |
Pablo Galindo | b86ed8e | 2021-04-12 16:59:30 +0100 | [diff] [blame] | 88 | SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='? |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 89 | |
Benjamin Peterson | bd3e362 | 2011-04-12 18:33:28 -0500 | [diff] [blame] | 90 | >>> b"" = 1 |
| 91 | Traceback (most recent call last): |
Pablo Galindo | b86ed8e | 2021-04-12 16:59:30 +0100 | [diff] [blame] | 92 | SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='? |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 93 | |
| 94 | >>> ... = 1 |
| 95 | Traceback (most recent call last): |
Pablo Galindo | 3283bf4 | 2021-06-03 22:22:28 +0100 | [diff] [blame] | 96 | SyntaxError: cannot assign to ellipsis here. Maybe you meant '==' instead of '='? |
Benjamin Peterson | bd3e362 | 2011-04-12 18:33:28 -0500 | [diff] [blame] | 97 | |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 98 | >>> `1` = 1 |
| 99 | Traceback (most recent call last): |
Brett Cannon | 8933cb4 | 2006-08-25 04:12:10 +0000 | [diff] [blame] | 100 | SyntaxError: invalid syntax |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 101 | |
| 102 | If the left-hand side of an assignment is a list or tuple, an illegal |
| 103 | expression inside that contain should still cause a syntax error. |
| 104 | This test just checks a couple of cases rather than enumerating all of |
| 105 | them. |
| 106 | |
Pablo Galindo | 16ab070 | 2020-05-15 02:04:52 +0100 | [diff] [blame] | 107 | >>> (a, "b", c) = (1, 2, 3) |
| 108 | Traceback (most recent call last): |
| 109 | SyntaxError: cannot assign to literal |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 110 | |
Pablo Galindo | 16ab070 | 2020-05-15 02:04:52 +0100 | [diff] [blame] | 111 | >>> (a, True, c) = (1, 2, 3) |
| 112 | Traceback (most recent call last): |
| 113 | SyntaxError: cannot assign to True |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 114 | |
| 115 | >>> (a, __debug__, c) = (1, 2, 3) |
| 116 | Traceback (most recent call last): |
| 117 | SyntaxError: cannot assign to __debug__ |
| 118 | |
Pablo Galindo | 16ab070 | 2020-05-15 02:04:52 +0100 | [diff] [blame] | 119 | >>> (a, *True, c) = (1, 2, 3) |
| 120 | Traceback (most recent call last): |
| 121 | SyntaxError: cannot assign to True |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 122 | |
| 123 | >>> (a, *__debug__, c) = (1, 2, 3) |
| 124 | Traceback (most recent call last): |
| 125 | SyntaxError: cannot assign to __debug__ |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 126 | |
Pablo Galindo | 16ab070 | 2020-05-15 02:04:52 +0100 | [diff] [blame] | 127 | >>> [a, b, c + 1] = [1, 2, 3] |
| 128 | Traceback (most recent call last): |
Pablo Galindo | b86ed8e | 2021-04-12 16:59:30 +0100 | [diff] [blame] | 129 | SyntaxError: cannot assign to expression |
Pablo Galindo | 16ab070 | 2020-05-15 02:04:52 +0100 | [diff] [blame] | 130 | |
| 131 | >>> [a, b[1], c + 1] = [1, 2, 3] |
| 132 | Traceback (most recent call last): |
Pablo Galindo | b86ed8e | 2021-04-12 16:59:30 +0100 | [diff] [blame] | 133 | SyntaxError: cannot assign to expression |
Pablo Galindo | 16ab070 | 2020-05-15 02:04:52 +0100 | [diff] [blame] | 134 | |
| 135 | >>> [a, b.c.d, c + 1] = [1, 2, 3] |
| 136 | Traceback (most recent call last): |
Pablo Galindo | b86ed8e | 2021-04-12 16:59:30 +0100 | [diff] [blame] | 137 | SyntaxError: cannot assign to expression |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 138 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 139 | >>> a if 1 else b = 1 |
| 140 | Traceback (most recent call last): |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 141 | SyntaxError: cannot assign to conditional expression |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 142 | |
Miss Islington (bot) | 5671762 | 2021-08-02 12:05:33 -0700 | [diff] [blame] | 143 | >>> a = 42 if True |
| 144 | Traceback (most recent call last): |
| 145 | SyntaxError: expected 'else' after 'if' expression |
| 146 | |
| 147 | >>> a = (42 if True) |
| 148 | Traceback (most recent call last): |
| 149 | SyntaxError: expected 'else' after 'if' expression |
| 150 | |
| 151 | >>> a = [1, 42 if True, 4] |
| 152 | Traceback (most recent call last): |
| 153 | SyntaxError: expected 'else' after 'if' expression |
| 154 | |
Pablo Galindo Salgado | b1bd16c | 2021-08-05 19:00:19 +0100 | [diff] [blame] | 155 | >>> if True: |
| 156 | ... print("Hello" |
| 157 | ... |
| 158 | ... if 2: |
| 159 | ... print(123)) |
| 160 | Traceback (most recent call last): |
| 161 | SyntaxError: invalid syntax |
| 162 | |
Pablo Galindo | 9f49590 | 2020-06-08 02:57:00 +0100 | [diff] [blame] | 163 | >>> True = True = 3 |
| 164 | Traceback (most recent call last): |
| 165 | SyntaxError: cannot assign to True |
| 166 | |
| 167 | >>> x = y = True = z = 3 |
| 168 | Traceback (most recent call last): |
| 169 | SyntaxError: cannot assign to True |
| 170 | |
| 171 | >>> x = y = yield = 1 |
| 172 | Traceback (most recent call last): |
| 173 | SyntaxError: assignment to yield expression not possible |
| 174 | |
Lysandros Nikolaou | ce21cfc | 2020-05-14 23:13:50 +0300 | [diff] [blame] | 175 | >>> a, b += 1, 2 |
| 176 | Traceback (most recent call last): |
Pablo Galindo | 16ab070 | 2020-05-15 02:04:52 +0100 | [diff] [blame] | 177 | SyntaxError: 'tuple' is an illegal expression for augmented assignment |
Lysandros Nikolaou | ce21cfc | 2020-05-14 23:13:50 +0300 | [diff] [blame] | 178 | |
| 179 | >>> (a, b) += 1, 2 |
| 180 | Traceback (most recent call last): |
Pablo Galindo | 16ab070 | 2020-05-15 02:04:52 +0100 | [diff] [blame] | 181 | SyntaxError: 'tuple' is an illegal expression for augmented assignment |
Lysandros Nikolaou | ce21cfc | 2020-05-14 23:13:50 +0300 | [diff] [blame] | 182 | |
| 183 | >>> [a, b] += 1, 2 |
| 184 | Traceback (most recent call last): |
Pablo Galindo | 16ab070 | 2020-05-15 02:04:52 +0100 | [diff] [blame] | 185 | SyntaxError: 'list' is an illegal expression for augmented assignment |
Lysandros Nikolaou | ce21cfc | 2020-05-14 23:13:50 +0300 | [diff] [blame] | 186 | |
Lysandros Nikolaou | 01ece63 | 2020-06-19 02:10:43 +0300 | [diff] [blame] | 187 | Invalid targets in `for` loops and `with` statements should also |
| 188 | produce a specialized error message |
| 189 | |
| 190 | >>> for a() in b: pass |
| 191 | Traceback (most recent call last): |
| 192 | SyntaxError: cannot assign to function call |
| 193 | |
| 194 | >>> for (a, b()) in b: pass |
| 195 | Traceback (most recent call last): |
| 196 | SyntaxError: cannot assign to function call |
| 197 | |
| 198 | >>> for [a, b()] in b: pass |
| 199 | Traceback (most recent call last): |
| 200 | SyntaxError: cannot assign to function call |
| 201 | |
| 202 | >>> for (*a, b, c+1) in b: pass |
| 203 | Traceback (most recent call last): |
Pablo Galindo | b86ed8e | 2021-04-12 16:59:30 +0100 | [diff] [blame] | 204 | SyntaxError: cannot assign to expression |
Lysandros Nikolaou | 01ece63 | 2020-06-19 02:10:43 +0300 | [diff] [blame] | 205 | |
| 206 | >>> for (x, *(y, z.d())) in b: pass |
| 207 | Traceback (most recent call last): |
| 208 | SyntaxError: cannot assign to function call |
| 209 | |
| 210 | >>> for a, b() in c: pass |
| 211 | Traceback (most recent call last): |
| 212 | SyntaxError: cannot assign to function call |
| 213 | |
| 214 | >>> for a, b, (c + 1, d()): pass |
| 215 | Traceback (most recent call last): |
Pablo Galindo | b86ed8e | 2021-04-12 16:59:30 +0100 | [diff] [blame] | 216 | SyntaxError: cannot assign to expression |
Lysandros Nikolaou | 01ece63 | 2020-06-19 02:10:43 +0300 | [diff] [blame] | 217 | |
| 218 | >>> for i < (): pass |
| 219 | Traceback (most recent call last): |
| 220 | SyntaxError: invalid syntax |
| 221 | |
Lysandros Nikolaou | 6c4e0bd | 2020-06-21 05:18:01 +0300 | [diff] [blame] | 222 | >>> for a, b |
| 223 | Traceback (most recent call last): |
| 224 | SyntaxError: invalid syntax |
| 225 | |
Lysandros Nikolaou | 01ece63 | 2020-06-19 02:10:43 +0300 | [diff] [blame] | 226 | >>> with a as b(): pass |
| 227 | Traceback (most recent call last): |
| 228 | SyntaxError: cannot assign to function call |
| 229 | |
| 230 | >>> with a as (b, c()): pass |
| 231 | Traceback (most recent call last): |
| 232 | SyntaxError: cannot assign to function call |
| 233 | |
| 234 | >>> with a as [b, c()]: pass |
| 235 | Traceback (most recent call last): |
| 236 | SyntaxError: cannot assign to function call |
| 237 | |
| 238 | >>> with a as (*b, c, d+1): pass |
| 239 | Traceback (most recent call last): |
Pablo Galindo | b86ed8e | 2021-04-12 16:59:30 +0100 | [diff] [blame] | 240 | SyntaxError: cannot assign to expression |
Lysandros Nikolaou | 01ece63 | 2020-06-19 02:10:43 +0300 | [diff] [blame] | 241 | |
| 242 | >>> with a as (x, *(y, z.d())): pass |
| 243 | Traceback (most recent call last): |
| 244 | SyntaxError: cannot assign to function call |
| 245 | |
| 246 | >>> with a as b, c as d(): pass |
| 247 | Traceback (most recent call last): |
| 248 | SyntaxError: cannot assign to function call |
| 249 | |
Lysandros Nikolaou | 6c4e0bd | 2020-06-21 05:18:01 +0300 | [diff] [blame] | 250 | >>> with a as b |
| 251 | Traceback (most recent call last): |
Pablo Galindo | 58fb156 | 2021-02-02 19:54:22 +0000 | [diff] [blame] | 252 | SyntaxError: expected ':' |
Lysandros Nikolaou | 6c4e0bd | 2020-06-21 05:18:01 +0300 | [diff] [blame] | 253 | |
Pablo Galindo | 9f49590 | 2020-06-08 02:57:00 +0100 | [diff] [blame] | 254 | >>> p = p = |
| 255 | Traceback (most recent call last): |
| 256 | SyntaxError: invalid syntax |
| 257 | |
Pablo Galindo | 835f14f | 2021-01-31 22:52:56 +0000 | [diff] [blame] | 258 | Comprehensions creating tuples without parentheses |
| 259 | should produce a specialized error message: |
| 260 | |
| 261 | >>> [x,y for x,y in range(100)] |
| 262 | Traceback (most recent call last): |
| 263 | SyntaxError: did you forget parentheses around the comprehension target? |
| 264 | |
| 265 | >>> {x,y for x,y in range(100)} |
| 266 | Traceback (most recent call last): |
| 267 | SyntaxError: did you forget parentheses around the comprehension target? |
| 268 | |
Pablo Galindo | d4e6ed7 | 2021-02-03 23:29:26 +0000 | [diff] [blame] | 269 | # Missing commas in literals collections should not |
| 270 | # produce special error messages regarding missing |
Pablo Galindo | b280248 | 2021-04-15 21:38:45 +0100 | [diff] [blame] | 271 | # parentheses, but about missing commas instead |
Pablo Galindo | d4e6ed7 | 2021-02-03 23:29:26 +0000 | [diff] [blame] | 272 | |
| 273 | >>> [1, 2 3] |
Pablo Galindo | 835f14f | 2021-01-31 22:52:56 +0000 | [diff] [blame] | 274 | Traceback (most recent call last): |
Pablo Galindo | b280248 | 2021-04-15 21:38:45 +0100 | [diff] [blame] | 275 | SyntaxError: invalid syntax. Perhaps you forgot a comma? |
Pablo Galindo | d4e6ed7 | 2021-02-03 23:29:26 +0000 | [diff] [blame] | 276 | |
| 277 | >>> {1, 2 3} |
| 278 | Traceback (most recent call last): |
Pablo Galindo | b280248 | 2021-04-15 21:38:45 +0100 | [diff] [blame] | 279 | SyntaxError: invalid syntax. Perhaps you forgot a comma? |
Pablo Galindo | d4e6ed7 | 2021-02-03 23:29:26 +0000 | [diff] [blame] | 280 | |
| 281 | >>> {1:2, 2:5 3:12} |
| 282 | Traceback (most recent call last): |
Pablo Galindo | b280248 | 2021-04-15 21:38:45 +0100 | [diff] [blame] | 283 | SyntaxError: invalid syntax. Perhaps you forgot a comma? |
Pablo Galindo | d4e6ed7 | 2021-02-03 23:29:26 +0000 | [diff] [blame] | 284 | |
| 285 | >>> (1, 2 3) |
| 286 | Traceback (most recent call last): |
Pablo Galindo | b280248 | 2021-04-15 21:38:45 +0100 | [diff] [blame] | 287 | SyntaxError: invalid syntax. Perhaps you forgot a comma? |
| 288 | |
| 289 | # Make sure soft keywords constructs don't raise specialized |
Miss Islington (bot) | f807a4f | 2021-06-09 14:45:43 -0700 | [diff] [blame] | 290 | # errors regarding missing commas or other spezialiced errors |
Pablo Galindo | b280248 | 2021-04-15 21:38:45 +0100 | [diff] [blame] | 291 | |
| 292 | >>> match x: |
| 293 | ... y = 3 |
| 294 | Traceback (most recent call last): |
| 295 | SyntaxError: invalid syntax |
| 296 | |
| 297 | >>> match x: |
| 298 | ... case y: |
| 299 | ... 3 $ 3 |
| 300 | Traceback (most recent call last): |
Pablo Galindo | d4e6ed7 | 2021-02-03 23:29:26 +0000 | [diff] [blame] | 301 | SyntaxError: invalid syntax |
Pablo Galindo | 835f14f | 2021-01-31 22:52:56 +0000 | [diff] [blame] | 302 | |
Miss Islington (bot) | f807a4f | 2021-06-09 14:45:43 -0700 | [diff] [blame] | 303 | >>> match x: |
| 304 | ... case $: |
| 305 | ... ... |
| 306 | Traceback (most recent call last): |
| 307 | SyntaxError: invalid syntax |
| 308 | |
| 309 | >>> match ...: |
| 310 | ... case {**rest, "key": value}: |
| 311 | ... ... |
| 312 | Traceback (most recent call last): |
| 313 | SyntaxError: invalid syntax |
| 314 | |
| 315 | >>> match ...: |
| 316 | ... case {**_}: |
| 317 | ... ... |
| 318 | Traceback (most recent call last): |
| 319 | SyntaxError: invalid syntax |
| 320 | |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 321 | From compiler_complex_args(): |
| 322 | |
| 323 | >>> def f(None=1): |
| 324 | ... pass |
| 325 | Traceback (most recent call last): |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 326 | SyntaxError: invalid syntax |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 327 | |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 328 | From ast_for_arguments(): |
| 329 | |
| 330 | >>> def f(x, y=1, z): |
| 331 | ... pass |
| 332 | Traceback (most recent call last): |
Guido van Rossum | 33d2689 | 2007-08-05 15:29:28 +0000 | [diff] [blame] | 333 | SyntaxError: non-default argument follows default argument |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 334 | |
| 335 | >>> def f(x, None): |
| 336 | ... pass |
| 337 | Traceback (most recent call last): |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 338 | SyntaxError: invalid syntax |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 339 | |
| 340 | >>> def f(*None): |
| 341 | ... pass |
| 342 | Traceback (most recent call last): |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 343 | SyntaxError: invalid syntax |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 344 | |
| 345 | >>> def f(**None): |
| 346 | ... pass |
| 347 | Traceback (most recent call last): |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 348 | SyntaxError: invalid syntax |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 349 | |
Lysandros Nikolaou | 75b863a | 2020-05-18 22:14:47 +0300 | [diff] [blame] | 350 | >>> import ast; ast.parse(''' |
| 351 | ... def f( |
| 352 | ... *, # type: int |
| 353 | ... a, # type: int |
| 354 | ... ): |
| 355 | ... pass |
| 356 | ... ''', type_comments=True) |
| 357 | Traceback (most recent call last): |
| 358 | SyntaxError: bare * has associated type comment |
| 359 | |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 360 | |
| 361 | From ast_for_funcdef(): |
| 362 | |
| 363 | >>> def None(x): |
| 364 | ... pass |
| 365 | Traceback (most recent call last): |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 366 | SyntaxError: invalid syntax |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 367 | |
| 368 | |
| 369 | From ast_for_call(): |
| 370 | |
Serhiy Storchaka | 9165f77 | 2017-11-15 08:49:40 +0200 | [diff] [blame] | 371 | >>> def f(it, *varargs, **kwargs): |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 372 | ... return list(it) |
| 373 | >>> L = range(10) |
| 374 | >>> f(x for x in L) |
| 375 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
| 376 | >>> f(x for x in L, 1) |
| 377 | Traceback (most recent call last): |
Serhiy Storchaka | 9165f77 | 2017-11-15 08:49:40 +0200 | [diff] [blame] | 378 | SyntaxError: Generator expression must be parenthesized |
| 379 | >>> f(x for x in L, y=1) |
| 380 | Traceback (most recent call last): |
| 381 | SyntaxError: Generator expression must be parenthesized |
| 382 | >>> f(x for x in L, *[]) |
| 383 | Traceback (most recent call last): |
| 384 | SyntaxError: Generator expression must be parenthesized |
| 385 | >>> f(x for x in L, **{}) |
| 386 | Traceback (most recent call last): |
| 387 | SyntaxError: Generator expression must be parenthesized |
Lysandros Nikolaou | ae14583 | 2020-05-22 03:56:52 +0300 | [diff] [blame] | 388 | >>> f(L, x for x in L) |
| 389 | Traceback (most recent call last): |
| 390 | SyntaxError: Generator expression must be parenthesized |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 391 | >>> f(x for x in L, y for y in L) |
| 392 | Traceback (most recent call last): |
Serhiy Storchaka | 9165f77 | 2017-11-15 08:49:40 +0200 | [diff] [blame] | 393 | SyntaxError: Generator expression must be parenthesized |
| 394 | >>> f(x for x in L,) |
| 395 | Traceback (most recent call last): |
| 396 | SyntaxError: Generator expression must be parenthesized |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 397 | >>> f((x for x in L), 1) |
| 398 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
Serhiy Storchaka | ddbce13 | 2017-11-15 17:39:37 +0200 | [diff] [blame] | 399 | >>> class C(x for x in L): |
| 400 | ... pass |
| 401 | Traceback (most recent call last): |
Pablo Galindo | 58fb156 | 2021-02-02 19:54:22 +0000 | [diff] [blame] | 402 | SyntaxError: expected ':' |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 403 | |
Serhiy Storchaka | 214678e | 2016-11-28 10:52:05 +0200 | [diff] [blame] | 404 | >>> def g(*args, **kwargs): |
| 405 | ... print(args, sorted(kwargs.items())) |
| 406 | >>> g(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, |
| 407 | ... 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, |
| 408 | ... 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, |
| 409 | ... 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, |
| 410 | ... 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, |
| 411 | ... 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, |
| 412 | ... 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, |
| 413 | ... 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, |
| 414 | ... 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, |
| 415 | ... 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, |
| 416 | ... 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, |
| 417 | ... 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, |
| 418 | ... 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, |
| 419 | ... 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, |
| 420 | ... 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, |
| 421 | ... 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, |
| 422 | ... 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261, |
| 423 | ... 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275, |
| 424 | ... 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289, |
| 425 | ... 290, 291, 292, 293, 294, 295, 296, 297, 298, 299) # doctest: +ELLIPSIS |
| 426 | (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] | 427 | |
Serhiy Storchaka | 214678e | 2016-11-28 10:52:05 +0200 | [diff] [blame] | 428 | >>> g(a000=0, a001=1, a002=2, a003=3, a004=4, a005=5, a006=6, a007=7, a008=8, |
| 429 | ... a009=9, a010=10, a011=11, a012=12, a013=13, a014=14, a015=15, a016=16, |
| 430 | ... a017=17, a018=18, a019=19, a020=20, a021=21, a022=22, a023=23, a024=24, |
| 431 | ... a025=25, a026=26, a027=27, a028=28, a029=29, a030=30, a031=31, a032=32, |
| 432 | ... a033=33, a034=34, a035=35, a036=36, a037=37, a038=38, a039=39, a040=40, |
| 433 | ... a041=41, a042=42, a043=43, a044=44, a045=45, a046=46, a047=47, a048=48, |
| 434 | ... a049=49, a050=50, a051=51, a052=52, a053=53, a054=54, a055=55, a056=56, |
| 435 | ... a057=57, a058=58, a059=59, a060=60, a061=61, a062=62, a063=63, a064=64, |
| 436 | ... a065=65, a066=66, a067=67, a068=68, a069=69, a070=70, a071=71, a072=72, |
| 437 | ... a073=73, a074=74, a075=75, a076=76, a077=77, a078=78, a079=79, a080=80, |
| 438 | ... a081=81, a082=82, a083=83, a084=84, a085=85, a086=86, a087=87, a088=88, |
| 439 | ... a089=89, a090=90, a091=91, a092=92, a093=93, a094=94, a095=95, a096=96, |
| 440 | ... a097=97, a098=98, a099=99, a100=100, a101=101, a102=102, a103=103, |
| 441 | ... a104=104, a105=105, a106=106, a107=107, a108=108, a109=109, a110=110, |
| 442 | ... a111=111, a112=112, a113=113, a114=114, a115=115, a116=116, a117=117, |
| 443 | ... a118=118, a119=119, a120=120, a121=121, a122=122, a123=123, a124=124, |
| 444 | ... a125=125, a126=126, a127=127, a128=128, a129=129, a130=130, a131=131, |
| 445 | ... a132=132, a133=133, a134=134, a135=135, a136=136, a137=137, a138=138, |
| 446 | ... a139=139, a140=140, a141=141, a142=142, a143=143, a144=144, a145=145, |
| 447 | ... a146=146, a147=147, a148=148, a149=149, a150=150, a151=151, a152=152, |
| 448 | ... a153=153, a154=154, a155=155, a156=156, a157=157, a158=158, a159=159, |
| 449 | ... a160=160, a161=161, a162=162, a163=163, a164=164, a165=165, a166=166, |
| 450 | ... a167=167, a168=168, a169=169, a170=170, a171=171, a172=172, a173=173, |
| 451 | ... a174=174, a175=175, a176=176, a177=177, a178=178, a179=179, a180=180, |
| 452 | ... a181=181, a182=182, a183=183, a184=184, a185=185, a186=186, a187=187, |
| 453 | ... a188=188, a189=189, a190=190, a191=191, a192=192, a193=193, a194=194, |
| 454 | ... a195=195, a196=196, a197=197, a198=198, a199=199, a200=200, a201=201, |
| 455 | ... a202=202, a203=203, a204=204, a205=205, a206=206, a207=207, a208=208, |
| 456 | ... a209=209, a210=210, a211=211, a212=212, a213=213, a214=214, a215=215, |
| 457 | ... a216=216, a217=217, a218=218, a219=219, a220=220, a221=221, a222=222, |
| 458 | ... a223=223, a224=224, a225=225, a226=226, a227=227, a228=228, a229=229, |
| 459 | ... a230=230, a231=231, a232=232, a233=233, a234=234, a235=235, a236=236, |
| 460 | ... a237=237, a238=238, a239=239, a240=240, a241=241, a242=242, a243=243, |
| 461 | ... a244=244, a245=245, a246=246, a247=247, a248=248, a249=249, a250=250, |
| 462 | ... a251=251, a252=252, a253=253, a254=254, a255=255, a256=256, a257=257, |
| 463 | ... a258=258, a259=259, a260=260, a261=261, a262=262, a263=263, a264=264, |
| 464 | ... a265=265, a266=266, a267=267, a268=268, a269=269, a270=270, a271=271, |
| 465 | ... a272=272, a273=273, a274=274, a275=275, a276=276, a277=277, a278=278, |
| 466 | ... a279=279, a280=280, a281=281, a282=282, a283=283, a284=284, a285=285, |
| 467 | ... a286=286, a287=287, a288=288, a289=289, a290=290, a291=291, a292=292, |
| 468 | ... a293=293, a294=294, a295=295, a296=296, a297=297, a298=298, a299=299) |
| 469 | ... # doctest: +ELLIPSIS |
| 470 | () [('a000', 0), ('a001', 1), ('a002', 2), ..., ('a298', 298), ('a299', 299)] |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 471 | |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 472 | >>> class C: |
| 473 | ... def meth(self, *args): |
| 474 | ... return args |
| 475 | >>> obj = C() |
| 476 | >>> obj.meth( |
| 477 | ... 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, |
| 478 | ... 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, |
| 479 | ... 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, |
| 480 | ... 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, |
| 481 | ... 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, |
| 482 | ... 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, |
| 483 | ... 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, |
| 484 | ... 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, |
| 485 | ... 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, |
| 486 | ... 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, |
| 487 | ... 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, |
| 488 | ... 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, |
| 489 | ... 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, |
| 490 | ... 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, |
| 491 | ... 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, |
| 492 | ... 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, |
| 493 | ... 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261, |
| 494 | ... 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275, |
| 495 | ... 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289, |
| 496 | ... 290, 291, 292, 293, 294, 295, 296, 297, 298, 299) # doctest: +ELLIPSIS |
| 497 | (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ..., 297, 298, 299) |
| 498 | |
Miss Islington (bot) | ec0699c | 2021-05-19 11:28:31 -0700 | [diff] [blame] | 499 | >>> f(lambda x: x[0] = 3) |
| 500 | Traceback (most recent call last): |
| 501 | SyntaxError: expression cannot contain assignment, perhaps you meant "=="? |
| 502 | |
| 503 | # Check that this error doesn't trigger for names: |
| 504 | >>> f(a={x: for x in {}}) |
| 505 | Traceback (most recent call last): |
| 506 | SyntaxError: invalid syntax |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 507 | |
| 508 | The grammar accepts any test (basically, any expression) in the |
| 509 | keyword slot of a call site. Test a few different options. |
| 510 | |
Miss Islington (bot) | ec0699c | 2021-05-19 11:28:31 -0700 | [diff] [blame] | 511 | >>> f(x()=2) |
| 512 | Traceback (most recent call last): |
| 513 | SyntaxError: expression cannot contain assignment, perhaps you meant "=="? |
| 514 | >>> f(a or b=1) |
| 515 | Traceback (most recent call last): |
| 516 | SyntaxError: expression cannot contain assignment, perhaps you meant "=="? |
| 517 | >>> f(x.y=1) |
| 518 | Traceback (most recent call last): |
| 519 | SyntaxError: expression cannot contain assignment, perhaps you meant "=="? |
| 520 | >>> f((x)=2) |
| 521 | Traceback (most recent call last): |
| 522 | SyntaxError: expression cannot contain assignment, perhaps you meant "=="? |
| 523 | >>> f(True=2) |
| 524 | Traceback (most recent call last): |
| 525 | SyntaxError: expression cannot contain assignment, perhaps you meant "=="? |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 526 | >>> f(__debug__=1) |
| 527 | Traceback (most recent call last): |
| 528 | SyntaxError: cannot assign to __debug__ |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 529 | >>> __debug__: int |
| 530 | Traceback (most recent call last): |
| 531 | SyntaxError: cannot assign to __debug__ |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 532 | |
| 533 | |
Benjamin Peterson | 87c8d87 | 2009-06-11 22:54:11 +0000 | [diff] [blame] | 534 | More set_context(): |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 535 | |
| 536 | >>> (x for x in x) += 1 |
| 537 | Traceback (most recent call last): |
Pablo Galindo | 16ab070 | 2020-05-15 02:04:52 +0100 | [diff] [blame] | 538 | SyntaxError: 'generator expression' is an illegal expression for augmented assignment |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 539 | >>> None += 1 |
| 540 | Traceback (most recent call last): |
Pablo Galindo | 16ab070 | 2020-05-15 02:04:52 +0100 | [diff] [blame] | 541 | SyntaxError: 'None' is an illegal expression for augmented assignment |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 542 | >>> __debug__ += 1 |
| 543 | Traceback (most recent call last): |
| 544 | SyntaxError: cannot assign to __debug__ |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 545 | >>> f() += 1 |
| 546 | Traceback (most recent call last): |
Pablo Galindo | 16ab070 | 2020-05-15 02:04:52 +0100 | [diff] [blame] | 547 | SyntaxError: 'function call' is an illegal expression for augmented assignment |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 548 | |
| 549 | |
| 550 | Test continue in finally in weird combinations. |
| 551 | |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 552 | continue in for loop under finally should be ok. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 553 | |
| 554 | >>> def test(): |
| 555 | ... try: |
| 556 | ... pass |
| 557 | ... finally: |
| 558 | ... for abc in range(10): |
| 559 | ... continue |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 560 | ... print(abc) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 561 | >>> test() |
| 562 | 9 |
| 563 | |
Serhiy Storchaka | fe2bbb1 | 2018-03-18 09:56:52 +0200 | [diff] [blame] | 564 | continue in a finally should be ok. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 565 | |
| 566 | >>> def test(): |
| 567 | ... for abc in range(10): |
| 568 | ... try: |
| 569 | ... pass |
| 570 | ... finally: |
| 571 | ... continue |
Serhiy Storchaka | fe2bbb1 | 2018-03-18 09:56:52 +0200 | [diff] [blame] | 572 | ... print(abc) |
| 573 | >>> test() |
| 574 | 9 |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 575 | |
| 576 | >>> def test(): |
| 577 | ... for abc in range(10): |
| 578 | ... try: |
| 579 | ... pass |
| 580 | ... finally: |
| 581 | ... try: |
| 582 | ... continue |
| 583 | ... except: |
| 584 | ... pass |
Serhiy Storchaka | fe2bbb1 | 2018-03-18 09:56:52 +0200 | [diff] [blame] | 585 | ... print(abc) |
| 586 | >>> test() |
| 587 | 9 |
| 588 | |
| 589 | >>> def test(): |
| 590 | ... for abc in range(10): |
| 591 | ... try: |
| 592 | ... pass |
| 593 | ... finally: |
| 594 | ... try: |
| 595 | ... pass |
| 596 | ... except: |
| 597 | ... continue |
| 598 | ... print(abc) |
| 599 | >>> test() |
| 600 | 9 |
| 601 | |
| 602 | A continue outside loop should not be allowed. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 603 | |
| 604 | >>> def foo(): |
| 605 | ... try: |
| 606 | ... pass |
| 607 | ... finally: |
| 608 | ... continue |
| 609 | Traceback (most recent call last): |
| 610 | ... |
Serhiy Storchaka | fe2bbb1 | 2018-03-18 09:56:52 +0200 | [diff] [blame] | 611 | SyntaxError: 'continue' not properly in loop |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 612 | |
| 613 | There is one test for a break that is not in a loop. The compiler |
| 614 | uses a single data structure to keep track of try-finally and loops, |
| 615 | so we need to be sure that a break is actually inside a loop. If it |
| 616 | isn't, there should be a syntax error. |
| 617 | |
| 618 | >>> try: |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 619 | ... print(1) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 620 | ... break |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 621 | ... print(2) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 622 | ... finally: |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 623 | ... print(3) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 624 | Traceback (most recent call last): |
| 625 | ... |
Guido van Rossum | 33d2689 | 2007-08-05 15:29:28 +0000 | [diff] [blame] | 626 | SyntaxError: 'break' outside loop |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 627 | |
Benjamin Peterson | e09ed54 | 2016-07-14 22:00:03 -0700 | [diff] [blame] | 628 | This raises a SyntaxError, it used to raise a SystemError. |
| 629 | Context for this change can be found on issue #27514 |
| 630 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 631 | In 2.5 there was a missing exception and an assert was triggered in a debug |
| 632 | build. The number of blocks must be greater than CO_MAXBLOCKS. SF #1565514 |
| 633 | |
| 634 | >>> while 1: |
| 635 | ... while 2: |
| 636 | ... while 3: |
| 637 | ... while 4: |
| 638 | ... while 5: |
| 639 | ... while 6: |
| 640 | ... while 8: |
| 641 | ... while 9: |
| 642 | ... while 10: |
| 643 | ... while 11: |
| 644 | ... while 12: |
| 645 | ... while 13: |
| 646 | ... while 14: |
| 647 | ... while 15: |
| 648 | ... while 16: |
| 649 | ... while 17: |
| 650 | ... while 18: |
| 651 | ... while 19: |
| 652 | ... while 20: |
| 653 | ... while 21: |
| 654 | ... while 22: |
| 655 | ... break |
| 656 | Traceback (most recent call last): |
| 657 | ... |
Benjamin Peterson | e09ed54 | 2016-07-14 22:00:03 -0700 | [diff] [blame] | 658 | SyntaxError: too many statically nested blocks |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 659 | |
Guido van Rossum | 6cff874 | 2016-09-09 09:36:26 -0700 | [diff] [blame] | 660 | Misuse of the nonlocal and global statement can lead to a few unique syntax errors. |
| 661 | |
| 662 | >>> def f(): |
Ivan Levkivskyi | 8c83c23 | 2017-10-26 23:28:35 +0200 | [diff] [blame] | 663 | ... print(x) |
| 664 | ... global x |
| 665 | Traceback (most recent call last): |
| 666 | ... |
| 667 | SyntaxError: name 'x' is used prior to global declaration |
| 668 | |
| 669 | >>> def f(): |
Guido van Rossum | 6cff874 | 2016-09-09 09:36:26 -0700 | [diff] [blame] | 670 | ... x = 1 |
| 671 | ... global x |
| 672 | Traceback (most recent call last): |
| 673 | ... |
| 674 | SyntaxError: name 'x' is assigned to before global declaration |
| 675 | |
Ivan Levkivskyi | 8c83c23 | 2017-10-26 23:28:35 +0200 | [diff] [blame] | 676 | >>> def f(x): |
| 677 | ... global x |
| 678 | Traceback (most recent call last): |
| 679 | ... |
| 680 | SyntaxError: name 'x' is parameter and global |
| 681 | |
Guido van Rossum | 6cff874 | 2016-09-09 09:36:26 -0700 | [diff] [blame] | 682 | >>> def f(): |
| 683 | ... x = 1 |
| 684 | ... def g(): |
| 685 | ... print(x) |
| 686 | ... nonlocal x |
| 687 | Traceback (most recent call last): |
| 688 | ... |
| 689 | SyntaxError: name 'x' is used prior to nonlocal declaration |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 690 | |
Serhiy Storchaka | 3b66ebe | 2017-10-24 00:27:14 +0300 | [diff] [blame] | 691 | >>> def f(): |
| 692 | ... x = 1 |
| 693 | ... def g(): |
| 694 | ... x = 2 |
| 695 | ... nonlocal x |
| 696 | Traceback (most recent call last): |
| 697 | ... |
| 698 | SyntaxError: name 'x' is assigned to before nonlocal declaration |
| 699 | |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 700 | >>> def f(x): |
| 701 | ... nonlocal x |
| 702 | Traceback (most recent call last): |
| 703 | ... |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 704 | SyntaxError: name 'x' is parameter and nonlocal |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 705 | |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 706 | >>> def f(): |
| 707 | ... global x |
| 708 | ... nonlocal x |
| 709 | Traceback (most recent call last): |
| 710 | ... |
| 711 | SyntaxError: name 'x' is nonlocal and global |
| 712 | |
| 713 | >>> def f(): |
| 714 | ... nonlocal x |
| 715 | Traceback (most recent call last): |
| 716 | ... |
| 717 | SyntaxError: no binding for nonlocal 'x' found |
| 718 | |
Nick Coghlan | 4138bfe | 2007-04-23 10:14:27 +0000 | [diff] [blame] | 719 | From SF bug #1705365 |
| 720 | >>> nonlocal x |
| 721 | Traceback (most recent call last): |
| 722 | ... |
| 723 | SyntaxError: nonlocal declaration not allowed at module level |
| 724 | |
Serhiy Storchaka | 3b66ebe | 2017-10-24 00:27:14 +0300 | [diff] [blame] | 725 | From https://bugs.python.org/issue25973 |
Benjamin Peterson | 3cc8f4b | 2015-12-29 10:08:34 -0600 | [diff] [blame] | 726 | >>> class A: |
| 727 | ... def f(self): |
| 728 | ... nonlocal __x |
| 729 | Traceback (most recent call last): |
| 730 | ... |
| 731 | SyntaxError: no binding for nonlocal '_A__x' found |
| 732 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 733 | |
| 734 | This tests assignment-context; there was a bug in Python 2.5 where compiling |
| 735 | a complex 'if' (one with 'elif') would fail to notice an invalid suite, |
| 736 | leading to spurious errors. |
| 737 | |
| 738 | >>> if 1: |
| 739 | ... x() = 1 |
| 740 | ... elif 1: |
| 741 | ... pass |
| 742 | Traceback (most recent call last): |
| 743 | ... |
Pablo Galindo | b86ed8e | 2021-04-12 16:59:30 +0100 | [diff] [blame] | 744 | SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='? |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 745 | |
| 746 | >>> if 1: |
| 747 | ... pass |
| 748 | ... elif 1: |
| 749 | ... x() = 1 |
| 750 | Traceback (most recent call last): |
| 751 | ... |
Pablo Galindo | b86ed8e | 2021-04-12 16:59:30 +0100 | [diff] [blame] | 752 | SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='? |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 753 | |
| 754 | >>> if 1: |
| 755 | ... x() = 1 |
| 756 | ... elif 1: |
| 757 | ... pass |
| 758 | ... else: |
| 759 | ... pass |
| 760 | Traceback (most recent call last): |
| 761 | ... |
Pablo Galindo | b86ed8e | 2021-04-12 16:59:30 +0100 | [diff] [blame] | 762 | SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='? |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 763 | |
| 764 | >>> if 1: |
| 765 | ... pass |
| 766 | ... elif 1: |
| 767 | ... x() = 1 |
| 768 | ... else: |
| 769 | ... pass |
| 770 | Traceback (most recent call last): |
| 771 | ... |
Pablo Galindo | b86ed8e | 2021-04-12 16:59:30 +0100 | [diff] [blame] | 772 | SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='? |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 773 | |
| 774 | >>> if 1: |
| 775 | ... pass |
| 776 | ... elif 1: |
| 777 | ... pass |
| 778 | ... else: |
| 779 | ... x() = 1 |
| 780 | Traceback (most recent call last): |
| 781 | ... |
Pablo Galindo | b86ed8e | 2021-04-12 16:59:30 +0100 | [diff] [blame] | 782 | SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='? |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 783 | |
Pablo Galindo | 58fb156 | 2021-02-02 19:54:22 +0000 | [diff] [blame] | 784 | Missing ':' before suites: |
| 785 | |
| 786 | >>> def f() |
| 787 | ... pass |
| 788 | Traceback (most recent call last): |
| 789 | SyntaxError: expected ':' |
| 790 | |
| 791 | >>> class A |
| 792 | ... pass |
| 793 | Traceback (most recent call last): |
| 794 | SyntaxError: expected ':' |
| 795 | |
| 796 | >>> if 1 |
| 797 | ... pass |
| 798 | ... elif 1: |
| 799 | ... pass |
| 800 | ... else: |
| 801 | ... x() = 1 |
| 802 | Traceback (most recent call last): |
| 803 | SyntaxError: expected ':' |
| 804 | |
| 805 | >>> if 1: |
| 806 | ... pass |
| 807 | ... elif 1 |
| 808 | ... pass |
| 809 | ... else: |
| 810 | ... x() = 1 |
| 811 | Traceback (most recent call last): |
| 812 | SyntaxError: expected ':' |
| 813 | |
| 814 | >>> if 1: |
| 815 | ... pass |
| 816 | ... elif 1: |
| 817 | ... pass |
| 818 | ... else |
| 819 | ... x() = 1 |
| 820 | Traceback (most recent call last): |
| 821 | SyntaxError: expected ':' |
| 822 | |
| 823 | >>> for x in range(10) |
| 824 | ... pass |
| 825 | Traceback (most recent call last): |
| 826 | SyntaxError: expected ':' |
| 827 | |
| 828 | >>> while True |
| 829 | ... pass |
| 830 | Traceback (most recent call last): |
| 831 | SyntaxError: expected ':' |
| 832 | |
| 833 | >>> with blech as something |
| 834 | ... pass |
| 835 | Traceback (most recent call last): |
| 836 | SyntaxError: expected ':' |
| 837 | |
| 838 | >>> with blech |
| 839 | ... pass |
| 840 | Traceback (most recent call last): |
| 841 | SyntaxError: expected ':' |
| 842 | |
| 843 | >>> with blech, block as something |
| 844 | ... pass |
| 845 | Traceback (most recent call last): |
| 846 | SyntaxError: expected ':' |
| 847 | |
| 848 | >>> with blech, block as something, bluch |
| 849 | ... pass |
| 850 | Traceback (most recent call last): |
| 851 | SyntaxError: expected ':' |
| 852 | |
| 853 | >>> with (blech as something) |
| 854 | ... pass |
| 855 | Traceback (most recent call last): |
| 856 | SyntaxError: expected ':' |
| 857 | |
| 858 | >>> with (blech) |
| 859 | ... pass |
| 860 | Traceback (most recent call last): |
| 861 | SyntaxError: expected ':' |
| 862 | |
| 863 | >>> with (blech, block as something) |
| 864 | ... pass |
| 865 | Traceback (most recent call last): |
| 866 | SyntaxError: expected ':' |
| 867 | |
| 868 | >>> with (blech, block as something, bluch) |
| 869 | ... pass |
| 870 | Traceback (most recent call last): |
| 871 | SyntaxError: expected ':' |
| 872 | |
| 873 | >>> try |
| 874 | ... pass |
| 875 | Traceback (most recent call last): |
| 876 | SyntaxError: expected ':' |
| 877 | |
| 878 | >>> try: |
| 879 | ... pass |
| 880 | ... except |
| 881 | ... pass |
| 882 | Traceback (most recent call last): |
| 883 | SyntaxError: expected ':' |
| 884 | |
Pablo Galindo | 08fb8ac | 2021-03-18 01:03:11 +0000 | [diff] [blame] | 885 | >>> match x |
| 886 | ... case list(): |
| 887 | ... pass |
| 888 | Traceback (most recent call last): |
| 889 | SyntaxError: expected ':' |
| 890 | |
| 891 | >>> match x: |
| 892 | ... case list() |
| 893 | ... pass |
| 894 | Traceback (most recent call last): |
| 895 | SyntaxError: expected ':' |
| 896 | |
| 897 | >>> match x: |
| 898 | ... case [y] if y > 0 |
| 899 | ... pass |
| 900 | Traceback (most recent call last): |
| 901 | SyntaxError: expected ':' |
| 902 | |
Pablo Galindo | b86ed8e | 2021-04-12 16:59:30 +0100 | [diff] [blame] | 903 | >>> if x = 3: |
| 904 | ... pass |
| 905 | Traceback (most recent call last): |
| 906 | SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='? |
| 907 | |
| 908 | >>> while x = 3: |
| 909 | ... pass |
| 910 | Traceback (most recent call last): |
| 911 | SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='? |
| 912 | |
| 913 | >>> if x.a = 3: |
| 914 | ... pass |
| 915 | Traceback (most recent call last): |
| 916 | SyntaxError: cannot assign to attribute here. Maybe you meant '==' instead of '='? |
| 917 | |
| 918 | >>> while x.a = 3: |
| 919 | ... pass |
| 920 | Traceback (most recent call last): |
| 921 | SyntaxError: cannot assign to attribute here. Maybe you meant '==' instead of '='? |
| 922 | |
Pablo Galindo | e53f72a | 2021-06-04 00:11:43 +0100 | [diff] [blame] | 923 | Custom error messages for try blocks that are not followed by except/finally |
| 924 | |
| 925 | >>> try: |
| 926 | ... x = 34 |
| 927 | ... |
| 928 | Traceback (most recent call last): |
| 929 | SyntaxError: expected 'except' or 'finally' block |
| 930 | |
Pablo Galindo | d9151cb | 2021-04-13 02:32:33 +0100 | [diff] [blame] | 931 | Ensure that early = are not matched by the parser as invalid comparisons |
Pablo Galindo | b280248 | 2021-04-15 21:38:45 +0100 | [diff] [blame] | 932 | >>> f(2, 4, x=34); 1 $ 2 |
Pablo Galindo | d9151cb | 2021-04-13 02:32:33 +0100 | [diff] [blame] | 933 | Traceback (most recent call last): |
| 934 | SyntaxError: invalid syntax |
| 935 | |
Pablo Galindo | 30ed93b | 2021-04-13 17:51:21 +0100 | [diff] [blame] | 936 | >>> dict(x=34); x $ y |
| 937 | Traceback (most recent call last): |
| 938 | SyntaxError: invalid syntax |
| 939 | |
| 940 | >>> dict(x=34, (x for x in range 10), 1); x $ y |
| 941 | Traceback (most recent call last): |
| 942 | SyntaxError: invalid syntax |
| 943 | |
| 944 | >>> dict(x=34, x=1, y=2); x $ y |
| 945 | Traceback (most recent call last): |
| 946 | SyntaxError: invalid syntax |
| 947 | |
Pablo Galindo | da74350 | 2021-04-15 14:06:39 +0100 | [diff] [blame] | 948 | Incomplete dictionary literals |
| 949 | |
| 950 | >>> {1:2, 3:4, 5} |
| 951 | Traceback (most recent call last): |
| 952 | SyntaxError: ':' expected after dictionary key |
| 953 | |
| 954 | >>> {1:2, 3:4, 5:} |
| 955 | Traceback (most recent call last): |
| 956 | SyntaxError: expression expected after dictionary key and ':' |
| 957 | |
| 958 | >>> {1: *12+1, 23: 1} |
| 959 | Traceback (most recent call last): |
| 960 | SyntaxError: cannot use a starred expression in a dictionary value |
| 961 | |
| 962 | >>> {1: *12+1} |
| 963 | Traceback (most recent call last): |
| 964 | SyntaxError: cannot use a starred expression in a dictionary value |
| 965 | |
| 966 | >>> {1: 23, 1: *12+1} |
| 967 | Traceback (most recent call last): |
| 968 | SyntaxError: cannot use a starred expression in a dictionary value |
| 969 | |
| 970 | >>> {1:} |
| 971 | Traceback (most recent call last): |
| 972 | SyntaxError: expression expected after dictionary key and ':' |
| 973 | |
| 974 | # Ensure that the error is not raise for syntax errors that happen after sets |
| 975 | |
| 976 | >>> {1} $ |
| 977 | Traceback (most recent call last): |
| 978 | SyntaxError: invalid syntax |
| 979 | |
Pablo Galindo | 56c95df | 2021-04-21 15:28:21 +0100 | [diff] [blame] | 980 | Specialized indentation errors: |
| 981 | |
| 982 | >>> while condition: |
| 983 | ... pass |
| 984 | Traceback (most recent call last): |
| 985 | IndentationError: expected an indented block after 'while' statement on line 1 |
| 986 | |
| 987 | >>> for x in range(10): |
| 988 | ... pass |
| 989 | Traceback (most recent call last): |
| 990 | IndentationError: expected an indented block after 'for' statement on line 1 |
| 991 | |
| 992 | >>> for x in range(10): |
| 993 | ... pass |
| 994 | ... else: |
| 995 | ... pass |
| 996 | Traceback (most recent call last): |
| 997 | IndentationError: expected an indented block after 'else' statement on line 3 |
| 998 | |
| 999 | >>> async for x in range(10): |
| 1000 | ... pass |
| 1001 | Traceback (most recent call last): |
| 1002 | IndentationError: expected an indented block after 'for' statement on line 1 |
| 1003 | |
| 1004 | >>> async for x in range(10): |
| 1005 | ... pass |
| 1006 | ... else: |
| 1007 | ... pass |
| 1008 | Traceback (most recent call last): |
| 1009 | IndentationError: expected an indented block after 'else' statement on line 3 |
| 1010 | |
| 1011 | >>> if something: |
| 1012 | ... pass |
| 1013 | Traceback (most recent call last): |
| 1014 | IndentationError: expected an indented block after 'if' statement on line 1 |
| 1015 | |
| 1016 | >>> if something: |
| 1017 | ... pass |
| 1018 | ... elif something_else: |
| 1019 | ... pass |
| 1020 | Traceback (most recent call last): |
| 1021 | IndentationError: expected an indented block after 'elif' statement on line 3 |
| 1022 | |
| 1023 | >>> if something: |
| 1024 | ... pass |
| 1025 | ... elif something_else: |
| 1026 | ... pass |
| 1027 | ... else: |
| 1028 | ... pass |
| 1029 | Traceback (most recent call last): |
| 1030 | IndentationError: expected an indented block after 'else' statement on line 5 |
| 1031 | |
| 1032 | >>> try: |
| 1033 | ... pass |
| 1034 | Traceback (most recent call last): |
| 1035 | IndentationError: expected an indented block after 'try' statement on line 1 |
| 1036 | |
| 1037 | >>> try: |
| 1038 | ... something() |
| 1039 | ... except A: |
| 1040 | ... pass |
| 1041 | Traceback (most recent call last): |
| 1042 | IndentationError: expected an indented block after 'except' statement on line 3 |
| 1043 | |
| 1044 | >>> try: |
| 1045 | ... something() |
| 1046 | ... except A: |
| 1047 | ... pass |
| 1048 | ... finally: |
| 1049 | ... pass |
| 1050 | Traceback (most recent call last): |
| 1051 | IndentationError: expected an indented block after 'finally' statement on line 5 |
| 1052 | |
| 1053 | >>> with A: |
| 1054 | ... pass |
| 1055 | Traceback (most recent call last): |
| 1056 | IndentationError: expected an indented block after 'with' statement on line 1 |
| 1057 | |
| 1058 | >>> with A as a, B as b: |
| 1059 | ... pass |
| 1060 | Traceback (most recent call last): |
| 1061 | IndentationError: expected an indented block after 'with' statement on line 1 |
| 1062 | |
| 1063 | >>> with (A as a, B as b): |
| 1064 | ... pass |
| 1065 | Traceback (most recent call last): |
| 1066 | IndentationError: expected an indented block after 'with' statement on line 1 |
| 1067 | |
| 1068 | >>> async with A: |
| 1069 | ... pass |
| 1070 | Traceback (most recent call last): |
| 1071 | IndentationError: expected an indented block after 'with' statement on line 1 |
| 1072 | |
| 1073 | >>> async with A as a, B as b: |
| 1074 | ... pass |
| 1075 | Traceback (most recent call last): |
| 1076 | IndentationError: expected an indented block after 'with' statement on line 1 |
| 1077 | |
| 1078 | >>> async with (A as a, B as b): |
| 1079 | ... pass |
| 1080 | Traceback (most recent call last): |
| 1081 | IndentationError: expected an indented block after 'with' statement on line 1 |
| 1082 | |
| 1083 | >>> def foo(x, /, y, *, z=2): |
| 1084 | ... pass |
| 1085 | Traceback (most recent call last): |
| 1086 | IndentationError: expected an indented block after function definition on line 1 |
| 1087 | |
| 1088 | >>> class Blech(A): |
| 1089 | ... pass |
| 1090 | Traceback (most recent call last): |
| 1091 | IndentationError: expected an indented block after class definition on line 1 |
| 1092 | |
| 1093 | >>> match something: |
| 1094 | ... pass |
| 1095 | Traceback (most recent call last): |
| 1096 | IndentationError: expected an indented block after 'match' statement on line 1 |
| 1097 | |
| 1098 | >>> match something: |
| 1099 | ... case []: |
| 1100 | ... pass |
| 1101 | Traceback (most recent call last): |
| 1102 | IndentationError: expected an indented block after 'case' statement on line 2 |
| 1103 | |
| 1104 | >>> match something: |
| 1105 | ... case []: |
| 1106 | ... ... |
| 1107 | ... case {}: |
| 1108 | ... pass |
| 1109 | Traceback (most recent call last): |
| 1110 | IndentationError: expected an indented block after 'case' statement on line 4 |
| 1111 | |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 1112 | Make sure that the old "raise X, Y[, Z]" form is gone: |
| 1113 | >>> raise X, Y |
| 1114 | Traceback (most recent call last): |
| 1115 | ... |
| 1116 | SyntaxError: invalid syntax |
| 1117 | >>> raise X, Y, Z |
| 1118 | Traceback (most recent call last): |
| 1119 | ... |
| 1120 | SyntaxError: invalid syntax |
| 1121 | |
Miss Islington (bot) | 9a0e65c | 2021-05-09 14:13:50 -0700 | [diff] [blame] | 1122 | Check that an multiple exception types with missing parentheses |
Pablo Galindo | 206cbda | 2021-02-07 18:42:21 +0000 | [diff] [blame] | 1123 | raise a custom exception |
| 1124 | |
| 1125 | >>> try: |
| 1126 | ... pass |
| 1127 | ... except A, B: |
| 1128 | ... pass |
| 1129 | Traceback (most recent call last): |
Miss Islington (bot) | 9a0e65c | 2021-05-09 14:13:50 -0700 | [diff] [blame] | 1130 | SyntaxError: multiple exception types must be parenthesized |
Pablo Galindo | 206cbda | 2021-02-07 18:42:21 +0000 | [diff] [blame] | 1131 | |
| 1132 | >>> try: |
| 1133 | ... pass |
| 1134 | ... except A, B, C: |
| 1135 | ... pass |
| 1136 | Traceback (most recent call last): |
Miss Islington (bot) | 9a0e65c | 2021-05-09 14:13:50 -0700 | [diff] [blame] | 1137 | SyntaxError: multiple exception types must be parenthesized |
Pablo Galindo | 206cbda | 2021-02-07 18:42:21 +0000 | [diff] [blame] | 1138 | |
| 1139 | >>> try: |
| 1140 | ... pass |
| 1141 | ... except A, B, C as blech: |
| 1142 | ... pass |
| 1143 | Traceback (most recent call last): |
Miss Islington (bot) | 9a0e65c | 2021-05-09 14:13:50 -0700 | [diff] [blame] | 1144 | SyntaxError: multiple exception types must be parenthesized |
Pablo Galindo | 206cbda | 2021-02-07 18:42:21 +0000 | [diff] [blame] | 1145 | |
| 1146 | >>> try: |
| 1147 | ... pass |
| 1148 | ... except A, B, C as blech: |
| 1149 | ... pass |
| 1150 | ... finally: |
| 1151 | ... pass |
| 1152 | Traceback (most recent call last): |
Miss Islington (bot) | 9a0e65c | 2021-05-09 14:13:50 -0700 | [diff] [blame] | 1153 | SyntaxError: multiple exception types must be parenthesized |
Pablo Galindo | 206cbda | 2021-02-07 18:42:21 +0000 | [diff] [blame] | 1154 | |
Benjamin Peterson | 07a1f94 | 2008-07-01 20:03:27 +0000 | [diff] [blame] | 1155 | |
| 1156 | >>> f(a=23, a=234) |
| 1157 | Traceback (most recent call last): |
| 1158 | ... |
Pablo Galindo | 254ec78 | 2020-04-03 20:37:13 +0100 | [diff] [blame] | 1159 | SyntaxError: keyword argument repeated: a |
Benjamin Peterson | 07a1f94 | 2008-07-01 20:03:27 +0000 | [diff] [blame] | 1160 | |
Benjamin Peterson | 1b1a1a4 | 2010-06-24 00:17:03 +0000 | [diff] [blame] | 1161 | >>> {1, 2, 3} = 42 |
| 1162 | Traceback (most recent call last): |
Pablo Galindo | b86ed8e | 2021-04-12 16:59:30 +0100 | [diff] [blame] | 1163 | SyntaxError: cannot assign to set display here. Maybe you meant '==' instead of '='? |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 1164 | |
| 1165 | >>> {1: 2, 3: 4} = 42 |
| 1166 | Traceback (most recent call last): |
Pablo Galindo | b86ed8e | 2021-04-12 16:59:30 +0100 | [diff] [blame] | 1167 | SyntaxError: cannot assign to dict literal here. Maybe you meant '==' instead of '='? |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 1168 | |
| 1169 | >>> f'{x}' = 42 |
| 1170 | Traceback (most recent call last): |
Pablo Galindo | b86ed8e | 2021-04-12 16:59:30 +0100 | [diff] [blame] | 1171 | SyntaxError: cannot assign to f-string expression here. Maybe you meant '==' instead of '='? |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 1172 | |
| 1173 | >>> f'{x}-{y}' = 42 |
| 1174 | Traceback (most recent call last): |
Pablo Galindo | b86ed8e | 2021-04-12 16:59:30 +0100 | [diff] [blame] | 1175 | SyntaxError: cannot assign to f-string expression here. Maybe you meant '==' instead of '='? |
Benjamin Peterson | 1b1a1a4 | 2010-06-24 00:17:03 +0000 | [diff] [blame] | 1176 | |
Miss Islington (bot) | ae1732d | 2021-05-21 11:20:43 -0700 | [diff] [blame] | 1177 | >>> (x, y, z=3, d, e) |
| 1178 | Traceback (most recent call last): |
| 1179 | SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='? |
| 1180 | |
| 1181 | >>> [x, y, z=3, d, e] |
| 1182 | Traceback (most recent call last): |
| 1183 | SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='? |
| 1184 | |
| 1185 | >>> [z=3] |
| 1186 | Traceback (most recent call last): |
| 1187 | SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='? |
| 1188 | |
| 1189 | >>> {x, y, z=3, d, e} |
| 1190 | Traceback (most recent call last): |
| 1191 | SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='? |
| 1192 | |
| 1193 | >>> {z=3} |
| 1194 | Traceback (most recent call last): |
| 1195 | SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='? |
| 1196 | |
Batuhan Taskaya | 72e0aa2 | 2020-05-21 23:41:58 +0300 | [diff] [blame] | 1197 | >>> from t import x, |
| 1198 | Traceback (most recent call last): |
| 1199 | SyntaxError: trailing comma not allowed without surrounding parentheses |
| 1200 | |
| 1201 | >>> from t import x,y, |
| 1202 | Traceback (most recent call last): |
| 1203 | SyntaxError: trailing comma not allowed without surrounding parentheses |
| 1204 | |
Miss Islington (bot) | 846a10f | 2021-08-18 13:32:01 -0700 | [diff] [blame^] | 1205 | # Check that we dont raise the "trailing comma" error if there is more |
| 1206 | # input to the left of the valid part that we parsed. |
| 1207 | |
| 1208 | >>> from t import x,y, and 3 |
| 1209 | Traceback (most recent call last): |
| 1210 | SyntaxError: invalid syntax |
| 1211 | |
Batuhan Taskaya | c8f29ad | 2020-06-27 21:33:08 +0300 | [diff] [blame] | 1212 | >>> (): int |
| 1213 | Traceback (most recent call last): |
| 1214 | SyntaxError: only single target (not tuple) can be annotated |
| 1215 | >>> []: int |
| 1216 | Traceback (most recent call last): |
| 1217 | SyntaxError: only single target (not list) can be annotated |
| 1218 | >>> (()): int |
| 1219 | Traceback (most recent call last): |
| 1220 | SyntaxError: only single target (not tuple) can be annotated |
| 1221 | >>> ([]): int |
| 1222 | Traceback (most recent call last): |
| 1223 | SyntaxError: only single target (not list) can be annotated |
| 1224 | |
Amaury Forgeot d'Arc | 12844e6 | 2010-08-19 21:32:38 +0000 | [diff] [blame] | 1225 | Corner-cases that used to fail to raise the correct error: |
| 1226 | |
| 1227 | >>> def f(*, x=lambda __debug__:0): pass |
| 1228 | Traceback (most recent call last): |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 1229 | SyntaxError: cannot assign to __debug__ |
Amaury Forgeot d'Arc | 12844e6 | 2010-08-19 21:32:38 +0000 | [diff] [blame] | 1230 | |
Pablo Galindo | b0544ba | 2021-04-21 12:41:19 +0100 | [diff] [blame] | 1231 | >>> def f(*args:(lambda __debug__:0)): pass |
| 1232 | Traceback (most recent call last): |
| 1233 | SyntaxError: cannot assign to __debug__ |
| 1234 | |
| 1235 | >>> def f(**kwargs:(lambda __debug__:0)): pass |
| 1236 | Traceback (most recent call last): |
| 1237 | SyntaxError: cannot assign to __debug__ |
| 1238 | |
Lysandros Nikolaou | e10e7c7 | 2020-05-04 13:58:31 +0300 | [diff] [blame] | 1239 | >>> with (lambda *:0): pass |
| 1240 | Traceback (most recent call last): |
| 1241 | SyntaxError: named arguments must follow bare * |
Amaury Forgeot d'Arc | 12844e6 | 2010-08-19 21:32:38 +0000 | [diff] [blame] | 1242 | |
| 1243 | Corner-cases that used to crash: |
| 1244 | |
| 1245 | >>> def f(**__debug__): pass |
| 1246 | Traceback (most recent call last): |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 1247 | SyntaxError: cannot assign to __debug__ |
Amaury Forgeot d'Arc | 12844e6 | 2010-08-19 21:32:38 +0000 | [diff] [blame] | 1248 | |
| 1249 | >>> def f(*xx, __debug__): pass |
| 1250 | Traceback (most recent call last): |
Serhiy Storchaka | 97f1efb | 2018-11-20 19:27:16 +0200 | [diff] [blame] | 1251 | SyntaxError: cannot assign to __debug__ |
Amaury Forgeot d'Arc | 12844e6 | 2010-08-19 21:32:38 +0000 | [diff] [blame] | 1252 | |
Lysandros Nikolaou | 7b7a21b | 2020-05-18 20:32:03 +0300 | [diff] [blame] | 1253 | >>> import ä £ |
| 1254 | Traceback (most recent call last): |
| 1255 | SyntaxError: invalid character '£' (U+00A3) |
Pablo Galindo | a8c418d | 2021-06-18 22:15:57 +0100 | [diff] [blame] | 1256 | |
| 1257 | Invalid pattern matching constructs: |
| 1258 | |
| 1259 | >>> match ...: |
| 1260 | ... case 42 as _: |
| 1261 | ... ... |
| 1262 | Traceback (most recent call last): |
| 1263 | SyntaxError: cannot use '_' as a target |
| 1264 | |
| 1265 | >>> match ...: |
| 1266 | ... case 42 as 1+2+4: |
| 1267 | ... ... |
| 1268 | Traceback (most recent call last): |
| 1269 | SyntaxError: invalid pattern target |
Miss Islington (bot) | 11f1a30 | 2021-06-24 08:34:28 -0700 | [diff] [blame] | 1270 | |
| 1271 | >>> match ...: |
| 1272 | ... case Foo(z=1, y=2, x): |
| 1273 | ... ... |
| 1274 | Traceback (most recent call last): |
| 1275 | SyntaxError: positional patterns follow keyword patterns |
| 1276 | |
| 1277 | >>> match ...: |
| 1278 | ... case Foo(a, z=1, y=2, x): |
| 1279 | ... ... |
| 1280 | Traceback (most recent call last): |
| 1281 | SyntaxError: positional patterns follow keyword patterns |
| 1282 | |
| 1283 | >>> match ...: |
| 1284 | ... case Foo(z=1, x, y=2): |
| 1285 | ... ... |
| 1286 | Traceback (most recent call last): |
| 1287 | SyntaxError: positional patterns follow keyword patterns |
| 1288 | |
| 1289 | >>> match ...: |
| 1290 | ... case C(a=b, c, d=e, f, g=h, i, j=k, ...): |
| 1291 | ... ... |
| 1292 | Traceback (most recent call last): |
| 1293 | SyntaxError: positional patterns follow keyword patterns |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1294 | """ |
| 1295 | |
Jeremy Hylton | 05ab2e6 | 2002-05-31 14:08:29 +0000 | [diff] [blame] | 1296 | import re |
| 1297 | import unittest |
| 1298 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1299 | from test import support |
Jeremy Hylton | 05ab2e6 | 2002-05-31 14:08:29 +0000 | [diff] [blame] | 1300 | |
| 1301 | class SyntaxTestCase(unittest.TestCase): |
| 1302 | |
| 1303 | def _check_error(self, code, errtext, |
Serhiy Storchaka | 8b58339 | 2016-12-11 14:39:01 +0200 | [diff] [blame] | 1304 | filename="<testcase>", mode="exec", subclass=None, lineno=None, offset=None): |
Jeremy Hylton | 05ab2e6 | 2002-05-31 14:08:29 +0000 | [diff] [blame] | 1305 | """Check that compiling code raises SyntaxError with errtext. |
| 1306 | |
| 1307 | errtest is a regular expression that must be present in the |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1308 | test of the exception raised. If subclass is specified it |
| 1309 | is the expected subclass of SyntaxError (e.g. IndentationError). |
Jeremy Hylton | 05ab2e6 | 2002-05-31 14:08:29 +0000 | [diff] [blame] | 1310 | """ |
| 1311 | try: |
| 1312 | compile(code, filename, mode) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 1313 | except SyntaxError as err: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1314 | if subclass and not isinstance(err, subclass): |
| 1315 | self.fail("SyntaxError is not a %s" % subclass.__name__) |
Jeremy Hylton | 05ab2e6 | 2002-05-31 14:08:29 +0000 | [diff] [blame] | 1316 | mo = re.search(errtext, str(err)) |
| 1317 | if mo is None: |
Shantanu | 27c0d9b | 2020-05-11 14:53:58 -0700 | [diff] [blame] | 1318 | self.fail("SyntaxError did not contain %r" % (errtext,)) |
Serhiy Storchaka | 8b58339 | 2016-12-11 14:39:01 +0200 | [diff] [blame] | 1319 | self.assertEqual(err.filename, filename) |
| 1320 | if lineno is not None: |
| 1321 | self.assertEqual(err.lineno, lineno) |
| 1322 | if offset is not None: |
| 1323 | self.assertEqual(err.offset, offset) |
Jeremy Hylton | 05ab2e6 | 2002-05-31 14:08:29 +0000 | [diff] [blame] | 1324 | else: |
| 1325 | self.fail("compile() did not raise SyntaxError") |
| 1326 | |
Pablo Galindo | 43c4fb6 | 2020-12-13 16:46:48 +0000 | [diff] [blame] | 1327 | def test_expression_with_assignment(self): |
| 1328 | self._check_error( |
| 1329 | "print(end1 + end2 = ' ')", |
Pablo Galindo | 30ed93b | 2021-04-13 17:51:21 +0100 | [diff] [blame] | 1330 | 'expression cannot contain assignment, perhaps you meant "=="?', |
Pablo Galindo | a77aac4 | 2021-04-23 14:27:05 +0100 | [diff] [blame] | 1331 | offset=7 |
Pablo Galindo | 43c4fb6 | 2020-12-13 16:46:48 +0000 | [diff] [blame] | 1332 | ) |
| 1333 | |
Lysandros Nikolaou | 15acc4e | 2020-10-27 20:54:20 +0200 | [diff] [blame] | 1334 | def test_curly_brace_after_primary_raises_immediately(self): |
Pablo Galindo Salgado | b977f85 | 2021-07-27 18:52:32 +0100 | [diff] [blame] | 1335 | self._check_error("f{}", "invalid syntax", mode="single") |
Lysandros Nikolaou | 15acc4e | 2020-10-27 20:54:20 +0200 | [diff] [blame] | 1336 | |
Jeremy Hylton | 05ab2e6 | 2002-05-31 14:08:29 +0000 | [diff] [blame] | 1337 | def test_assign_call(self): |
| 1338 | self._check_error("f() = 1", "assign") |
| 1339 | |
| 1340 | def test_assign_del(self): |
Shantanu | 27c0d9b | 2020-05-11 14:53:58 -0700 | [diff] [blame] | 1341 | self._check_error("del (,)", "invalid syntax") |
Pablo Galindo | b86ed8e | 2021-04-12 16:59:30 +0100 | [diff] [blame] | 1342 | self._check_error("del 1", "cannot delete literal") |
| 1343 | self._check_error("del (1, 2)", "cannot delete literal") |
| 1344 | self._check_error("del None", "cannot delete None") |
| 1345 | self._check_error("del *x", "cannot delete starred") |
| 1346 | self._check_error("del (*x)", "cannot use starred expression") |
| 1347 | self._check_error("del (*x,)", "cannot delete starred") |
| 1348 | self._check_error("del [*x,]", "cannot delete starred") |
| 1349 | self._check_error("del f()", "cannot delete function call") |
| 1350 | self._check_error("del f(a, b)", "cannot delete function call") |
| 1351 | self._check_error("del o.f()", "cannot delete function call") |
| 1352 | self._check_error("del a[0]()", "cannot delete function call") |
| 1353 | self._check_error("del x, f()", "cannot delete function call") |
| 1354 | self._check_error("del f(), x", "cannot delete function call") |
| 1355 | self._check_error("del [a, b, ((c), (d,), e.f())]", "cannot delete function call") |
| 1356 | self._check_error("del (a if True else b)", "cannot delete conditional") |
| 1357 | self._check_error("del +a", "cannot delete expression") |
| 1358 | self._check_error("del a, +b", "cannot delete expression") |
| 1359 | self._check_error("del a + b", "cannot delete expression") |
| 1360 | self._check_error("del (a + b, c)", "cannot delete expression") |
| 1361 | self._check_error("del (c[0], a + b)", "cannot delete expression") |
| 1362 | self._check_error("del a.b.c + 2", "cannot delete expression") |
| 1363 | self._check_error("del a.b.c[0] + 2", "cannot delete expression") |
| 1364 | self._check_error("del (a, b, (c, d.e.f + 2))", "cannot delete expression") |
| 1365 | self._check_error("del [a, b, (c, d.e.f[0] + 2)]", "cannot delete expression") |
| 1366 | self._check_error("del (a := 5)", "cannot delete named expression") |
Shantanu | 27c0d9b | 2020-05-11 14:53:58 -0700 | [diff] [blame] | 1367 | # We don't have a special message for this, but make sure we don't |
| 1368 | # report "cannot delete name" |
| 1369 | self._check_error("del a += b", "invalid syntax") |
Jeremy Hylton | 05ab2e6 | 2002-05-31 14:08:29 +0000 | [diff] [blame] | 1370 | |
Ivan Levkivskyi | 8c83c23 | 2017-10-26 23:28:35 +0200 | [diff] [blame] | 1371 | def test_global_param_err_first(self): |
Amaury Forgeot d'Arc | fe7b405 | 2010-09-10 19:47:43 +0000 | [diff] [blame] | 1372 | source = """if 1: |
| 1373 | def error(a): |
| 1374 | global a # SyntaxError |
Ivan Levkivskyi | 8c83c23 | 2017-10-26 23:28:35 +0200 | [diff] [blame] | 1375 | def error2(): |
Amaury Forgeot d'Arc | fe7b405 | 2010-09-10 19:47:43 +0000 | [diff] [blame] | 1376 | b = 1 |
Ivan Levkivskyi | 8c83c23 | 2017-10-26 23:28:35 +0200 | [diff] [blame] | 1377 | global b # SyntaxError |
Amaury Forgeot d'Arc | fe7b405 | 2010-09-10 19:47:43 +0000 | [diff] [blame] | 1378 | """ |
Ivan Levkivskyi | 8c83c23 | 2017-10-26 23:28:35 +0200 | [diff] [blame] | 1379 | self._check_error(source, "parameter and global", lineno=3) |
| 1380 | |
| 1381 | def test_nonlocal_param_err_first(self): |
| 1382 | source = """if 1: |
| 1383 | def error(a): |
| 1384 | nonlocal a # SyntaxError |
| 1385 | def error2(): |
| 1386 | b = 1 |
| 1387 | global b # SyntaxError |
| 1388 | """ |
| 1389 | self._check_error(source, "parameter and nonlocal", lineno=3) |
Jeremy Hylton | 42d9016 | 2003-07-15 20:24:27 +0000 | [diff] [blame] | 1390 | |
Neal Norwitz | fcf4435 | 2005-11-27 20:37:43 +0000 | [diff] [blame] | 1391 | def test_break_outside_loop(self): |
| 1392 | self._check_error("break", "outside loop") |
| 1393 | |
Pablo Galindo | af8646c | 2019-05-17 11:37:08 +0100 | [diff] [blame] | 1394 | def test_yield_outside_function(self): |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 1395 | self._check_error("if 0: yield", "outside function") |
| 1396 | self._check_error("if 0: yield\nelse: x=1", "outside function") |
| 1397 | self._check_error("if 1: pass\nelse: yield", "outside function") |
| 1398 | self._check_error("while 0: yield", "outside function") |
| 1399 | self._check_error("while 0: yield\nelse: x=1", "outside function") |
| 1400 | self._check_error("class C:\n if 0: yield", "outside function") |
| 1401 | self._check_error("class C:\n if 1: pass\n else: yield", |
| 1402 | "outside function") |
| 1403 | self._check_error("class C:\n while 0: yield", "outside function") |
| 1404 | self._check_error("class C:\n while 0: yield\n else: x = 1", |
| 1405 | "outside function") |
Pablo Galindo | af8646c | 2019-05-17 11:37:08 +0100 | [diff] [blame] | 1406 | |
| 1407 | def test_return_outside_function(self): |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 1408 | self._check_error("if 0: return", "outside function") |
| 1409 | self._check_error("if 0: return\nelse: x=1", "outside function") |
| 1410 | self._check_error("if 1: pass\nelse: return", "outside function") |
| 1411 | self._check_error("while 0: return", "outside function") |
| 1412 | self._check_error("class C:\n if 0: return", "outside function") |
| 1413 | self._check_error("class C:\n while 0: return", "outside function") |
| 1414 | self._check_error("class C:\n while 0: return\n else: x=1", |
| 1415 | "outside function") |
| 1416 | self._check_error("class C:\n if 0: return\n else: x= 1", |
| 1417 | "outside function") |
| 1418 | self._check_error("class C:\n if 1: pass\n else: return", |
| 1419 | "outside function") |
Pablo Galindo | af8646c | 2019-05-17 11:37:08 +0100 | [diff] [blame] | 1420 | |
| 1421 | def test_break_outside_loop(self): |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 1422 | self._check_error("if 0: break", "outside loop") |
| 1423 | self._check_error("if 0: break\nelse: x=1", "outside loop") |
| 1424 | self._check_error("if 1: pass\nelse: break", "outside loop") |
| 1425 | self._check_error("class C:\n if 0: break", "outside loop") |
| 1426 | self._check_error("class C:\n if 1: pass\n else: break", |
| 1427 | "outside loop") |
Pablo Galindo | af8646c | 2019-05-17 11:37:08 +0100 | [diff] [blame] | 1428 | |
| 1429 | def test_continue_outside_loop(self): |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 1430 | self._check_error("if 0: continue", "not properly in loop") |
| 1431 | self._check_error("if 0: continue\nelse: x=1", "not properly in loop") |
| 1432 | self._check_error("if 1: pass\nelse: continue", "not properly in loop") |
| 1433 | self._check_error("class C:\n if 0: continue", "not properly in loop") |
| 1434 | self._check_error("class C:\n if 1: pass\n else: continue", |
| 1435 | "not properly in loop") |
Pablo Galindo | af8646c | 2019-05-17 11:37:08 +0100 | [diff] [blame] | 1436 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1437 | def test_unexpected_indent(self): |
| 1438 | self._check_error("foo()\n bar()\n", "unexpected indent", |
| 1439 | subclass=IndentationError) |
| 1440 | |
| 1441 | def test_no_indent(self): |
| 1442 | self._check_error("if 1:\nfoo()", "expected an indented block", |
| 1443 | subclass=IndentationError) |
| 1444 | |
| 1445 | def test_bad_outdent(self): |
| 1446 | self._check_error("if 1:\n foo()\n bar()", |
| 1447 | "unindent does not match .* level", |
| 1448 | subclass=IndentationError) |
| 1449 | |
| 1450 | def test_kwargs_last(self): |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1451 | self._check_error("int(base=10, '2')", |
| 1452 | "positional argument follows keyword argument") |
| 1453 | |
| 1454 | def test_kwargs_last2(self): |
Serhiy Storchaka | 0cc99c8 | 2018-01-04 10:36:35 +0200 | [diff] [blame] | 1455 | self._check_error("int(**{'base': 10}, '2')", |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1456 | "positional argument follows " |
| 1457 | "keyword argument unpacking") |
| 1458 | |
| 1459 | def test_kwargs_last3(self): |
Serhiy Storchaka | 0cc99c8 | 2018-01-04 10:36:35 +0200 | [diff] [blame] | 1460 | self._check_error("int(**{'base': 10}, *['2'])", |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1461 | "iterable argument unpacking follows " |
| 1462 | "keyword argument unpacking") |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1463 | |
Lysandros Nikolaou | 896f4cf | 2020-06-11 02:56:08 +0300 | [diff] [blame] | 1464 | def test_empty_line_after_linecont(self): |
| 1465 | # See issue-40847 |
| 1466 | s = r"""\ |
| 1467 | pass |
| 1468 | \ |
| 1469 | |
| 1470 | pass |
| 1471 | """ |
| 1472 | try: |
| 1473 | compile(s, '<string>', 'exec') |
| 1474 | except SyntaxError: |
| 1475 | self.fail("Empty line after a line continuation character is valid.") |
| 1476 | |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 1477 | @support.cpython_only |
| 1478 | def test_nested_named_except_blocks(self): |
| 1479 | code = "" |
| 1480 | for i in range(12): |
| 1481 | code += f"{' '*i}try:\n" |
| 1482 | code += f"{' '*(i+1)}raise Exception\n" |
| 1483 | code += f"{' '*i}except Exception as e:\n" |
| 1484 | code += f"{' '*4*12}pass" |
| 1485 | self._check_error(code, "too many statically nested blocks") |
Lysandros Nikolaou | 896f4cf | 2020-06-11 02:56:08 +0300 | [diff] [blame] | 1486 | |
Pablo Galindo | 06f8c33 | 2020-10-30 23:48:42 +0000 | [diff] [blame] | 1487 | def test_barry_as_flufl_with_syntax_errors(self): |
| 1488 | # The "barry_as_flufl" rule can produce some "bugs-at-a-distance" if |
| 1489 | # is reading the wrong token in the presence of syntax errors later |
| 1490 | # in the file. See bpo-42214 for more information. |
| 1491 | code = """ |
| 1492 | def func1(): |
| 1493 | if a != b: |
| 1494 | raise ValueError |
| 1495 | |
| 1496 | def func2(): |
| 1497 | try |
| 1498 | return 1 |
| 1499 | finally: |
| 1500 | pass |
| 1501 | """ |
Pablo Galindo | 58fb156 | 2021-02-02 19:54:22 +0000 | [diff] [blame] | 1502 | self._check_error(code, "expected ':'") |
Pablo Galindo | 06f8c33 | 2020-10-30 23:48:42 +0000 | [diff] [blame] | 1503 | |
Pablo Galindo | 96eeff5 | 2021-03-22 17:28:11 +0000 | [diff] [blame] | 1504 | def test_invalid_line_continuation_error_position(self): |
| 1505 | self._check_error(r"a = 3 \ 4", |
| 1506 | "unexpected character after line continuation character", |
| 1507 | lineno=1, offset=9) |
| 1508 | |
Lysandros Nikolaou | 02cdfc9 | 2020-10-31 20:31:41 +0200 | [diff] [blame] | 1509 | def test_invalid_line_continuation_left_recursive(self): |
| 1510 | # Check bpo-42218: SyntaxErrors following left-recursive rules |
| 1511 | # (t_primary_raw in this case) need to be tested explicitly |
| 1512 | self._check_error("A.\u018a\\ ", |
| 1513 | "unexpected character after line continuation character") |
| 1514 | self._check_error("A.\u03bc\\\n", |
| 1515 | "unexpected EOF while parsing") |
| 1516 | |
Pablo Galindo | d6d6371 | 2021-01-19 23:59:33 +0000 | [diff] [blame] | 1517 | def test_error_parenthesis(self): |
| 1518 | for paren in "([{": |
| 1519 | self._check_error(paren + "1 + 2", f"\\{paren}' was never closed") |
| 1520 | |
| 1521 | for paren in ")]}": |
| 1522 | self._check_error(paren + "1 + 2", f"unmatched '\\{paren}'") |
| 1523 | |
Pablo Galindo | 08fb8ac | 2021-03-18 01:03:11 +0000 | [diff] [blame] | 1524 | def test_match_call_does_not_raise_syntax_error(self): |
| 1525 | code = """ |
| 1526 | def match(x): |
| 1527 | return 1+1 |
| 1528 | |
| 1529 | match(34) |
| 1530 | """ |
| 1531 | compile(code, "<string>", "exec") |
| 1532 | |
| 1533 | def test_case_call_does_not_raise_syntax_error(self): |
| 1534 | code = """ |
| 1535 | def case(x): |
| 1536 | return 1+1 |
| 1537 | |
| 1538 | case(34) |
| 1539 | """ |
| 1540 | compile(code, "<string>", "exec") |
Miss Islington (bot) | ec0699c | 2021-05-19 11:28:31 -0700 | [diff] [blame] | 1541 | |
Miss Islington (bot) | 13de28f | 2021-05-07 13:40:09 -0700 | [diff] [blame] | 1542 | def test_multiline_compiler_error_points_to_the_end(self): |
| 1543 | self._check_error( |
| 1544 | "call(\na=1,\na=1\n)", |
| 1545 | "keyword argument repeated", |
| 1546 | lineno=3 |
| 1547 | ) |
Pablo Galindo | 08fb8ac | 2021-03-18 01:03:11 +0000 | [diff] [blame] | 1548 | |
Pablo Galindo | d6d6371 | 2021-01-19 23:59:33 +0000 | [diff] [blame] | 1549 | |
Jeremy Hylton | 05ab2e6 | 2002-05-31 14:08:29 +0000 | [diff] [blame] | 1550 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1551 | support.run_unittest(SyntaxTestCase) |
Jeremy Hylton | c960f26 | 2006-01-27 15:18:39 +0000 | [diff] [blame] | 1552 | from test import test_syntax |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1553 | support.run_doctest(test_syntax, verbosity=True) |
Jeremy Hylton | 05ab2e6 | 2002-05-31 14:08:29 +0000 | [diff] [blame] | 1554 | |
| 1555 | if __name__ == "__main__": |
| 1556 | test_main() |