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