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