• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Python test set -- part 1, grammar.
2# This just tests whether the parser accepts them all.
3
4from test.support import check_syntax_error
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        # Check ast errors in *args and *kwargs
579        check_syntax_error(self, "f(*g(1=2))")
580        check_syntax_error(self, "f(**g(1=2))")
581
582        # argument annotation tests
583        def f(x) -> list: pass
584        self.assertEqual(f.__annotations__, {'return': list})
585        def f(x: int): pass
586        self.assertEqual(f.__annotations__, {'x': int})
587        def f(*x: str): pass
588        self.assertEqual(f.__annotations__, {'x': str})
589        def f(**x: float): pass
590        self.assertEqual(f.__annotations__, {'x': float})
591        def f(x, y: 1+2): pass
592        self.assertEqual(f.__annotations__, {'y': 3})
593        def f(a, b: 1, c: 2, d): pass
594        self.assertEqual(f.__annotations__, {'b': 1, 'c': 2})
595        def f(a, b: 1, c: 2, d, e: 3 = 4, f=5, *g: 6): pass
596        self.assertEqual(f.__annotations__,
597                         {'b': 1, 'c': 2, 'e': 3, 'g': 6})
598        def f(a, b: 1, c: 2, d, e: 3 = 4, f=5, *g: 6, h: 7, i=8, j: 9 = 10,
599              **k: 11) -> 12: pass
600        self.assertEqual(f.__annotations__,
601                         {'b': 1, 'c': 2, 'e': 3, 'g': 6, 'h': 7, 'j': 9,
602                          'k': 11, 'return': 12})
603        # Check for issue #20625 -- annotations mangling
604        class Spam:
605            def f(self, *, __kw: 1):
606                pass
607        class Ham(Spam): pass
608        self.assertEqual(Spam.f.__annotations__, {'_Spam__kw': 1})
609        self.assertEqual(Ham.f.__annotations__, {'_Spam__kw': 1})
610        # Check for SF Bug #1697248 - mixing decorators and a return annotation
611        def null(x): return x
612        @null
613        def f(x) -> list: pass
614        self.assertEqual(f.__annotations__, {'return': list})
615
616        # test closures with a variety of opargs
617        closure = 1
618        def f(): return closure
619        def f(x=1): return closure
620        def f(*, k=1): return closure
621        def f() -> int: return closure
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_break_in_finally(self):
809        count = 0
810        while count < 2:
811            count += 1
812            try:
813                pass
814            finally:
815                break
816        self.assertEqual(count, 1)
817
818        count = 0
819        while count < 2:
820            count += 1
821            try:
822                continue
823            finally:
824                break
825        self.assertEqual(count, 1)
826
827        count = 0
828        while count < 2:
829            count += 1
830            try:
831                1/0
832            finally:
833                break
834        self.assertEqual(count, 1)
835
836        for count in [0, 1]:
837            self.assertEqual(count, 0)
838            try:
839                pass
840            finally:
841                break
842        self.assertEqual(count, 0)
843
844        for count in [0, 1]:
845            self.assertEqual(count, 0)
846            try:
847                continue
848            finally:
849                break
850        self.assertEqual(count, 0)
851
852        for count in [0, 1]:
853            self.assertEqual(count, 0)
854            try:
855                1/0
856            finally:
857                break
858        self.assertEqual(count, 0)
859
860    def test_return_in_finally(self):
861        def g1():
862            try:
863                pass
864            finally:
865                return 1
866        self.assertEqual(g1(), 1)
867
868        def g2():
869            try:
870                return 2
871            finally:
872                return 3
873        self.assertEqual(g2(), 3)
874
875        def g3():
876            try:
877                1/0
878            finally:
879                return 4
880        self.assertEqual(g3(), 4)
881
882    def test_yield(self):
883        # Allowed as standalone statement
884        def g(): yield 1
885        def g(): yield from ()
886        # Allowed as RHS of assignment
887        def g(): x = yield 1
888        def g(): x = yield from ()
889        # Ordinary yield accepts implicit tuples
890        def g(): yield 1, 1
891        def g(): x = yield 1, 1
892        # 'yield from' does not
893        check_syntax_error(self, "def g(): yield from (), 1")
894        check_syntax_error(self, "def g(): x = yield from (), 1")
895        # Requires parentheses as subexpression
896        def g(): 1, (yield 1)
897        def g(): 1, (yield from ())
898        check_syntax_error(self, "def g(): 1, yield 1")
899        check_syntax_error(self, "def g(): 1, yield from ()")
900        # Requires parentheses as call argument
901        def g(): f((yield 1))
902        def g(): f((yield 1), 1)
903        def g(): f((yield from ()))
904        def g(): f((yield from ()), 1)
905        check_syntax_error(self, "def g(): f(yield 1)")
906        check_syntax_error(self, "def g(): f(yield 1, 1)")
907        check_syntax_error(self, "def g(): f(yield from ())")
908        check_syntax_error(self, "def g(): f(yield from (), 1)")
909        # Not allowed at top level
910        check_syntax_error(self, "yield")
911        check_syntax_error(self, "yield from")
912        # Not allowed at class scope
913        check_syntax_error(self, "class foo:yield 1")
914        check_syntax_error(self, "class foo:yield from ()")
915        # Check annotation refleak on SyntaxError
916        check_syntax_error(self, "def g(a:(yield)): pass")
917
918    def test_yield_in_comprehensions(self):
919        # Check yield in comprehensions
920        def g(): [x for x in [(yield 1)]]
921        def g(): [x for x in [(yield from ())]]
922
923        def check(code, warntext):
924            with self.assertWarnsRegex(DeprecationWarning, warntext):
925                compile(code, '<test string>', 'exec')
926            import warnings
927            with warnings.catch_warnings():
928                warnings.filterwarnings('error', category=DeprecationWarning)
929                with self.assertRaisesRegex(SyntaxError, warntext):
930                    compile(code, '<test string>', 'exec')
931
932        check("def g(): [(yield x) for x in ()]",
933              "'yield' inside list comprehension")
934        check("def g(): [x for x in () if not (yield x)]",
935              "'yield' inside list comprehension")
936        check("def g(): [y for x in () for y in [(yield x)]]",
937              "'yield' inside list comprehension")
938        check("def g(): {(yield x) for x in ()}",
939              "'yield' inside set comprehension")
940        check("def g(): {(yield x): x for x in ()}",
941              "'yield' inside dict comprehension")
942        check("def g(): {x: (yield x) for x in ()}",
943              "'yield' inside dict comprehension")
944        check("def g(): ((yield x) for x in ())",
945              "'yield' inside generator expression")
946        check("def g(): [(yield from x) for x in ()]",
947              "'yield' inside list comprehension")
948        check("class C: [(yield x) for x in ()]",
949              "'yield' inside list comprehension")
950        check("[(yield x) for x in ()]",
951              "'yield' inside list comprehension")
952
953    def test_raise(self):
954        # 'raise' test [',' test]
955        try: raise RuntimeError('just testing')
956        except RuntimeError: pass
957        try: raise KeyboardInterrupt
958        except KeyboardInterrupt: pass
959
960    def test_import(self):
961        # 'import' dotted_as_names
962        import sys
963        import time, sys
964        # 'from' dotted_name 'import' ('*' | '(' import_as_names ')' | import_as_names)
965        from time import time
966        from time import (time)
967        # not testable inside a function, but already done at top of the module
968        # from sys import *
969        from sys import path, argv
970        from sys import (path, argv)
971        from sys import (path, argv,)
972
973    def test_global(self):
974        # 'global' NAME (',' NAME)*
975        global a
976        global a, b
977        global one, two, three, four, five, six, seven, eight, nine, ten
978
979    def test_nonlocal(self):
980        # 'nonlocal' NAME (',' NAME)*
981        x = 0
982        y = 0
983        def f():
984            nonlocal x
985            nonlocal x, y
986
987    def test_assert(self):
988        # assertTruestmt: 'assert' test [',' test]
989        assert 1
990        assert 1, 1
991        assert lambda x:x
992        assert 1, lambda x:x+1
993
994        try:
995            assert True
996        except AssertionError as e:
997            self.fail("'assert True' should not have raised an AssertionError")
998
999        try:
1000            assert True, 'this should always pass'
1001        except AssertionError as e:
1002            self.fail("'assert True, msg' should not have "
1003                      "raised an AssertionError")
1004
1005    # these tests fail if python is run with -O, so check __debug__
1006    @unittest.skipUnless(__debug__, "Won't work if __debug__ is False")
1007    def testAssert2(self):
1008        try:
1009            assert 0, "msg"
1010        except AssertionError as e:
1011            self.assertEqual(e.args[0], "msg")
1012        else:
1013            self.fail("AssertionError not raised by assert 0")
1014
1015        try:
1016            assert False
1017        except AssertionError as e:
1018            self.assertEqual(len(e.args), 0)
1019        else:
1020            self.fail("AssertionError not raised by 'assert False'")
1021
1022
1023    ### compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
1024    # Tested below
1025
1026    def test_if(self):
1027        # 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
1028        if 1: pass
1029        if 1: pass
1030        else: pass
1031        if 0: pass
1032        elif 0: pass
1033        if 0: pass
1034        elif 0: pass
1035        elif 0: pass
1036        elif 0: pass
1037        else: pass
1038
1039    def test_while(self):
1040        # 'while' test ':' suite ['else' ':' suite]
1041        while 0: pass
1042        while 0: pass
1043        else: pass
1044
1045        # Issue1920: "while 0" is optimized away,
1046        # ensure that the "else" clause is still present.
1047        x = 0
1048        while 0:
1049            x = 1
1050        else:
1051            x = 2
1052        self.assertEqual(x, 2)
1053
1054    def test_for(self):
1055        # 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite]
1056        for i in 1, 2, 3: pass
1057        for i, j, k in (): pass
1058        else: pass
1059        class Squares:
1060            def __init__(self, max):
1061                self.max = max
1062                self.sofar = []
1063            def __len__(self): return len(self.sofar)
1064            def __getitem__(self, i):
1065                if not 0 <= i < self.max: raise IndexError
1066                n = len(self.sofar)
1067                while n <= i:
1068                    self.sofar.append(n*n)
1069                    n = n+1
1070                return self.sofar[i]
1071        n = 0
1072        for x in Squares(10): n = n+x
1073        if n != 285:
1074            self.fail('for over growing sequence')
1075
1076        result = []
1077        for x, in [(1,), (2,), (3,)]:
1078            result.append(x)
1079        self.assertEqual(result, [1, 2, 3])
1080
1081    def test_try(self):
1082        ### try_stmt: 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
1083        ###         | 'try' ':' suite 'finally' ':' suite
1084        ### except_clause: 'except' [expr ['as' expr]]
1085        try:
1086            1/0
1087        except ZeroDivisionError:
1088            pass
1089        else:
1090            pass
1091        try: 1/0
1092        except EOFError: pass
1093        except TypeError as msg: pass
1094        except: pass
1095        else: pass
1096        try: 1/0
1097        except (EOFError, TypeError, ZeroDivisionError): pass
1098        try: 1/0
1099        except (EOFError, TypeError, ZeroDivisionError) as msg: pass
1100        try: pass
1101        finally: pass
1102
1103    def test_suite(self):
1104        # simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT
1105        if 1: pass
1106        if 1:
1107            pass
1108        if 1:
1109            #
1110            #
1111            #
1112            pass
1113            pass
1114            #
1115            pass
1116            #
1117
1118    def test_test(self):
1119        ### and_test ('or' and_test)*
1120        ### and_test: not_test ('and' not_test)*
1121        ### not_test: 'not' not_test | comparison
1122        if not 1: pass
1123        if 1 and 1: pass
1124        if 1 or 1: pass
1125        if not not not 1: pass
1126        if not 1 and 1 and 1: pass
1127        if 1 and 1 or 1 and 1 and 1 or not 1 and 1: pass
1128
1129    def test_comparison(self):
1130        ### comparison: expr (comp_op expr)*
1131        ### comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'|'is' 'not'
1132        if 1: pass
1133        x = (1 == 1)
1134        if 1 == 1: pass
1135        if 1 != 1: pass
1136        if 1 < 1: pass
1137        if 1 > 1: pass
1138        if 1 <= 1: pass
1139        if 1 >= 1: pass
1140        if 1 is 1: pass
1141        if 1 is not 1: pass
1142        if 1 in (): pass
1143        if 1 not in (): pass
1144        if 1 < 1 > 1 == 1 >= 1 <= 1 != 1 in 1 not in 1 is 1 is not 1: pass
1145
1146    def test_binary_mask_ops(self):
1147        x = 1 & 1
1148        x = 1 ^ 1
1149        x = 1 | 1
1150
1151    def test_shift_ops(self):
1152        x = 1 << 1
1153        x = 1 >> 1
1154        x = 1 << 1 >> 1
1155
1156    def test_additive_ops(self):
1157        x = 1
1158        x = 1 + 1
1159        x = 1 - 1 - 1
1160        x = 1 - 1 + 1 - 1 + 1
1161
1162    def test_multiplicative_ops(self):
1163        x = 1 * 1
1164        x = 1 / 1
1165        x = 1 % 1
1166        x = 1 / 1 * 1 % 1
1167
1168    def test_unary_ops(self):
1169        x = +1
1170        x = -1
1171        x = ~1
1172        x = ~1 ^ 1 & 1 | 1 & 1 ^ -1
1173        x = -1*1/1 + 1*1 - ---1*1
1174
1175    def test_selectors(self):
1176        ### trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME
1177        ### subscript: expr | [expr] ':' [expr]
1178
1179        import sys, time
1180        c = sys.path[0]
1181        x = time.time()
1182        x = sys.modules['time'].time()
1183        a = '01234'
1184        c = a[0]
1185        c = a[-1]
1186        s = a[0:5]
1187        s = a[:5]
1188        s = a[0:]
1189        s = a[:]
1190        s = a[-5:]
1191        s = a[:-1]
1192        s = a[-4:-3]
1193        # A rough test of SF bug 1333982.  http://python.org/sf/1333982
1194        # The testing here is fairly incomplete.
1195        # Test cases should include: commas with 1 and 2 colons
1196        d = {}
1197        d[1] = 1
1198        d[1,] = 2
1199        d[1,2] = 3
1200        d[1,2,3] = 4
1201        L = list(d)
1202        L.sort(key=lambda x: (type(x).__name__, x))
1203        self.assertEqual(str(L), '[1, (1,), (1, 2), (1, 2, 3)]')
1204
1205    def test_atoms(self):
1206        ### atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictsetmaker] '}' | NAME | NUMBER | STRING
1207        ### dictsetmaker: (test ':' test (',' test ':' test)* [',']) | (test (',' test)* [','])
1208
1209        x = (1)
1210        x = (1 or 2 or 3)
1211        x = (1 or 2 or 3, 2, 3)
1212
1213        x = []
1214        x = [1]
1215        x = [1 or 2 or 3]
1216        x = [1 or 2 or 3, 2, 3]
1217        x = []
1218
1219        x = {}
1220        x = {'one': 1}
1221        x = {'one': 1,}
1222        x = {'one' or 'two': 1 or 2}
1223        x = {'one': 1, 'two': 2}
1224        x = {'one': 1, 'two': 2,}
1225        x = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6}
1226
1227        x = {'one'}
1228        x = {'one', 1,}
1229        x = {'one', 'two', 'three'}
1230        x = {2, 3, 4,}
1231
1232        x = x
1233        x = 'x'
1234        x = 123
1235
1236    ### exprlist: expr (',' expr)* [',']
1237    ### testlist: test (',' test)* [',']
1238    # These have been exercised enough above
1239
1240    def test_classdef(self):
1241        # 'class' NAME ['(' [testlist] ')'] ':' suite
1242        class B: pass
1243        class B2(): pass
1244        class C1(B): pass
1245        class C2(B): pass
1246        class D(C1, C2, B): pass
1247        class C:
1248            def meth1(self): pass
1249            def meth2(self, arg): pass
1250            def meth3(self, a1, a2): pass
1251
1252        # decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
1253        # decorators: decorator+
1254        # decorated: decorators (classdef | funcdef)
1255        def class_decorator(x): return x
1256        @class_decorator
1257        class G: pass
1258
1259    def test_dictcomps(self):
1260        # dictorsetmaker: ( (test ':' test (comp_for |
1261        #                                   (',' test ':' test)* [','])) |
1262        #                   (test (comp_for | (',' test)* [','])) )
1263        nums = [1, 2, 3]
1264        self.assertEqual({i:i+1 for i in nums}, {1: 2, 2: 3, 3: 4})
1265
1266    def test_listcomps(self):
1267        # list comprehension tests
1268        nums = [1, 2, 3, 4, 5]
1269        strs = ["Apple", "Banana", "Coconut"]
1270        spcs = ["  Apple", " Banana ", "Coco  nut  "]
1271
1272        self.assertEqual([s.strip() for s in spcs], ['Apple', 'Banana', 'Coco  nut'])
1273        self.assertEqual([3 * x for x in nums], [3, 6, 9, 12, 15])
1274        self.assertEqual([x for x in nums if x > 2], [3, 4, 5])
1275        self.assertEqual([(i, s) for i in nums for s in strs],
1276                         [(1, 'Apple'), (1, 'Banana'), (1, 'Coconut'),
1277                          (2, 'Apple'), (2, 'Banana'), (2, 'Coconut'),
1278                          (3, 'Apple'), (3, 'Banana'), (3, 'Coconut'),
1279                          (4, 'Apple'), (4, 'Banana'), (4, 'Coconut'),
1280                          (5, 'Apple'), (5, 'Banana'), (5, 'Coconut')])
1281        self.assertEqual([(i, s) for i in nums for s in [f for f in strs if "n" in f]],
1282                         [(1, 'Banana'), (1, 'Coconut'), (2, 'Banana'), (2, 'Coconut'),
1283                          (3, 'Banana'), (3, 'Coconut'), (4, 'Banana'), (4, 'Coconut'),
1284                          (5, 'Banana'), (5, 'Coconut')])
1285        self.assertEqual([(lambda a:[a**i for i in range(a+1)])(j) for j in range(5)],
1286                         [[1], [1, 1], [1, 2, 4], [1, 3, 9, 27], [1, 4, 16, 64, 256]])
1287
1288        def test_in_func(l):
1289            return [0 < x < 3 for x in l if x > 2]
1290
1291        self.assertEqual(test_in_func(nums), [False, False, False])
1292
1293        def test_nested_front():
1294            self.assertEqual([[y for y in [x, x + 1]] for x in [1,3,5]],
1295                             [[1, 2], [3, 4], [5, 6]])
1296
1297        test_nested_front()
1298
1299        check_syntax_error(self, "[i, s for i in nums for s in strs]")
1300        check_syntax_error(self, "[x if y]")
1301
1302        suppliers = [
1303          (1, "Boeing"),
1304          (2, "Ford"),
1305          (3, "Macdonalds")
1306        ]
1307
1308        parts = [
1309          (10, "Airliner"),
1310          (20, "Engine"),
1311          (30, "Cheeseburger")
1312        ]
1313
1314        suppart = [
1315          (1, 10), (1, 20), (2, 20), (3, 30)
1316        ]
1317
1318        x = [
1319          (sname, pname)
1320            for (sno, sname) in suppliers
1321              for (pno, pname) in parts
1322                for (sp_sno, sp_pno) in suppart
1323                  if sno == sp_sno and pno == sp_pno
1324        ]
1325
1326        self.assertEqual(x, [('Boeing', 'Airliner'), ('Boeing', 'Engine'), ('Ford', 'Engine'),
1327                             ('Macdonalds', 'Cheeseburger')])
1328
1329    def test_genexps(self):
1330        # generator expression tests
1331        g = ([x for x in range(10)] for x in range(1))
1332        self.assertEqual(next(g), [x for x in range(10)])
1333        try:
1334            next(g)
1335            self.fail('should produce StopIteration exception')
1336        except StopIteration:
1337            pass
1338
1339        a = 1
1340        try:
1341            g = (a for d in a)
1342            next(g)
1343            self.fail('should produce TypeError')
1344        except TypeError:
1345            pass
1346
1347        self.assertEqual(list((x, y) for x in 'abcd' for y in 'abcd'), [(x, y) for x in 'abcd' for y in 'abcd'])
1348        self.assertEqual(list((x, y) for x in 'ab' for y in 'xy'), [(x, y) for x in 'ab' for y in 'xy'])
1349
1350        a = [x for x in range(10)]
1351        b = (x for x in (y for y in a))
1352        self.assertEqual(sum(b), sum([x for x in range(10)]))
1353
1354        self.assertEqual(sum(x**2 for x in range(10)), sum([x**2 for x in range(10)]))
1355        self.assertEqual(sum(x*x for x in range(10) if x%2), sum([x*x for x in range(10) if x%2]))
1356        self.assertEqual(sum(x for x in (y for y in range(10))), sum([x for x in range(10)]))
1357        self.assertEqual(sum(x for x in (y for y in (z for z in range(10)))), sum([x for x in range(10)]))
1358        self.assertEqual(sum(x for x in [y for y in (z for z in range(10))]), sum([x for x in range(10)]))
1359        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)]))
1360        self.assertEqual(sum(x for x in (y for y in (z for z in range(10) if True) if False) if True), 0)
1361        check_syntax_error(self, "foo(x for x in range(10), 100)")
1362        check_syntax_error(self, "foo(100, x for x in range(10))")
1363
1364    def test_comprehension_specials(self):
1365        # test for outmost iterable precomputation
1366        x = 10; g = (i for i in range(x)); x = 5
1367        self.assertEqual(len(list(g)), 10)
1368
1369        # This should hold, since we're only precomputing outmost iterable.
1370        x = 10; t = False; g = ((i,j) for i in range(x) if t for j in range(x))
1371        x = 5; t = True;
1372        self.assertEqual([(i,j) for i in range(10) for j in range(5)], list(g))
1373
1374        # Grammar allows multiple adjacent 'if's in listcomps and genexps,
1375        # even though it's silly. Make sure it works (ifelse broke this.)
1376        self.assertEqual([ x for x in range(10) if x % 2 if x % 3 ], [1, 5, 7])
1377        self.assertEqual(list(x for x in range(10) if x % 2 if x % 3), [1, 5, 7])
1378
1379        # verify unpacking single element tuples in listcomp/genexp.
1380        self.assertEqual([x for x, in [(4,), (5,), (6,)]], [4, 5, 6])
1381        self.assertEqual(list(x for x, in [(7,), (8,), (9,)]), [7, 8, 9])
1382
1383    def test_with_statement(self):
1384        class manager(object):
1385            def __enter__(self):
1386                return (1, 2)
1387            def __exit__(self, *args):
1388                pass
1389
1390        with manager():
1391            pass
1392        with manager() as x:
1393            pass
1394        with manager() as (x, y):
1395            pass
1396        with manager(), manager():
1397            pass
1398        with manager() as x, manager() as y:
1399            pass
1400        with manager() as x, manager():
1401            pass
1402
1403    def test_if_else_expr(self):
1404        # Test ifelse expressions in various cases
1405        def _checkeval(msg, ret):
1406            "helper to check that evaluation of expressions is done correctly"
1407            print(msg)
1408            return ret
1409
1410        # the next line is not allowed anymore
1411        #self.assertEqual([ x() for x in lambda: True, lambda: False if x() ], [True])
1412        self.assertEqual([ x() for x in (lambda: True, lambda: False) if x() ], [True])
1413        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])
1414        self.assertEqual((5 if 1 else _checkeval("check 1", 0)), 5)
1415        self.assertEqual((_checkeval("check 2", 0) if 0 else 5), 5)
1416        self.assertEqual((5 and 6 if 0 else 1), 1)
1417        self.assertEqual(((5 and 6) if 0 else 1), 1)
1418        self.assertEqual((5 and (6 if 1 else 1)), 6)
1419        self.assertEqual((0 or _checkeval("check 3", 2) if 0 else 3), 3)
1420        self.assertEqual((1 or _checkeval("check 4", 2) if 1 else _checkeval("check 5", 3)), 1)
1421        self.assertEqual((0 or 5 if 1 else _checkeval("check 6", 3)), 5)
1422        self.assertEqual((not 5 if 1 else 1), False)
1423        self.assertEqual((not 5 if 0 else 1), 1)
1424        self.assertEqual((6 + 1 if 1 else 2), 7)
1425        self.assertEqual((6 - 1 if 1 else 2), 5)
1426        self.assertEqual((6 * 2 if 1 else 4), 12)
1427        self.assertEqual((6 / 2 if 1 else 3), 3)
1428        self.assertEqual((6 < 4 if 0 else 2), 2)
1429
1430    def test_paren_evaluation(self):
1431        self.assertEqual(16 // (4 // 2), 8)
1432        self.assertEqual((16 // 4) // 2, 2)
1433        self.assertEqual(16 // 4 // 2, 2)
1434        self.assertTrue(False is (2 is 3))
1435        self.assertFalse((False is 2) is 3)
1436        self.assertFalse(False is 2 is 3)
1437
1438    def test_matrix_mul(self):
1439        # This is not intended to be a comprehensive test, rather just to be few
1440        # samples of the @ operator in test_grammar.py.
1441        class M:
1442            def __matmul__(self, o):
1443                return 4
1444            def __imatmul__(self, o):
1445                self.other = o
1446                return self
1447        m = M()
1448        self.assertEqual(m @ m, 4)
1449        m @= 42
1450        self.assertEqual(m.other, 42)
1451
1452    def test_async_await(self):
1453        async def test():
1454            def sum():
1455                pass
1456            if 1:
1457                await someobj()
1458
1459        self.assertEqual(test.__name__, 'test')
1460        self.assertTrue(bool(test.__code__.co_flags & inspect.CO_COROUTINE))
1461
1462        def decorator(func):
1463            setattr(func, '_marked', True)
1464            return func
1465
1466        @decorator
1467        async def test2():
1468            return 22
1469        self.assertTrue(test2._marked)
1470        self.assertEqual(test2.__name__, 'test2')
1471        self.assertTrue(bool(test2.__code__.co_flags & inspect.CO_COROUTINE))
1472
1473    def test_async_for(self):
1474        class Done(Exception): pass
1475
1476        class AIter:
1477            def __aiter__(self):
1478                return self
1479            async def __anext__(self):
1480                raise StopAsyncIteration
1481
1482        async def foo():
1483            async for i in AIter():
1484                pass
1485            async for i, j in AIter():
1486                pass
1487            async for i in AIter():
1488                pass
1489            else:
1490                pass
1491            raise Done
1492
1493        with self.assertRaises(Done):
1494            foo().send(None)
1495
1496    def test_async_with(self):
1497        class Done(Exception): pass
1498
1499        class manager:
1500            async def __aenter__(self):
1501                return (1, 2)
1502            async def __aexit__(self, *exc):
1503                return False
1504
1505        async def foo():
1506            async with manager():
1507                pass
1508            async with manager() as x:
1509                pass
1510            async with manager() as (x, y):
1511                pass
1512            async with manager(), manager():
1513                pass
1514            async with manager() as x, manager() as y:
1515                pass
1516            async with manager() as x, manager():
1517                pass
1518            raise Done
1519
1520        with self.assertRaises(Done):
1521            foo().send(None)
1522
1523
1524if __name__ == '__main__':
1525    unittest.main()
1526