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