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