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