(root)/
Python-3.11.7/
Lib/
test/
test_grammar.py
       1  # Python test set -- part 1, grammar.
       2  # This just tests whether the parser accepts them all.
       3  
       4  from test.support import check_syntax_error
       5  from test.support import import_helper
       6  from test.support.warnings_helper import check_syntax_warning
       7  import inspect
       8  import unittest
       9  import sys
      10  import warnings
      11  # testing import *
      12  from sys import *
      13  
      14  # different import patterns to check that __annotations__ does not interfere
      15  # with import machinery
      16  import test.typinganndata.ann_module as ann_module
      17  import typing
      18  from test.typinganndata import ann_module2
      19  import test
      20  
      21  # These are shared with test_tokenize and other test modules.
      22  #
      23  # Note: since several test cases filter out floats by looking for "e" and ".",
      24  # don't add hexadecimal literals that contain "e" or "E".
      25  VALID_UNDERSCORE_LITERALS = [
      26      '0_0_0',
      27      '4_2',
      28      '1_0000_0000',
      29      '0b1001_0100',
      30      '0xffff_ffff',
      31      '0o5_7_7',
      32      '1_00_00.5',
      33      '1_00_00.5e5',
      34      '1_00_00e5_1',
      35      '1e1_0',
      36      '.1_4',
      37      '.1_4e1',
      38      '0b_0',
      39      '0x_f',
      40      '0o_5',
      41      '1_00_00j',
      42      '1_00_00.5j',
      43      '1_00_00e5_1j',
      44      '.1_4j',
      45      '(1_2.5+3_3j)',
      46      '(.5_6j)',
      47  ]
      48  INVALID_UNDERSCORE_LITERALS = [
      49      # Trailing underscores:
      50      '0_',
      51      '42_',
      52      '1.4j_',
      53      '0x_',
      54      '0b1_',
      55      '0xf_',
      56      '0o5_',
      57      '0 if 1_Else 1',
      58      # Underscores in the base selector:
      59      '0_b0',
      60      '0_xf',
      61      '0_o5',
      62      # Old-style octal, still disallowed:
      63      '0_7',
      64      '09_99',
      65      # Multiple consecutive underscores:
      66      '4_______2',
      67      '0.1__4',
      68      '0.1__4j',
      69      '0b1001__0100',
      70      '0xffff__ffff',
      71      '0x___',
      72      '0o5__77',
      73      '1e1__0',
      74      '1e1__0j',
      75      # Underscore right before a dot:
      76      '1_.4',
      77      '1_.4j',
      78      # Underscore right after a dot:
      79      '1._4',
      80      '1._4j',
      81      '._5',
      82      '._5j',
      83      # Underscore right after a sign:
      84      '1.0e+_1',
      85      '1.0e+_1j',
      86      # Underscore right before j:
      87      '1.4_j',
      88      '1.4e5_j',
      89      # Underscore right before e:
      90      '1_e1',
      91      '1.4_e1',
      92      '1.4_e1j',
      93      # Underscore right after e:
      94      '1e_1',
      95      '1.4e_1',
      96      '1.4e_1j',
      97      # Complex cases with parens:
      98      '(1+1.5_j_)',
      99      '(1+1.5_j)',
     100  ]
     101  
     102  
     103  class ESC[4;38;5;81mTokenTests(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
     104  
     105      from test.support import check_syntax_error
     106      from test.support.warnings_helper import check_syntax_warning
     107  
     108      def test_backslash(self):
     109          # Backslash means line continuation:
     110          x = 1 \
     111          + 1
     112          self.assertEqual(x, 2, 'backslash for line continuation')
     113  
     114          # Backslash does not means continuation in comments :\
     115          x = 0
     116          self.assertEqual(x, 0, 'backslash ending comment')
     117  
     118      def test_plain_integers(self):
     119          self.assertEqual(type(000), type(0))
     120          self.assertEqual(0xff, 255)
     121          self.assertEqual(0o377, 255)
     122          self.assertEqual(2147483647, 0o17777777777)
     123          self.assertEqual(0b1001, 9)
     124          # "0x" is not a valid literal
     125          self.assertRaises(SyntaxError, eval, "0x")
     126          from sys import maxsize
     127          if maxsize == 2147483647:
     128              self.assertEqual(-2147483647-1, -0o20000000000)
     129              # XXX -2147483648
     130              self.assertTrue(0o37777777777 > 0)
     131              self.assertTrue(0xffffffff > 0)
     132              self.assertTrue(0b1111111111111111111111111111111 > 0)
     133              for s in ('2147483648', '0o40000000000', '0x100000000',
     134                        '0b10000000000000000000000000000000'):
     135                  try:
     136                      x = eval(s)
     137                  except OverflowError:
     138                      self.fail("OverflowError on huge integer literal %r" % s)
     139          elif maxsize == 9223372036854775807:
     140              self.assertEqual(-9223372036854775807-1, -0o1000000000000000000000)
     141              self.assertTrue(0o1777777777777777777777 > 0)
     142              self.assertTrue(0xffffffffffffffff > 0)
     143              self.assertTrue(0b11111111111111111111111111111111111111111111111111111111111111 > 0)
     144              for s in '9223372036854775808', '0o2000000000000000000000', \
     145                       '0x10000000000000000', \
     146                       '0b100000000000000000000000000000000000000000000000000000000000000':
     147                  try:
     148                      x = eval(s)
     149                  except OverflowError:
     150                      self.fail("OverflowError on huge integer literal %r" % s)
     151          else:
     152              self.fail('Weird maxsize value %r' % maxsize)
     153  
     154      def test_long_integers(self):
     155          x = 0
     156          x = 0xffffffffffffffff
     157          x = 0Xffffffffffffffff
     158          x = 0o77777777777777777
     159          x = 0O77777777777777777
     160          x = 123456789012345678901234567890
     161          x = 0b100000000000000000000000000000000000000000000000000000000000000000000
     162          x = 0B111111111111111111111111111111111111111111111111111111111111111111111
     163  
     164      def test_floats(self):
     165          x = 3.14
     166          x = 314.
     167          x = 0.314
     168          # XXX x = 000.314
     169          x = .314
     170          x = 3e14
     171          x = 3E14
     172          x = 3e-14
     173          x = 3e+14
     174          x = 3.e14
     175          x = .3e14
     176          x = 3.1e4
     177  
     178      def test_float_exponent_tokenization(self):
     179          # See issue 21642.
     180          with warnings.catch_warnings():
     181              warnings.simplefilter('ignore', SyntaxWarning)
     182              self.assertEqual(eval("1 if 1else 0"), 1)
     183              self.assertEqual(eval("1 if 0else 0"), 0)
     184          self.assertRaises(SyntaxError, eval, "0 if 1Else 0")
     185  
     186      def test_underscore_literals(self):
     187          for lit in VALID_UNDERSCORE_LITERALS:
     188              self.assertEqual(eval(lit), eval(lit.replace('_', '')))
     189          for lit in INVALID_UNDERSCORE_LITERALS:
     190              self.assertRaises(SyntaxError, eval, lit)
     191          # Sanity check: no literal begins with an underscore
     192          self.assertRaises(NameError, eval, "_0")
     193  
     194      def test_bad_numerical_literals(self):
     195          check = self.check_syntax_error
     196          check("0b12", "invalid digit '2' in binary literal")
     197          check("0b1_2", "invalid digit '2' in binary literal")
     198          check("0b2", "invalid digit '2' in binary literal")
     199          check("0b1_", "invalid binary literal")
     200          check("0b", "invalid binary literal")
     201          check("0o18", "invalid digit '8' in octal literal")
     202          check("0o1_8", "invalid digit '8' in octal literal")
     203          check("0o8", "invalid digit '8' in octal literal")
     204          check("0o1_", "invalid octal literal")
     205          check("0o", "invalid octal literal")
     206          check("0x1_", "invalid hexadecimal literal")
     207          check("0x", "invalid hexadecimal literal")
     208          check("1_", "invalid decimal literal")
     209          check("012",
     210                "leading zeros in decimal integer literals are not permitted; "
     211                "use an 0o prefix for octal integers")
     212          check("1.2_", "invalid decimal literal")
     213          check("1e2_", "invalid decimal literal")
     214          check("1e+", "invalid decimal literal")
     215  
     216      def test_end_of_numerical_literals(self):
     217          def check(test, error=False):
     218              with self.subTest(expr=test):
     219                  if error:
     220                      with warnings.catch_warnings(record=True) as w:
     221                          with self.assertRaisesRegex(SyntaxError,
     222                                      r'invalid \w+ literal'):
     223                              compile(test, "<testcase>", "eval")
     224                      self.assertEqual(w,  [])
     225                  else:
     226                      self.check_syntax_warning(test,
     227                              errtext=r'invalid \w+ literal')
     228  
     229          for num in "0xf", "0o7", "0b1", "9", "0", "1.", "1e3", "1j":
     230              compile(num, "<testcase>", "eval")
     231              check(f"{num}and x", error=(num == "0xf"))
     232              check(f"{num}or x", error=(num == "0"))
     233              check(f"{num}in x")
     234              check(f"{num}not in x")
     235              check(f"{num}if x else y")
     236              check(f"x if {num}else y", error=(num == "0xf"))
     237              check(f"[{num}for x in ()]")
     238              check(f"{num}spam", error=True)
     239  
     240              # gh-88943: Invalid non-ASCII character following a numerical literal.
     241              with self.assertRaisesRegex(SyntaxError, r"invalid character '⁄' \(U\+2044\)"):
     242                  compile(f"{num}⁄7", "<testcase>", "eval")
     243  
     244              with warnings.catch_warnings():
     245                  warnings.filterwarnings('ignore', '"is" with a literal',
     246                                          SyntaxWarning)
     247                  with self.assertWarnsRegex(SyntaxWarning,
     248                              r'invalid \w+ literal'):
     249                      compile(f"{num}is x", "<testcase>", "eval")
     250                  warnings.simplefilter('error', SyntaxWarning)
     251                  with self.assertRaisesRegex(SyntaxError,
     252                              r'invalid \w+ literal'):
     253                      compile(f"{num}is x", "<testcase>", "eval")
     254  
     255          check("[0x1ffor x in ()]")
     256          check("[0x1for x in ()]")
     257          check("[0xfor x in ()]")
     258  
     259      def test_string_literals(self):
     260          x = ''; y = ""; self.assertTrue(len(x) == 0 and x == y)
     261          x = '\''; y = "'"; self.assertTrue(len(x) == 1 and x == y and ord(x) == 39)
     262          x = '"'; y = "\""; self.assertTrue(len(x) == 1 and x == y and ord(x) == 34)
     263          x = "doesn't \"shrink\" does it"
     264          y = 'doesn\'t "shrink" does it'
     265          self.assertTrue(len(x) == 24 and x == y)
     266          x = "does \"shrink\" doesn't it"
     267          y = 'does "shrink" doesn\'t it'
     268          self.assertTrue(len(x) == 24 and x == y)
     269          x = """
     270  The "quick"
     271  brown fox
     272  jumps over
     273  the 'lazy' dog.
     274  """
     275          y = '\nThe "quick"\nbrown fox\njumps over\nthe \'lazy\' dog.\n'
     276          self.assertEqual(x, y)
     277          y = '''
     278  The "quick"
     279  brown fox
     280  jumps over
     281  the 'lazy' dog.
     282  '''
     283          self.assertEqual(x, y)
     284          y = "\n\
     285  The \"quick\"\n\
     286  brown fox\n\
     287  jumps over\n\
     288  the 'lazy' dog.\n\
     289  "
     290          self.assertEqual(x, y)
     291          y = '\n\
     292  The \"quick\"\n\
     293  brown fox\n\
     294  jumps over\n\
     295  the \'lazy\' dog.\n\
     296  '
     297          self.assertEqual(x, y)
     298  
     299      def test_ellipsis(self):
     300          x = ...
     301          self.assertTrue(x is Ellipsis)
     302          self.assertRaises(SyntaxError, eval, ".. .")
     303  
     304      def test_eof_error(self):
     305          samples = ("def foo(", "\ndef foo(", "def foo(\n")
     306          for s in samples:
     307              with self.assertRaises(SyntaxError) as cm:
     308                  compile(s, "<test>", "exec")
     309              self.assertIn("was never closed", str(cm.exception))
     310  
     311  var_annot_global: int # a global annotated is necessary for test_var_annot
     312  
     313  # custom namespace for testing __annotations__
     314  
     315  class ESC[4;38;5;81mCNS:
     316      def __init__(self):
     317          self._dct = {}
     318      def __setitem__(self, item, value):
     319          self._dct[item.lower()] = value
     320      def __getitem__(self, item):
     321          return self._dct[item]
     322  
     323  
     324  class ESC[4;38;5;81mGrammarTests(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
     325  
     326      from test.support import check_syntax_error
     327      from test.support.warnings_helper import check_syntax_warning
     328      from test.support.warnings_helper import check_no_warnings
     329  
     330      # single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE
     331      # XXX can't test in a script -- this rule is only used when interactive
     332  
     333      # file_input: (NEWLINE | stmt)* ENDMARKER
     334      # Being tested as this very moment this very module
     335  
     336      # expr_input: testlist NEWLINE
     337      # XXX Hard to test -- used only in calls to input()
     338  
     339      def test_eval_input(self):
     340          # testlist ENDMARKER
     341          x = eval('1, 0 or 1')
     342  
     343      def test_var_annot_basics(self):
     344          # all these should be allowed
     345          var1: int = 5
     346          var2: [int, str]
     347          my_lst = [42]
     348          def one():
     349              return 1
     350          int.new_attr: int
     351          [list][0]: type
     352          my_lst[one()-1]: int = 5
     353          self.assertEqual(my_lst, [5])
     354  
     355      def test_var_annot_syntax_errors(self):
     356          # parser pass
     357          check_syntax_error(self, "def f: int")
     358          check_syntax_error(self, "x: int: str")
     359          check_syntax_error(self, "def f():\n"
     360                                   "    nonlocal x: int\n")
     361          check_syntax_error(self, "def f():\n"
     362                                   "    global x: int\n")
     363          check_syntax_error(self, "x: int = y = 1")
     364          check_syntax_error(self, "z = w: int = 1")
     365          check_syntax_error(self, "x: int = y: int = 1")
     366          # AST pass
     367          check_syntax_error(self, "[x, 0]: int\n")
     368          check_syntax_error(self, "f(): int\n")
     369          check_syntax_error(self, "(x,): int")
     370          check_syntax_error(self, "def f():\n"
     371                                   "    (x, y): int = (1, 2)\n")
     372          # symtable pass
     373          check_syntax_error(self, "def f():\n"
     374                                   "    x: int\n"
     375                                   "    global x\n")
     376          check_syntax_error(self, "def f():\n"
     377                                   "    global x\n"
     378                                   "    x: int\n")
     379          check_syntax_error(self, "def f():\n"
     380                                   "    x: int\n"
     381                                   "    nonlocal x\n")
     382          check_syntax_error(self, "def f():\n"
     383                                   "    nonlocal x\n"
     384                                   "    x: int\n")
     385  
     386      def test_var_annot_basic_semantics(self):
     387          # execution order
     388          with self.assertRaises(ZeroDivisionError):
     389              no_name[does_not_exist]: no_name_again = 1/0
     390          with self.assertRaises(NameError):
     391              no_name[does_not_exist]: 1/0 = 0
     392          global var_annot_global
     393  
     394          # function semantics
     395          def f():
     396              st: str = "Hello"
     397              a.b: int = (1, 2)
     398              return st
     399          self.assertEqual(f.__annotations__, {})
     400          def f_OK():
     401              x: 1/0
     402          f_OK()
     403          def fbad():
     404              x: int
     405              print(x)
     406          with self.assertRaises(UnboundLocalError):
     407              fbad()
     408          def f2bad():
     409              (no_such_global): int
     410              print(no_such_global)
     411          try:
     412              f2bad()
     413          except Exception as e:
     414              self.assertIs(type(e), NameError)
     415  
     416          # class semantics
     417          class ESC[4;38;5;81mC:
     418              __foo: int
     419              s: str = "attr"
     420              z = 2
     421              def __init__(self, x):
     422                  self.x: int = x
     423          self.assertEqual(C.__annotations__, {'_C__foo': int, 's': str})
     424          with self.assertRaises(NameError):
     425              class ESC[4;38;5;81mCBad:
     426                  no_such_name_defined.attr: int = 0
     427          with self.assertRaises(NameError):
     428              class ESC[4;38;5;81mCbad2(ESC[4;38;5;149mC):
     429                  x: int
     430                  x.y: list = []
     431  
     432      def test_annotations_inheritance(self):
     433          # Check that annotations are not inherited by derived classes
     434          class ESC[4;38;5;81mA:
     435              attr: int
     436          class ESC[4;38;5;81mB(ESC[4;38;5;149mA):
     437              pass
     438          class ESC[4;38;5;81mC(ESC[4;38;5;149mA):
     439              attr: str
     440          class ESC[4;38;5;81mD:
     441              attr2: int
     442          class ESC[4;38;5;81mE(ESC[4;38;5;149mA, ESC[4;38;5;149mD):
     443              pass
     444          class ESC[4;38;5;81mF(ESC[4;38;5;149mC, ESC[4;38;5;149mA):
     445              pass
     446          self.assertEqual(A.__annotations__, {"attr": int})
     447          self.assertEqual(B.__annotations__, {})
     448          self.assertEqual(C.__annotations__, {"attr" : str})
     449          self.assertEqual(D.__annotations__, {"attr2" : int})
     450          self.assertEqual(E.__annotations__, {})
     451          self.assertEqual(F.__annotations__, {})
     452  
     453  
     454      def test_var_annot_metaclass_semantics(self):
     455          class ESC[4;38;5;81mCMeta(ESC[4;38;5;149mtype):
     456              @classmethod
     457              def __prepare__(metacls, name, bases, **kwds):
     458                  return {'__annotations__': CNS()}
     459          class ESC[4;38;5;81mCC(metaclass=ESC[4;38;5;149mCMeta):
     460              XX: 'ANNOT'
     461          self.assertEqual(CC.__annotations__['xx'], 'ANNOT')
     462  
     463      def test_var_annot_module_semantics(self):
     464          self.assertEqual(test.__annotations__, {})
     465          self.assertEqual(ann_module.__annotations__,
     466                       {1: 2, 'x': int, 'y': str, 'f': typing.Tuple[int, int], 'u': int | float})
     467          self.assertEqual(ann_module.M.__annotations__,
     468                                {'123': 123, 'o': type})
     469          self.assertEqual(ann_module2.__annotations__, {})
     470  
     471      def test_var_annot_in_module(self):
     472          # check that functions fail the same way when executed
     473          # outside of module where they were defined
     474          ann_module3 = import_helper.import_fresh_module("test.typinganndata.ann_module3")
     475          with self.assertRaises(NameError):
     476              ann_module3.f_bad_ann()
     477          with self.assertRaises(NameError):
     478              ann_module3.g_bad_ann()
     479          with self.assertRaises(NameError):
     480              ann_module3.D_bad_ann(5)
     481  
     482      def test_var_annot_simple_exec(self):
     483          gns = {}; lns= {}
     484          exec("'docstring'\n"
     485               "__annotations__[1] = 2\n"
     486               "x: int = 5\n", gns, lns)
     487          self.assertEqual(lns["__annotations__"], {1: 2, 'x': int})
     488          with self.assertRaises(KeyError):
     489              gns['__annotations__']
     490  
     491      def test_var_annot_custom_maps(self):
     492          # tests with custom locals() and __annotations__
     493          ns = {'__annotations__': CNS()}
     494          exec('X: int; Z: str = "Z"; (w): complex = 1j', ns)
     495          self.assertEqual(ns['__annotations__']['x'], int)
     496          self.assertEqual(ns['__annotations__']['z'], str)
     497          with self.assertRaises(KeyError):
     498              ns['__annotations__']['w']
     499          nonloc_ns = {}
     500          class ESC[4;38;5;81mCNS2:
     501              def __init__(self):
     502                  self._dct = {}
     503              def __setitem__(self, item, value):
     504                  nonlocal nonloc_ns
     505                  self._dct[item] = value
     506                  nonloc_ns[item] = value
     507              def __getitem__(self, item):
     508                  return self._dct[item]
     509          exec('x: int = 1', {}, CNS2())
     510          self.assertEqual(nonloc_ns['__annotations__']['x'], int)
     511  
     512      def test_var_annot_refleak(self):
     513          # complex case: custom locals plus custom __annotations__
     514          # this was causing refleak
     515          cns = CNS()
     516          nonloc_ns = {'__annotations__': cns}
     517          class ESC[4;38;5;81mCNS2:
     518              def __init__(self):
     519                  self._dct = {'__annotations__': cns}
     520              def __setitem__(self, item, value):
     521                  nonlocal nonloc_ns
     522                  self._dct[item] = value
     523                  nonloc_ns[item] = value
     524              def __getitem__(self, item):
     525                  return self._dct[item]
     526          exec('X: str', {}, CNS2())
     527          self.assertEqual(nonloc_ns['__annotations__']['x'], str)
     528  
     529      def test_var_annot_rhs(self):
     530          ns = {}
     531          exec('x: tuple = 1, 2', ns)
     532          self.assertEqual(ns['x'], (1, 2))
     533          stmt = ('def f():\n'
     534                  '    x: int = yield')
     535          exec(stmt, ns)
     536          self.assertEqual(list(ns['f']()), [None])
     537  
     538          ns = {"a": 1, 'b': (2, 3, 4), "c":5, "Tuple": typing.Tuple}
     539          exec('x: Tuple[int, ...] = a,*b,c', ns)
     540          self.assertEqual(ns['x'], (1, 2, 3, 4, 5))
     541  
     542      def test_funcdef(self):
     543          ### [decorators] 'def' NAME parameters ['->' test] ':' suite
     544          ### decorator: '@' namedexpr_test NEWLINE
     545          ### decorators: decorator+
     546          ### parameters: '(' [typedargslist] ')'
     547          ### typedargslist: ((tfpdef ['=' test] ',')*
     548          ###                ('*' [tfpdef] (',' tfpdef ['=' test])* [',' '**' tfpdef] | '**' tfpdef)
     549          ###                | tfpdef ['=' test] (',' tfpdef ['=' test])* [','])
     550          ### tfpdef: NAME [':' test]
     551          ### varargslist: ((vfpdef ['=' test] ',')*
     552          ###              ('*' [vfpdef] (',' vfpdef ['=' test])*  [',' '**' vfpdef] | '**' vfpdef)
     553          ###              | vfpdef ['=' test] (',' vfpdef ['=' test])* [','])
     554          ### vfpdef: NAME
     555          def f1(): pass
     556          f1()
     557          f1(*())
     558          f1(*(), **{})
     559          def f2(one_argument): pass
     560          def f3(two, arguments): pass
     561          self.assertEqual(f2.__code__.co_varnames, ('one_argument',))
     562          self.assertEqual(f3.__code__.co_varnames, ('two', 'arguments'))
     563          def a1(one_arg,): pass
     564          def a2(two, args,): pass
     565          def v0(*rest): pass
     566          def v1(a, *rest): pass
     567          def v2(a, b, *rest): pass
     568  
     569          f1()
     570          f2(1)
     571          f2(1,)
     572          f3(1, 2)
     573          f3(1, 2,)
     574          v0()
     575          v0(1)
     576          v0(1,)
     577          v0(1,2)
     578          v0(1,2,3,4,5,6,7,8,9,0)
     579          v1(1)
     580          v1(1,)
     581          v1(1,2)
     582          v1(1,2,3)
     583          v1(1,2,3,4,5,6,7,8,9,0)
     584          v2(1,2)
     585          v2(1,2,3)
     586          v2(1,2,3,4)
     587          v2(1,2,3,4,5,6,7,8,9,0)
     588  
     589          def d01(a=1): pass
     590          d01()
     591          d01(1)
     592          d01(*(1,))
     593          d01(*[] or [2])
     594          d01(*() or (), *{} and (), **() or {})
     595          d01(**{'a':2})
     596          d01(**{'a':2} or {})
     597          def d11(a, b=1): pass
     598          d11(1)
     599          d11(1, 2)
     600          d11(1, **{'b':2})
     601          def d21(a, b, c=1): pass
     602          d21(1, 2)
     603          d21(1, 2, 3)
     604          d21(*(1, 2, 3))
     605          d21(1, *(2, 3))
     606          d21(1, 2, *(3,))
     607          d21(1, 2, **{'c':3})
     608          def d02(a=1, b=2): pass
     609          d02()
     610          d02(1)
     611          d02(1, 2)
     612          d02(*(1, 2))
     613          d02(1, *(2,))
     614          d02(1, **{'b':2})
     615          d02(**{'a': 1, 'b': 2})
     616          def d12(a, b=1, c=2): pass
     617          d12(1)
     618          d12(1, 2)
     619          d12(1, 2, 3)
     620          def d22(a, b, c=1, d=2): pass
     621          d22(1, 2)
     622          d22(1, 2, 3)
     623          d22(1, 2, 3, 4)
     624          def d01v(a=1, *rest): pass
     625          d01v()
     626          d01v(1)
     627          d01v(1, 2)
     628          d01v(*(1, 2, 3, 4))
     629          d01v(*(1,))
     630          d01v(**{'a':2})
     631          def d11v(a, b=1, *rest): pass
     632          d11v(1)
     633          d11v(1, 2)
     634          d11v(1, 2, 3)
     635          def d21v(a, b, c=1, *rest): pass
     636          d21v(1, 2)
     637          d21v(1, 2, 3)
     638          d21v(1, 2, 3, 4)
     639          d21v(*(1, 2, 3, 4))
     640          d21v(1, 2, **{'c': 3})
     641          def d02v(a=1, b=2, *rest): pass
     642          d02v()
     643          d02v(1)
     644          d02v(1, 2)
     645          d02v(1, 2, 3)
     646          d02v(1, *(2, 3, 4))
     647          d02v(**{'a': 1, 'b': 2})
     648          def d12v(a, b=1, c=2, *rest): pass
     649          d12v(1)
     650          d12v(1, 2)
     651          d12v(1, 2, 3)
     652          d12v(1, 2, 3, 4)
     653          d12v(*(1, 2, 3, 4))
     654          d12v(1, 2, *(3, 4, 5))
     655          d12v(1, *(2,), **{'c': 3})
     656          def d22v(a, b, c=1, d=2, *rest): pass
     657          d22v(1, 2)
     658          d22v(1, 2, 3)
     659          d22v(1, 2, 3, 4)
     660          d22v(1, 2, 3, 4, 5)
     661          d22v(*(1, 2, 3, 4))
     662          d22v(1, 2, *(3, 4, 5))
     663          d22v(1, *(2, 3), **{'d': 4})
     664  
     665          # keyword argument type tests
     666          with warnings.catch_warnings():
     667              warnings.simplefilter('ignore', BytesWarning)
     668              try:
     669                  str('x', **{b'foo':1 })
     670              except TypeError:
     671                  pass
     672              else:
     673                  self.fail('Bytes should not work as keyword argument names')
     674          # keyword only argument tests
     675          def pos0key1(*, key): return key
     676          pos0key1(key=100)
     677          def pos2key2(p1, p2, *, k1, k2=100): return p1,p2,k1,k2
     678          pos2key2(1, 2, k1=100)
     679          pos2key2(1, 2, k1=100, k2=200)
     680          pos2key2(1, 2, k2=100, k1=200)
     681          def pos2key2dict(p1, p2, *, k1=100, k2, **kwarg): return p1,p2,k1,k2,kwarg
     682          pos2key2dict(1,2,k2=100,tokwarg1=100,tokwarg2=200)
     683          pos2key2dict(1,2,tokwarg1=100,tokwarg2=200, k2=100)
     684  
     685          self.assertRaises(SyntaxError, eval, "def f(*): pass")
     686          self.assertRaises(SyntaxError, eval, "def f(*,): pass")
     687          self.assertRaises(SyntaxError, eval, "def f(*, **kwds): pass")
     688  
     689          # keyword arguments after *arglist
     690          def f(*args, **kwargs):
     691              return args, kwargs
     692          self.assertEqual(f(1, x=2, *[3, 4], y=5), ((1, 3, 4),
     693                                                      {'x':2, 'y':5}))
     694          self.assertEqual(f(1, *(2,3), 4), ((1, 2, 3, 4), {}))
     695          self.assertRaises(SyntaxError, eval, "f(1, x=2, *(3,4), x=5)")
     696          self.assertEqual(f(**{'eggs':'scrambled', 'spam':'fried'}),
     697                           ((), {'eggs':'scrambled', 'spam':'fried'}))
     698          self.assertEqual(f(spam='fried', **{'eggs':'scrambled'}),
     699                           ((), {'eggs':'scrambled', 'spam':'fried'}))
     700  
     701          # Check ast errors in *args and *kwargs
     702          check_syntax_error(self, "f(*g(1=2))")
     703          check_syntax_error(self, "f(**g(1=2))")
     704  
     705          # argument annotation tests
     706          def f(x) -> list: pass
     707          self.assertEqual(f.__annotations__, {'return': list})
     708          def f(x: int): pass
     709          self.assertEqual(f.__annotations__, {'x': int})
     710          def f(x: int, /): pass
     711          self.assertEqual(f.__annotations__, {'x': int})
     712          def f(x: int = 34, /): pass
     713          self.assertEqual(f.__annotations__, {'x': int})
     714          def f(*x: str): pass
     715          self.assertEqual(f.__annotations__, {'x': str})
     716          def f(**x: float): pass
     717          self.assertEqual(f.__annotations__, {'x': float})
     718          def f(x, y: 1+2): pass
     719          self.assertEqual(f.__annotations__, {'y': 3})
     720          def f(x, y: 1+2, /): pass
     721          self.assertEqual(f.__annotations__, {'y': 3})
     722          def f(a, b: 1, c: 2, d): pass
     723          self.assertEqual(f.__annotations__, {'b': 1, 'c': 2})
     724          def f(a, b: 1, /, c: 2, d): pass
     725          self.assertEqual(f.__annotations__, {'b': 1, 'c': 2})
     726          def f(a, b: 1, c: 2, d, e: 3 = 4, f=5, *g: 6): pass
     727          self.assertEqual(f.__annotations__,
     728                           {'b': 1, 'c': 2, 'e': 3, 'g': 6})
     729          def f(a, b: 1, c: 2, d, e: 3 = 4, f=5, *g: 6, h: 7, i=8, j: 9 = 10,
     730                **k: 11) -> 12: pass
     731          self.assertEqual(f.__annotations__,
     732                           {'b': 1, 'c': 2, 'e': 3, 'g': 6, 'h': 7, 'j': 9,
     733                            'k': 11, 'return': 12})
     734          def f(a, b: 1, c: 2, d, e: 3 = 4, f: int = 5, /, *g: 6, h: 7, i=8, j: 9 = 10,
     735                **k: 11) -> 12: pass
     736          self.assertEqual(f.__annotations__,
     737                            {'b': 1, 'c': 2, 'e': 3, 'f': int, 'g': 6, 'h': 7, 'j': 9,
     738                             'k': 11, 'return': 12})
     739          # Check for issue #20625 -- annotations mangling
     740          class ESC[4;38;5;81mSpam:
     741              def f(self, *, __kw: 1):
     742                  pass
     743          class ESC[4;38;5;81mHam(ESC[4;38;5;149mSpam): pass
     744          self.assertEqual(Spam.f.__annotations__, {'_Spam__kw': 1})
     745          self.assertEqual(Ham.f.__annotations__, {'_Spam__kw': 1})
     746          # Check for SF Bug #1697248 - mixing decorators and a return annotation
     747          def null(x): return x
     748          @null
     749          def f(x) -> list: pass
     750          self.assertEqual(f.__annotations__, {'return': list})
     751  
     752          # Test expressions as decorators (PEP 614):
     753          @False or null
     754          def f(x): pass
     755          @d := null
     756          def f(x): pass
     757          @lambda f: null(f)
     758          def f(x): pass
     759          @[..., null, ...][1]
     760          def f(x): pass
     761          @null(null)(null)
     762          def f(x): pass
     763          @[null][0].__call__.__call__
     764          def f(x): pass
     765  
     766          # test closures with a variety of opargs
     767          closure = 1
     768          def f(): return closure
     769          def f(x=1): return closure
     770          def f(*, k=1): return closure
     771          def f() -> int: return closure
     772  
     773          # Check trailing commas are permitted in funcdef argument list
     774          def f(a,): pass
     775          def f(*args,): pass
     776          def f(**kwds,): pass
     777          def f(a, *args,): pass
     778          def f(a, **kwds,): pass
     779          def f(*args, b,): pass
     780          def f(*, b,): pass
     781          def f(*args, **kwds,): pass
     782          def f(a, *args, b,): pass
     783          def f(a, *, b,): pass
     784          def f(a, *args, **kwds,): pass
     785          def f(*args, b, **kwds,): pass
     786          def f(*, b, **kwds,): pass
     787          def f(a, *args, b, **kwds,): pass
     788          def f(a, *, b, **kwds,): pass
     789  
     790      def test_lambdef(self):
     791          ### lambdef: 'lambda' [varargslist] ':' test
     792          l1 = lambda : 0
     793          self.assertEqual(l1(), 0)
     794          l2 = lambda : a[d] # XXX just testing the expression
     795          l3 = lambda : [2 < x for x in [-1, 3, 0]]
     796          self.assertEqual(l3(), [0, 1, 0])
     797          l4 = lambda x = lambda y = lambda z=1 : z : y() : x()
     798          self.assertEqual(l4(), 1)
     799          l5 = lambda x, y, z=2: x + y + z
     800          self.assertEqual(l5(1, 2), 5)
     801          self.assertEqual(l5(1, 2, 3), 6)
     802          check_syntax_error(self, "lambda x: x = 2")
     803          check_syntax_error(self, "lambda (None,): None")
     804          l6 = lambda x, y, *, k=20: x+y+k
     805          self.assertEqual(l6(1,2), 1+2+20)
     806          self.assertEqual(l6(1,2,k=10), 1+2+10)
     807  
     808          # check that trailing commas are permitted
     809          l10 = lambda a,: 0
     810          l11 = lambda *args,: 0
     811          l12 = lambda **kwds,: 0
     812          l13 = lambda a, *args,: 0
     813          l14 = lambda a, **kwds,: 0
     814          l15 = lambda *args, b,: 0
     815          l16 = lambda *, b,: 0
     816          l17 = lambda *args, **kwds,: 0
     817          l18 = lambda a, *args, b,: 0
     818          l19 = lambda a, *, b,: 0
     819          l20 = lambda a, *args, **kwds,: 0
     820          l21 = lambda *args, b, **kwds,: 0
     821          l22 = lambda *, b, **kwds,: 0
     822          l23 = lambda a, *args, b, **kwds,: 0
     823          l24 = lambda a, *, b, **kwds,: 0
     824  
     825  
     826      ### stmt: simple_stmt | compound_stmt
     827      # Tested below
     828  
     829      def test_simple_stmt(self):
     830          ### simple_stmt: small_stmt (';' small_stmt)* [';']
     831          x = 1; pass; del x
     832          def foo():
     833              # verify statements that end with semi-colons
     834              x = 1; pass; del x;
     835          foo()
     836  
     837      ### small_stmt: expr_stmt | pass_stmt | del_stmt | flow_stmt | import_stmt | global_stmt | access_stmt
     838      # Tested below
     839  
     840      def test_expr_stmt(self):
     841          # (exprlist '=')* exprlist
     842          1
     843          1, 2, 3
     844          x = 1
     845          x = 1, 2, 3
     846          x = y = z = 1, 2, 3
     847          x, y, z = 1, 2, 3
     848          abc = a, b, c = x, y, z = xyz = 1, 2, (3, 4)
     849  
     850          check_syntax_error(self, "x + 1 = 1")
     851          check_syntax_error(self, "a + 1 = b + 2")
     852  
     853      # Check the heuristic for print & exec covers significant cases
     854      # As well as placing some limits on false positives
     855      def test_former_statements_refer_to_builtins(self):
     856          keywords = "print", "exec"
     857          # Cases where we want the custom error
     858          cases = [
     859              "{} foo",
     860              "{} {{1:foo}}",
     861              "if 1: {} foo",
     862              "if 1: {} {{1:foo}}",
     863              "if 1:\n    {} foo",
     864              "if 1:\n    {} {{1:foo}}",
     865          ]
     866          for keyword in keywords:
     867              custom_msg = "call to '{}'".format(keyword)
     868              for case in cases:
     869                  source = case.format(keyword)
     870                  with self.subTest(source=source):
     871                      with self.assertRaisesRegex(SyntaxError, custom_msg):
     872                          exec(source)
     873                  source = source.replace("foo", "(foo.)")
     874                  with self.subTest(source=source):
     875                      with self.assertRaisesRegex(SyntaxError, "invalid syntax"):
     876                          exec(source)
     877  
     878      def test_del_stmt(self):
     879          # 'del' exprlist
     880          abc = [1,2,3]
     881          x, y, z = abc
     882          xyz = x, y, z
     883  
     884          del abc
     885          del x, y, (z, xyz)
     886  
     887          x, y, z = "xyz"
     888          del x
     889          del y,
     890          del (z)
     891          del ()
     892  
     893          a, b, c, d, e, f, g = "abcdefg"
     894          del a, (b, c), (d, (e, f))
     895  
     896          a, b, c, d, e, f, g = "abcdefg"
     897          del a, [b, c], (d, [e, f])
     898  
     899          abcd = list("abcd")
     900          del abcd[1:2]
     901  
     902          compile("del a, (b[0].c, (d.e, f.g[1:2])), [h.i.j], ()", "<testcase>", "exec")
     903  
     904      def test_pass_stmt(self):
     905          # 'pass'
     906          pass
     907  
     908      # flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
     909      # Tested below
     910  
     911      def test_break_stmt(self):
     912          # 'break'
     913          while 1: break
     914  
     915      def test_continue_stmt(self):
     916          # 'continue'
     917          i = 1
     918          while i: i = 0; continue
     919  
     920          msg = ""
     921          while not msg:
     922              msg = "ok"
     923              try:
     924                  continue
     925                  msg = "continue failed to continue inside try"
     926              except:
     927                  msg = "continue inside try called except block"
     928          if msg != "ok":
     929              self.fail(msg)
     930  
     931          msg = ""
     932          while not msg:
     933              msg = "finally block not called"
     934              try:
     935                  continue
     936              finally:
     937                  msg = "ok"
     938          if msg != "ok":
     939              self.fail(msg)
     940  
     941      def test_break_continue_loop(self):
     942          # This test warrants an explanation. It is a test specifically for SF bugs
     943          # #463359 and #462937. The bug is that a 'break' statement executed or
     944          # exception raised inside a try/except inside a loop, *after* a continue
     945          # statement has been executed in that loop, will cause the wrong number of
     946          # arguments to be popped off the stack and the instruction pointer reset to
     947          # a very small number (usually 0.) Because of this, the following test
     948          # *must* written as a function, and the tracking vars *must* be function
     949          # arguments with default values. Otherwise, the test will loop and loop.
     950  
     951          def test_inner(extra_burning_oil = 1, count=0):
     952              big_hippo = 2
     953              while big_hippo:
     954                  count += 1
     955                  try:
     956                      if extra_burning_oil and big_hippo == 1:
     957                          extra_burning_oil -= 1
     958                          break
     959                      big_hippo -= 1
     960                      continue
     961                  except:
     962                      raise
     963              if count > 2 or big_hippo != 1:
     964                  self.fail("continue then break in try/except in loop broken!")
     965          test_inner()
     966  
     967      def test_return(self):
     968          # 'return' [testlist_star_expr]
     969          def g1(): return
     970          def g2(): return 1
     971          def g3():
     972              z = [2, 3]
     973              return 1, *z
     974  
     975          g1()
     976          x = g2()
     977          y = g3()
     978          self.assertEqual(y, (1, 2, 3), "unparenthesized star expr return")
     979          check_syntax_error(self, "class foo:return 1")
     980  
     981      def test_break_in_finally(self):
     982          count = 0
     983          while count < 2:
     984              count += 1
     985              try:
     986                  pass
     987              finally:
     988                  break
     989          self.assertEqual(count, 1)
     990  
     991          count = 0
     992          while count < 2:
     993              count += 1
     994              try:
     995                  continue
     996              finally:
     997                  break
     998          self.assertEqual(count, 1)
     999  
    1000          count = 0
    1001          while count < 2:
    1002              count += 1
    1003              try:
    1004                  1/0
    1005              finally:
    1006                  break
    1007          self.assertEqual(count, 1)
    1008  
    1009          for count in [0, 1]:
    1010              self.assertEqual(count, 0)
    1011              try:
    1012                  pass
    1013              finally:
    1014                  break
    1015          self.assertEqual(count, 0)
    1016  
    1017          for count in [0, 1]:
    1018              self.assertEqual(count, 0)
    1019              try:
    1020                  continue
    1021              finally:
    1022                  break
    1023          self.assertEqual(count, 0)
    1024  
    1025          for count in [0, 1]:
    1026              self.assertEqual(count, 0)
    1027              try:
    1028                  1/0
    1029              finally:
    1030                  break
    1031          self.assertEqual(count, 0)
    1032  
    1033      def test_continue_in_finally(self):
    1034          count = 0
    1035          while count < 2:
    1036              count += 1
    1037              try:
    1038                  pass
    1039              finally:
    1040                  continue
    1041              break
    1042          self.assertEqual(count, 2)
    1043  
    1044          count = 0
    1045          while count < 2:
    1046              count += 1
    1047              try:
    1048                  break
    1049              finally:
    1050                  continue
    1051          self.assertEqual(count, 2)
    1052  
    1053          count = 0
    1054          while count < 2:
    1055              count += 1
    1056              try:
    1057                  1/0
    1058              finally:
    1059                  continue
    1060              break
    1061          self.assertEqual(count, 2)
    1062  
    1063          for count in [0, 1]:
    1064              try:
    1065                  pass
    1066              finally:
    1067                  continue
    1068              break
    1069          self.assertEqual(count, 1)
    1070  
    1071          for count in [0, 1]:
    1072              try:
    1073                  break
    1074              finally:
    1075                  continue
    1076          self.assertEqual(count, 1)
    1077  
    1078          for count in [0, 1]:
    1079              try:
    1080                  1/0
    1081              finally:
    1082                  continue
    1083              break
    1084          self.assertEqual(count, 1)
    1085  
    1086      def test_return_in_finally(self):
    1087          def g1():
    1088              try:
    1089                  pass
    1090              finally:
    1091                  return 1
    1092          self.assertEqual(g1(), 1)
    1093  
    1094          def g2():
    1095              try:
    1096                  return 2
    1097              finally:
    1098                  return 3
    1099          self.assertEqual(g2(), 3)
    1100  
    1101          def g3():
    1102              try:
    1103                  1/0
    1104              finally:
    1105                  return 4
    1106          self.assertEqual(g3(), 4)
    1107  
    1108      def test_break_in_finally_after_return(self):
    1109          # See issue #37830
    1110          def g1(x):
    1111              for count in [0, 1]:
    1112                  count2 = 0
    1113                  while count2 < 20:
    1114                      count2 += 10
    1115                      try:
    1116                          return count + count2
    1117                      finally:
    1118                          if x:
    1119                              break
    1120              return 'end', count, count2
    1121          self.assertEqual(g1(False), 10)
    1122          self.assertEqual(g1(True), ('end', 1, 10))
    1123  
    1124          def g2(x):
    1125              for count in [0, 1]:
    1126                  for count2 in [10, 20]:
    1127                      try:
    1128                          return count + count2
    1129                      finally:
    1130                          if x:
    1131                              break
    1132              return 'end', count, count2
    1133          self.assertEqual(g2(False), 10)
    1134          self.assertEqual(g2(True), ('end', 1, 10))
    1135  
    1136      def test_continue_in_finally_after_return(self):
    1137          # See issue #37830
    1138          def g1(x):
    1139              count = 0
    1140              while count < 100:
    1141                  count += 1
    1142                  try:
    1143                      return count
    1144                  finally:
    1145                      if x:
    1146                          continue
    1147              return 'end', count
    1148          self.assertEqual(g1(False), 1)
    1149          self.assertEqual(g1(True), ('end', 100))
    1150  
    1151          def g2(x):
    1152              for count in [0, 1]:
    1153                  try:
    1154                      return count
    1155                  finally:
    1156                      if x:
    1157                          continue
    1158              return 'end', count
    1159          self.assertEqual(g2(False), 0)
    1160          self.assertEqual(g2(True), ('end', 1))
    1161  
    1162      def test_yield(self):
    1163          # Allowed as standalone statement
    1164          def g(): yield 1
    1165          def g(): yield from ()
    1166          # Allowed as RHS of assignment
    1167          def g(): x = yield 1
    1168          def g(): x = yield from ()
    1169          # Ordinary yield accepts implicit tuples
    1170          def g(): yield 1, 1
    1171          def g(): x = yield 1, 1
    1172          # 'yield from' does not
    1173          check_syntax_error(self, "def g(): yield from (), 1")
    1174          check_syntax_error(self, "def g(): x = yield from (), 1")
    1175          # Requires parentheses as subexpression
    1176          def g(): 1, (yield 1)
    1177          def g(): 1, (yield from ())
    1178          check_syntax_error(self, "def g(): 1, yield 1")
    1179          check_syntax_error(self, "def g(): 1, yield from ()")
    1180          # Requires parentheses as call argument
    1181          def g(): f((yield 1))
    1182          def g(): f((yield 1), 1)
    1183          def g(): f((yield from ()))
    1184          def g(): f((yield from ()), 1)
    1185          # Do not require parenthesis for tuple unpacking
    1186          def g(): rest = 4, 5, 6; yield 1, 2, 3, *rest
    1187          self.assertEqual(list(g()), [(1, 2, 3, 4, 5, 6)])
    1188          check_syntax_error(self, "def g(): f(yield 1)")
    1189          check_syntax_error(self, "def g(): f(yield 1, 1)")
    1190          check_syntax_error(self, "def g(): f(yield from ())")
    1191          check_syntax_error(self, "def g(): f(yield from (), 1)")
    1192          # Not allowed at top level
    1193          check_syntax_error(self, "yield")
    1194          check_syntax_error(self, "yield from")
    1195          # Not allowed at class scope
    1196          check_syntax_error(self, "class foo:yield 1")
    1197          check_syntax_error(self, "class foo:yield from ()")
    1198          # Check annotation refleak on SyntaxError
    1199          check_syntax_error(self, "def g(a:(yield)): pass")
    1200  
    1201      def test_yield_in_comprehensions(self):
    1202          # Check yield in comprehensions
    1203          def g(): [x for x in [(yield 1)]]
    1204          def g(): [x for x in [(yield from ())]]
    1205  
    1206          check = self.check_syntax_error
    1207          check("def g(): [(yield x) for x in ()]",
    1208                "'yield' inside list comprehension")
    1209          check("def g(): [x for x in () if not (yield x)]",
    1210                "'yield' inside list comprehension")
    1211          check("def g(): [y for x in () for y in [(yield x)]]",
    1212                "'yield' inside list comprehension")
    1213          check("def g(): {(yield x) for x in ()}",
    1214                "'yield' inside set comprehension")
    1215          check("def g(): {(yield x): x for x in ()}",
    1216                "'yield' inside dict comprehension")
    1217          check("def g(): {x: (yield x) for x in ()}",
    1218                "'yield' inside dict comprehension")
    1219          check("def g(): ((yield x) for x in ())",
    1220                "'yield' inside generator expression")
    1221          check("def g(): [(yield from x) for x in ()]",
    1222                "'yield' inside list comprehension")
    1223          check("class C: [(yield x) for x in ()]",
    1224                "'yield' inside list comprehension")
    1225          check("[(yield x) for x in ()]",
    1226                "'yield' inside list comprehension")
    1227  
    1228      def test_raise(self):
    1229          # 'raise' test [',' test]
    1230          try: raise RuntimeError('just testing')
    1231          except RuntimeError: pass
    1232          try: raise KeyboardInterrupt
    1233          except KeyboardInterrupt: pass
    1234  
    1235      def test_import(self):
    1236          # 'import' dotted_as_names
    1237          import sys
    1238          import time, sys
    1239          # 'from' dotted_name 'import' ('*' | '(' import_as_names ')' | import_as_names)
    1240          from time import time
    1241          from time import (time)
    1242          # not testable inside a function, but already done at top of the module
    1243          # from sys import *
    1244          from sys import path, argv
    1245          from sys import (path, argv)
    1246          from sys import (path, argv,)
    1247  
    1248      def test_global(self):
    1249          # 'global' NAME (',' NAME)*
    1250          global a
    1251          global a, b
    1252          global one, two, three, four, five, six, seven, eight, nine, ten
    1253  
    1254      def test_nonlocal(self):
    1255          # 'nonlocal' NAME (',' NAME)*
    1256          x = 0
    1257          y = 0
    1258          def f():
    1259              nonlocal x
    1260              nonlocal x, y
    1261  
    1262      def test_assert(self):
    1263          # assertTruestmt: 'assert' test [',' test]
    1264          assert 1
    1265          assert 1, 1
    1266          assert lambda x:x
    1267          assert 1, lambda x:x+1
    1268  
    1269          try:
    1270              assert True
    1271          except AssertionError as e:
    1272              self.fail("'assert True' should not have raised an AssertionError")
    1273  
    1274          try:
    1275              assert True, 'this should always pass'
    1276          except AssertionError as e:
    1277              self.fail("'assert True, msg' should not have "
    1278                        "raised an AssertionError")
    1279  
    1280      # these tests fail if python is run with -O, so check __debug__
    1281      @unittest.skipUnless(__debug__, "Won't work if __debug__ is False")
    1282      def test_assert_failures(self):
    1283          try:
    1284              assert 0, "msg"
    1285          except AssertionError as e:
    1286              self.assertEqual(e.args[0], "msg")
    1287          else:
    1288              self.fail("AssertionError not raised by assert 0")
    1289  
    1290          try:
    1291              assert False
    1292          except AssertionError as e:
    1293              self.assertEqual(len(e.args), 0)
    1294          else:
    1295              self.fail("AssertionError not raised by 'assert False'")
    1296  
    1297      def test_assert_syntax_warnings(self):
    1298          # Ensure that we warn users if they provide a non-zero length tuple as
    1299          # the assertion test.
    1300          self.check_syntax_warning('assert(x, "msg")',
    1301                                    'assertion is always true')
    1302          self.check_syntax_warning('assert(False, "msg")',
    1303                                    'assertion is always true')
    1304          self.check_syntax_warning('assert(False,)',
    1305                                    'assertion is always true')
    1306  
    1307          with self.check_no_warnings(category=SyntaxWarning):
    1308              compile('assert x, "msg"', '<testcase>', 'exec')
    1309              compile('assert False, "msg"', '<testcase>', 'exec')
    1310  
    1311      def test_assert_warning_promotes_to_syntax_error(self):
    1312          # If SyntaxWarning is configured to be an error, it actually raises a
    1313          # SyntaxError.
    1314          # https://bugs.python.org/issue35029
    1315          with warnings.catch_warnings():
    1316              warnings.simplefilter('error', SyntaxWarning)
    1317              try:
    1318                  compile('assert x, "msg" ', '<testcase>', 'exec')
    1319              except SyntaxError:
    1320                  self.fail('SyntaxError incorrectly raised for \'assert x, "msg"\'')
    1321              with self.assertRaises(SyntaxError):
    1322                  compile('assert(x, "msg")', '<testcase>', 'exec')
    1323              with self.assertRaises(SyntaxError):
    1324                  compile('assert(False, "msg")', '<testcase>', 'exec')
    1325              with self.assertRaises(SyntaxError):
    1326                  compile('assert(False,)', '<testcase>', 'exec')
    1327  
    1328  
    1329      ### compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
    1330      # Tested below
    1331  
    1332      def test_if(self):
    1333          # 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
    1334          if 1: pass
    1335          if 1: pass
    1336          else: pass
    1337          if 0: pass
    1338          elif 0: pass
    1339          if 0: pass
    1340          elif 0: pass
    1341          elif 0: pass
    1342          elif 0: pass
    1343          else: pass
    1344  
    1345      def test_while(self):
    1346          # 'while' test ':' suite ['else' ':' suite]
    1347          while 0: pass
    1348          while 0: pass
    1349          else: pass
    1350  
    1351          # Issue1920: "while 0" is optimized away,
    1352          # ensure that the "else" clause is still present.
    1353          x = 0
    1354          while 0:
    1355              x = 1
    1356          else:
    1357              x = 2
    1358          self.assertEqual(x, 2)
    1359  
    1360      def test_for(self):
    1361          # 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite]
    1362          for i in 1, 2, 3: pass
    1363          for i, j, k in (): pass
    1364          else: pass
    1365          class ESC[4;38;5;81mSquares:
    1366              def __init__(self, max):
    1367                  self.max = max
    1368                  self.sofar = []
    1369              def __len__(self): return len(self.sofar)
    1370              def __getitem__(self, i):
    1371                  if not 0 <= i < self.max: raise IndexError
    1372                  n = len(self.sofar)
    1373                  while n <= i:
    1374                      self.sofar.append(n*n)
    1375                      n = n+1
    1376                  return self.sofar[i]
    1377          n = 0
    1378          for x in Squares(10): n = n+x
    1379          if n != 285:
    1380              self.fail('for over growing sequence')
    1381  
    1382          result = []
    1383          for x, in [(1,), (2,), (3,)]:
    1384              result.append(x)
    1385          self.assertEqual(result, [1, 2, 3])
    1386  
    1387          result = []
    1388          a = b = c = [1, 2, 3]
    1389          for x in *a, *b, *c:
    1390              result.append(x)
    1391          self.assertEqual(result, 3 * a)
    1392  
    1393      def test_try(self):
    1394          ### try_stmt: 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
    1395          ###         | 'try' ':' suite 'finally' ':' suite
    1396          ### except_clause: 'except' [expr ['as' NAME]]
    1397          try:
    1398              1/0
    1399          except ZeroDivisionError:
    1400              pass
    1401          else:
    1402              pass
    1403          try: 1/0
    1404          except EOFError: pass
    1405          except TypeError as msg: pass
    1406          except: pass
    1407          else: pass
    1408          try: 1/0
    1409          except (EOFError, TypeError, ZeroDivisionError): pass
    1410          try: 1/0
    1411          except (EOFError, TypeError, ZeroDivisionError) as msg: pass
    1412          try: pass
    1413          finally: pass
    1414          with self.assertRaises(SyntaxError):
    1415              compile("try:\n    pass\nexcept Exception as a.b:\n    pass", "?", "exec")
    1416              compile("try:\n    pass\nexcept Exception as a[b]:\n    pass", "?", "exec")
    1417  
    1418      def test_try_star(self):
    1419          ### try_stmt: 'try': suite (except_star_clause : suite) + ['else' ':' suite]
    1420          ### except_star_clause: 'except*' expr ['as' NAME]
    1421          try:
    1422              1/0
    1423          except* ZeroDivisionError:
    1424              pass
    1425          else:
    1426              pass
    1427          try: 1/0
    1428          except* EOFError: pass
    1429          except* ZeroDivisionError as msg: pass
    1430          else: pass
    1431          try: 1/0
    1432          except* (EOFError, TypeError, ZeroDivisionError): pass
    1433          try: 1/0
    1434          except* (EOFError, TypeError, ZeroDivisionError) as msg: pass
    1435          try: pass
    1436          finally: pass
    1437          with self.assertRaises(SyntaxError):
    1438              compile("try:\n    pass\nexcept* Exception as a.b:\n    pass", "?", "exec")
    1439              compile("try:\n    pass\nexcept* Exception as a[b]:\n    pass", "?", "exec")
    1440              compile("try:\n    pass\nexcept*:\n    pass", "?", "exec")
    1441  
    1442      def test_suite(self):
    1443          # simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT
    1444          if 1: pass
    1445          if 1:
    1446              pass
    1447          if 1:
    1448              #
    1449              #
    1450              #
    1451              pass
    1452              pass
    1453              #
    1454              pass
    1455              #
    1456  
    1457      def test_test(self):
    1458          ### and_test ('or' and_test)*
    1459          ### and_test: not_test ('and' not_test)*
    1460          ### not_test: 'not' not_test | comparison
    1461          if not 1: pass
    1462          if 1 and 1: pass
    1463          if 1 or 1: pass
    1464          if not not not 1: pass
    1465          if not 1 and 1 and 1: pass
    1466          if 1 and 1 or 1 and 1 and 1 or not 1 and 1: pass
    1467  
    1468      def test_comparison(self):
    1469          ### comparison: expr (comp_op expr)*
    1470          ### comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'|'is' 'not'
    1471          if 1: pass
    1472          x = (1 == 1)
    1473          if 1 == 1: pass
    1474          if 1 != 1: pass
    1475          if 1 < 1: pass
    1476          if 1 > 1: pass
    1477          if 1 <= 1: pass
    1478          if 1 >= 1: pass
    1479          if x is x: pass
    1480          if x is not x: pass
    1481          if 1 in (): pass
    1482          if 1 not in (): pass
    1483          if 1 < 1 > 1 == 1 >= 1 <= 1 != 1 in 1 not in x is x is not x: pass
    1484  
    1485      def test_comparison_is_literal(self):
    1486          def check(test, msg='"is" with a literal'):
    1487              self.check_syntax_warning(test, msg)
    1488  
    1489          check('x is 1')
    1490          check('x is "thing"')
    1491          check('1 is x')
    1492          check('x is y is 1')
    1493          check('x is not 1', '"is not" with a literal')
    1494  
    1495          with warnings.catch_warnings():
    1496              warnings.simplefilter('error', SyntaxWarning)
    1497              compile('x is None', '<testcase>', 'exec')
    1498              compile('x is False', '<testcase>', 'exec')
    1499              compile('x is True', '<testcase>', 'exec')
    1500              compile('x is ...', '<testcase>', 'exec')
    1501  
    1502      def test_warn_missed_comma(self):
    1503          def check(test):
    1504              self.check_syntax_warning(test, msg)
    1505  
    1506          msg=r'is not callable; perhaps you missed a comma\?'
    1507          check('[(1, 2) (3, 4)]')
    1508          check('[(x, y) (3, 4)]')
    1509          check('[[1, 2] (3, 4)]')
    1510          check('[{1, 2} (3, 4)]')
    1511          check('[{1: 2} (3, 4)]')
    1512          check('[[i for i in range(5)] (3, 4)]')
    1513          check('[{i for i in range(5)} (3, 4)]')
    1514          check('[(i for i in range(5)) (3, 4)]')
    1515          check('[{i: i for i in range(5)} (3, 4)]')
    1516          check('[f"{x}" (3, 4)]')
    1517          check('[f"x={x}" (3, 4)]')
    1518          check('["abc" (3, 4)]')
    1519          check('[b"abc" (3, 4)]')
    1520          check('[123 (3, 4)]')
    1521          check('[12.3 (3, 4)]')
    1522          check('[12.3j (3, 4)]')
    1523          check('[None (3, 4)]')
    1524          check('[True (3, 4)]')
    1525          check('[... (3, 4)]')
    1526  
    1527          msg=r'is not subscriptable; perhaps you missed a comma\?'
    1528          check('[{1, 2} [i, j]]')
    1529          check('[{i for i in range(5)} [i, j]]')
    1530          check('[(i for i in range(5)) [i, j]]')
    1531          check('[(lambda x, y: x) [i, j]]')
    1532          check('[123 [i, j]]')
    1533          check('[12.3 [i, j]]')
    1534          check('[12.3j [i, j]]')
    1535          check('[None [i, j]]')
    1536          check('[True [i, j]]')
    1537          check('[... [i, j]]')
    1538  
    1539          msg=r'indices must be integers or slices, not tuple; perhaps you missed a comma\?'
    1540          check('[(1, 2) [i, j]]')
    1541          check('[(x, y) [i, j]]')
    1542          check('[[1, 2] [i, j]]')
    1543          check('[[i for i in range(5)] [i, j]]')
    1544          check('[f"{x}" [i, j]]')
    1545          check('[f"x={x}" [i, j]]')
    1546          check('["abc" [i, j]]')
    1547          check('[b"abc" [i, j]]')
    1548  
    1549          msg=r'indices must be integers or slices, not tuple;'
    1550          check('[[1, 2] [3, 4]]')
    1551          msg=r'indices must be integers or slices, not list;'
    1552          check('[[1, 2] [[3, 4]]]')
    1553          check('[[1, 2] [[i for i in range(5)]]]')
    1554          msg=r'indices must be integers or slices, not set;'
    1555          check('[[1, 2] [{3, 4}]]')
    1556          check('[[1, 2] [{i for i in range(5)}]]')
    1557          msg=r'indices must be integers or slices, not dict;'
    1558          check('[[1, 2] [{3: 4}]]')
    1559          check('[[1, 2] [{i: i for i in range(5)}]]')
    1560          msg=r'indices must be integers or slices, not generator;'
    1561          check('[[1, 2] [(i for i in range(5))]]')
    1562          msg=r'indices must be integers or slices, not function;'
    1563          check('[[1, 2] [(lambda x, y: x)]]')
    1564          msg=r'indices must be integers or slices, not str;'
    1565          check('[[1, 2] [f"{x}"]]')
    1566          check('[[1, 2] [f"x={x}"]]')
    1567          check('[[1, 2] ["abc"]]')
    1568          msg=r'indices must be integers or slices, not'
    1569          check('[[1, 2] [b"abc"]]')
    1570          check('[[1, 2] [12.3]]')
    1571          check('[[1, 2] [12.3j]]')
    1572          check('[[1, 2] [None]]')
    1573          check('[[1, 2] [...]]')
    1574  
    1575          with warnings.catch_warnings():
    1576              warnings.simplefilter('error', SyntaxWarning)
    1577              compile('[(lambda x, y: x) (3, 4)]', '<testcase>', 'exec')
    1578              compile('[[1, 2] [i]]', '<testcase>', 'exec')
    1579              compile('[[1, 2] [0]]', '<testcase>', 'exec')
    1580              compile('[[1, 2] [True]]', '<testcase>', 'exec')
    1581              compile('[[1, 2] [1:2]]', '<testcase>', 'exec')
    1582              compile('[{(1, 2): 3} [i, j]]', '<testcase>', 'exec')
    1583  
    1584      def test_binary_mask_ops(self):
    1585          x = 1 & 1
    1586          x = 1 ^ 1
    1587          x = 1 | 1
    1588  
    1589      def test_shift_ops(self):
    1590          x = 1 << 1
    1591          x = 1 >> 1
    1592          x = 1 << 1 >> 1
    1593  
    1594      def test_additive_ops(self):
    1595          x = 1
    1596          x = 1 + 1
    1597          x = 1 - 1 - 1
    1598          x = 1 - 1 + 1 - 1 + 1
    1599  
    1600      def test_multiplicative_ops(self):
    1601          x = 1 * 1
    1602          x = 1 / 1
    1603          x = 1 % 1
    1604          x = 1 / 1 * 1 % 1
    1605  
    1606      def test_unary_ops(self):
    1607          x = +1
    1608          x = -1
    1609          x = ~1
    1610          x = ~1 ^ 1 & 1 | 1 & 1 ^ -1
    1611          x = -1*1/1 + 1*1 - ---1*1
    1612  
    1613      def test_selectors(self):
    1614          ### trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME
    1615          ### subscript: expr | [expr] ':' [expr]
    1616  
    1617          import sys, time
    1618          c = sys.path[0]
    1619          x = time.time()
    1620          x = sys.modules['time'].time()
    1621          a = '01234'
    1622          c = a[0]
    1623          c = a[-1]
    1624          s = a[0:5]
    1625          s = a[:5]
    1626          s = a[0:]
    1627          s = a[:]
    1628          s = a[-5:]
    1629          s = a[:-1]
    1630          s = a[-4:-3]
    1631          # A rough test of SF bug 1333982.  http://python.org/sf/1333982
    1632          # The testing here is fairly incomplete.
    1633          # Test cases should include: commas with 1 and 2 colons
    1634          d = {}
    1635          d[1] = 1
    1636          d[1,] = 2
    1637          d[1,2] = 3
    1638          d[1,2,3] = 4
    1639          L = list(d)
    1640          L.sort(key=lambda x: (type(x).__name__, x))
    1641          self.assertEqual(str(L), '[1, (1,), (1, 2), (1, 2, 3)]')
    1642  
    1643      def test_atoms(self):
    1644          ### atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictsetmaker] '}' | NAME | NUMBER | STRING
    1645          ### dictsetmaker: (test ':' test (',' test ':' test)* [',']) | (test (',' test)* [','])
    1646  
    1647          x = (1)
    1648          x = (1 or 2 or 3)
    1649          x = (1 or 2 or 3, 2, 3)
    1650  
    1651          x = []
    1652          x = [1]
    1653          x = [1 or 2 or 3]
    1654          x = [1 or 2 or 3, 2, 3]
    1655          x = []
    1656  
    1657          x = {}
    1658          x = {'one': 1}
    1659          x = {'one': 1,}
    1660          x = {'one' or 'two': 1 or 2}
    1661          x = {'one': 1, 'two': 2}
    1662          x = {'one': 1, 'two': 2,}
    1663          x = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6}
    1664  
    1665          x = {'one'}
    1666          x = {'one', 1,}
    1667          x = {'one', 'two', 'three'}
    1668          x = {2, 3, 4,}
    1669  
    1670          x = x
    1671          x = 'x'
    1672          x = 123
    1673  
    1674      ### exprlist: expr (',' expr)* [',']
    1675      ### testlist: test (',' test)* [',']
    1676      # These have been exercised enough above
    1677  
    1678      def test_classdef(self):
    1679          # 'class' NAME ['(' [testlist] ')'] ':' suite
    1680          class ESC[4;38;5;81mB: pass
    1681          class ESC[4;38;5;81mB2(): pass
    1682          class ESC[4;38;5;81mC1(ESC[4;38;5;149mB): pass
    1683          class ESC[4;38;5;81mC2(ESC[4;38;5;149mB): pass
    1684          class ESC[4;38;5;81mD(ESC[4;38;5;149mC1, ESC[4;38;5;149mC2, ESC[4;38;5;149mB): pass
    1685          class ESC[4;38;5;81mC:
    1686              def meth1(self): pass
    1687              def meth2(self, arg): pass
    1688              def meth3(self, a1, a2): pass
    1689  
    1690          # decorator: '@' namedexpr_test NEWLINE
    1691          # decorators: decorator+
    1692          # decorated: decorators (classdef | funcdef)
    1693          def class_decorator(x): return x
    1694          @class_decorator
    1695          class ESC[4;38;5;81mG: pass
    1696  
    1697          # Test expressions as decorators (PEP 614):
    1698          @False or class_decorator
    1699          class ESC[4;38;5;81mH: pass
    1700          @d := class_decorator
    1701          class ESC[4;38;5;81mI: pass
    1702          @lambda c: class_decorator(c)
    1703          class ESC[4;38;5;81mJ: pass
    1704          @[..., class_decorator, ...][1]
    1705          class ESC[4;38;5;81mK: pass
    1706          @class_decorator(class_decorator)(class_decorator)
    1707          class ESC[4;38;5;81mL: pass
    1708          @[class_decorator][0].__call__.__call__
    1709          class ESC[4;38;5;81mM: pass
    1710  
    1711      def test_dictcomps(self):
    1712          # dictorsetmaker: ( (test ':' test (comp_for |
    1713          #                                   (',' test ':' test)* [','])) |
    1714          #                   (test (comp_for | (',' test)* [','])) )
    1715          nums = [1, 2, 3]
    1716          self.assertEqual({i:i+1 for i in nums}, {1: 2, 2: 3, 3: 4})
    1717  
    1718      def test_listcomps(self):
    1719          # list comprehension tests
    1720          nums = [1, 2, 3, 4, 5]
    1721          strs = ["Apple", "Banana", "Coconut"]
    1722          spcs = ["  Apple", " Banana ", "Coco  nut  "]
    1723  
    1724          self.assertEqual([s.strip() for s in spcs], ['Apple', 'Banana', 'Coco  nut'])
    1725          self.assertEqual([3 * x for x in nums], [3, 6, 9, 12, 15])
    1726          self.assertEqual([x for x in nums if x > 2], [3, 4, 5])
    1727          self.assertEqual([(i, s) for i in nums for s in strs],
    1728                           [(1, 'Apple'), (1, 'Banana'), (1, 'Coconut'),
    1729                            (2, 'Apple'), (2, 'Banana'), (2, 'Coconut'),
    1730                            (3, 'Apple'), (3, 'Banana'), (3, 'Coconut'),
    1731                            (4, 'Apple'), (4, 'Banana'), (4, 'Coconut'),
    1732                            (5, 'Apple'), (5, 'Banana'), (5, 'Coconut')])
    1733          self.assertEqual([(i, s) for i in nums for s in [f for f in strs if "n" in f]],
    1734                           [(1, 'Banana'), (1, 'Coconut'), (2, 'Banana'), (2, 'Coconut'),
    1735                            (3, 'Banana'), (3, 'Coconut'), (4, 'Banana'), (4, 'Coconut'),
    1736                            (5, 'Banana'), (5, 'Coconut')])
    1737          self.assertEqual([(lambda a:[a**i for i in range(a+1)])(j) for j in range(5)],
    1738                           [[1], [1, 1], [1, 2, 4], [1, 3, 9, 27], [1, 4, 16, 64, 256]])
    1739  
    1740          def test_in_func(l):
    1741              return [0 < x < 3 for x in l if x > 2]
    1742  
    1743          self.assertEqual(test_in_func(nums), [False, False, False])
    1744  
    1745          def test_nested_front():
    1746              self.assertEqual([[y for y in [x, x + 1]] for x in [1,3,5]],
    1747                               [[1, 2], [3, 4], [5, 6]])
    1748  
    1749          test_nested_front()
    1750  
    1751          check_syntax_error(self, "[i, s for i in nums for s in strs]")
    1752          check_syntax_error(self, "[x if y]")
    1753  
    1754          suppliers = [
    1755            (1, "Boeing"),
    1756            (2, "Ford"),
    1757            (3, "Macdonalds")
    1758          ]
    1759  
    1760          parts = [
    1761            (10, "Airliner"),
    1762            (20, "Engine"),
    1763            (30, "Cheeseburger")
    1764          ]
    1765  
    1766          suppart = [
    1767            (1, 10), (1, 20), (2, 20), (3, 30)
    1768          ]
    1769  
    1770          x = [
    1771            (sname, pname)
    1772              for (sno, sname) in suppliers
    1773                for (pno, pname) in parts
    1774                  for (sp_sno, sp_pno) in suppart
    1775                    if sno == sp_sno and pno == sp_pno
    1776          ]
    1777  
    1778          self.assertEqual(x, [('Boeing', 'Airliner'), ('Boeing', 'Engine'), ('Ford', 'Engine'),
    1779                               ('Macdonalds', 'Cheeseburger')])
    1780  
    1781      def test_genexps(self):
    1782          # generator expression tests
    1783          g = ([x for x in range(10)] for x in range(1))
    1784          self.assertEqual(next(g), [x for x in range(10)])
    1785          try:
    1786              next(g)
    1787              self.fail('should produce StopIteration exception')
    1788          except StopIteration:
    1789              pass
    1790  
    1791          a = 1
    1792          try:
    1793              g = (a for d in a)
    1794              next(g)
    1795              self.fail('should produce TypeError')
    1796          except TypeError:
    1797              pass
    1798  
    1799          self.assertEqual(list((x, y) for x in 'abcd' for y in 'abcd'), [(x, y) for x in 'abcd' for y in 'abcd'])
    1800          self.assertEqual(list((x, y) for x in 'ab' for y in 'xy'), [(x, y) for x in 'ab' for y in 'xy'])
    1801  
    1802          a = [x for x in range(10)]
    1803          b = (x for x in (y for y in a))
    1804          self.assertEqual(sum(b), sum([x for x in range(10)]))
    1805  
    1806          self.assertEqual(sum(x**2 for x in range(10)), sum([x**2 for x in range(10)]))
    1807          self.assertEqual(sum(x*x for x in range(10) if x%2), sum([x*x for x in range(10) if x%2]))
    1808          self.assertEqual(sum(x for x in (y for y in range(10))), sum([x for x in range(10)]))
    1809          self.assertEqual(sum(x for x in (y for y in (z for z in range(10)))), sum([x for x in range(10)]))
    1810          self.assertEqual(sum(x for x in [y for y in (z for z in range(10))]), sum([x for x in range(10)]))
    1811          self.assertEqual(sum(x for x in (y for y in (z for z in range(10) if True)) if True), sum([x for x in range(10)]))
    1812          self.assertEqual(sum(x for x in (y for y in (z for z in range(10) if True) if False) if True), 0)
    1813          check_syntax_error(self, "foo(x for x in range(10), 100)")
    1814          check_syntax_error(self, "foo(100, x for x in range(10))")
    1815  
    1816      def test_comprehension_specials(self):
    1817          # test for outmost iterable precomputation
    1818          x = 10; g = (i for i in range(x)); x = 5
    1819          self.assertEqual(len(list(g)), 10)
    1820  
    1821          # This should hold, since we're only precomputing outmost iterable.
    1822          x = 10; t = False; g = ((i,j) for i in range(x) if t for j in range(x))
    1823          x = 5; t = True;
    1824          self.assertEqual([(i,j) for i in range(10) for j in range(5)], list(g))
    1825  
    1826          # Grammar allows multiple adjacent 'if's in listcomps and genexps,
    1827          # even though it's silly. Make sure it works (ifelse broke this.)
    1828          self.assertEqual([ x for x in range(10) if x % 2 if x % 3 ], [1, 5, 7])
    1829          self.assertEqual(list(x for x in range(10) if x % 2 if x % 3), [1, 5, 7])
    1830  
    1831          # verify unpacking single element tuples in listcomp/genexp.
    1832          self.assertEqual([x for x, in [(4,), (5,), (6,)]], [4, 5, 6])
    1833          self.assertEqual(list(x for x, in [(7,), (8,), (9,)]), [7, 8, 9])
    1834  
    1835      def test_with_statement(self):
    1836          class ESC[4;38;5;81mmanager(ESC[4;38;5;149mobject):
    1837              def __enter__(self):
    1838                  return (1, 2)
    1839              def __exit__(self, *args):
    1840                  pass
    1841  
    1842          with manager():
    1843              pass
    1844          with manager() as x:
    1845              pass
    1846          with manager() as (x, y):
    1847              pass
    1848          with manager(), manager():
    1849              pass
    1850          with manager() as x, manager() as y:
    1851              pass
    1852          with manager() as x, manager():
    1853              pass
    1854  
    1855          with (
    1856              manager()
    1857          ):
    1858              pass
    1859  
    1860          with (
    1861              manager() as x
    1862          ):
    1863              pass
    1864  
    1865          with (
    1866              manager() as (x, y),
    1867              manager() as z,
    1868          ):
    1869              pass
    1870  
    1871          with (
    1872              manager(),
    1873              manager()
    1874          ):
    1875              pass
    1876  
    1877          with (
    1878              manager() as x,
    1879              manager() as y
    1880          ):
    1881              pass
    1882  
    1883          with (
    1884              manager() as x,
    1885              manager()
    1886          ):
    1887              pass
    1888  
    1889          with (
    1890              manager() as x,
    1891              manager() as y,
    1892              manager() as z,
    1893          ):
    1894              pass
    1895  
    1896          with (
    1897              manager() as x,
    1898              manager() as y,
    1899              manager(),
    1900          ):
    1901              pass
    1902  
    1903      def test_if_else_expr(self):
    1904          # Test ifelse expressions in various cases
    1905          def _checkeval(msg, ret):
    1906              "helper to check that evaluation of expressions is done correctly"
    1907              print(msg)
    1908              return ret
    1909  
    1910          # the next line is not allowed anymore
    1911          #self.assertEqual([ x() for x in lambda: True, lambda: False if x() ], [True])
    1912          self.assertEqual([ x() for x in (lambda: True, lambda: False) if x() ], [True])
    1913          self.assertEqual([ x(False) for x in (lambda x: False if x else True, lambda x: True if x else False) if x(False) ], [True])
    1914          self.assertEqual((5 if 1 else _checkeval("check 1", 0)), 5)
    1915          self.assertEqual((_checkeval("check 2", 0) if 0 else 5), 5)
    1916          self.assertEqual((5 and 6 if 0 else 1), 1)
    1917          self.assertEqual(((5 and 6) if 0 else 1), 1)
    1918          self.assertEqual((5 and (6 if 1 else 1)), 6)
    1919          self.assertEqual((0 or _checkeval("check 3", 2) if 0 else 3), 3)
    1920          self.assertEqual((1 or _checkeval("check 4", 2) if 1 else _checkeval("check 5", 3)), 1)
    1921          self.assertEqual((0 or 5 if 1 else _checkeval("check 6", 3)), 5)
    1922          self.assertEqual((not 5 if 1 else 1), False)
    1923          self.assertEqual((not 5 if 0 else 1), 1)
    1924          self.assertEqual((6 + 1 if 1 else 2), 7)
    1925          self.assertEqual((6 - 1 if 1 else 2), 5)
    1926          self.assertEqual((6 * 2 if 1 else 4), 12)
    1927          self.assertEqual((6 / 2 if 1 else 3), 3)
    1928          self.assertEqual((6 < 4 if 0 else 2), 2)
    1929  
    1930      def test_paren_evaluation(self):
    1931          self.assertEqual(16 // (4 // 2), 8)
    1932          self.assertEqual((16 // 4) // 2, 2)
    1933          self.assertEqual(16 // 4 // 2, 2)
    1934          x = 2
    1935          y = 3
    1936          self.assertTrue(False is (x is y))
    1937          self.assertFalse((False is x) is y)
    1938          self.assertFalse(False is x is y)
    1939  
    1940      def test_matrix_mul(self):
    1941          # This is not intended to be a comprehensive test, rather just to be few
    1942          # samples of the @ operator in test_grammar.py.
    1943          class ESC[4;38;5;81mM:
    1944              def __matmul__(self, o):
    1945                  return 4
    1946              def __imatmul__(self, o):
    1947                  self.other = o
    1948                  return self
    1949          m = M()
    1950          self.assertEqual(m @ m, 4)
    1951          m @= 42
    1952          self.assertEqual(m.other, 42)
    1953  
    1954      def test_async_await(self):
    1955          async def test():
    1956              def sum():
    1957                  pass
    1958              if 1:
    1959                  await someobj()
    1960  
    1961          self.assertEqual(test.__name__, 'test')
    1962          self.assertTrue(bool(test.__code__.co_flags & inspect.CO_COROUTINE))
    1963  
    1964          def decorator(func):
    1965              setattr(func, '_marked', True)
    1966              return func
    1967  
    1968          @decorator
    1969          async def test2():
    1970              return 22
    1971          self.assertTrue(test2._marked)
    1972          self.assertEqual(test2.__name__, 'test2')
    1973          self.assertTrue(bool(test2.__code__.co_flags & inspect.CO_COROUTINE))
    1974  
    1975      def test_async_for(self):
    1976          class ESC[4;38;5;81mDone(ESC[4;38;5;149mException): pass
    1977  
    1978          class ESC[4;38;5;81mAIter:
    1979              def __aiter__(self):
    1980                  return self
    1981              async def __anext__(self):
    1982                  raise StopAsyncIteration
    1983  
    1984          async def foo():
    1985              async for i in AIter():
    1986                  pass
    1987              async for i, j in AIter():
    1988                  pass
    1989              async for i in AIter():
    1990                  pass
    1991              else:
    1992                  pass
    1993              raise Done
    1994  
    1995          with self.assertRaises(Done):
    1996              foo().send(None)
    1997  
    1998      def test_async_with(self):
    1999          class ESC[4;38;5;81mDone(ESC[4;38;5;149mException): pass
    2000  
    2001          class ESC[4;38;5;81mmanager:
    2002              async def __aenter__(self):
    2003                  return (1, 2)
    2004              async def __aexit__(self, *exc):
    2005                  return False
    2006  
    2007          async def foo():
    2008              async with manager():
    2009                  pass
    2010              async with manager() as x:
    2011                  pass
    2012              async with manager() as (x, y):
    2013                  pass
    2014              async with manager(), manager():
    2015                  pass
    2016              async with manager() as x, manager() as y:
    2017                  pass
    2018              async with manager() as x, manager():
    2019                  pass
    2020              raise Done
    2021  
    2022          with self.assertRaises(Done):
    2023              foo().send(None)
    2024  
    2025  
    2026  if __name__ == '__main__':
    2027      unittest.main()