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