1"""This module tests SyntaxErrors. 2 3Here's an example of the sort of thing that is tested. 4 5>>> def f(x): 6... global x 7Traceback (most recent call last): 8SyntaxError: name 'x' is parameter and global 9 10The tests are all raise SyntaxErrors. They were created by checking 11each C call that raises SyntaxError. There are several modules that 12raise these exceptions-- ast.c, compile.c, future.c, pythonrun.c, and 13symtable.c. 14 15The parser itself outlaws a lot of invalid syntax. None of these 16errors are tested here at the moment. We should add some tests; since 17there are infinitely many programs with invalid syntax, we would need 18to be judicious in selecting some. 19 20The compiler generates a synthetic module name for code executed by 21doctest. 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 23order of tests in this module means renumbering all the errors after 24it. (Maybe we should enable the ellipsis option for these tests.) 25 26In ast.c, syntax errors are raised by calling ast_error(). 27 28Errors from set_context(): 29 30>>> obj.None = 1 31Traceback (most recent call last): 32SyntaxError: invalid syntax 33 34>>> None = 1 35Traceback (most recent call last): 36SyntaxError: cannot assign to None 37 38>>> obj.True = 1 39Traceback (most recent call last): 40SyntaxError: invalid syntax 41 42>>> True = 1 43Traceback (most recent call last): 44SyntaxError: cannot assign to True 45 46>>> (True := 1) 47Traceback (most recent call last): 48SyntaxError: cannot use assignment expressions with True 49 50>>> obj.__debug__ = 1 51Traceback (most recent call last): 52SyntaxError: cannot assign to __debug__ 53 54>>> __debug__ = 1 55Traceback (most recent call last): 56SyntaxError: cannot assign to __debug__ 57 58>>> (__debug__ := 1) 59Traceback (most recent call last): 60SyntaxError: cannot assign to __debug__ 61 62>>> del __debug__ 63Traceback (most recent call last): 64SyntaxError: cannot delete __debug__ 65 66>>> f() = 1 67Traceback (most recent call last): 68SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='? 69 70>>> yield = 1 71Traceback (most recent call last): 72SyntaxError: assignment to yield expression not possible 73 74>>> del f() 75Traceback (most recent call last): 76SyntaxError: cannot delete function call 77 78>>> a + 1 = 2 79Traceback (most recent call last): 80SyntaxError: cannot assign to expression here. Maybe you meant '==' instead of '='? 81 82>>> (x for x in x) = 1 83Traceback (most recent call last): 84SyntaxError: cannot assign to generator expression 85 86>>> 1 = 1 87Traceback (most recent call last): 88SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='? 89 90>>> "abc" = 1 91Traceback (most recent call last): 92SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='? 93 94>>> b"" = 1 95Traceback (most recent call last): 96SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='? 97 98>>> ... = 1 99Traceback (most recent call last): 100SyntaxError: cannot assign to ellipsis here. Maybe you meant '==' instead of '='? 101 102>>> `1` = 1 103Traceback (most recent call last): 104SyntaxError: invalid syntax 105 106If the left-hand side of an assignment is a list or tuple, an illegal 107expression inside that contain should still cause a syntax error. 108This test just checks a couple of cases rather than enumerating all of 109them. 110 111>>> (a, "b", c) = (1, 2, 3) 112Traceback (most recent call last): 113SyntaxError: cannot assign to literal 114 115>>> (a, True, c) = (1, 2, 3) 116Traceback (most recent call last): 117SyntaxError: cannot assign to True 118 119>>> (a, __debug__, c) = (1, 2, 3) 120Traceback (most recent call last): 121SyntaxError: cannot assign to __debug__ 122 123>>> (a, *True, c) = (1, 2, 3) 124Traceback (most recent call last): 125SyntaxError: cannot assign to True 126 127>>> (a, *__debug__, c) = (1, 2, 3) 128Traceback (most recent call last): 129SyntaxError: cannot assign to __debug__ 130 131>>> [a, b, c + 1] = [1, 2, 3] 132Traceback (most recent call last): 133SyntaxError: cannot assign to expression 134 135>>> [a, b[1], c + 1] = [1, 2, 3] 136Traceback (most recent call last): 137SyntaxError: cannot assign to expression 138 139>>> [a, b.c.d, c + 1] = [1, 2, 3] 140Traceback (most recent call last): 141SyntaxError: cannot assign to expression 142 143>>> a if 1 else b = 1 144Traceback (most recent call last): 145SyntaxError: cannot assign to conditional expression 146 147>>> a = 42 if True 148Traceback (most recent call last): 149SyntaxError: expected 'else' after 'if' expression 150 151>>> a = (42 if True) 152Traceback (most recent call last): 153SyntaxError: expected 'else' after 'if' expression 154 155>>> a = [1, 42 if True, 4] 156Traceback (most recent call last): 157SyntaxError: expected 'else' after 'if' expression 158 159>>> if True: 160... print("Hello" 161... 162... if 2: 163... print(123)) 164Traceback (most recent call last): 165SyntaxError: invalid syntax 166 167>>> True = True = 3 168Traceback (most recent call last): 169SyntaxError: cannot assign to True 170 171>>> x = y = True = z = 3 172Traceback (most recent call last): 173SyntaxError: cannot assign to True 174 175>>> x = y = yield = 1 176Traceback (most recent call last): 177SyntaxError: assignment to yield expression not possible 178 179>>> a, b += 1, 2 180Traceback (most recent call last): 181SyntaxError: 'tuple' is an illegal expression for augmented assignment 182 183>>> (a, b) += 1, 2 184Traceback (most recent call last): 185SyntaxError: 'tuple' is an illegal expression for augmented assignment 186 187>>> [a, b] += 1, 2 188Traceback (most recent call last): 189SyntaxError: 'list' is an illegal expression for augmented assignment 190 191Invalid targets in `for` loops and `with` statements should also 192produce a specialized error message 193 194>>> for a() in b: pass 195Traceback (most recent call last): 196SyntaxError: cannot assign to function call 197 198>>> for (a, b()) in b: pass 199Traceback (most recent call last): 200SyntaxError: cannot assign to function call 201 202>>> for [a, b()] in b: pass 203Traceback (most recent call last): 204SyntaxError: cannot assign to function call 205 206>>> for (*a, b, c+1) in b: pass 207Traceback (most recent call last): 208SyntaxError: cannot assign to expression 209 210>>> for (x, *(y, z.d())) in b: pass 211Traceback (most recent call last): 212SyntaxError: cannot assign to function call 213 214>>> for a, b() in c: pass 215Traceback (most recent call last): 216SyntaxError: cannot assign to function call 217 218>>> for a, b, (c + 1, d()): pass 219Traceback (most recent call last): 220SyntaxError: cannot assign to expression 221 222>>> for i < (): pass 223Traceback (most recent call last): 224SyntaxError: invalid syntax 225 226>>> for a, b 227Traceback (most recent call last): 228SyntaxError: invalid syntax 229 230>>> with a as b(): pass 231Traceback (most recent call last): 232SyntaxError: cannot assign to function call 233 234>>> with a as (b, c()): pass 235Traceback (most recent call last): 236SyntaxError: cannot assign to function call 237 238>>> with a as [b, c()]: pass 239Traceback (most recent call last): 240SyntaxError: cannot assign to function call 241 242>>> with a as (*b, c, d+1): pass 243Traceback (most recent call last): 244SyntaxError: cannot assign to expression 245 246>>> with a as (x, *(y, z.d())): pass 247Traceback (most recent call last): 248SyntaxError: cannot assign to function call 249 250>>> with a as b, c as d(): pass 251Traceback (most recent call last): 252SyntaxError: cannot assign to function call 253 254>>> with a as b 255Traceback (most recent call last): 256SyntaxError: expected ':' 257 258>>> p = p = 259Traceback (most recent call last): 260SyntaxError: invalid syntax 261 262Comprehensions creating tuples without parentheses 263should produce a specialized error message: 264 265>>> [x,y for x,y in range(100)] 266Traceback (most recent call last): 267SyntaxError: did you forget parentheses around the comprehension target? 268 269>>> {x,y for x,y in range(100)} 270Traceback (most recent call last): 271SyntaxError: 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] 278Traceback (most recent call last): 279SyntaxError: invalid syntax. Perhaps you forgot a comma? 280 281>>> {1, 2 3} 282Traceback (most recent call last): 283SyntaxError: invalid syntax. Perhaps you forgot a comma? 284 285>>> {1:2, 2:5 3:12} 286Traceback (most recent call last): 287SyntaxError: invalid syntax. Perhaps you forgot a comma? 288 289>>> (1, 2 3) 290Traceback (most recent call last): 291SyntaxError: 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 298Traceback (most recent call last): 299SyntaxError: invalid syntax 300 301>>> match x: 302... case y: 303... 3 $ 3 304Traceback (most recent call last): 305SyntaxError: invalid syntax 306 307>>> match x: 308... case $: 309... ... 310Traceback (most recent call last): 311SyntaxError: invalid syntax 312 313>>> match ...: 314... case {**rest, "key": value}: 315... ... 316Traceback (most recent call last): 317SyntaxError: invalid syntax 318 319>>> match ...: 320... case {**_}: 321... ... 322Traceback (most recent call last): 323SyntaxError: invalid syntax 324 325From compiler_complex_args(): 326 327>>> def f(None=1): 328... pass 329Traceback (most recent call last): 330SyntaxError: invalid syntax 331 332From ast_for_arguments(): 333 334>>> def f(x, y=1, z): 335... pass 336Traceback (most recent call last): 337SyntaxError: non-default argument follows default argument 338 339>>> def f(x, None): 340... pass 341Traceback (most recent call last): 342SyntaxError: invalid syntax 343 344>>> def f(*None): 345... pass 346Traceback (most recent call last): 347SyntaxError: invalid syntax 348 349>>> def f(**None): 350... pass 351Traceback (most recent call last): 352SyntaxError: invalid syntax 353 354>>> def foo(/,a,b=,c): 355... pass 356Traceback (most recent call last): 357SyntaxError: at least one argument must precede / 358 359>>> def foo(a,/,/,b,c): 360... pass 361Traceback (most recent call last): 362SyntaxError: / may appear only once 363 364>>> def foo(a,/,a1,/,b,c): 365... pass 366Traceback (most recent call last): 367SyntaxError: / may appear only once 368 369>>> def foo(a=1,/,/,*b,/,c): 370... pass 371Traceback (most recent call last): 372SyntaxError: / may appear only once 373 374>>> def foo(a,/,a1=1,/,b,c): 375... pass 376Traceback (most recent call last): 377SyntaxError: / may appear only once 378 379>>> def foo(a,*b,c,/,d,e): 380... pass 381Traceback (most recent call last): 382SyntaxError: / must be ahead of * 383 384>>> def foo(a=1,*b,c=3,/,d,e): 385... pass 386Traceback (most recent call last): 387SyntaxError: / must be ahead of * 388 389>>> def foo(a,*b=3,c): 390... pass 391Traceback (most recent call last): 392SyntaxError: var-positional argument cannot have default value 393 394>>> def foo(a,*b: int=,c): 395... pass 396Traceback (most recent call last): 397SyntaxError: var-positional argument cannot have default value 398 399>>> def foo(a,**b=3): 400... pass 401Traceback (most recent call last): 402SyntaxError: var-keyword argument cannot have default value 403 404>>> def foo(a,**b: int=3): 405... pass 406Traceback (most recent call last): 407SyntaxError: var-keyword argument cannot have default value 408 409>>> def foo(a,*a, b, **c, d): 410... pass 411Traceback (most recent call last): 412SyntaxError: arguments cannot follow var-keyword argument 413 414>>> def foo(a,*a, b, **c, d=4): 415... pass 416Traceback (most recent call last): 417SyntaxError: arguments cannot follow var-keyword argument 418 419>>> def foo(a,*a, b, **c, *d): 420... pass 421Traceback (most recent call last): 422SyntaxError: arguments cannot follow var-keyword argument 423 424>>> def foo(a,*a, b, **c, **d): 425... pass 426Traceback (most recent call last): 427SyntaxError: arguments cannot follow var-keyword argument 428 429>>> def foo(a=1,/,**b,/,c): 430... pass 431Traceback (most recent call last): 432SyntaxError: arguments cannot follow var-keyword argument 433 434>>> def foo(*b,*d): 435... pass 436Traceback (most recent call last): 437SyntaxError: * argument may appear only once 438 439>>> def foo(a,*b,c,*d,*e,c): 440... pass 441Traceback (most recent call last): 442SyntaxError: * argument may appear only once 443 444>>> def foo(a,b,/,c,*b,c,*d,*e,c): 445... pass 446Traceback (most recent call last): 447SyntaxError: * argument may appear only once 448 449>>> def foo(a,b,/,c,*b,c,*d,**e): 450... pass 451Traceback (most recent call last): 452SyntaxError: * argument may appear only once 453 454>>> def foo(a=1,/*,b,c): 455... pass 456Traceback (most recent call last): 457SyntaxError: expected comma between / and * 458 459>>> def foo(a=1,d=,c): 460... pass 461Traceback (most recent call last): 462SyntaxError: expected default value expression 463 464>>> def foo(a,d=,c): 465... pass 466Traceback (most recent call last): 467SyntaxError: expected default value expression 468 469>>> def foo(a,d: int=,c): 470... pass 471Traceback (most recent call last): 472SyntaxError: expected default value expression 473 474>>> lambda /,a,b,c: None 475Traceback (most recent call last): 476SyntaxError: at least one argument must precede / 477 478>>> lambda a,/,/,b,c: None 479Traceback (most recent call last): 480SyntaxError: / may appear only once 481 482>>> lambda a,/,a1,/,b,c: None 483Traceback (most recent call last): 484SyntaxError: / may appear only once 485 486>>> lambda a=1,/,/,*b,/,c: None 487Traceback (most recent call last): 488SyntaxError: / may appear only once 489 490>>> lambda a,/,a1=1,/,b,c: None 491Traceback (most recent call last): 492SyntaxError: / may appear only once 493 494>>> lambda a,*b,c,/,d,e: None 495Traceback (most recent call last): 496SyntaxError: / must be ahead of * 497 498>>> lambda a=1,*b,c=3,/,d,e: None 499Traceback (most recent call last): 500SyntaxError: / must be ahead of * 501 502>>> lambda a=1,/*,b,c: None 503Traceback (most recent call last): 504SyntaxError: expected comma between / and * 505 506>>> lambda a,*b=3,c: None 507Traceback (most recent call last): 508SyntaxError: var-positional argument cannot have default value 509 510>>> lambda a,**b=3: None 511Traceback (most recent call last): 512SyntaxError: var-keyword argument cannot have default value 513 514>>> lambda a, *a, b, **c, d: None 515Traceback (most recent call last): 516SyntaxError: arguments cannot follow var-keyword argument 517 518>>> lambda a,*a, b, **c, d=4: None 519Traceback (most recent call last): 520SyntaxError: arguments cannot follow var-keyword argument 521 522>>> lambda a,*a, b, **c, *d: None 523Traceback (most recent call last): 524SyntaxError: arguments cannot follow var-keyword argument 525 526>>> lambda a,*a, b, **c, **d: None 527Traceback (most recent call last): 528SyntaxError: arguments cannot follow var-keyword argument 529 530>>> lambda a=1,/,**b,/,c: None 531Traceback (most recent call last): 532SyntaxError: arguments cannot follow var-keyword argument 533 534>>> lambda *b,*d: None 535Traceback (most recent call last): 536SyntaxError: * argument may appear only once 537 538>>> lambda a,*b,c,*d,*e,c: None 539Traceback (most recent call last): 540SyntaxError: * argument may appear only once 541 542>>> lambda a,b,/,c,*b,c,*d,*e,c: None 543Traceback (most recent call last): 544SyntaxError: * argument may appear only once 545 546>>> lambda a,b,/,c,*b,c,*d,**e: None 547Traceback (most recent call last): 548SyntaxError: * argument may appear only once 549 550>>> lambda a=1,d=,c: None 551Traceback (most recent call last): 552SyntaxError: expected default value expression 553 554>>> lambda a,d=,c: None 555Traceback (most recent call last): 556SyntaxError: 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) 565Traceback (most recent call last): 566SyntaxError: bare * has associated type comment 567 568 569From ast_for_funcdef(): 570 571>>> def None(x): 572... pass 573Traceback (most recent call last): 574SyntaxError: invalid syntax 575 576 577From 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) 585Traceback (most recent call last): 586SyntaxError: Generator expression must be parenthesized 587>>> f(x for x in L, y=1) 588Traceback (most recent call last): 589SyntaxError: Generator expression must be parenthesized 590>>> f(x for x in L, *[]) 591Traceback (most recent call last): 592SyntaxError: Generator expression must be parenthesized 593>>> f(x for x in L, **{}) 594Traceback (most recent call last): 595SyntaxError: Generator expression must be parenthesized 596>>> f(L, x for x in L) 597Traceback (most recent call last): 598SyntaxError: Generator expression must be parenthesized 599>>> f(x for x in L, y for y in L) 600Traceback (most recent call last): 601SyntaxError: Generator expression must be parenthesized 602>>> f(x for x in L,) 603Traceback (most recent call last): 604SyntaxError: 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 609Traceback (most recent call last): 610SyntaxError: 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) 708Traceback (most recent call last): 709SyntaxError: 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 {}}) 713Traceback (most recent call last): 714SyntaxError: invalid syntax 715 716The grammar accepts any test (basically, any expression) in the 717keyword slot of a call site. Test a few different options. 718 719>>> f(x()=2) 720Traceback (most recent call last): 721SyntaxError: expression cannot contain assignment, perhaps you meant "=="? 722>>> f(a or b=1) 723Traceback (most recent call last): 724SyntaxError: expression cannot contain assignment, perhaps you meant "=="? 725>>> f(x.y=1) 726Traceback (most recent call last): 727SyntaxError: expression cannot contain assignment, perhaps you meant "=="? 728>>> f((x)=2) 729Traceback (most recent call last): 730SyntaxError: expression cannot contain assignment, perhaps you meant "=="? 731>>> f(True=1) 732Traceback (most recent call last): 733SyntaxError: cannot assign to True 734>>> f(False=1) 735Traceback (most recent call last): 736SyntaxError: cannot assign to False 737>>> f(None=1) 738Traceback (most recent call last): 739SyntaxError: cannot assign to None 740>>> f(__debug__=1) 741Traceback (most recent call last): 742SyntaxError: cannot assign to __debug__ 743>>> __debug__: int 744Traceback (most recent call last): 745SyntaxError: cannot assign to __debug__ 746 747 748More set_context(): 749 750>>> (x for x in x) += 1 751Traceback (most recent call last): 752SyntaxError: 'generator expression' is an illegal expression for augmented assignment 753>>> None += 1 754Traceback (most recent call last): 755SyntaxError: 'None' is an illegal expression for augmented assignment 756>>> __debug__ += 1 757Traceback (most recent call last): 758SyntaxError: cannot assign to __debug__ 759>>> f() += 1 760Traceback (most recent call last): 761SyntaxError: 'function call' is an illegal expression for augmented assignment 762 763 764Test continue in finally in weird combinations. 765 766continue 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 778continue 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 816A 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 827There is one test for a break that is not in a loop. The compiler 828uses a single data structure to keep track of try-finally and loops, 829so we need to be sure that a break is actually inside a loop. If it 830isn'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 842Misuse 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 901From SF bug #1705365 902 >>> nonlocal x 903 Traceback (most recent call last): 904 ... 905 SyntaxError: nonlocal declaration not allowed at module level 906 907From 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 916This tests assignment-context; there was a bug in Python 2.5 where compiling 917a complex 'if' (one with 'elif') would fail to notice an invalid suite, 918leading 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 966Missing ':' 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 1127Missing 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 1137Parenthesized 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 1175Custom 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 1183Custom 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 1225Ensure 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 1242Incomplete 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 1284Specialized 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 1439Make 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 1449Check that an multiple exception types with missing parentheses 1450raise 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 1513Custom 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) 1526Traceback (most recent call last): 1527 ... 1528SyntaxError: keyword argument repeated: a 1529 1530>>> {1, 2, 3} = 42 1531Traceback (most recent call last): 1532SyntaxError: cannot assign to set display here. Maybe you meant '==' instead of '='? 1533 1534>>> {1: 2, 3: 4} = 42 1535Traceback (most recent call last): 1536SyntaxError: cannot assign to dict literal here. Maybe you meant '==' instead of '='? 1537 1538>>> f'{x}' = 42 1539Traceback (most recent call last): 1540SyntaxError: cannot assign to f-string expression here. Maybe you meant '==' instead of '='? 1541 1542>>> f'{x}-{y}' = 42 1543Traceback (most recent call last): 1544SyntaxError: cannot assign to f-string expression here. Maybe you meant '==' instead of '='? 1545 1546>>> (x, y, z=3, d, e) 1547Traceback (most recent call last): 1548SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='? 1549 1550>>> [x, y, z=3, d, e] 1551Traceback (most recent call last): 1552SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='? 1553 1554>>> [z=3] 1555Traceback (most recent call last): 1556SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='? 1557 1558>>> {x, y, z=3, d, e} 1559Traceback (most recent call last): 1560SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='? 1561 1562>>> {z=3} 1563Traceback (most recent call last): 1564SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='? 1565 1566>>> from t import x, 1567Traceback (most recent call last): 1568SyntaxError: trailing comma not allowed without surrounding parentheses 1569 1570>>> from t import x,y, 1571Traceback (most recent call last): 1572SyntaxError: 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 1578Traceback (most recent call last): 1579SyntaxError: invalid syntax 1580 1581>>> (): int 1582Traceback (most recent call last): 1583SyntaxError: only single target (not tuple) can be annotated 1584>>> []: int 1585Traceback (most recent call last): 1586SyntaxError: only single target (not list) can be annotated 1587>>> (()): int 1588Traceback (most recent call last): 1589SyntaxError: only single target (not tuple) can be annotated 1590>>> ([]): int 1591Traceback (most recent call last): 1592SyntaxError: only single target (not list) can be annotated 1593 1594Corner-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 1612Corner-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 1664Uses of the star operator which should fail: 1665 1666A[:*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 1685A[*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 1704A[*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 1723A[*(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 1738A[*:] 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 1749A[*] 1750 1751 >>> A[*] 1752 Traceback (most recent call last): 1753 ... 1754 SyntaxError: invalid syntax 1755 1756A[**] 1757 1758 >>> A[**] 1759 Traceback (most recent call last): 1760 ... 1761 SyntaxError: invalid syntax 1762 1763A[**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 1778def 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 1796x: *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 1807Invalid 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""" 1831 1832import re 1833import doctest 1834import unittest 1835 1836from test import support 1837 1838class SyntaxTestCase(unittest.TestCase): 1839 1840 def _check_error(self, code, errtext, 1841 filename="<testcase>", mode="exec", subclass=None, 1842 lineno=None, offset=None, end_lineno=None, end_offset=None): 1843 """Check that compiling code raises SyntaxError with errtext. 1844 1845 errtest is a regular expression that must be present in the 1846 test of the exception raised. If subclass is specified it 1847 is the expected subclass of SyntaxError (e.g. IndentationError). 1848 """ 1849 try: 1850 compile(code, filename, mode) 1851 except SyntaxError as err: 1852 if subclass and not isinstance(err, subclass): 1853 self.fail("SyntaxError is not a %s" % subclass.__name__) 1854 mo = re.search(errtext, str(err)) 1855 if mo is None: 1856 self.fail("SyntaxError did not contain %r" % (errtext,)) 1857 self.assertEqual(err.filename, filename) 1858 if lineno is not None: 1859 self.assertEqual(err.lineno, lineno) 1860 if offset is not None: 1861 self.assertEqual(err.offset, offset) 1862 if end_lineno is not None: 1863 self.assertEqual(err.end_lineno, end_lineno) 1864 if end_offset is not None: 1865 self.assertEqual(err.end_offset, end_offset) 1866 1867 else: 1868 self.fail("compile() did not raise SyntaxError") 1869 1870 def test_expression_with_assignment(self): 1871 self._check_error( 1872 "print(end1 + end2 = ' ')", 1873 'expression cannot contain assignment, perhaps you meant "=="?', 1874 offset=7 1875 ) 1876 1877 def test_curly_brace_after_primary_raises_immediately(self): 1878 self._check_error("f{}", "invalid syntax", mode="single") 1879 1880 def test_assign_call(self): 1881 self._check_error("f() = 1", "assign") 1882 1883 def test_assign_del(self): 1884 self._check_error("del (,)", "invalid syntax") 1885 self._check_error("del 1", "cannot delete literal") 1886 self._check_error("del (1, 2)", "cannot delete literal") 1887 self._check_error("del None", "cannot delete None") 1888 self._check_error("del *x", "cannot delete starred") 1889 self._check_error("del (*x)", "cannot use starred expression") 1890 self._check_error("del (*x,)", "cannot delete starred") 1891 self._check_error("del [*x,]", "cannot delete starred") 1892 self._check_error("del f()", "cannot delete function call") 1893 self._check_error("del f(a, b)", "cannot delete function call") 1894 self._check_error("del o.f()", "cannot delete function call") 1895 self._check_error("del a[0]()", "cannot delete function call") 1896 self._check_error("del x, f()", "cannot delete function call") 1897 self._check_error("del f(), x", "cannot delete function call") 1898 self._check_error("del [a, b, ((c), (d,), e.f())]", "cannot delete function call") 1899 self._check_error("del (a if True else b)", "cannot delete conditional") 1900 self._check_error("del +a", "cannot delete expression") 1901 self._check_error("del a, +b", "cannot delete expression") 1902 self._check_error("del a + b", "cannot delete expression") 1903 self._check_error("del (a + b, c)", "cannot delete expression") 1904 self._check_error("del (c[0], a + b)", "cannot delete expression") 1905 self._check_error("del a.b.c + 2", "cannot delete expression") 1906 self._check_error("del a.b.c[0] + 2", "cannot delete expression") 1907 self._check_error("del (a, b, (c, d.e.f + 2))", "cannot delete expression") 1908 self._check_error("del [a, b, (c, d.e.f[0] + 2)]", "cannot delete expression") 1909 self._check_error("del (a := 5)", "cannot delete named expression") 1910 # We don't have a special message for this, but make sure we don't 1911 # report "cannot delete name" 1912 self._check_error("del a += b", "invalid syntax") 1913 1914 def test_global_param_err_first(self): 1915 source = """if 1: 1916 def error(a): 1917 global a # SyntaxError 1918 def error2(): 1919 b = 1 1920 global b # SyntaxError 1921 """ 1922 self._check_error(source, "parameter and global", lineno=3) 1923 1924 def test_nonlocal_param_err_first(self): 1925 source = """if 1: 1926 def error(a): 1927 nonlocal a # SyntaxError 1928 def error2(): 1929 b = 1 1930 global b # SyntaxError 1931 """ 1932 self._check_error(source, "parameter and nonlocal", lineno=3) 1933 1934 def test_yield_outside_function(self): 1935 self._check_error("if 0: yield", "outside function") 1936 self._check_error("if 0: yield\nelse: x=1", "outside function") 1937 self._check_error("if 1: pass\nelse: yield", "outside function") 1938 self._check_error("while 0: yield", "outside function") 1939 self._check_error("while 0: yield\nelse: x=1", "outside function") 1940 self._check_error("class C:\n if 0: yield", "outside function") 1941 self._check_error("class C:\n if 1: pass\n else: yield", 1942 "outside function") 1943 self._check_error("class C:\n while 0: yield", "outside function") 1944 self._check_error("class C:\n while 0: yield\n else: x = 1", 1945 "outside function") 1946 1947 def test_return_outside_function(self): 1948 self._check_error("if 0: return", "outside function") 1949 self._check_error("if 0: return\nelse: x=1", "outside function") 1950 self._check_error("if 1: pass\nelse: return", "outside function") 1951 self._check_error("while 0: return", "outside function") 1952 self._check_error("class C:\n if 0: return", "outside function") 1953 self._check_error("class C:\n while 0: return", "outside function") 1954 self._check_error("class C:\n while 0: return\n else: x=1", 1955 "outside function") 1956 self._check_error("class C:\n if 0: return\n else: x= 1", 1957 "outside function") 1958 self._check_error("class C:\n if 1: pass\n else: return", 1959 "outside function") 1960 1961 def test_break_outside_loop(self): 1962 msg = "outside loop" 1963 self._check_error("break", msg, lineno=1) 1964 self._check_error("if 0: break", msg, lineno=1) 1965 self._check_error("if 0: break\nelse: x=1", msg, lineno=1) 1966 self._check_error("if 1: pass\nelse: break", msg, lineno=2) 1967 self._check_error("class C:\n if 0: break", msg, lineno=2) 1968 self._check_error("class C:\n if 1: pass\n else: break", 1969 msg, lineno=3) 1970 self._check_error("with object() as obj:\n break", 1971 msg, lineno=2) 1972 1973 def test_continue_outside_loop(self): 1974 msg = "not properly in loop" 1975 self._check_error("if 0: continue", msg, lineno=1) 1976 self._check_error("if 0: continue\nelse: x=1", msg, lineno=1) 1977 self._check_error("if 1: pass\nelse: continue", msg, lineno=2) 1978 self._check_error("class C:\n if 0: continue", msg, lineno=2) 1979 self._check_error("class C:\n if 1: pass\n else: continue", 1980 msg, lineno=3) 1981 self._check_error("with object() as obj:\n continue", 1982 msg, lineno=2) 1983 1984 def test_unexpected_indent(self): 1985 self._check_error("foo()\n bar()\n", "unexpected indent", 1986 subclass=IndentationError) 1987 1988 def test_no_indent(self): 1989 self._check_error("if 1:\nfoo()", "expected an indented block", 1990 subclass=IndentationError) 1991 1992 def test_bad_outdent(self): 1993 self._check_error("if 1:\n foo()\n bar()", 1994 "unindent does not match .* level", 1995 subclass=IndentationError) 1996 1997 def test_kwargs_last(self): 1998 self._check_error("int(base=10, '2')", 1999 "positional argument follows keyword argument") 2000 2001 def test_kwargs_last2(self): 2002 self._check_error("int(**{'base': 10}, '2')", 2003 "positional argument follows " 2004 "keyword argument unpacking") 2005 2006 def test_kwargs_last3(self): 2007 self._check_error("int(**{'base': 10}, *['2'])", 2008 "iterable argument unpacking follows " 2009 "keyword argument unpacking") 2010 2011 def test_generator_in_function_call(self): 2012 self._check_error("foo(x, y for y in range(3) for z in range(2) if z , p)", 2013 "Generator expression must be parenthesized", 2014 lineno=1, end_lineno=1, offset=11, end_offset=53) 2015 2016 def test_except_then_except_star(self): 2017 self._check_error("try: pass\nexcept ValueError: pass\nexcept* TypeError: pass", 2018 r"cannot have both 'except' and 'except\*' on the same 'try'", 2019 lineno=3, end_lineno=3, offset=1, end_offset=8) 2020 2021 def test_except_star_then_except(self): 2022 self._check_error("try: pass\nexcept* ValueError: pass\nexcept TypeError: pass", 2023 r"cannot have both 'except' and 'except\*' on the same 'try'", 2024 lineno=3, end_lineno=3, offset=1, end_offset=7) 2025 2026 def test_empty_line_after_linecont(self): 2027 # See issue-40847 2028 s = r"""\ 2029pass 2030 \ 2031 2032pass 2033""" 2034 try: 2035 compile(s, '<string>', 'exec') 2036 except SyntaxError: 2037 self.fail("Empty line after a line continuation character is valid.") 2038 2039 # See issue-46091 2040 s1 = r"""\ 2041def fib(n): 2042 \ 2043'''Print a Fibonacci series up to n.''' 2044 \ 2045a, b = 0, 1 2046""" 2047 s2 = r"""\ 2048def fib(n): 2049 '''Print a Fibonacci series up to n.''' 2050 a, b = 0, 1 2051""" 2052 try: 2053 compile(s1, '<string>', 'exec') 2054 compile(s2, '<string>', 'exec') 2055 except SyntaxError: 2056 self.fail("Indented statement over multiple lines is valid") 2057 2058 def test_continuation_bad_indentation(self): 2059 # Check that code that breaks indentation across multiple lines raises a syntax error 2060 2061 code = r"""\ 2062if x: 2063 y = 1 2064 \ 2065 foo = 1 2066 """ 2067 2068 self.assertRaises(IndentationError, exec, code) 2069 2070 @support.cpython_only 2071 def test_nested_named_except_blocks(self): 2072 code = "" 2073 for i in range(12): 2074 code += f"{' '*i}try:\n" 2075 code += f"{' '*(i+1)}raise Exception\n" 2076 code += f"{' '*i}except Exception as e:\n" 2077 code += f"{' '*4*12}pass" 2078 self._check_error(code, "too many statically nested blocks") 2079 2080 def test_barry_as_flufl_with_syntax_errors(self): 2081 # The "barry_as_flufl" rule can produce some "bugs-at-a-distance" if 2082 # is reading the wrong token in the presence of syntax errors later 2083 # in the file. See bpo-42214 for more information. 2084 code = """ 2085def func1(): 2086 if a != b: 2087 raise ValueError 2088 2089def func2(): 2090 try 2091 return 1 2092 finally: 2093 pass 2094""" 2095 self._check_error(code, "expected ':'") 2096 2097 def test_invalid_line_continuation_error_position(self): 2098 self._check_error(r"a = 3 \ 4", 2099 "unexpected character after line continuation character", 2100 lineno=1, offset=8) 2101 self._check_error('1,\\#\n2', 2102 "unexpected character after line continuation character", 2103 lineno=1, offset=4) 2104 self._check_error('\nfgdfgf\n1,\\#\n2\n', 2105 "unexpected character after line continuation character", 2106 lineno=3, offset=4) 2107 2108 def test_invalid_line_continuation_left_recursive(self): 2109 # Check bpo-42218: SyntaxErrors following left-recursive rules 2110 # (t_primary_raw in this case) need to be tested explicitly 2111 self._check_error("A.\u018a\\ ", 2112 "unexpected character after line continuation character") 2113 self._check_error("A.\u03bc\\\n", 2114 "unexpected EOF while parsing") 2115 2116 def test_error_parenthesis(self): 2117 for paren in "([{": 2118 self._check_error(paren + "1 + 2", f"\\{paren}' was never closed") 2119 2120 for paren in "([{": 2121 self._check_error(f"a = {paren} 1, 2, 3\nb=3", f"\\{paren}' was never closed") 2122 2123 for paren in ")]}": 2124 self._check_error(paren + "1 + 2", f"unmatched '\\{paren}'") 2125 2126 # Some more complex examples: 2127 code = """\ 2128func( 2129 a=["unclosed], # Need a quote in this comment: " 2130 b=2, 2131) 2132""" 2133 self._check_error(code, "parenthesis '\\)' does not match opening parenthesis '\\['") 2134 2135 def test_error_string_literal(self): 2136 2137 self._check_error("'blech", "unterminated string literal") 2138 self._check_error('"blech', "unterminated string literal") 2139 self._check_error("'''blech", "unterminated triple-quoted string literal") 2140 self._check_error('"""blech', "unterminated triple-quoted string literal") 2141 2142 def test_invisible_characters(self): 2143 self._check_error('print\x17("Hello")', "invalid non-printable character") 2144 2145 def test_match_call_does_not_raise_syntax_error(self): 2146 code = """ 2147def match(x): 2148 return 1+1 2149 2150match(34) 2151""" 2152 compile(code, "<string>", "exec") 2153 2154 def test_case_call_does_not_raise_syntax_error(self): 2155 code = """ 2156def case(x): 2157 return 1+1 2158 2159case(34) 2160""" 2161 compile(code, "<string>", "exec") 2162 2163 def test_multiline_compiler_error_points_to_the_end(self): 2164 self._check_error( 2165 "call(\na=1,\na=1\n)", 2166 "keyword argument repeated", 2167 lineno=3 2168 ) 2169 2170 @support.cpython_only 2171 def test_syntax_error_on_deeply_nested_blocks(self): 2172 # This raises a SyntaxError, it used to raise a SystemError. Context 2173 # for this change can be found on issue #27514 2174 2175 # In 2.5 there was a missing exception and an assert was triggered in a 2176 # debug build. The number of blocks must be greater than CO_MAXBLOCKS. 2177 # SF #1565514 2178 2179 source = """ 2180while 1: 2181 while 2: 2182 while 3: 2183 while 4: 2184 while 5: 2185 while 6: 2186 while 8: 2187 while 9: 2188 while 10: 2189 while 11: 2190 while 12: 2191 while 13: 2192 while 14: 2193 while 15: 2194 while 16: 2195 while 17: 2196 while 18: 2197 while 19: 2198 while 20: 2199 while 21: 2200 while 22: 2201 break 2202""" 2203 self._check_error(source, "too many statically nested blocks") 2204 2205 @support.cpython_only 2206 def test_error_on_parser_stack_overflow(self): 2207 source = "-" * 100000 + "4" 2208 for mode in ["exec", "eval", "single"]: 2209 with self.subTest(mode=mode): 2210 with self.assertRaises(MemoryError): 2211 compile(source, "<string>", mode) 2212 2213 @support.cpython_only 2214 def test_deep_invalid_rule(self): 2215 # Check that a very deep invalid rule in the PEG 2216 # parser doesn't have exponential backtracking. 2217 source = "d{{{{{{{{{{{{{{{{{{{{{{{{{```{{{{{{{ef f():y" 2218 with self.assertRaises(SyntaxError): 2219 compile(source, "<string>", "exec") 2220 2221 2222def load_tests(loader, tests, pattern): 2223 tests.addTest(doctest.DocTestSuite()) 2224 return tests 2225 2226 2227if __name__ == "__main__": 2228 unittest.main() 2229