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
239var_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    # single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE
255    # XXX can't test in a script -- this rule is only used when interactive
256
257    # file_input: (NEWLINE | stmt)* ENDMARKER
258    # Being tested as this very moment this very module
259
260    # expr_input: testlist NEWLINE
261    # XXX Hard to test -- used only in calls to input()
262
263    def test_eval_input(self):
264        # testlist ENDMARKER
265        x = eval('1, 0 or 1')
266
267    def test_var_annot_basics(self):
268        # all these should be allowed
269        var1: int = 5
270        var2: [int, str]
271        my_lst = [42]
272        def one():
273            return 1
274        int.new_attr: int
275        [list][0]: type
276        my_lst[one()-1]: int = 5
277        self.assertEqual(my_lst, [5])
278
279    def test_var_annot_syntax_errors(self):
280        # parser pass
281        check_syntax_error(self, "def f: int")
282        check_syntax_error(self, "x: int: str")
283        check_syntax_error(self, "def f():\n"
284                                 "    nonlocal x: int\n")
285        # AST pass
286        check_syntax_error(self, "[x, 0]: int\n")
287        check_syntax_error(self, "f(): int\n")
288        check_syntax_error(self, "(x,): int")
289        check_syntax_error(self, "def f():\n"
290                                 "    (x, y): int = (1, 2)\n")
291        # symtable pass
292        check_syntax_error(self, "def f():\n"
293                                 "    x: int\n"
294                                 "    global x\n")
295        check_syntax_error(self, "def f():\n"
296                                 "    global x\n"
297                                 "    x: int\n")
298
299    def test_var_annot_basic_semantics(self):
300        # execution order
301        with self.assertRaises(ZeroDivisionError):
302            no_name[does_not_exist]: no_name_again = 1/0
303        with self.assertRaises(NameError):
304            no_name[does_not_exist]: 1/0 = 0
305        global var_annot_global
306
307        # function semantics
308        def f():
309            st: str = "Hello"
310            a.b: int = (1, 2)
311            return st
312        self.assertEqual(f.__annotations__, {})
313        def f_OK():
314            x: 1/0
315        f_OK()
316        def fbad():
317            x: int
318            print(x)
319        with self.assertRaises(UnboundLocalError):
320            fbad()
321        def f2bad():
322            (no_such_global): int
323            print(no_such_global)
324        try:
325            f2bad()
326        except Exception as e:
327            self.assertIs(type(e), NameError)
328
329        # class semantics
330        class C:
331            __foo: int
332            s: str = "attr"
333            z = 2
334            def __init__(self, x):
335                self.x: int = x
336        self.assertEqual(C.__annotations__, {'_C__foo': int, 's': str})
337        with self.assertRaises(NameError):
338            class CBad:
339                no_such_name_defined.attr: int = 0
340        with self.assertRaises(NameError):
341            class Cbad2(C):
342                x: int
343                x.y: list = []
344
345    def test_var_annot_metaclass_semantics(self):
346        class CMeta(type):
347            @classmethod
348            def __prepare__(metacls, name, bases, **kwds):
349                return {'__annotations__': CNS()}
350        class CC(metaclass=CMeta):
351            XX: 'ANNOT'
352        self.assertEqual(CC.__annotations__['xx'], 'ANNOT')
353
354    def test_var_annot_module_semantics(self):
355        with self.assertRaises(AttributeError):
356            print(test.__annotations__)
357        self.assertEqual(ann_module.__annotations__,
358                     {1: 2, 'x': int, 'y': str, 'f': typing.Tuple[int, int]})
359        self.assertEqual(ann_module.M.__annotations__,
360                              {'123': 123, 'o': type})
361        self.assertEqual(ann_module2.__annotations__, {})
362
363    def test_var_annot_in_module(self):
364        # check that functions fail the same way when executed
365        # outside of module where they were defined
366        from test.ann_module3 import f_bad_ann, g_bad_ann, D_bad_ann
367        with self.assertRaises(NameError):
368            f_bad_ann()
369        with self.assertRaises(NameError):
370            g_bad_ann()
371        with self.assertRaises(NameError):
372            D_bad_ann(5)
373
374    def test_var_annot_simple_exec(self):
375        gns = {}; lns= {}
376        exec("'docstring'\n"
377             "__annotations__[1] = 2\n"
378             "x: int = 5\n", gns, lns)
379        self.assertEqual(lns["__annotations__"], {1: 2, 'x': int})
380        with self.assertRaises(KeyError):
381            gns['__annotations__']
382
383    def test_var_annot_custom_maps(self):
384        # tests with custom locals() and __annotations__
385        ns = {'__annotations__': CNS()}
386        exec('X: int; Z: str = "Z"; (w): complex = 1j', ns)
387        self.assertEqual(ns['__annotations__']['x'], int)
388        self.assertEqual(ns['__annotations__']['z'], str)
389        with self.assertRaises(KeyError):
390            ns['__annotations__']['w']
391        nonloc_ns = {}
392        class CNS2:
393            def __init__(self):
394                self._dct = {}
395            def __setitem__(self, item, value):
396                nonlocal nonloc_ns
397                self._dct[item] = value
398                nonloc_ns[item] = value
399            def __getitem__(self, item):
400                return self._dct[item]
401        exec('x: int = 1', {}, CNS2())
402        self.assertEqual(nonloc_ns['__annotations__']['x'], int)
403
404    def test_var_annot_refleak(self):
405        # complex case: custom locals plus custom __annotations__
406        # this was causing refleak
407        cns = CNS()
408        nonloc_ns = {'__annotations__': cns}
409        class CNS2:
410            def __init__(self):
411                self._dct = {'__annotations__': cns}
412            def __setitem__(self, item, value):
413                nonlocal nonloc_ns
414                self._dct[item] = value
415                nonloc_ns[item] = value
416            def __getitem__(self, item):
417                return self._dct[item]
418        exec('X: str', {}, CNS2())
419        self.assertEqual(nonloc_ns['__annotations__']['x'], str)
420
421    def test_funcdef(self):
422        ### [decorators] 'def' NAME parameters ['->' test] ':' suite
423        ### decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
424        ### decorators: decorator+
425        ### parameters: '(' [typedargslist] ')'
426        ### typedargslist: ((tfpdef ['=' test] ',')*
427        ###                ('*' [tfpdef] (',' tfpdef ['=' test])* [',' '**' tfpdef] | '**' tfpdef)
428        ###                | tfpdef ['=' test] (',' tfpdef ['=' test])* [','])
429        ### tfpdef: NAME [':' test]
430        ### varargslist: ((vfpdef ['=' test] ',')*
431        ###              ('*' [vfpdef] (',' vfpdef ['=' test])*  [',' '**' vfpdef] | '**' vfpdef)
432        ###              | vfpdef ['=' test] (',' vfpdef ['=' test])* [','])
433        ### vfpdef: NAME
434        def f1(): pass
435        f1()
436        f1(*())
437        f1(*(), **{})
438        def f2(one_argument): pass
439        def f3(two, arguments): pass
440        self.assertEqual(f2.__code__.co_varnames, ('one_argument',))
441        self.assertEqual(f3.__code__.co_varnames, ('two', 'arguments'))
442        def a1(one_arg,): pass
443        def a2(two, args,): pass
444        def v0(*rest): pass
445        def v1(a, *rest): pass
446        def v2(a, b, *rest): pass
447
448        f1()
449        f2(1)
450        f2(1,)
451        f3(1, 2)
452        f3(1, 2,)
453        v0()
454        v0(1)
455        v0(1,)
456        v0(1,2)
457        v0(1,2,3,4,5,6,7,8,9,0)
458        v1(1)
459        v1(1,)
460        v1(1,2)
461        v1(1,2,3)
462        v1(1,2,3,4,5,6,7,8,9,0)
463        v2(1,2)
464        v2(1,2,3)
465        v2(1,2,3,4)
466        v2(1,2,3,4,5,6,7,8,9,0)
467
468        def d01(a=1): pass
469        d01()
470        d01(1)
471        d01(*(1,))
472        d01(*[] or [2])
473        d01(*() or (), *{} and (), **() or {})
474        d01(**{'a':2})
475        d01(**{'a':2} or {})
476        def d11(a, b=1): pass
477        d11(1)
478        d11(1, 2)
479        d11(1, **{'b':2})
480        def d21(a, b, c=1): pass
481        d21(1, 2)
482        d21(1, 2, 3)
483        d21(*(1, 2, 3))
484        d21(1, *(2, 3))
485        d21(1, 2, *(3,))
486        d21(1, 2, **{'c':3})
487        def d02(a=1, b=2): pass
488        d02()
489        d02(1)
490        d02(1, 2)
491        d02(*(1, 2))
492        d02(1, *(2,))
493        d02(1, **{'b':2})
494        d02(**{'a': 1, 'b': 2})
495        def d12(a, b=1, c=2): pass
496        d12(1)
497        d12(1, 2)
498        d12(1, 2, 3)
499        def d22(a, b, c=1, d=2): pass
500        d22(1, 2)
501        d22(1, 2, 3)
502        d22(1, 2, 3, 4)
503        def d01v(a=1, *rest): pass
504        d01v()
505        d01v(1)
506        d01v(1, 2)
507        d01v(*(1, 2, 3, 4))
508        d01v(*(1,))
509        d01v(**{'a':2})
510        def d11v(a, b=1, *rest): pass
511        d11v(1)
512        d11v(1, 2)
513        d11v(1, 2, 3)
514        def d21v(a, b, c=1, *rest): pass
515        d21v(1, 2)
516        d21v(1, 2, 3)
517        d21v(1, 2, 3, 4)
518        d21v(*(1, 2, 3, 4))
519        d21v(1, 2, **{'c': 3})
520        def d02v(a=1, b=2, *rest): pass
521        d02v()
522        d02v(1)
523        d02v(1, 2)
524        d02v(1, 2, 3)
525        d02v(1, *(2, 3, 4))
526        d02v(**{'a': 1, 'b': 2})
527        def d12v(a, b=1, c=2, *rest): pass
528        d12v(1)
529        d12v(1, 2)
530        d12v(1, 2, 3)
531        d12v(1, 2, 3, 4)
532        d12v(*(1, 2, 3, 4))
533        d12v(1, 2, *(3, 4, 5))
534        d12v(1, *(2,), **{'c': 3})
535        def d22v(a, b, c=1, d=2, *rest): pass
536        d22v(1, 2)
537        d22v(1, 2, 3)
538        d22v(1, 2, 3, 4)
539        d22v(1, 2, 3, 4, 5)
540        d22v(*(1, 2, 3, 4))
541        d22v(1, 2, *(3, 4, 5))
542        d22v(1, *(2, 3), **{'d': 4})
543
544        # keyword argument type tests
545        try:
546            str('x', **{b'foo':1 })
547        except TypeError:
548            pass
549        else:
550            self.fail('Bytes should not work as keyword argument names')
551        # keyword only argument tests
552        def pos0key1(*, key): return key
553        pos0key1(key=100)
554        def pos2key2(p1, p2, *, k1, k2=100): return p1,p2,k1,k2
555        pos2key2(1, 2, k1=100)
556        pos2key2(1, 2, k1=100, k2=200)
557        pos2key2(1, 2, k2=100, k1=200)
558        def pos2key2dict(p1, p2, *, k1=100, k2, **kwarg): return p1,p2,k1,k2,kwarg
559        pos2key2dict(1,2,k2=100,tokwarg1=100,tokwarg2=200)
560        pos2key2dict(1,2,tokwarg1=100,tokwarg2=200, k2=100)
561
562        self.assertRaises(SyntaxError, eval, "def f(*): pass")
563        self.assertRaises(SyntaxError, eval, "def f(*,): pass")
564        self.assertRaises(SyntaxError, eval, "def f(*, **kwds): pass")
565
566        # keyword arguments after *arglist
567        def f(*args, **kwargs):
568            return args, kwargs
569        self.assertEqual(f(1, x=2, *[3, 4], y=5), ((1, 3, 4),
570                                                    {'x':2, 'y':5}))
571        self.assertEqual(f(1, *(2,3), 4), ((1, 2, 3, 4), {}))
572        self.assertRaises(SyntaxError, eval, "f(1, x=2, *(3,4), x=5)")
573        self.assertEqual(f(**{'eggs':'scrambled', 'spam':'fried'}),
574                         ((), {'eggs':'scrambled', 'spam':'fried'}))
575        self.assertEqual(f(spam='fried', **{'eggs':'scrambled'}),
576                         ((), {'eggs':'scrambled', 'spam':'fried'}))
577
578        # argument annotation tests
579        def f(x) -> list: pass
580        self.assertEqual(f.__annotations__, {'return': list})
581        def f(x: int): pass
582        self.assertEqual(f.__annotations__, {'x': int})
583        def f(*x: str): pass
584        self.assertEqual(f.__annotations__, {'x': str})
585        def f(**x: float): pass
586        self.assertEqual(f.__annotations__, {'x': float})
587        def f(x, y: 1+2): pass
588        self.assertEqual(f.__annotations__, {'y': 3})
589        def f(a, b: 1, c: 2, d): pass
590        self.assertEqual(f.__annotations__, {'b': 1, 'c': 2})
591        def f(a, b: 1, c: 2, d, e: 3 = 4, f=5, *g: 6): pass
592        self.assertEqual(f.__annotations__,
593                         {'b': 1, 'c': 2, 'e': 3, 'g': 6})
594        def f(a, b: 1, c: 2, d, e: 3 = 4, f=5, *g: 6, h: 7, i=8, j: 9 = 10,
595              **k: 11) -> 12: pass
596        self.assertEqual(f.__annotations__,
597                         {'b': 1, 'c': 2, 'e': 3, 'g': 6, 'h': 7, 'j': 9,
598                          'k': 11, 'return': 12})
599        # Check for issue #20625 -- annotations mangling
600        class Spam:
601            def f(self, *, __kw: 1):
602                pass
603        class Ham(Spam): pass
604        self.assertEqual(Spam.f.__annotations__, {'_Spam__kw': 1})
605        self.assertEqual(Ham.f.__annotations__, {'_Spam__kw': 1})
606        # Check for SF Bug #1697248 - mixing decorators and a return annotation
607        def null(x): return x
608        @null
609        def f(x) -> list: pass
610        self.assertEqual(f.__annotations__, {'return': list})
611
612        # test closures with a variety of opargs
613        closure = 1
614        def f(): return closure
615        def f(x=1): return closure
616        def f(*, k=1): return closure
617        def f() -> int: return closure
618
619        # Check ast errors in *args and *kwargs
620        check_syntax_error(self, "f(*g(1=2))")
621        check_syntax_error(self, "f(**g(1=2))")
622
623        # Check trailing commas are permitted in funcdef argument list
624        def f(a,): pass
625        def f(*args,): pass
626        def f(**kwds,): pass
627        def f(a, *args,): pass
628        def f(a, **kwds,): pass
629        def f(*args, b,): pass
630        def f(*, b,): pass
631        def f(*args, **kwds,): pass
632        def f(a, *args, b,): pass
633        def f(a, *, b,): pass
634        def f(a, *args, **kwds,): pass
635        def f(*args, b, **kwds,): pass
636        def f(*, b, **kwds,): pass
637        def f(a, *args, b, **kwds,): pass
638        def f(a, *, b, **kwds,): pass
639
640    def test_lambdef(self):
641        ### lambdef: 'lambda' [varargslist] ':' test
642        l1 = lambda : 0
643        self.assertEqual(l1(), 0)
644        l2 = lambda : a[d] # XXX just testing the expression
645        l3 = lambda : [2 < x for x in [-1, 3, 0]]
646        self.assertEqual(l3(), [0, 1, 0])
647        l4 = lambda x = lambda y = lambda z=1 : z : y() : x()
648        self.assertEqual(l4(), 1)
649        l5 = lambda x, y, z=2: x + y + z
650        self.assertEqual(l5(1, 2), 5)
651        self.assertEqual(l5(1, 2, 3), 6)
652        check_syntax_error(self, "lambda x: x = 2")
653        check_syntax_error(self, "lambda (None,): None")
654        l6 = lambda x, y, *, k=20: x+y+k
655        self.assertEqual(l6(1,2), 1+2+20)
656        self.assertEqual(l6(1,2,k=10), 1+2+10)
657
658        # check that trailing commas are permitted
659        l10 = lambda a,: 0
660        l11 = lambda *args,: 0
661        l12 = lambda **kwds,: 0
662        l13 = lambda a, *args,: 0
663        l14 = lambda a, **kwds,: 0
664        l15 = lambda *args, b,: 0
665        l16 = lambda *, b,: 0
666        l17 = lambda *args, **kwds,: 0
667        l18 = lambda a, *args, b,: 0
668        l19 = lambda a, *, b,: 0
669        l20 = lambda a, *args, **kwds,: 0
670        l21 = lambda *args, b, **kwds,: 0
671        l22 = lambda *, b, **kwds,: 0
672        l23 = lambda a, *args, b, **kwds,: 0
673        l24 = lambda a, *, b, **kwds,: 0
674
675
676    ### stmt: simple_stmt | compound_stmt
677    # Tested below
678
679    def test_simple_stmt(self):
680        ### simple_stmt: small_stmt (';' small_stmt)* [';']
681        x = 1; pass; del x
682        def foo():
683            # verify statements that end with semi-colons
684            x = 1; pass; del x;
685        foo()
686
687    ### small_stmt: expr_stmt | pass_stmt | del_stmt | flow_stmt | import_stmt | global_stmt | access_stmt
688    # Tested below
689
690    def test_expr_stmt(self):
691        # (exprlist '=')* exprlist
692        1
693        1, 2, 3
694        x = 1
695        x = 1, 2, 3
696        x = y = z = 1, 2, 3
697        x, y, z = 1, 2, 3
698        abc = a, b, c = x, y, z = xyz = 1, 2, (3, 4)
699
700        check_syntax_error(self, "x + 1 = 1")
701        check_syntax_error(self, "a + 1 = b + 2")
702
703    # Check the heuristic for print & exec covers significant cases
704    # As well as placing some limits on false positives
705    def test_former_statements_refer_to_builtins(self):
706        keywords = "print", "exec"
707        # Cases where we want the custom error
708        cases = [
709            "{} foo",
710            "{} {{1:foo}}",
711            "if 1: {} foo",
712            "if 1: {} {{1:foo}}",
713            "if 1:\n    {} foo",
714            "if 1:\n    {} {{1:foo}}",
715        ]
716        for keyword in keywords:
717            custom_msg = "call to '{}'".format(keyword)
718            for case in cases:
719                source = case.format(keyword)
720                with self.subTest(source=source):
721                    with self.assertRaisesRegex(SyntaxError, custom_msg):
722                        exec(source)
723                source = source.replace("foo", "(foo.)")
724                with self.subTest(source=source):
725                    with self.assertRaisesRegex(SyntaxError, "invalid syntax"):
726                        exec(source)
727
728    def test_del_stmt(self):
729        # 'del' exprlist
730        abc = [1,2,3]
731        x, y, z = abc
732        xyz = x, y, z
733
734        del abc
735        del x, y, (z, xyz)
736
737    def test_pass_stmt(self):
738        # 'pass'
739        pass
740
741    # flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
742    # Tested below
743
744    def test_break_stmt(self):
745        # 'break'
746        while 1: break
747
748    def test_continue_stmt(self):
749        # 'continue'
750        i = 1
751        while i: i = 0; continue
752
753        msg = ""
754        while not msg:
755            msg = "ok"
756            try:
757                continue
758                msg = "continue failed to continue inside try"
759            except:
760                msg = "continue inside try called except block"
761        if msg != "ok":
762            self.fail(msg)
763
764        msg = ""
765        while not msg:
766            msg = "finally block not called"
767            try:
768                continue
769            finally:
770                msg = "ok"
771        if msg != "ok":
772            self.fail(msg)
773
774    def test_break_continue_loop(self):
775        # This test warrants an explanation. It is a test specifically for SF bugs
776        # #463359 and #462937. The bug is that a 'break' statement executed or
777        # exception raised inside a try/except inside a loop, *after* a continue
778        # statement has been executed in that loop, will cause the wrong number of
779        # arguments to be popped off the stack and the instruction pointer reset to
780        # a very small number (usually 0.) Because of this, the following test
781        # *must* written as a function, and the tracking vars *must* be function
782        # arguments with default values. Otherwise, the test will loop and loop.
783
784        def test_inner(extra_burning_oil = 1, count=0):
785            big_hippo = 2
786            while big_hippo:
787                count += 1
788                try:
789                    if extra_burning_oil and big_hippo == 1:
790                        extra_burning_oil -= 1
791                        break
792                    big_hippo -= 1
793                    continue
794                except:
795                    raise
796            if count > 2 or big_hippo != 1:
797                self.fail("continue then break in try/except in loop broken!")
798        test_inner()
799
800    def test_return(self):
801        # 'return' [testlist]
802        def g1(): return
803        def g2(): return 1
804        g1()
805        x = g2()
806        check_syntax_error(self, "class foo:return 1")
807
808    def test_yield(self):
809        # Allowed as standalone statement
810        def g(): yield 1
811        def g(): yield from ()
812        # Allowed as RHS of assignment
813        def g(): x = yield 1
814        def g(): x = yield from ()
815        # Ordinary yield accepts implicit tuples
816        def g(): yield 1, 1
817        def g(): x = yield 1, 1
818        # 'yield from' does not
819        check_syntax_error(self, "def g(): yield from (), 1")
820        check_syntax_error(self, "def g(): x = yield from (), 1")
821        # Requires parentheses as subexpression
822        def g(): 1, (yield 1)
823        def g(): 1, (yield from ())
824        check_syntax_error(self, "def g(): 1, yield 1")
825        check_syntax_error(self, "def g(): 1, yield from ()")
826        # Requires parentheses as call argument
827        def g(): f((yield 1))
828        def g(): f((yield 1), 1)
829        def g(): f((yield from ()))
830        def g(): f((yield from ()), 1)
831        check_syntax_error(self, "def g(): f(yield 1)")
832        check_syntax_error(self, "def g(): f(yield 1, 1)")
833        check_syntax_error(self, "def g(): f(yield from ())")
834        check_syntax_error(self, "def g(): f(yield from (), 1)")
835        # Not allowed at top level
836        check_syntax_error(self, "yield")
837        check_syntax_error(self, "yield from")
838        # Not allowed at class scope
839        check_syntax_error(self, "class foo:yield 1")
840        check_syntax_error(self, "class foo:yield from ()")
841        # Check annotation refleak on SyntaxError
842        check_syntax_error(self, "def g(a:(yield)): pass")
843
844    def test_raise(self):
845        # 'raise' test [',' test]
846        try: raise RuntimeError('just testing')
847        except RuntimeError: pass
848        try: raise KeyboardInterrupt
849        except KeyboardInterrupt: pass
850
851    def test_import(self):
852        # 'import' dotted_as_names
853        import sys
854        import time, sys
855        # 'from' dotted_name 'import' ('*' | '(' import_as_names ')' | import_as_names)
856        from time import time
857        from time import (time)
858        # not testable inside a function, but already done at top of the module
859        # from sys import *
860        from sys import path, argv
861        from sys import (path, argv)
862        from sys import (path, argv,)
863
864    def test_global(self):
865        # 'global' NAME (',' NAME)*
866        global a
867        global a, b
868        global one, two, three, four, five, six, seven, eight, nine, ten
869
870    def test_nonlocal(self):
871        # 'nonlocal' NAME (',' NAME)*
872        x = 0
873        y = 0
874        def f():
875            nonlocal x
876            nonlocal x, y
877
878    def test_assert(self):
879        # assertTruestmt: 'assert' test [',' test]
880        assert 1
881        assert 1, 1
882        assert lambda x:x
883        assert 1, lambda x:x+1
884
885        try:
886            assert True
887        except AssertionError as e:
888            self.fail("'assert True' should not have raised an AssertionError")
889
890        try:
891            assert True, 'this should always pass'
892        except AssertionError as e:
893            self.fail("'assert True, msg' should not have "
894                      "raised an AssertionError")
895
896    # these tests fail if python is run with -O, so check __debug__
897    @unittest.skipUnless(__debug__, "Won't work if __debug__ is False")
898    def testAssert2(self):
899        try:
900            assert 0, "msg"
901        except AssertionError as e:
902            self.assertEqual(e.args[0], "msg")
903        else:
904            self.fail("AssertionError not raised by assert 0")
905
906        try:
907            assert False
908        except AssertionError as e:
909            self.assertEqual(len(e.args), 0)
910        else:
911            self.fail("AssertionError not raised by 'assert False'")
912
913
914    ### compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
915    # Tested below
916
917    def test_if(self):
918        # 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
919        if 1: pass
920        if 1: pass
921        else: pass
922        if 0: pass
923        elif 0: pass
924        if 0: pass
925        elif 0: pass
926        elif 0: pass
927        elif 0: pass
928        else: pass
929
930    def test_while(self):
931        # 'while' test ':' suite ['else' ':' suite]
932        while 0: pass
933        while 0: pass
934        else: pass
935
936        # Issue1920: "while 0" is optimized away,
937        # ensure that the "else" clause is still present.
938        x = 0
939        while 0:
940            x = 1
941        else:
942            x = 2
943        self.assertEqual(x, 2)
944
945    def test_for(self):
946        # 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite]
947        for i in 1, 2, 3: pass
948        for i, j, k in (): pass
949        else: pass
950        class Squares:
951            def __init__(self, max):
952                self.max = max
953                self.sofar = []
954            def __len__(self): return len(self.sofar)
955            def __getitem__(self, i):
956                if not 0 <= i < self.max: raise IndexError
957                n = len(self.sofar)
958                while n <= i:
959                    self.sofar.append(n*n)
960                    n = n+1
961                return self.sofar[i]
962        n = 0
963        for x in Squares(10): n = n+x
964        if n != 285:
965            self.fail('for over growing sequence')
966
967        result = []
968        for x, in [(1,), (2,), (3,)]:
969            result.append(x)
970        self.assertEqual(result, [1, 2, 3])
971
972    def test_try(self):
973        ### try_stmt: 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
974        ###         | 'try' ':' suite 'finally' ':' suite
975        ### except_clause: 'except' [expr ['as' expr]]
976        try:
977            1/0
978        except ZeroDivisionError:
979            pass
980        else:
981            pass
982        try: 1/0
983        except EOFError: pass
984        except TypeError as msg: pass
985        except RuntimeError as msg: pass
986        except: pass
987        else: pass
988        try: 1/0
989        except (EOFError, TypeError, ZeroDivisionError): pass
990        try: 1/0
991        except (EOFError, TypeError, ZeroDivisionError) as msg: pass
992        try: pass
993        finally: pass
994
995    def test_suite(self):
996        # simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT
997        if 1: pass
998        if 1:
999            pass
1000        if 1:
1001            #
1002            #
1003            #
1004            pass
1005            pass
1006            #
1007            pass
1008            #
1009
1010    def test_test(self):
1011        ### and_test ('or' and_test)*
1012        ### and_test: not_test ('and' not_test)*
1013        ### not_test: 'not' not_test | comparison
1014        if not 1: pass
1015        if 1 and 1: pass
1016        if 1 or 1: pass
1017        if not not not 1: pass
1018        if not 1 and 1 and 1: pass
1019        if 1 and 1 or 1 and 1 and 1 or not 1 and 1: pass
1020
1021    def test_comparison(self):
1022        ### comparison: expr (comp_op expr)*
1023        ### comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'|'is' 'not'
1024        if 1: pass
1025        x = (1 == 1)
1026        if 1 == 1: pass
1027        if 1 != 1: pass
1028        if 1 < 1: pass
1029        if 1 > 1: pass
1030        if 1 <= 1: pass
1031        if 1 >= 1: pass
1032        if 1 is 1: pass
1033        if 1 is not 1: pass
1034        if 1 in (): pass
1035        if 1 not in (): pass
1036        if 1 < 1 > 1 == 1 >= 1 <= 1 != 1 in 1 not in 1 is 1 is not 1: pass
1037
1038    def test_binary_mask_ops(self):
1039        x = 1 & 1
1040        x = 1 ^ 1
1041        x = 1 | 1
1042
1043    def test_shift_ops(self):
1044        x = 1 << 1
1045        x = 1 >> 1
1046        x = 1 << 1 >> 1
1047
1048    def test_additive_ops(self):
1049        x = 1
1050        x = 1 + 1
1051        x = 1 - 1 - 1
1052        x = 1 - 1 + 1 - 1 + 1
1053
1054    def test_multiplicative_ops(self):
1055        x = 1 * 1
1056        x = 1 / 1
1057        x = 1 % 1
1058        x = 1 / 1 * 1 % 1
1059
1060    def test_unary_ops(self):
1061        x = +1
1062        x = -1
1063        x = ~1
1064        x = ~1 ^ 1 & 1 | 1 & 1 ^ -1
1065        x = -1*1/1 + 1*1 - ---1*1
1066
1067    def test_selectors(self):
1068        ### trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME
1069        ### subscript: expr | [expr] ':' [expr]
1070
1071        import sys, time
1072        c = sys.path[0]
1073        x = time.time()
1074        x = sys.modules['time'].time()
1075        a = '01234'
1076        c = a[0]
1077        c = a[-1]
1078        s = a[0:5]
1079        s = a[:5]
1080        s = a[0:]
1081        s = a[:]
1082        s = a[-5:]
1083        s = a[:-1]
1084        s = a[-4:-3]
1085        # A rough test of SF bug 1333982.  http://python.org/sf/1333982
1086        # The testing here is fairly incomplete.
1087        # Test cases should include: commas with 1 and 2 colons
1088        d = {}
1089        d[1] = 1
1090        d[1,] = 2
1091        d[1,2] = 3
1092        d[1,2,3] = 4
1093        L = list(d)
1094        L.sort(key=lambda x: x if isinstance(x, tuple) else ())
1095        self.assertEqual(str(L), '[1, (1,), (1, 2), (1, 2, 3)]')
1096
1097    def test_atoms(self):
1098        ### atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictsetmaker] '}' | NAME | NUMBER | STRING
1099        ### dictsetmaker: (test ':' test (',' test ':' test)* [',']) | (test (',' test)* [','])
1100
1101        x = (1)
1102        x = (1 or 2 or 3)
1103        x = (1 or 2 or 3, 2, 3)
1104
1105        x = []
1106        x = [1]
1107        x = [1 or 2 or 3]
1108        x = [1 or 2 or 3, 2, 3]
1109        x = []
1110
1111        x = {}
1112        x = {'one': 1}
1113        x = {'one': 1,}
1114        x = {'one' or 'two': 1 or 2}
1115        x = {'one': 1, 'two': 2}
1116        x = {'one': 1, 'two': 2,}
1117        x = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6}
1118
1119        x = {'one'}
1120        x = {'one', 1,}
1121        x = {'one', 'two', 'three'}
1122        x = {2, 3, 4,}
1123
1124        x = x
1125        x = 'x'
1126        x = 123
1127
1128    ### exprlist: expr (',' expr)* [',']
1129    ### testlist: test (',' test)* [',']
1130    # These have been exercised enough above
1131
1132    def test_classdef(self):
1133        # 'class' NAME ['(' [testlist] ')'] ':' suite
1134        class B: pass
1135        class B2(): pass
1136        class C1(B): pass
1137        class C2(B): pass
1138        class D(C1, C2, B): pass
1139        class C:
1140            def meth1(self): pass
1141            def meth2(self, arg): pass
1142            def meth3(self, a1, a2): pass
1143
1144        # decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
1145        # decorators: decorator+
1146        # decorated: decorators (classdef | funcdef)
1147        def class_decorator(x): return x
1148        @class_decorator
1149        class G: pass
1150
1151    def test_dictcomps(self):
1152        # dictorsetmaker: ( (test ':' test (comp_for |
1153        #                                   (',' test ':' test)* [','])) |
1154        #                   (test (comp_for | (',' test)* [','])) )
1155        nums = [1, 2, 3]
1156        self.assertEqual({i:i+1 for i in nums}, {1: 2, 2: 3, 3: 4})
1157
1158    def test_listcomps(self):
1159        # list comprehension tests
1160        nums = [1, 2, 3, 4, 5]
1161        strs = ["Apple", "Banana", "Coconut"]
1162        spcs = ["  Apple", " Banana ", "Coco  nut  "]
1163
1164        self.assertEqual([s.strip() for s in spcs], ['Apple', 'Banana', 'Coco  nut'])
1165        self.assertEqual([3 * x for x in nums], [3, 6, 9, 12, 15])
1166        self.assertEqual([x for x in nums if x > 2], [3, 4, 5])
1167        self.assertEqual([(i, s) for i in nums for s in strs],
1168                         [(1, 'Apple'), (1, 'Banana'), (1, 'Coconut'),
1169                          (2, 'Apple'), (2, 'Banana'), (2, 'Coconut'),
1170                          (3, 'Apple'), (3, 'Banana'), (3, 'Coconut'),
1171                          (4, 'Apple'), (4, 'Banana'), (4, 'Coconut'),
1172                          (5, 'Apple'), (5, 'Banana'), (5, 'Coconut')])
1173        self.assertEqual([(i, s) for i in nums for s in [f for f in strs if "n" in f]],
1174                         [(1, 'Banana'), (1, 'Coconut'), (2, 'Banana'), (2, 'Coconut'),
1175                          (3, 'Banana'), (3, 'Coconut'), (4, 'Banana'), (4, 'Coconut'),
1176                          (5, 'Banana'), (5, 'Coconut')])
1177        self.assertEqual([(lambda a:[a**i for i in range(a+1)])(j) for j in range(5)],
1178                         [[1], [1, 1], [1, 2, 4], [1, 3, 9, 27], [1, 4, 16, 64, 256]])
1179
1180        def test_in_func(l):
1181            return [0 < x < 3 for x in l if x > 2]
1182
1183        self.assertEqual(test_in_func(nums), [False, False, False])
1184
1185        def test_nested_front():
1186            self.assertEqual([[y for y in [x, x + 1]] for x in [1,3,5]],
1187                             [[1, 2], [3, 4], [5, 6]])
1188
1189        test_nested_front()
1190
1191        check_syntax_error(self, "[i, s for i in nums for s in strs]")
1192        check_syntax_error(self, "[x if y]")
1193
1194        suppliers = [
1195          (1, "Boeing"),
1196          (2, "Ford"),
1197          (3, "Macdonalds")
1198        ]
1199
1200        parts = [
1201          (10, "Airliner"),
1202          (20, "Engine"),
1203          (30, "Cheeseburger")
1204        ]
1205
1206        suppart = [
1207          (1, 10), (1, 20), (2, 20), (3, 30)
1208        ]
1209
1210        x = [
1211          (sname, pname)
1212            for (sno, sname) in suppliers
1213              for (pno, pname) in parts
1214                for (sp_sno, sp_pno) in suppart
1215                  if sno == sp_sno and pno == sp_pno
1216        ]
1217
1218        self.assertEqual(x, [('Boeing', 'Airliner'), ('Boeing', 'Engine'), ('Ford', 'Engine'),
1219                             ('Macdonalds', 'Cheeseburger')])
1220
1221    def test_genexps(self):
1222        # generator expression tests
1223        g = ([x for x in range(10)] for x in range(1))
1224        self.assertEqual(next(g), [x for x in range(10)])
1225        try:
1226            next(g)
1227            self.fail('should produce StopIteration exception')
1228        except StopIteration:
1229            pass
1230
1231        a = 1
1232        try:
1233            g = (a for d in a)
1234            next(g)
1235            self.fail('should produce TypeError')
1236        except TypeError:
1237            pass
1238
1239        self.assertEqual(list((x, y) for x in 'abcd' for y in 'abcd'), [(x, y) for x in 'abcd' for y in 'abcd'])
1240        self.assertEqual(list((x, y) for x in 'ab' for y in 'xy'), [(x, y) for x in 'ab' for y in 'xy'])
1241
1242        a = [x for x in range(10)]
1243        b = (x for x in (y for y in a))
1244        self.assertEqual(sum(b), sum([x for x in range(10)]))
1245
1246        self.assertEqual(sum(x**2 for x in range(10)), sum([x**2 for x in range(10)]))
1247        self.assertEqual(sum(x*x for x in range(10) if x%2), sum([x*x for x in range(10) if x%2]))
1248        self.assertEqual(sum(x for x in (y for y in range(10))), sum([x for x in range(10)]))
1249        self.assertEqual(sum(x for x in (y for y in (z for z in range(10)))), sum([x for x in range(10)]))
1250        self.assertEqual(sum(x for x in [y for y in (z for z in range(10))]), sum([x for x in range(10)]))
1251        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)]))
1252        self.assertEqual(sum(x for x in (y for y in (z for z in range(10) if True) if False) if True), 0)
1253        check_syntax_error(self, "foo(x for x in range(10), 100)")
1254        check_syntax_error(self, "foo(100, x for x in range(10))")
1255
1256    def test_comprehension_specials(self):
1257        # test for outmost iterable precomputation
1258        x = 10; g = (i for i in range(x)); x = 5
1259        self.assertEqual(len(list(g)), 10)
1260
1261        # This should hold, since we're only precomputing outmost iterable.
1262        x = 10; t = False; g = ((i,j) for i in range(x) if t for j in range(x))
1263        x = 5; t = True;
1264        self.assertEqual([(i,j) for i in range(10) for j in range(5)], list(g))
1265
1266        # Grammar allows multiple adjacent 'if's in listcomps and genexps,
1267        # even though it's silly. Make sure it works (ifelse broke this.)
1268        self.assertEqual([ x for x in range(10) if x % 2 if x % 3 ], [1, 5, 7])
1269        self.assertEqual(list(x for x in range(10) if x % 2 if x % 3), [1, 5, 7])
1270
1271        # verify unpacking single element tuples in listcomp/genexp.
1272        self.assertEqual([x for x, in [(4,), (5,), (6,)]], [4, 5, 6])
1273        self.assertEqual(list(x for x, in [(7,), (8,), (9,)]), [7, 8, 9])
1274
1275    def test_with_statement(self):
1276        class manager(object):
1277            def __enter__(self):
1278                return (1, 2)
1279            def __exit__(self, *args):
1280                pass
1281
1282        with manager():
1283            pass
1284        with manager() as x:
1285            pass
1286        with manager() as (x, y):
1287            pass
1288        with manager(), manager():
1289            pass
1290        with manager() as x, manager() as y:
1291            pass
1292        with manager() as x, manager():
1293            pass
1294
1295    def test_if_else_expr(self):
1296        # Test ifelse expressions in various cases
1297        def _checkeval(msg, ret):
1298            "helper to check that evaluation of expressions is done correctly"
1299            print(msg)
1300            return ret
1301
1302        # the next line is not allowed anymore
1303        #self.assertEqual([ x() for x in lambda: True, lambda: False if x() ], [True])
1304        self.assertEqual([ x() for x in (lambda: True, lambda: False) if x() ], [True])
1305        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])
1306        self.assertEqual((5 if 1 else _checkeval("check 1", 0)), 5)
1307        self.assertEqual((_checkeval("check 2", 0) if 0 else 5), 5)
1308        self.assertEqual((5 and 6 if 0 else 1), 1)
1309        self.assertEqual(((5 and 6) if 0 else 1), 1)
1310        self.assertEqual((5 and (6 if 1 else 1)), 6)
1311        self.assertEqual((0 or _checkeval("check 3", 2) if 0 else 3), 3)
1312        self.assertEqual((1 or _checkeval("check 4", 2) if 1 else _checkeval("check 5", 3)), 1)
1313        self.assertEqual((0 or 5 if 1 else _checkeval("check 6", 3)), 5)
1314        self.assertEqual((not 5 if 1 else 1), False)
1315        self.assertEqual((not 5 if 0 else 1), 1)
1316        self.assertEqual((6 + 1 if 1 else 2), 7)
1317        self.assertEqual((6 - 1 if 1 else 2), 5)
1318        self.assertEqual((6 * 2 if 1 else 4), 12)
1319        self.assertEqual((6 / 2 if 1 else 3), 3)
1320        self.assertEqual((6 < 4 if 0 else 2), 2)
1321
1322    def test_paren_evaluation(self):
1323        self.assertEqual(16 // (4 // 2), 8)
1324        self.assertEqual((16 // 4) // 2, 2)
1325        self.assertEqual(16 // 4 // 2, 2)
1326        self.assertTrue(False is (2 is 3))
1327        self.assertFalse((False is 2) is 3)
1328        self.assertFalse(False is 2 is 3)
1329
1330    def test_matrix_mul(self):
1331        # This is not intended to be a comprehensive test, rather just to be few
1332        # samples of the @ operator in test_grammar.py.
1333        class M:
1334            def __matmul__(self, o):
1335                return 4
1336            def __imatmul__(self, o):
1337                self.other = o
1338                return self
1339        m = M()
1340        self.assertEqual(m @ m, 4)
1341        m @= 42
1342        self.assertEqual(m.other, 42)
1343
1344    def test_async_await(self):
1345        async def test():
1346            def sum():
1347                pass
1348            if 1:
1349                await someobj()
1350
1351        self.assertEqual(test.__name__, 'test')
1352        self.assertTrue(bool(test.__code__.co_flags & inspect.CO_COROUTINE))
1353
1354        def decorator(func):
1355            setattr(func, '_marked', True)
1356            return func
1357
1358        @decorator
1359        async def test2():
1360            return 22
1361        self.assertTrue(test2._marked)
1362        self.assertEqual(test2.__name__, 'test2')
1363        self.assertTrue(bool(test2.__code__.co_flags & inspect.CO_COROUTINE))
1364
1365    def test_async_for(self):
1366        class Done(Exception): pass
1367
1368        class AIter:
1369            def __aiter__(self):
1370                return self
1371            async def __anext__(self):
1372                raise StopAsyncIteration
1373
1374        async def foo():
1375            async for i in AIter():
1376                pass
1377            async for i, j in AIter():
1378                pass
1379            async for i in AIter():
1380                pass
1381            else:
1382                pass
1383            raise Done
1384
1385        with self.assertRaises(Done):
1386            foo().send(None)
1387
1388    def test_async_with(self):
1389        class Done(Exception): pass
1390
1391        class manager:
1392            async def __aenter__(self):
1393                return (1, 2)
1394            async def __aexit__(self, *exc):
1395                return False
1396
1397        async def foo():
1398            async with manager():
1399                pass
1400            async with manager() as x:
1401                pass
1402            async with manager() as (x, y):
1403                pass
1404            async with manager(), manager():
1405                pass
1406            async with manager() as x, manager() as y:
1407                pass
1408            async with manager() as x, manager():
1409                pass
1410            raise Done
1411
1412        with self.assertRaises(Done):
1413            foo().send(None)
1414
1415
1416if __name__ == '__main__':
1417    unittest.main()
1418