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