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):
8 SyntaxError: name 'x' is parameter and global
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
30 >>> obj.None = 1
31 Traceback (most recent call last):
32 SyntaxError: invalid syntax
33
34 >>> None = 1
35 Traceback (most recent call last):
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
46 >>> (True := 1)
47 Traceback (most recent call last):
48 SyntaxError: cannot use assignment expressions with True
49
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__
57
58 >>> (__debug__ := 1)
59 Traceback (most recent call last):
60 SyntaxError: cannot assign to __debug__
61
62 >>> del __debug__
63 Traceback (most recent call last):
64 SyntaxError: cannot delete __debug__
65
66 >>> f() = 1
67 Traceback (most recent call last):
68 SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='?
69
70 >>> yield = 1
71 Traceback (most recent call last):
72 SyntaxError: assignment to yield expression not possible
73
74 >>> del f()
75 Traceback (most recent call last):
76 SyntaxError: cannot delete function call
77
78 >>> a + 1 = 2
79 Traceback (most recent call last):
80 SyntaxError: cannot assign to expression here. Maybe you meant '==' instead of '='?
81
82 >>> (x for x in x) = 1
83 Traceback (most recent call last):
84 SyntaxError: cannot assign to generator expression
85
86 >>> 1 = 1
87 Traceback (most recent call last):
88 SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='?
89
90 >>> "abc" = 1
91 Traceback (most recent call last):
92 SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='?
93
94 >>> b"" = 1
95 Traceback (most recent call last):
96 SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='?
97
98 >>> ... = 1
99 Traceback (most recent call last):
100 SyntaxError: cannot assign to ellipsis here. Maybe you meant '==' instead of '='?
101
102 >>> `1` = 1
103 Traceback (most recent call last):
104 SyntaxError: invalid syntax
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
111 >>> (a, "b", c) = (1, 2, 3)
112 Traceback (most recent call last):
113 SyntaxError: cannot assign to literal
114
115 >>> (a, True, c) = (1, 2, 3)
116 Traceback (most recent call last):
117 SyntaxError: cannot assign to True
118
119 >>> (a, __debug__, c) = (1, 2, 3)
120 Traceback (most recent call last):
121 SyntaxError: cannot assign to __debug__
122
123 >>> (a, *True, c) = (1, 2, 3)
124 Traceback (most recent call last):
125 SyntaxError: cannot assign to True
126
127 >>> (a, *__debug__, c) = (1, 2, 3)
128 Traceback (most recent call last):
129 SyntaxError: cannot assign to __debug__
130
131 >>> [a, b, c + 1] = [1, 2, 3]
132 Traceback (most recent call last):
133 SyntaxError: cannot assign to expression
134
135 >>> [a, b[1], c + 1] = [1, 2, 3]
136 Traceback (most recent call last):
137 SyntaxError: cannot assign to expression
138
139 >>> [a, b.c.d, c + 1] = [1, 2, 3]
140 Traceback (most recent call last):
141 SyntaxError: cannot assign to expression
142
143 >>> a if 1 else b = 1
144 Traceback (most recent call last):
145 SyntaxError: cannot assign to conditional expression
146
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
159 >>> if True:
160 ... print("Hello"
161 ...
162 ... if 2:
163 ... print(123))
164 Traceback (most recent call last):
165 SyntaxError: invalid syntax
166
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
179 >>> a, b += 1, 2
180 Traceback (most recent call last):
181 SyntaxError: 'tuple' is an illegal expression for augmented assignment
182
183 >>> (a, b) += 1, 2
184 Traceback (most recent call last):
185 SyntaxError: 'tuple' is an illegal expression for augmented assignment
186
187 >>> [a, b] += 1, 2
188 Traceback (most recent call last):
189 SyntaxError: 'list' is an illegal expression for augmented assignment
190
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):
208 SyntaxError: cannot assign to expression
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):
220 SyntaxError: cannot assign to expression
221
222 >>> for i < (): pass
223 Traceback (most recent call last):
224 SyntaxError: invalid syntax
225
226 >>> for a, b
227 Traceback (most recent call last):
228 SyntaxError: invalid syntax
229
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):
244 SyntaxError: cannot assign to expression
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
254 >>> with a as b
255 Traceback (most recent call last):
256 SyntaxError: expected ':'
257
258 >>> p = p =
259 Traceback (most recent call last):
260 SyntaxError: invalid syntax
261
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
273 # Missing commas in literals collections should not
274 # produce special error messages regarding missing
275 # parentheses, but about missing commas instead
276
277 >>> [1, 2 3]
278 Traceback (most recent call last):
279 SyntaxError: invalid syntax. Perhaps you forgot a comma?
280
281 >>> {1, 2 3}
282 Traceback (most recent call last):
283 SyntaxError: invalid syntax. Perhaps you forgot a comma?
284
285 >>> {1:2, 2:5 3:12}
286 Traceback (most recent call last):
287 SyntaxError: invalid syntax. Perhaps you forgot a comma?
288
289 >>> (1, 2 3)
290 Traceback (most recent call last):
291 SyntaxError: invalid syntax. Perhaps you forgot a comma?
292
293 # Make sure soft keywords constructs don't raise specialized
294 # errors regarding missing commas or other spezialiced errors
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):
305 SyntaxError: invalid syntax
306
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
325 From compiler_complex_args():
326
327 >>> def f(None=1):
328 ... pass
329 Traceback (most recent call last):
330 SyntaxError: invalid syntax
331
332 From ast_for_arguments():
333
334 >>> def f(x, y=1, z):
335 ... pass
336 Traceback (most recent call last):
337 SyntaxError: non-default argument follows default argument
338
339 >>> def f(x, None):
340 ... pass
341 Traceback (most recent call last):
342 SyntaxError: invalid syntax
343
344 >>> def f(*None):
345 ... pass
346 Traceback (most recent call last):
347 SyntaxError: invalid syntax
348
349 >>> def f(**None):
350 ... pass
351 Traceback (most recent call last):
352 SyntaxError: invalid syntax
353
354 >>> def foo(/,a,b=,c):
355 ... pass
356 Traceback (most recent call last):
357 SyntaxError: at least one argument must precede /
358
359 >>> def foo(a,/,/,b,c):
360 ... pass
361 Traceback (most recent call last):
362 SyntaxError: / may appear only once
363
364 >>> def foo(a,/,a1,/,b,c):
365 ... pass
366 Traceback (most recent call last):
367 SyntaxError: / may appear only once
368
369 >>> def foo(a=1,/,/,*b,/,c):
370 ... pass
371 Traceback (most recent call last):
372 SyntaxError: / may appear only once
373
374 >>> def foo(a,/,a1=1,/,b,c):
375 ... pass
376 Traceback (most recent call last):
377 SyntaxError: / may appear only once
378
379 >>> def foo(a,*b,c,/,d,e):
380 ... pass
381 Traceback (most recent call last):
382 SyntaxError: / must be ahead of *
383
384 >>> def foo(a=1,*b,c=3,/,d,e):
385 ... pass
386 Traceback (most recent call last):
387 SyntaxError: / must be ahead of *
388
389 >>> def foo(a,*b=3,c):
390 ... pass
391 Traceback (most recent call last):
392 SyntaxError: var-positional argument cannot have default value
393
394 >>> def foo(a,*b: int=,c):
395 ... pass
396 Traceback (most recent call last):
397 SyntaxError: var-positional argument cannot have default value
398
399 >>> def foo(a,**b=3):
400 ... pass
401 Traceback (most recent call last):
402 SyntaxError: var-keyword argument cannot have default value
403
404 >>> def foo(a,**b: int=3):
405 ... pass
406 Traceback (most recent call last):
407 SyntaxError: var-keyword argument cannot have default value
408
409 >>> def foo(a,*a, b, **c, d):
410 ... pass
411 Traceback (most recent call last):
412 SyntaxError: arguments cannot follow var-keyword argument
413
414 >>> def foo(a,*a, b, **c, d=4):
415 ... pass
416 Traceback (most recent call last):
417 SyntaxError: arguments cannot follow var-keyword argument
418
419 >>> def foo(a,*a, b, **c, *d):
420 ... pass
421 Traceback (most recent call last):
422 SyntaxError: arguments cannot follow var-keyword argument
423
424 >>> def foo(a,*a, b, **c, **d):
425 ... pass
426 Traceback (most recent call last):
427 SyntaxError: arguments cannot follow var-keyword argument
428
429 >>> def foo(a=1,/,**b,/,c):
430 ... pass
431 Traceback (most recent call last):
432 SyntaxError: arguments cannot follow var-keyword argument
433
434 >>> def foo(*b,*d):
435 ... pass
436 Traceback (most recent call last):
437 SyntaxError: * argument may appear only once
438
439 >>> def foo(a,*b,c,*d,*e,c):
440 ... pass
441 Traceback (most recent call last):
442 SyntaxError: * argument may appear only once
443
444 >>> def foo(a,b,/,c,*b,c,*d,*e,c):
445 ... pass
446 Traceback (most recent call last):
447 SyntaxError: * argument may appear only once
448
449 >>> def foo(a,b,/,c,*b,c,*d,**e):
450 ... pass
451 Traceback (most recent call last):
452 SyntaxError: * argument may appear only once
453
454 >>> def foo(a=1,/*,b,c):
455 ... pass
456 Traceback (most recent call last):
457 SyntaxError: expected comma between / and *
458
459 >>> def foo(a=1,d=,c):
460 ... pass
461 Traceback (most recent call last):
462 SyntaxError: expected default value expression
463
464 >>> def foo(a,d=,c):
465 ... pass
466 Traceback (most recent call last):
467 SyntaxError: expected default value expression
468
469 >>> def foo(a,d: int=,c):
470 ... pass
471 Traceback (most recent call last):
472 SyntaxError: expected default value expression
473
474 >>> lambda /,a,b,c: None
475 Traceback (most recent call last):
476 SyntaxError: at least one argument must precede /
477
478 >>> lambda a,/,/,b,c: None
479 Traceback (most recent call last):
480 SyntaxError: / may appear only once
481
482 >>> lambda a,/,a1,/,b,c: None
483 Traceback (most recent call last):
484 SyntaxError: / may appear only once
485
486 >>> lambda a=1,/,/,*b,/,c: None
487 Traceback (most recent call last):
488 SyntaxError: / may appear only once
489
490 >>> lambda a,/,a1=1,/,b,c: None
491 Traceback (most recent call last):
492 SyntaxError: / may appear only once
493
494 >>> lambda a,*b,c,/,d,e: None
495 Traceback (most recent call last):
496 SyntaxError: / must be ahead of *
497
498 >>> lambda a=1,*b,c=3,/,d,e: None
499 Traceback (most recent call last):
500 SyntaxError: / must be ahead of *
501
502 >>> lambda a=1,/*,b,c: None
503 Traceback (most recent call last):
504 SyntaxError: expected comma between / and *
505
506 >>> lambda a,*b=3,c: None
507 Traceback (most recent call last):
508 SyntaxError: var-positional argument cannot have default value
509
510 >>> lambda a,**b=3: None
511 Traceback (most recent call last):
512 SyntaxError: var-keyword argument cannot have default value
513
514 >>> lambda a, *a, b, **c, d: None
515 Traceback (most recent call last):
516 SyntaxError: arguments cannot follow var-keyword argument
517
518 >>> lambda a,*a, b, **c, d=4: None
519 Traceback (most recent call last):
520 SyntaxError: arguments cannot follow var-keyword argument
521
522 >>> lambda a,*a, b, **c, *d: None
523 Traceback (most recent call last):
524 SyntaxError: arguments cannot follow var-keyword argument
525
526 >>> lambda a,*a, b, **c, **d: None
527 Traceback (most recent call last):
528 SyntaxError: arguments cannot follow var-keyword argument
529
530 >>> lambda a=1,/,**b,/,c: None
531 Traceback (most recent call last):
532 SyntaxError: arguments cannot follow var-keyword argument
533
534 >>> lambda *b,*d: None
535 Traceback (most recent call last):
536 SyntaxError: * argument may appear only once
537
538 >>> lambda a,*b,c,*d,*e,c: None
539 Traceback (most recent call last):
540 SyntaxError: * argument may appear only once
541
542 >>> lambda a,b,/,c,*b,c,*d,*e,c: None
543 Traceback (most recent call last):
544 SyntaxError: * argument may appear only once
545
546 >>> lambda a,b,/,c,*b,c,*d,**e: None
547 Traceback (most recent call last):
548 SyntaxError: * argument may appear only once
549
550 >>> lambda a=1,d=,c: None
551 Traceback (most recent call last):
552 SyntaxError: expected default value expression
553
554 >>> lambda a,d=,c: None
555 Traceback (most recent call last):
556 SyntaxError: expected default value expression
557
558 >>> import ast; ast.parse('''
559 ... def f(
560 ... *, # type: int
561 ... a, # type: int
562 ... ):
563 ... pass
564 ... ''', type_comments=True)
565 Traceback (most recent call last):
566 SyntaxError: bare * has associated type comment
567
568
569 From ast_for_funcdef():
570
571 >>> def None(x):
572 ... pass
573 Traceback (most recent call last):
574 SyntaxError: invalid syntax
575
576
577 From ast_for_call():
578
579 >>> def f(it, *varargs, **kwargs):
580 ... return list(it)
581 >>> L = range(10)
582 >>> f(x for x in L)
583 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
584 >>> f(x for x in L, 1)
585 Traceback (most recent call last):
586 SyntaxError: Generator expression must be parenthesized
587 >>> f(x for x in L, y=1)
588 Traceback (most recent call last):
589 SyntaxError: Generator expression must be parenthesized
590 >>> f(x for x in L, *[])
591 Traceback (most recent call last):
592 SyntaxError: Generator expression must be parenthesized
593 >>> f(x for x in L, **{})
594 Traceback (most recent call last):
595 SyntaxError: Generator expression must be parenthesized
596 >>> f(L, x for x in L)
597 Traceback (most recent call last):
598 SyntaxError: Generator expression must be parenthesized
599 >>> f(x for x in L, y for y in L)
600 Traceback (most recent call last):
601 SyntaxError: Generator expression must be parenthesized
602 >>> f(x for x in L,)
603 Traceback (most recent call last):
604 SyntaxError: Generator expression must be parenthesized
605 >>> f((x for x in L), 1)
606 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
607 >>> class C(x for x in L):
608 ... pass
609 Traceback (most recent call last):
610 SyntaxError: invalid syntax
611
612 >>> def g(*args, **kwargs):
613 ... print(args, sorted(kwargs.items()))
614 >>> g(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
615 ... 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37,
616 ... 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55,
617 ... 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73,
618 ... 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91,
619 ... 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107,
620 ... 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121,
621 ... 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135,
622 ... 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149,
623 ... 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163,
624 ... 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177,
625 ... 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
626 ... 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205,
627 ... 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219,
628 ... 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233,
629 ... 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247,
630 ... 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261,
631 ... 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275,
632 ... 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289,
633 ... 290, 291, 292, 293, 294, 295, 296, 297, 298, 299) # doctest: +ELLIPSIS
634 (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ..., 297, 298, 299) []
635
636 >>> g(a000=0, a001=1, a002=2, a003=3, a004=4, a005=5, a006=6, a007=7, a008=8,
637 ... a009=9, a010=10, a011=11, a012=12, a013=13, a014=14, a015=15, a016=16,
638 ... a017=17, a018=18, a019=19, a020=20, a021=21, a022=22, a023=23, a024=24,
639 ... a025=25, a026=26, a027=27, a028=28, a029=29, a030=30, a031=31, a032=32,
640 ... a033=33, a034=34, a035=35, a036=36, a037=37, a038=38, a039=39, a040=40,
641 ... a041=41, a042=42, a043=43, a044=44, a045=45, a046=46, a047=47, a048=48,
642 ... a049=49, a050=50, a051=51, a052=52, a053=53, a054=54, a055=55, a056=56,
643 ... a057=57, a058=58, a059=59, a060=60, a061=61, a062=62, a063=63, a064=64,
644 ... a065=65, a066=66, a067=67, a068=68, a069=69, a070=70, a071=71, a072=72,
645 ... a073=73, a074=74, a075=75, a076=76, a077=77, a078=78, a079=79, a080=80,
646 ... a081=81, a082=82, a083=83, a084=84, a085=85, a086=86, a087=87, a088=88,
647 ... a089=89, a090=90, a091=91, a092=92, a093=93, a094=94, a095=95, a096=96,
648 ... a097=97, a098=98, a099=99, a100=100, a101=101, a102=102, a103=103,
649 ... a104=104, a105=105, a106=106, a107=107, a108=108, a109=109, a110=110,
650 ... a111=111, a112=112, a113=113, a114=114, a115=115, a116=116, a117=117,
651 ... a118=118, a119=119, a120=120, a121=121, a122=122, a123=123, a124=124,
652 ... a125=125, a126=126, a127=127, a128=128, a129=129, a130=130, a131=131,
653 ... a132=132, a133=133, a134=134, a135=135, a136=136, a137=137, a138=138,
654 ... a139=139, a140=140, a141=141, a142=142, a143=143, a144=144, a145=145,
655 ... a146=146, a147=147, a148=148, a149=149, a150=150, a151=151, a152=152,
656 ... a153=153, a154=154, a155=155, a156=156, a157=157, a158=158, a159=159,
657 ... a160=160, a161=161, a162=162, a163=163, a164=164, a165=165, a166=166,
658 ... a167=167, a168=168, a169=169, a170=170, a171=171, a172=172, a173=173,
659 ... a174=174, a175=175, a176=176, a177=177, a178=178, a179=179, a180=180,
660 ... a181=181, a182=182, a183=183, a184=184, a185=185, a186=186, a187=187,
661 ... a188=188, a189=189, a190=190, a191=191, a192=192, a193=193, a194=194,
662 ... a195=195, a196=196, a197=197, a198=198, a199=199, a200=200, a201=201,
663 ... a202=202, a203=203, a204=204, a205=205, a206=206, a207=207, a208=208,
664 ... a209=209, a210=210, a211=211, a212=212, a213=213, a214=214, a215=215,
665 ... a216=216, a217=217, a218=218, a219=219, a220=220, a221=221, a222=222,
666 ... a223=223, a224=224, a225=225, a226=226, a227=227, a228=228, a229=229,
667 ... a230=230, a231=231, a232=232, a233=233, a234=234, a235=235, a236=236,
668 ... a237=237, a238=238, a239=239, a240=240, a241=241, a242=242, a243=243,
669 ... a244=244, a245=245, a246=246, a247=247, a248=248, a249=249, a250=250,
670 ... a251=251, a252=252, a253=253, a254=254, a255=255, a256=256, a257=257,
671 ... a258=258, a259=259, a260=260, a261=261, a262=262, a263=263, a264=264,
672 ... a265=265, a266=266, a267=267, a268=268, a269=269, a270=270, a271=271,
673 ... a272=272, a273=273, a274=274, a275=275, a276=276, a277=277, a278=278,
674 ... a279=279, a280=280, a281=281, a282=282, a283=283, a284=284, a285=285,
675 ... a286=286, a287=287, a288=288, a289=289, a290=290, a291=291, a292=292,
676 ... a293=293, a294=294, a295=295, a296=296, a297=297, a298=298, a299=299)
677 ... # doctest: +ELLIPSIS
678 () [('a000', 0), ('a001', 1), ('a002', 2), ..., ('a298', 298), ('a299', 299)]
679
680 >>> class C:
681 ... def meth(self, *args):
682 ... return args
683 >>> obj = C()
684 >>> obj.meth(
685 ... 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
686 ... 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37,
687 ... 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55,
688 ... 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73,
689 ... 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91,
690 ... 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107,
691 ... 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121,
692 ... 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135,
693 ... 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149,
694 ... 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163,
695 ... 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177,
696 ... 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
697 ... 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205,
698 ... 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219,
699 ... 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233,
700 ... 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247,
701 ... 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261,
702 ... 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275,
703 ... 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289,
704 ... 290, 291, 292, 293, 294, 295, 296, 297, 298, 299) # doctest: +ELLIPSIS
705 (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ..., 297, 298, 299)
706
707 >>> f(lambda x: x[0] = 3)
708 Traceback (most recent call last):
709 SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
710
711 # Check that this error doesn't trigger for names:
712 >>> f(a={x: for x in {}})
713 Traceback (most recent call last):
714 SyntaxError: invalid syntax
715
716 The grammar accepts any test (basically, any expression) in the
717 keyword slot of a call site. Test a few different options.
718
719 >>> f(x()=2)
720 Traceback (most recent call last):
721 SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
722 >>> f(a or b=1)
723 Traceback (most recent call last):
724 SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
725 >>> f(x.y=1)
726 Traceback (most recent call last):
727 SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
728 >>> f((x)=2)
729 Traceback (most recent call last):
730 SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
731 >>> f(True=1)
732 Traceback (most recent call last):
733 SyntaxError: cannot assign to True
734 >>> f(False=1)
735 Traceback (most recent call last):
736 SyntaxError: cannot assign to False
737 >>> f(None=1)
738 Traceback (most recent call last):
739 SyntaxError: cannot assign to None
740 >>> f(__debug__=1)
741 Traceback (most recent call last):
742 SyntaxError: cannot assign to __debug__
743 >>> __debug__: int
744 Traceback (most recent call last):
745 SyntaxError: cannot assign to __debug__
746
747
748 More set_context():
749
750 >>> (x for x in x) += 1
751 Traceback (most recent call last):
752 SyntaxError: 'generator expression' is an illegal expression for augmented assignment
753 >>> None += 1
754 Traceback (most recent call last):
755 SyntaxError: 'None' is an illegal expression for augmented assignment
756 >>> __debug__ += 1
757 Traceback (most recent call last):
758 SyntaxError: cannot assign to __debug__
759 >>> f() += 1
760 Traceback (most recent call last):
761 SyntaxError: 'function call' is an illegal expression for augmented assignment
762
763
764 Test continue in finally in weird combinations.
765
766 continue in for loop under finally should be ok.
767
768 >>> def test():
769 ... try:
770 ... pass
771 ... finally:
772 ... for abc in range(10):
773 ... continue
774 ... print(abc)
775 >>> test()
776 9
777
778 continue in a finally should be ok.
779
780 >>> def test():
781 ... for abc in range(10):
782 ... try:
783 ... pass
784 ... finally:
785 ... continue
786 ... print(abc)
787 >>> test()
788 9
789
790 >>> def test():
791 ... for abc in range(10):
792 ... try:
793 ... pass
794 ... finally:
795 ... try:
796 ... continue
797 ... except:
798 ... pass
799 ... print(abc)
800 >>> test()
801 9
802
803 >>> def test():
804 ... for abc in range(10):
805 ... try:
806 ... pass
807 ... finally:
808 ... try:
809 ... pass
810 ... except:
811 ... continue
812 ... print(abc)
813 >>> test()
814 9
815
816 A continue outside loop should not be allowed.
817
818 >>> def foo():
819 ... try:
820 ... pass
821 ... finally:
822 ... continue
823 Traceback (most recent call last):
824 ...
825 SyntaxError: 'continue' not properly in loop
826
827 There is one test for a break that is not in a loop. The compiler
828 uses a single data structure to keep track of try-finally and loops,
829 so we need to be sure that a break is actually inside a loop. If it
830 isn't, there should be a syntax error.
831
832 >>> try:
833 ... print(1)
834 ... break
835 ... print(2)
836 ... finally:
837 ... print(3)
838 Traceback (most recent call last):
839 ...
840 SyntaxError: 'break' outside loop
841
842 Misuse of the nonlocal and global statement can lead to a few unique syntax errors.
843
844 >>> def f():
845 ... print(x)
846 ... global x
847 Traceback (most recent call last):
848 ...
849 SyntaxError: name 'x' is used prior to global declaration
850
851 >>> def f():
852 ... x = 1
853 ... global x
854 Traceback (most recent call last):
855 ...
856 SyntaxError: name 'x' is assigned to before global declaration
857
858 >>> def f(x):
859 ... global x
860 Traceback (most recent call last):
861 ...
862 SyntaxError: name 'x' is parameter and global
863
864 >>> def f():
865 ... x = 1
866 ... def g():
867 ... print(x)
868 ... nonlocal x
869 Traceback (most recent call last):
870 ...
871 SyntaxError: name 'x' is used prior to nonlocal declaration
872
873 >>> def f():
874 ... x = 1
875 ... def g():
876 ... x = 2
877 ... nonlocal x
878 Traceback (most recent call last):
879 ...
880 SyntaxError: name 'x' is assigned to before nonlocal declaration
881
882 >>> def f(x):
883 ... nonlocal x
884 Traceback (most recent call last):
885 ...
886 SyntaxError: name 'x' is parameter and nonlocal
887
888 >>> def f():
889 ... global x
890 ... nonlocal x
891 Traceback (most recent call last):
892 ...
893 SyntaxError: name 'x' is nonlocal and global
894
895 >>> def f():
896 ... nonlocal x
897 Traceback (most recent call last):
898 ...
899 SyntaxError: no binding for nonlocal 'x' found
900
901 From SF bug #1705365
902 >>> nonlocal x
903 Traceback (most recent call last):
904 ...
905 SyntaxError: nonlocal declaration not allowed at module level
906
907 From https://bugs.python.org/issue25973
908 >>> class A:
909 ... def f(self):
910 ... nonlocal __x
911 Traceback (most recent call last):
912 ...
913 SyntaxError: no binding for nonlocal '_A__x' found
914
915
916 This tests assignment-context; there was a bug in Python 2.5 where compiling
917 a complex 'if' (one with 'elif') would fail to notice an invalid suite,
918 leading to spurious errors.
919
920 >>> if 1:
921 ... x() = 1
922 ... elif 1:
923 ... pass
924 Traceback (most recent call last):
925 ...
926 SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='?
927
928 >>> if 1:
929 ... pass
930 ... elif 1:
931 ... x() = 1
932 Traceback (most recent call last):
933 ...
934 SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='?
935
936 >>> if 1:
937 ... x() = 1
938 ... elif 1:
939 ... pass
940 ... else:
941 ... pass
942 Traceback (most recent call last):
943 ...
944 SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='?
945
946 >>> if 1:
947 ... pass
948 ... elif 1:
949 ... x() = 1
950 ... else:
951 ... pass
952 Traceback (most recent call last):
953 ...
954 SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='?
955
956 >>> if 1:
957 ... pass
958 ... elif 1:
959 ... pass
960 ... else:
961 ... x() = 1
962 Traceback (most recent call last):
963 ...
964 SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='?
965
966 Missing ':' before suites:
967
968 >>> def f()
969 ... pass
970 Traceback (most recent call last):
971 SyntaxError: expected ':'
972
973 >>> class A
974 ... pass
975 Traceback (most recent call last):
976 SyntaxError: expected ':'
977
978 >>> class R&D:
979 ... pass
980 Traceback (most recent call last):
981 SyntaxError: invalid syntax
982
983 >>> if 1
984 ... pass
985 ... elif 1:
986 ... pass
987 ... else:
988 ... x() = 1
989 Traceback (most recent call last):
990 SyntaxError: expected ':'
991
992 >>> if 1:
993 ... pass
994 ... elif 1
995 ... pass
996 ... else:
997 ... x() = 1
998 Traceback (most recent call last):
999 SyntaxError: expected ':'
1000
1001 >>> if 1:
1002 ... pass
1003 ... elif 1:
1004 ... pass
1005 ... else
1006 ... x() = 1
1007 Traceback (most recent call last):
1008 SyntaxError: expected ':'
1009
1010 >>> for x in range(10)
1011 ... pass
1012 Traceback (most recent call last):
1013 SyntaxError: expected ':'
1014
1015 >>> for x in range 10:
1016 ... pass
1017 Traceback (most recent call last):
1018 SyntaxError: invalid syntax
1019
1020 >>> while True
1021 ... pass
1022 Traceback (most recent call last):
1023 SyntaxError: expected ':'
1024
1025 >>> with blech as something
1026 ... pass
1027 Traceback (most recent call last):
1028 SyntaxError: expected ':'
1029
1030 >>> with blech
1031 ... pass
1032 Traceback (most recent call last):
1033 SyntaxError: expected ':'
1034
1035 >>> with blech, block as something
1036 ... pass
1037 Traceback (most recent call last):
1038 SyntaxError: expected ':'
1039
1040 >>> with blech, block as something, bluch
1041 ... pass
1042 Traceback (most recent call last):
1043 SyntaxError: expected ':'
1044
1045 >>> with (blech as something)
1046 ... pass
1047 Traceback (most recent call last):
1048 SyntaxError: expected ':'
1049
1050 >>> with (blech)
1051 ... pass
1052 Traceback (most recent call last):
1053 SyntaxError: expected ':'
1054
1055 >>> with (blech, block as something)
1056 ... pass
1057 Traceback (most recent call last):
1058 SyntaxError: expected ':'
1059
1060 >>> with (blech, block as something, bluch)
1061 ... pass
1062 Traceback (most recent call last):
1063 SyntaxError: expected ':'
1064
1065 >>> with block ad something:
1066 ... pass
1067 Traceback (most recent call last):
1068 SyntaxError: invalid syntax
1069
1070 >>> try
1071 ... pass
1072 Traceback (most recent call last):
1073 SyntaxError: expected ':'
1074
1075 >>> try:
1076 ... pass
1077 ... except
1078 ... pass
1079 Traceback (most recent call last):
1080 SyntaxError: expected ':'
1081
1082 >>> match x
1083 ... case list():
1084 ... pass
1085 Traceback (most recent call last):
1086 SyntaxError: expected ':'
1087
1088 >>> match x x:
1089 ... case list():
1090 ... pass
1091 Traceback (most recent call last):
1092 SyntaxError: invalid syntax
1093
1094 >>> match x:
1095 ... case list()
1096 ... pass
1097 Traceback (most recent call last):
1098 SyntaxError: expected ':'
1099
1100 >>> match x:
1101 ... case [y] if y > 0
1102 ... pass
1103 Traceback (most recent call last):
1104 SyntaxError: expected ':'
1105
1106 >>> if x = 3:
1107 ... pass
1108 Traceback (most recent call last):
1109 SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?
1110
1111 >>> while x = 3:
1112 ... pass
1113 Traceback (most recent call last):
1114 SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?
1115
1116 >>> if x.a = 3:
1117 ... pass
1118 Traceback (most recent call last):
1119 SyntaxError: cannot assign to attribute here. Maybe you meant '==' instead of '='?
1120
1121 >>> while x.a = 3:
1122 ... pass
1123 Traceback (most recent call last):
1124 SyntaxError: cannot assign to attribute here. Maybe you meant '==' instead of '='?
1125
1126
1127 Missing parens after function definition
1128
1129 >>> def f:
1130 Traceback (most recent call last):
1131 SyntaxError: expected '('
1132
1133 >>> async def f:
1134 Traceback (most recent call last):
1135 SyntaxError: expected '('
1136
1137 Parenthesized arguments in function definitions
1138
1139 >>> def f(x, (y, z), w):
1140 ... pass
1141 Traceback (most recent call last):
1142 SyntaxError: Function parameters cannot be parenthesized
1143
1144 >>> def f((x, y, z, w)):
1145 ... pass
1146 Traceback (most recent call last):
1147 SyntaxError: Function parameters cannot be parenthesized
1148
1149 >>> def f(x, (y, z, w)):
1150 ... pass
1151 Traceback (most recent call last):
1152 SyntaxError: Function parameters cannot be parenthesized
1153
1154 >>> def f((x, y, z), w):
1155 ... pass
1156 Traceback (most recent call last):
1157 SyntaxError: Function parameters cannot be parenthesized
1158
1159 >>> lambda x, (y, z), w: None
1160 Traceback (most recent call last):
1161 SyntaxError: Lambda expression parameters cannot be parenthesized
1162
1163 >>> lambda (x, y, z, w): None
1164 Traceback (most recent call last):
1165 SyntaxError: Lambda expression parameters cannot be parenthesized
1166
1167 >>> lambda x, (y, z, w): None
1168 Traceback (most recent call last):
1169 SyntaxError: Lambda expression parameters cannot be parenthesized
1170
1171 >>> lambda (x, y, z), w: None
1172 Traceback (most recent call last):
1173 SyntaxError: Lambda expression parameters cannot be parenthesized
1174
1175 Custom error messages for try blocks that are not followed by except/finally
1176
1177 >>> try:
1178 ... x = 34
1179 ...
1180 Traceback (most recent call last):
1181 SyntaxError: expected 'except' or 'finally' block
1182
1183 Custom error message for try block mixing except and except*
1184
1185 >>> try:
1186 ... pass
1187 ... except TypeError:
1188 ... pass
1189 ... except* ValueError:
1190 ... pass
1191 Traceback (most recent call last):
1192 SyntaxError: cannot have both 'except' and 'except*' on the same 'try'
1193
1194 >>> try:
1195 ... pass
1196 ... except* TypeError:
1197 ... pass
1198 ... except ValueError:
1199 ... pass
1200 Traceback (most recent call last):
1201 SyntaxError: cannot have both 'except' and 'except*' on the same 'try'
1202
1203 >>> try:
1204 ... pass
1205 ... except TypeError:
1206 ... pass
1207 ... except TypeError:
1208 ... pass
1209 ... except* ValueError:
1210 ... pass
1211 Traceback (most recent call last):
1212 SyntaxError: cannot have both 'except' and 'except*' on the same 'try'
1213
1214 >>> try:
1215 ... pass
1216 ... except* TypeError:
1217 ... pass
1218 ... except* TypeError:
1219 ... pass
1220 ... except ValueError:
1221 ... pass
1222 Traceback (most recent call last):
1223 SyntaxError: cannot have both 'except' and 'except*' on the same 'try'
1224
1225 Ensure that early = are not matched by the parser as invalid comparisons
1226 >>> f(2, 4, x=34); 1 $ 2
1227 Traceback (most recent call last):
1228 SyntaxError: invalid syntax
1229
1230 >>> dict(x=34); x $ y
1231 Traceback (most recent call last):
1232 SyntaxError: invalid syntax
1233
1234 >>> dict(x=34, (x for x in range 10), 1); x $ y
1235 Traceback (most recent call last):
1236 SyntaxError: invalid syntax
1237
1238 >>> dict(x=34, x=1, y=2); x $ y
1239 Traceback (most recent call last):
1240 SyntaxError: invalid syntax
1241
1242 Incomplete dictionary literals
1243
1244 >>> {1:2, 3:4, 5}
1245 Traceback (most recent call last):
1246 SyntaxError: ':' expected after dictionary key
1247
1248 >>> {1:2, 3:4, 5:}
1249 Traceback (most recent call last):
1250 SyntaxError: expression expected after dictionary key and ':'
1251
1252 >>> {1: *12+1, 23: 1}
1253 Traceback (most recent call last):
1254 SyntaxError: cannot use a starred expression in a dictionary value
1255
1256 >>> {1: *12+1}
1257 Traceback (most recent call last):
1258 SyntaxError: cannot use a starred expression in a dictionary value
1259
1260 >>> {1: 23, 1: *12+1}
1261 Traceback (most recent call last):
1262 SyntaxError: cannot use a starred expression in a dictionary value
1263
1264 >>> {1:}
1265 Traceback (most recent call last):
1266 SyntaxError: expression expected after dictionary key and ':'
1267
1268 # Ensure that the error is not raised for syntax errors that happen after sets
1269
1270 >>> {1} $
1271 Traceback (most recent call last):
1272 SyntaxError: invalid syntax
1273
1274 # Ensure that the error is not raised for invalid expressions
1275
1276 >>> {1: 2, 3: foo(,), 4: 5}
1277 Traceback (most recent call last):
1278 SyntaxError: invalid syntax
1279
1280 >>> {1: $, 2: 3}
1281 Traceback (most recent call last):
1282 SyntaxError: invalid syntax
1283
1284 Specialized indentation errors:
1285
1286 >>> while condition:
1287 ... pass
1288 Traceback (most recent call last):
1289 IndentationError: expected an indented block after 'while' statement on line 1
1290
1291 >>> for x in range(10):
1292 ... pass
1293 Traceback (most recent call last):
1294 IndentationError: expected an indented block after 'for' statement on line 1
1295
1296 >>> for x in range(10):
1297 ... pass
1298 ... else:
1299 ... pass
1300 Traceback (most recent call last):
1301 IndentationError: expected an indented block after 'else' statement on line 3
1302
1303 >>> async for x in range(10):
1304 ... pass
1305 Traceback (most recent call last):
1306 IndentationError: expected an indented block after 'for' statement on line 1
1307
1308 >>> async for x in range(10):
1309 ... pass
1310 ... else:
1311 ... pass
1312 Traceback (most recent call last):
1313 IndentationError: expected an indented block after 'else' statement on line 3
1314
1315 >>> if something:
1316 ... pass
1317 Traceback (most recent call last):
1318 IndentationError: expected an indented block after 'if' statement on line 1
1319
1320 >>> if something:
1321 ... pass
1322 ... elif something_else:
1323 ... pass
1324 Traceback (most recent call last):
1325 IndentationError: expected an indented block after 'elif' statement on line 3
1326
1327 >>> if something:
1328 ... pass
1329 ... elif something_else:
1330 ... pass
1331 ... else:
1332 ... pass
1333 Traceback (most recent call last):
1334 IndentationError: expected an indented block after 'else' statement on line 5
1335
1336 >>> try:
1337 ... pass
1338 Traceback (most recent call last):
1339 IndentationError: expected an indented block after 'try' statement on line 1
1340
1341 >>> try:
1342 ... something()
1343 ... except:
1344 ... pass
1345 Traceback (most recent call last):
1346 IndentationError: expected an indented block after 'except' statement on line 3
1347
1348 >>> try:
1349 ... something()
1350 ... except A:
1351 ... pass
1352 Traceback (most recent call last):
1353 IndentationError: expected an indented block after 'except' statement on line 3
1354
1355 >>> try:
1356 ... something()
1357 ... except* A:
1358 ... pass
1359 Traceback (most recent call last):
1360 IndentationError: expected an indented block after 'except*' statement on line 3
1361
1362 >>> try:
1363 ... something()
1364 ... except A:
1365 ... pass
1366 ... finally:
1367 ... pass
1368 Traceback (most recent call last):
1369 IndentationError: expected an indented block after 'finally' statement on line 5
1370
1371 >>> try:
1372 ... something()
1373 ... except* A:
1374 ... pass
1375 ... finally:
1376 ... pass
1377 Traceback (most recent call last):
1378 IndentationError: expected an indented block after 'finally' statement on line 5
1379
1380 >>> with A:
1381 ... pass
1382 Traceback (most recent call last):
1383 IndentationError: expected an indented block after 'with' statement on line 1
1384
1385 >>> with A as a, B as b:
1386 ... pass
1387 Traceback (most recent call last):
1388 IndentationError: expected an indented block after 'with' statement on line 1
1389
1390 >>> with (A as a, B as b):
1391 ... pass
1392 Traceback (most recent call last):
1393 IndentationError: expected an indented block after 'with' statement on line 1
1394
1395 >>> async with A:
1396 ... pass
1397 Traceback (most recent call last):
1398 IndentationError: expected an indented block after 'with' statement on line 1
1399
1400 >>> async with A as a, B as b:
1401 ... pass
1402 Traceback (most recent call last):
1403 IndentationError: expected an indented block after 'with' statement on line 1
1404
1405 >>> async with (A as a, B as b):
1406 ... pass
1407 Traceback (most recent call last):
1408 IndentationError: expected an indented block after 'with' statement on line 1
1409
1410 >>> def foo(x, /, y, *, z=2):
1411 ... pass
1412 Traceback (most recent call last):
1413 IndentationError: expected an indented block after function definition on line 1
1414
1415 >>> class Blech(A):
1416 ... pass
1417 Traceback (most recent call last):
1418 IndentationError: expected an indented block after class definition on line 1
1419
1420 >>> match something:
1421 ... pass
1422 Traceback (most recent call last):
1423 IndentationError: expected an indented block after 'match' statement on line 1
1424
1425 >>> match something:
1426 ... case []:
1427 ... pass
1428 Traceback (most recent call last):
1429 IndentationError: expected an indented block after 'case' statement on line 2
1430
1431 >>> match something:
1432 ... case []:
1433 ... ...
1434 ... case {}:
1435 ... pass
1436 Traceback (most recent call last):
1437 IndentationError: expected an indented block after 'case' statement on line 4
1438
1439 Make sure that the old "raise X, Y[, Z]" form is gone:
1440 >>> raise X, Y
1441 Traceback (most recent call last):
1442 ...
1443 SyntaxError: invalid syntax
1444 >>> raise X, Y, Z
1445 Traceback (most recent call last):
1446 ...
1447 SyntaxError: invalid syntax
1448
1449 Check that an multiple exception types with missing parentheses
1450 raise a custom exception
1451
1452 >>> try:
1453 ... pass
1454 ... except A, B:
1455 ... pass
1456 Traceback (most recent call last):
1457 SyntaxError: multiple exception types must be parenthesized
1458
1459 >>> try:
1460 ... pass
1461 ... except A, B, C:
1462 ... pass
1463 Traceback (most recent call last):
1464 SyntaxError: multiple exception types must be parenthesized
1465
1466 >>> try:
1467 ... pass
1468 ... except A, B, C as blech:
1469 ... pass
1470 Traceback (most recent call last):
1471 SyntaxError: multiple exception types must be parenthesized
1472
1473 >>> try:
1474 ... pass
1475 ... except A, B, C as blech:
1476 ... pass
1477 ... finally:
1478 ... pass
1479 Traceback (most recent call last):
1480 SyntaxError: multiple exception types must be parenthesized
1481
1482
1483 >>> try:
1484 ... pass
1485 ... except* A, B:
1486 ... pass
1487 Traceback (most recent call last):
1488 SyntaxError: multiple exception types must be parenthesized
1489
1490 >>> try:
1491 ... pass
1492 ... except* A, B, C:
1493 ... pass
1494 Traceback (most recent call last):
1495 SyntaxError: multiple exception types must be parenthesized
1496
1497 >>> try:
1498 ... pass
1499 ... except* A, B, C as blech:
1500 ... pass
1501 Traceback (most recent call last):
1502 SyntaxError: multiple exception types must be parenthesized
1503
1504 >>> try:
1505 ... pass
1506 ... except* A, B, C as blech:
1507 ... pass
1508 ... finally:
1509 ... pass
1510 Traceback (most recent call last):
1511 SyntaxError: multiple exception types must be parenthesized
1512
1513 Custom exception for 'except*' without an exception type
1514
1515 >>> try:
1516 ... pass
1517 ... except* A as a:
1518 ... pass
1519 ... except*:
1520 ... pass
1521 Traceback (most recent call last):
1522 SyntaxError: expected one or more exception types
1523
1524
1525 >>> f(a=23, a=234)
1526 Traceback (most recent call last):
1527 ...
1528 SyntaxError: keyword argument repeated: a
1529
1530 >>> {1, 2, 3} = 42
1531 Traceback (most recent call last):
1532 SyntaxError: cannot assign to set display here. Maybe you meant '==' instead of '='?
1533
1534 >>> {1: 2, 3: 4} = 42
1535 Traceback (most recent call last):
1536 SyntaxError: cannot assign to dict literal here. Maybe you meant '==' instead of '='?
1537
1538 >>> f'{x}' = 42
1539 Traceback (most recent call last):
1540 SyntaxError: cannot assign to f-string expression here. Maybe you meant '==' instead of '='?
1541
1542 >>> f'{x}-{y}' = 42
1543 Traceback (most recent call last):
1544 SyntaxError: cannot assign to f-string expression here. Maybe you meant '==' instead of '='?
1545
1546 >>> (x, y, z=3, d, e)
1547 Traceback (most recent call last):
1548 SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?
1549
1550 >>> [x, y, z=3, d, e]
1551 Traceback (most recent call last):
1552 SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?
1553
1554 >>> [z=3]
1555 Traceback (most recent call last):
1556 SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?
1557
1558 >>> {x, y, z=3, d, e}
1559 Traceback (most recent call last):
1560 SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?
1561
1562 >>> {z=3}
1563 Traceback (most recent call last):
1564 SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?
1565
1566 >>> from t import x,
1567 Traceback (most recent call last):
1568 SyntaxError: trailing comma not allowed without surrounding parentheses
1569
1570 >>> from t import x,y,
1571 Traceback (most recent call last):
1572 SyntaxError: trailing comma not allowed without surrounding parentheses
1573
1574 # Check that we dont raise the "trailing comma" error if there is more
1575 # input to the left of the valid part that we parsed.
1576
1577 >>> from t import x,y, and 3
1578 Traceback (most recent call last):
1579 SyntaxError: invalid syntax
1580
1581 >>> (): int
1582 Traceback (most recent call last):
1583 SyntaxError: only single target (not tuple) can be annotated
1584 >>> []: int
1585 Traceback (most recent call last):
1586 SyntaxError: only single target (not list) can be annotated
1587 >>> (()): int
1588 Traceback (most recent call last):
1589 SyntaxError: only single target (not tuple) can be annotated
1590 >>> ([]): int
1591 Traceback (most recent call last):
1592 SyntaxError: only single target (not list) can be annotated
1593
1594 Corner-cases that used to fail to raise the correct error:
1595
1596 >>> def f(*, x=lambda __debug__:0): pass
1597 Traceback (most recent call last):
1598 SyntaxError: cannot assign to __debug__
1599
1600 >>> def f(*args:(lambda __debug__:0)): pass
1601 Traceback (most recent call last):
1602 SyntaxError: cannot assign to __debug__
1603
1604 >>> def f(**kwargs:(lambda __debug__:0)): pass
1605 Traceback (most recent call last):
1606 SyntaxError: cannot assign to __debug__
1607
1608 >>> with (lambda *:0): pass
1609 Traceback (most recent call last):
1610 SyntaxError: named arguments must follow bare *
1611
1612 Corner-cases that used to crash:
1613
1614 >>> def f(**__debug__): pass
1615 Traceback (most recent call last):
1616 SyntaxError: cannot assign to __debug__
1617
1618 >>> def f(*xx, __debug__): pass
1619 Traceback (most recent call last):
1620 SyntaxError: cannot assign to __debug__
1621
1622 >>> import ä £
1623 Traceback (most recent call last):
1624 SyntaxError: invalid character '£' (U+00A3)
1625
1626 Invalid pattern matching constructs:
1627
1628 >>> match ...:
1629 ... case 42 as _:
1630 ... ...
1631 Traceback (most recent call last):
1632 SyntaxError: cannot use '_' as a target
1633
1634 >>> match ...:
1635 ... case 42 as 1+2+4:
1636 ... ...
1637 Traceback (most recent call last):
1638 SyntaxError: invalid pattern target
1639
1640 >>> match ...:
1641 ... case Foo(z=1, y=2, x):
1642 ... ...
1643 Traceback (most recent call last):
1644 SyntaxError: positional patterns follow keyword patterns
1645
1646 >>> match ...:
1647 ... case Foo(a, z=1, y=2, x):
1648 ... ...
1649 Traceback (most recent call last):
1650 SyntaxError: positional patterns follow keyword patterns
1651
1652 >>> match ...:
1653 ... case Foo(z=1, x, y=2):
1654 ... ...
1655 Traceback (most recent call last):
1656 SyntaxError: positional patterns follow keyword patterns
1657
1658 >>> match ...:
1659 ... case C(a=b, c, d=e, f, g=h, i, j=k, ...):
1660 ... ...
1661 Traceback (most recent call last):
1662 SyntaxError: positional patterns follow keyword patterns
1663
1664 Uses of the star operator which should fail:
1665
1666 A[:*b]
1667
1668 >>> A[:*b]
1669 Traceback (most recent call last):
1670 ...
1671 SyntaxError: invalid syntax
1672 >>> A[:(*b)]
1673 Traceback (most recent call last):
1674 ...
1675 SyntaxError: cannot use starred expression here
1676 >>> A[:*b] = 1
1677 Traceback (most recent call last):
1678 ...
1679 SyntaxError: invalid syntax
1680 >>> del A[:*b]
1681 Traceback (most recent call last):
1682 ...
1683 SyntaxError: invalid syntax
1684
1685 A[*b:]
1686
1687 >>> A[*b:]
1688 Traceback (most recent call last):
1689 ...
1690 SyntaxError: invalid syntax
1691 >>> A[(*b):]
1692 Traceback (most recent call last):
1693 ...
1694 SyntaxError: cannot use starred expression here
1695 >>> A[*b:] = 1
1696 Traceback (most recent call last):
1697 ...
1698 SyntaxError: invalid syntax
1699 >>> del A[*b:]
1700 Traceback (most recent call last):
1701 ...
1702 SyntaxError: invalid syntax
1703
1704 A[*b:*b]
1705
1706 >>> A[*b:*b]
1707 Traceback (most recent call last):
1708 ...
1709 SyntaxError: invalid syntax
1710 >>> A[(*b:*b)]
1711 Traceback (most recent call last):
1712 ...
1713 SyntaxError: invalid syntax
1714 >>> A[*b:*b] = 1
1715 Traceback (most recent call last):
1716 ...
1717 SyntaxError: invalid syntax
1718 >>> del A[*b:*b]
1719 Traceback (most recent call last):
1720 ...
1721 SyntaxError: invalid syntax
1722
1723 A[*(1:2)]
1724
1725 >>> A[*(1:2)]
1726 Traceback (most recent call last):
1727 ...
1728 SyntaxError: invalid syntax
1729 >>> A[*(1:2)] = 1
1730 Traceback (most recent call last):
1731 ...
1732 SyntaxError: invalid syntax
1733 >>> del A[*(1:2)]
1734 Traceback (most recent call last):
1735 ...
1736 SyntaxError: invalid syntax
1737
1738 A[*:] and A[:*]
1739
1740 >>> A[*:]
1741 Traceback (most recent call last):
1742 ...
1743 SyntaxError: invalid syntax
1744 >>> A[:*]
1745 Traceback (most recent call last):
1746 ...
1747 SyntaxError: invalid syntax
1748
1749 A[*]
1750
1751 >>> A[*]
1752 Traceback (most recent call last):
1753 ...
1754 SyntaxError: invalid syntax
1755
1756 A[**]
1757
1758 >>> A[**]
1759 Traceback (most recent call last):
1760 ...
1761 SyntaxError: invalid syntax
1762
1763 A[**b]
1764
1765 >>> A[**b]
1766 Traceback (most recent call last):
1767 ...
1768 SyntaxError: invalid syntax
1769 >>> A[**b] = 1
1770 Traceback (most recent call last):
1771 ...
1772 SyntaxError: invalid syntax
1773 >>> del A[**b]
1774 Traceback (most recent call last):
1775 ...
1776 SyntaxError: invalid syntax
1777
1778 def f(x: *b)
1779
1780 >>> def f6(x: *b): pass
1781 Traceback (most recent call last):
1782 ...
1783 SyntaxError: invalid syntax
1784 >>> def f7(x: *b = 1): pass
1785 Traceback (most recent call last):
1786 ...
1787 SyntaxError: invalid syntax
1788
1789 **kwargs: *a
1790
1791 >>> def f8(**kwargs: *a): pass
1792 Traceback (most recent call last):
1793 ...
1794 SyntaxError: invalid syntax
1795
1796 x: *b
1797
1798 >>> x: *b
1799 Traceback (most recent call last):
1800 ...
1801 SyntaxError: invalid syntax
1802 >>> x: *b = 1
1803 Traceback (most recent call last):
1804 ...
1805 SyntaxError: invalid syntax
1806
1807 Invalid bytes literals:
1808
1809 >>> b"Ä€"
1810 Traceback (most recent call last):
1811 ...
1812 b"Ä€"
1813 ^^^
1814 SyntaxError: bytes can only contain ASCII literal characters
1815
1816 >>> b"абвгде"
1817 Traceback (most recent call last):
1818 ...
1819 b"абвгде"
1820 ^^^^^^^^
1821 SyntaxError: bytes can only contain ASCII literal characters
1822
1823 >>> b"abc ъющый" # first 3 letters are ascii
1824 Traceback (most recent call last):
1825 ...
1826 b"abc ъющый"
1827 ^^^^^^^^^^^
1828 SyntaxError: bytes can only contain ASCII literal characters
1829
1830 >>> f(**x, *y)
1831 Traceback (most recent call last):
1832 SyntaxError: iterable argument unpacking follows keyword argument unpacking
1833
1834 >>> f(**x, *)
1835 Traceback (most recent call last):
1836 SyntaxError: iterable argument unpacking follows keyword argument unpacking
1837
1838 >>> f(x, *:)
1839 Traceback (most recent call last):
1840 SyntaxError: invalid syntax
1841 """
1842
1843 import re
1844 import doctest
1845 import unittest
1846
1847 from test import support
1848
1849 class ESC[4;38;5;81mSyntaxTestCase(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
1850
1851 def _check_error(self, code, errtext,
1852 filename="<testcase>", mode="exec", subclass=None,
1853 lineno=None, offset=None, end_lineno=None, end_offset=None):
1854 """Check that compiling code raises SyntaxError with errtext.
1855
1856 errtest is a regular expression that must be present in the
1857 test of the exception raised. If subclass is specified it
1858 is the expected subclass of SyntaxError (e.g. IndentationError).
1859 """
1860 try:
1861 compile(code, filename, mode)
1862 except SyntaxError as err:
1863 if subclass and not isinstance(err, subclass):
1864 self.fail("SyntaxError is not a %s" % subclass.__name__)
1865 mo = re.search(errtext, str(err))
1866 if mo is None:
1867 self.fail("SyntaxError did not contain %r" % (errtext,))
1868 self.assertEqual(err.filename, filename)
1869 if lineno is not None:
1870 self.assertEqual(err.lineno, lineno)
1871 if offset is not None:
1872 self.assertEqual(err.offset, offset)
1873 if end_lineno is not None:
1874 self.assertEqual(err.end_lineno, end_lineno)
1875 if end_offset is not None:
1876 self.assertEqual(err.end_offset, end_offset)
1877
1878 else:
1879 self.fail("compile() did not raise SyntaxError")
1880
1881 def test_expression_with_assignment(self):
1882 self._check_error(
1883 "print(end1 + end2 = ' ')",
1884 'expression cannot contain assignment, perhaps you meant "=="?',
1885 offset=7
1886 )
1887
1888 def test_curly_brace_after_primary_raises_immediately(self):
1889 self._check_error("f{}", "invalid syntax", mode="single")
1890
1891 def test_assign_call(self):
1892 self._check_error("f() = 1", "assign")
1893
1894 def test_assign_del(self):
1895 self._check_error("del (,)", "invalid syntax")
1896 self._check_error("del 1", "cannot delete literal")
1897 self._check_error("del (1, 2)", "cannot delete literal")
1898 self._check_error("del None", "cannot delete None")
1899 self._check_error("del *x", "cannot delete starred")
1900 self._check_error("del (*x)", "cannot use starred expression")
1901 self._check_error("del (*x,)", "cannot delete starred")
1902 self._check_error("del [*x,]", "cannot delete starred")
1903 self._check_error("del f()", "cannot delete function call")
1904 self._check_error("del f(a, b)", "cannot delete function call")
1905 self._check_error("del o.f()", "cannot delete function call")
1906 self._check_error("del a[0]()", "cannot delete function call")
1907 self._check_error("del x, f()", "cannot delete function call")
1908 self._check_error("del f(), x", "cannot delete function call")
1909 self._check_error("del [a, b, ((c), (d,), e.f())]", "cannot delete function call")
1910 self._check_error("del (a if True else b)", "cannot delete conditional")
1911 self._check_error("del +a", "cannot delete expression")
1912 self._check_error("del a, +b", "cannot delete expression")
1913 self._check_error("del a + b", "cannot delete expression")
1914 self._check_error("del (a + b, c)", "cannot delete expression")
1915 self._check_error("del (c[0], a + b)", "cannot delete expression")
1916 self._check_error("del a.b.c + 2", "cannot delete expression")
1917 self._check_error("del a.b.c[0] + 2", "cannot delete expression")
1918 self._check_error("del (a, b, (c, d.e.f + 2))", "cannot delete expression")
1919 self._check_error("del [a, b, (c, d.e.f[0] + 2)]", "cannot delete expression")
1920 self._check_error("del (a := 5)", "cannot delete named expression")
1921 # We don't have a special message for this, but make sure we don't
1922 # report "cannot delete name"
1923 self._check_error("del a += b", "invalid syntax")
1924
1925 def test_global_param_err_first(self):
1926 source = """if 1:
1927 def error(a):
1928 global a # SyntaxError
1929 def error2():
1930 b = 1
1931 global b # SyntaxError
1932 """
1933 self._check_error(source, "parameter and global", lineno=3)
1934
1935 def test_nonlocal_param_err_first(self):
1936 source = """if 1:
1937 def error(a):
1938 nonlocal a # SyntaxError
1939 def error2():
1940 b = 1
1941 global b # SyntaxError
1942 """
1943 self._check_error(source, "parameter and nonlocal", lineno=3)
1944
1945 def test_yield_outside_function(self):
1946 self._check_error("if 0: yield", "outside function")
1947 self._check_error("if 0: yield\nelse: x=1", "outside function")
1948 self._check_error("if 1: pass\nelse: yield", "outside function")
1949 self._check_error("while 0: yield", "outside function")
1950 self._check_error("while 0: yield\nelse: x=1", "outside function")
1951 self._check_error("class C:\n if 0: yield", "outside function")
1952 self._check_error("class C:\n if 1: pass\n else: yield",
1953 "outside function")
1954 self._check_error("class C:\n while 0: yield", "outside function")
1955 self._check_error("class C:\n while 0: yield\n else: x = 1",
1956 "outside function")
1957
1958 def test_return_outside_function(self):
1959 self._check_error("if 0: return", "outside function")
1960 self._check_error("if 0: return\nelse: x=1", "outside function")
1961 self._check_error("if 1: pass\nelse: return", "outside function")
1962 self._check_error("while 0: return", "outside function")
1963 self._check_error("class C:\n if 0: return", "outside function")
1964 self._check_error("class C:\n while 0: return", "outside function")
1965 self._check_error("class C:\n while 0: return\n else: x=1",
1966 "outside function")
1967 self._check_error("class C:\n if 0: return\n else: x= 1",
1968 "outside function")
1969 self._check_error("class C:\n if 1: pass\n else: return",
1970 "outside function")
1971
1972 def test_break_outside_loop(self):
1973 msg = "outside loop"
1974 self._check_error("break", msg, lineno=1)
1975 self._check_error("if 0: break", msg, lineno=1)
1976 self._check_error("if 0: break\nelse: x=1", msg, lineno=1)
1977 self._check_error("if 1: pass\nelse: break", msg, lineno=2)
1978 self._check_error("class C:\n if 0: break", msg, lineno=2)
1979 self._check_error("class C:\n if 1: pass\n else: break",
1980 msg, lineno=3)
1981 self._check_error("with object() as obj:\n break",
1982 msg, lineno=2)
1983
1984 def test_continue_outside_loop(self):
1985 msg = "not properly in loop"
1986 self._check_error("if 0: continue", msg, lineno=1)
1987 self._check_error("if 0: continue\nelse: x=1", msg, lineno=1)
1988 self._check_error("if 1: pass\nelse: continue", msg, lineno=2)
1989 self._check_error("class C:\n if 0: continue", msg, lineno=2)
1990 self._check_error("class C:\n if 1: pass\n else: continue",
1991 msg, lineno=3)
1992 self._check_error("with object() as obj:\n continue",
1993 msg, lineno=2)
1994
1995 def test_unexpected_indent(self):
1996 self._check_error("foo()\n bar()\n", "unexpected indent",
1997 subclass=IndentationError)
1998
1999 def test_no_indent(self):
2000 self._check_error("if 1:\nfoo()", "expected an indented block",
2001 subclass=IndentationError)
2002
2003 def test_bad_outdent(self):
2004 self._check_error("if 1:\n foo()\n bar()",
2005 "unindent does not match .* level",
2006 subclass=IndentationError)
2007
2008 def test_kwargs_last(self):
2009 self._check_error("int(base=10, '2')",
2010 "positional argument follows keyword argument")
2011
2012 def test_kwargs_last2(self):
2013 self._check_error("int(**{'base': 10}, '2')",
2014 "positional argument follows "
2015 "keyword argument unpacking")
2016
2017 def test_kwargs_last3(self):
2018 self._check_error("int(**{'base': 10}, *['2'])",
2019 "iterable argument unpacking follows "
2020 "keyword argument unpacking")
2021
2022 def test_generator_in_function_call(self):
2023 self._check_error("foo(x, y for y in range(3) for z in range(2) if z , p)",
2024 "Generator expression must be parenthesized",
2025 lineno=1, end_lineno=1, offset=11, end_offset=53)
2026
2027 def test_except_then_except_star(self):
2028 self._check_error("try: pass\nexcept ValueError: pass\nexcept* TypeError: pass",
2029 r"cannot have both 'except' and 'except\*' on the same 'try'",
2030 lineno=3, end_lineno=3, offset=1, end_offset=8)
2031
2032 def test_except_star_then_except(self):
2033 self._check_error("try: pass\nexcept* ValueError: pass\nexcept TypeError: pass",
2034 r"cannot have both 'except' and 'except\*' on the same 'try'",
2035 lineno=3, end_lineno=3, offset=1, end_offset=7)
2036
2037 def test_empty_line_after_linecont(self):
2038 # See issue-40847
2039 s = r"""\
2040 pass
2041 \
2042
2043 pass
2044 """
2045 try:
2046 compile(s, '<string>', 'exec')
2047 except SyntaxError:
2048 self.fail("Empty line after a line continuation character is valid.")
2049
2050 # See issue-46091
2051 s1 = r"""\
2052 def fib(n):
2053 \
2054 '''Print a Fibonacci series up to n.'''
2055 \
2056 a, b = 0, 1
2057 """
2058 s2 = r"""\
2059 def fib(n):
2060 '''Print a Fibonacci series up to n.'''
2061 a, b = 0, 1
2062 """
2063 try:
2064 compile(s1, '<string>', 'exec')
2065 compile(s2, '<string>', 'exec')
2066 except SyntaxError:
2067 self.fail("Indented statement over multiple lines is valid")
2068
2069 def test_continuation_bad_indentation(self):
2070 # Check that code that breaks indentation across multiple lines raises a syntax error
2071
2072 code = r"""\
2073 if x:
2074 y = 1
2075 \
2076 foo = 1
2077 """
2078
2079 self.assertRaises(IndentationError, exec, code)
2080
2081 @support.cpython_only
2082 def test_nested_named_except_blocks(self):
2083 code = ""
2084 for i in range(12):
2085 code += f"{' '*i}try:\n"
2086 code += f"{' '*(i+1)}raise Exception\n"
2087 code += f"{' '*i}except Exception as e:\n"
2088 code += f"{' '*4*12}pass"
2089 self._check_error(code, "too many statically nested blocks")
2090
2091 def test_barry_as_flufl_with_syntax_errors(self):
2092 # The "barry_as_flufl" rule can produce some "bugs-at-a-distance" if
2093 # is reading the wrong token in the presence of syntax errors later
2094 # in the file. See bpo-42214 for more information.
2095 code = """
2096 def func1():
2097 if a != b:
2098 raise ValueError
2099
2100 def func2():
2101 try
2102 return 1
2103 finally:
2104 pass
2105 """
2106 self._check_error(code, "expected ':'")
2107
2108 def test_invalid_line_continuation_error_position(self):
2109 self._check_error(r"a = 3 \ 4",
2110 "unexpected character after line continuation character",
2111 lineno=1, offset=8)
2112 self._check_error('1,\\#\n2',
2113 "unexpected character after line continuation character",
2114 lineno=1, offset=4)
2115 self._check_error('\nfgdfgf\n1,\\#\n2\n',
2116 "unexpected character after line continuation character",
2117 lineno=3, offset=4)
2118
2119 def test_invalid_line_continuation_left_recursive(self):
2120 # Check bpo-42218: SyntaxErrors following left-recursive rules
2121 # (t_primary_raw in this case) need to be tested explicitly
2122 self._check_error("A.\u018a\\ ",
2123 "unexpected character after line continuation character")
2124 self._check_error("A.\u03bc\\\n",
2125 "unexpected EOF while parsing")
2126
2127 def test_error_parenthesis(self):
2128 for paren in "([{":
2129 self._check_error(paren + "1 + 2", f"\\{paren}' was never closed")
2130
2131 for paren in "([{":
2132 self._check_error(f"a = {paren} 1, 2, 3\nb=3", f"\\{paren}' was never closed")
2133
2134 for paren in ")]}":
2135 self._check_error(paren + "1 + 2", f"unmatched '\\{paren}'")
2136
2137 # Some more complex examples:
2138 code = """\
2139 func(
2140 a=["unclosed], # Need a quote in this comment: "
2141 b=2,
2142 )
2143 """
2144 self._check_error(code, "parenthesis '\\)' does not match opening parenthesis '\\['")
2145
2146 # Examples with dencodings
2147 s = b'# coding=latin\n(aaaaaaaaaaaaaaaaa\naaaaaaaaaaa\xb5'
2148 self._check_error(s, "'\(' was never closed")
2149
2150 def test_error_string_literal(self):
2151
2152 self._check_error("'blech", "unterminated string literal")
2153 self._check_error('"blech', "unterminated string literal")
2154 self._check_error("'''blech", "unterminated triple-quoted string literal")
2155 self._check_error('"""blech', "unterminated triple-quoted string literal")
2156
2157 def test_invisible_characters(self):
2158 self._check_error('print\x17("Hello")', "invalid non-printable character")
2159 self._check_error(b"with(0,,):\n\x01", "invalid non-printable character")
2160
2161 def test_match_call_does_not_raise_syntax_error(self):
2162 code = """
2163 def match(x):
2164 return 1+1
2165
2166 match(34)
2167 """
2168 compile(code, "<string>", "exec")
2169
2170 def test_case_call_does_not_raise_syntax_error(self):
2171 code = """
2172 def case(x):
2173 return 1+1
2174
2175 case(34)
2176 """
2177 compile(code, "<string>", "exec")
2178
2179 def test_multiline_compiler_error_points_to_the_end(self):
2180 self._check_error(
2181 "call(\na=1,\na=1\n)",
2182 "keyword argument repeated",
2183 lineno=3
2184 )
2185
2186 @support.cpython_only
2187 def test_syntax_error_on_deeply_nested_blocks(self):
2188 # This raises a SyntaxError, it used to raise a SystemError. Context
2189 # for this change can be found on issue #27514
2190
2191 # In 2.5 there was a missing exception and an assert was triggered in a
2192 # debug build. The number of blocks must be greater than CO_MAXBLOCKS.
2193 # SF #1565514
2194
2195 source = """
2196 while 1:
2197 while 2:
2198 while 3:
2199 while 4:
2200 while 5:
2201 while 6:
2202 while 8:
2203 while 9:
2204 while 10:
2205 while 11:
2206 while 12:
2207 while 13:
2208 while 14:
2209 while 15:
2210 while 16:
2211 while 17:
2212 while 18:
2213 while 19:
2214 while 20:
2215 while 21:
2216 while 22:
2217 break
2218 """
2219 self._check_error(source, "too many statically nested blocks")
2220
2221 @support.cpython_only
2222 def test_error_on_parser_stack_overflow(self):
2223 source = "-" * 100000 + "4"
2224 for mode in ["exec", "eval", "single"]:
2225 with self.subTest(mode=mode):
2226 with self.assertRaises(MemoryError):
2227 compile(source, "<string>", mode)
2228
2229 @support.cpython_only
2230 def test_deep_invalid_rule(self):
2231 # Check that a very deep invalid rule in the PEG
2232 # parser doesn't have exponential backtracking.
2233 source = "d{{{{{{{{{{{{{{{{{{{{{{{{{```{{{{{{{ef f():y"
2234 with self.assertRaises(SyntaxError):
2235 compile(source, "<string>", "exec")
2236
2237
2238 def load_tests(loader, tests, pattern):
2239 tests.addTest(doctest.DocTestSuite())
2240 return tests
2241
2242
2243 if __name__ == "__main__":
2244 unittest.main()