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