1# Python test set -- part 1, grammar.
2# This just tests whether the parser accepts them all.
3
4from test.test_support import run_unittest, check_syntax_error, \
5                              check_py3k_warnings
6import unittest
7import sys
8# testing import *
9from sys import *
10
11
12class TokenTests(unittest.TestCase):
13
14    def testBackslash(self):
15        # Backslash means line continuation:
16        x = 1 \
17        + 1
18        self.assertEqual(x, 2, 'backslash for line continuation')
19
20        # Backslash does not means continuation in comments :\
21        x = 0
22        self.assertEqual(x, 0, 'backslash ending comment')
23
24    def testPlainIntegers(self):
25        self.assertEqual(0xff, 255)
26        self.assertEqual(0377, 255)
27        self.assertEqual(2147483647, 017777777777)
28        # "0x" is not a valid literal
29        self.assertRaises(SyntaxError, eval, "0x")
30        from sys import maxint
31        if maxint == 2147483647:
32            self.assertEqual(-2147483647-1, -020000000000)
33            # XXX -2147483648
34            self.assertTrue(037777777777 > 0)
35            self.assertTrue(0xffffffff > 0)
36            for s in '2147483648', '040000000000', '0x100000000':
37                try:
38                    x = eval(s)
39                except OverflowError:
40                    self.fail("OverflowError on huge integer literal %r" % s)
41        elif maxint == 9223372036854775807:
42            self.assertEqual(-9223372036854775807-1, -01000000000000000000000)
43            self.assertTrue(01777777777777777777777 > 0)
44            self.assertTrue(0xffffffffffffffff > 0)
45            for s in '9223372036854775808', '02000000000000000000000', \
46                     '0x10000000000000000':
47                try:
48                    x = eval(s)
49                except OverflowError:
50                    self.fail("OverflowError on huge integer literal %r" % s)
51        else:
52            self.fail('Weird maxint value %r' % maxint)
53
54    def testLongIntegers(self):
55        x = 0L
56        x = 0l
57        x = 0xffffffffffffffffL
58        x = 0xffffffffffffffffl
59        x = 077777777777777777L
60        x = 077777777777777777l
61        x = 123456789012345678901234567890L
62        x = 123456789012345678901234567890l
63
64    def testFloats(self):
65        x = 3.14
66        x = 314.
67        x = 0.314
68        # XXX x = 000.314
69        x = .314
70        x = 3e14
71        x = 3E14
72        x = 3e-14
73        x = 3e+14
74        x = 3.e14
75        x = .3e14
76        x = 3.1e4
77
78    def test_float_exponent_tokenization(self):
79        # See issue 21642.
80        self.assertEqual(1 if 1else 0, 1)
81        self.assertEqual(1 if 0else 0, 0)
82        self.assertRaises(SyntaxError, eval, "0 if 1Else 0")
83
84    def testStringLiterals(self):
85        x = ''; y = ""; self.assertTrue(len(x) == 0 and x == y)
86        x = '\''; y = "'"; self.assertTrue(len(x) == 1 and x == y and ord(x) == 39)
87        x = '"'; y = "\""; self.assertTrue(len(x) == 1 and x == y and ord(x) == 34)
88        x = "doesn't \"shrink\" does it"
89        y = 'doesn\'t "shrink" does it'
90        self.assertTrue(len(x) == 24 and x == y)
91        x = "does \"shrink\" doesn't it"
92        y = 'does "shrink" doesn\'t it'
93        self.assertTrue(len(x) == 24 and x == y)
94        x = """
95The "quick"
96brown fox
97jumps over
98the 'lazy' dog.
99"""
100        y = '\nThe "quick"\nbrown fox\njumps over\nthe \'lazy\' dog.\n'
101        self.assertEqual(x, y)
102        y = '''
103The "quick"
104brown fox
105jumps over
106the 'lazy' dog.
107'''
108        self.assertEqual(x, y)
109        y = "\n\
110The \"quick\"\n\
111brown fox\n\
112jumps over\n\
113the 'lazy' dog.\n\
114"
115        self.assertEqual(x, y)
116        y = '\n\
117The \"quick\"\n\
118brown fox\n\
119jumps over\n\
120the \'lazy\' dog.\n\
121'
122        self.assertEqual(x, y)
123
124
125class GrammarTests(unittest.TestCase):
126
127    # single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE
128    # XXX can't test in a script -- this rule is only used when interactive
129
130    # file_input: (NEWLINE | stmt)* ENDMARKER
131    # Being tested as this very moment this very module
132
133    # expr_input: testlist NEWLINE
134    # XXX Hard to test -- used only in calls to input()
135
136    def testEvalInput(self):
137        # testlist ENDMARKER
138        x = eval('1, 0 or 1')
139
140    def testFuncdef(self):
141        ### 'def' NAME parameters ':' suite
142        ### parameters: '(' [varargslist] ')'
143        ### varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' ('**'|'*' '*') NAME]
144        ###            | ('**'|'*' '*') NAME)
145        ###            | fpdef ['=' test] (',' fpdef ['=' test])* [',']
146        ### fpdef: NAME | '(' fplist ')'
147        ### fplist: fpdef (',' fpdef)* [',']
148        ### arglist: (argument ',')* (argument | *' test [',' '**' test] | '**' test)
149        ### argument: [test '='] test   # Really [keyword '='] test
150        def f1(): pass
151        f1()
152        f1(*())
153        f1(*(), **{})
154        def f2(one_argument): pass
155        def f3(two, arguments): pass
156        # Silence Py3k warning
157        exec('def f4(two, (compound, (argument, list))): pass')
158        exec('def f5((compound, first), two): pass')
159        self.assertEqual(f2.func_code.co_varnames, ('one_argument',))
160        self.assertEqual(f3.func_code.co_varnames, ('two', 'arguments'))
161        if sys.platform.startswith('java'):
162            self.assertEqual(f4.func_code.co_varnames,
163                   ('two', '(compound, (argument, list))', 'compound', 'argument',
164                                'list',))
165            self.assertEqual(f5.func_code.co_varnames,
166                   ('(compound, first)', 'two', 'compound', 'first'))
167        else:
168            self.assertEqual(f4.func_code.co_varnames,
169                  ('two', '.1', 'compound', 'argument',  'list'))
170            self.assertEqual(f5.func_code.co_varnames,
171                  ('.0', 'two', 'compound', 'first'))
172        def a1(one_arg,): pass
173        def a2(two, args,): pass
174        def v0(*rest): pass
175        def v1(a, *rest): pass
176        def v2(a, b, *rest): pass
177        # Silence Py3k warning
178        exec('def v3(a, (b, c), *rest): return a, b, c, rest')
179
180        f1()
181        f2(1)
182        f2(1,)
183        f3(1, 2)
184        f3(1, 2,)
185        f4(1, (2, (3, 4)))
186        v0()
187        v0(1)
188        v0(1,)
189        v0(1,2)
190        v0(1,2,3,4,5,6,7,8,9,0)
191        v1(1)
192        v1(1,)
193        v1(1,2)
194        v1(1,2,3)
195        v1(1,2,3,4,5,6,7,8,9,0)
196        v2(1,2)
197        v2(1,2,3)
198        v2(1,2,3,4)
199        v2(1,2,3,4,5,6,7,8,9,0)
200        v3(1,(2,3))
201        v3(1,(2,3),4)
202        v3(1,(2,3),4,5,6,7,8,9,0)
203
204        # ceval unpacks the formal arguments into the first argcount names;
205        # thus, the names nested inside tuples must appear after these names.
206        if sys.platform.startswith('java'):
207            self.assertEqual(v3.func_code.co_varnames, ('a', '(b, c)', 'rest', 'b', 'c'))
208        else:
209            self.assertEqual(v3.func_code.co_varnames, ('a', '.1', 'rest', 'b', 'c'))
210        self.assertEqual(v3(1, (2, 3), 4), (1, 2, 3, (4,)))
211        def d01(a=1): pass
212        d01()
213        d01(1)
214        d01(*(1,))
215        d01(**{'a':2})
216        def d11(a, b=1): pass
217        d11(1)
218        d11(1, 2)
219        d11(1, **{'b':2})
220        def d21(a, b, c=1): pass
221        d21(1, 2)
222        d21(1, 2, 3)
223        d21(*(1, 2, 3))
224        d21(1, *(2, 3))
225        d21(1, 2, *(3,))
226        d21(1, 2, **{'c':3})
227        def d02(a=1, b=2): pass
228        d02()
229        d02(1)
230        d02(1, 2)
231        d02(*(1, 2))
232        d02(1, *(2,))
233        d02(1, **{'b':2})
234        d02(**{'a': 1, 'b': 2})
235        def d12(a, b=1, c=2): pass
236        d12(1)
237        d12(1, 2)
238        d12(1, 2, 3)
239        def d22(a, b, c=1, d=2): pass
240        d22(1, 2)
241        d22(1, 2, 3)
242        d22(1, 2, 3, 4)
243        def d01v(a=1, *rest): pass
244        d01v()
245        d01v(1)
246        d01v(1, 2)
247        d01v(*(1, 2, 3, 4))
248        d01v(*(1,))
249        d01v(**{'a':2})
250        def d11v(a, b=1, *rest): pass
251        d11v(1)
252        d11v(1, 2)
253        d11v(1, 2, 3)
254        def d21v(a, b, c=1, *rest): pass
255        d21v(1, 2)
256        d21v(1, 2, 3)
257        d21v(1, 2, 3, 4)
258        d21v(*(1, 2, 3, 4))
259        d21v(1, 2, **{'c': 3})
260        def d02v(a=1, b=2, *rest): pass
261        d02v()
262        d02v(1)
263        d02v(1, 2)
264        d02v(1, 2, 3)
265        d02v(1, *(2, 3, 4))
266        d02v(**{'a': 1, 'b': 2})
267        def d12v(a, b=1, c=2, *rest): pass
268        d12v(1)
269        d12v(1, 2)
270        d12v(1, 2, 3)
271        d12v(1, 2, 3, 4)
272        d12v(*(1, 2, 3, 4))
273        d12v(1, 2, *(3, 4, 5))
274        d12v(1, *(2,), **{'c': 3})
275        def d22v(a, b, c=1, d=2, *rest): pass
276        d22v(1, 2)
277        d22v(1, 2, 3)
278        d22v(1, 2, 3, 4)
279        d22v(1, 2, 3, 4, 5)
280        d22v(*(1, 2, 3, 4))
281        d22v(1, 2, *(3, 4, 5))
282        d22v(1, *(2, 3), **{'d': 4})
283        # Silence Py3k warning
284        exec('def d31v((x)): pass')
285        exec('def d32v((x,)): pass')
286        d31v(1)
287        d32v((1,))
288
289        # keyword arguments after *arglist
290        def f(*args, **kwargs):
291            return args, kwargs
292        self.assertEqual(f(1, x=2, *[3, 4], y=5), ((1, 3, 4),
293                                                    {'x':2, 'y':5}))
294        self.assertRaises(SyntaxError, eval, "f(1, *(2,3), 4)")
295        self.assertRaises(SyntaxError, eval, "f(1, x=2, *(3,4), x=5)")
296
297        # Check ast errors in *args and *kwargs
298        check_syntax_error(self, "f(*g(1=2))")
299        check_syntax_error(self, "f(**g(1=2))")
300
301    def testLambdef(self):
302        ### lambdef: 'lambda' [varargslist] ':' test
303        l1 = lambda : 0
304        self.assertEqual(l1(), 0)
305        l2 = lambda : a[d] # XXX just testing the expression
306        l3 = lambda : [2 < x for x in [-1, 3, 0L]]
307        self.assertEqual(l3(), [0, 1, 0])
308        l4 = lambda x = lambda y = lambda z=1 : z : y() : x()
309        self.assertEqual(l4(), 1)
310        l5 = lambda x, y, z=2: x + y + z
311        self.assertEqual(l5(1, 2), 5)
312        self.assertEqual(l5(1, 2, 3), 6)
313        check_syntax_error(self, "lambda x: x = 2")
314        check_syntax_error(self, "lambda (None,): None")
315
316    ### stmt: simple_stmt | compound_stmt
317    # Tested below
318
319    def testSimpleStmt(self):
320        ### simple_stmt: small_stmt (';' small_stmt)* [';']
321        x = 1; pass; del x
322        def foo():
323            # verify statements that end with semi-colons
324            x = 1; pass; del x;
325        foo()
326
327    ### small_stmt: expr_stmt | print_stmt  | pass_stmt | del_stmt | flow_stmt | import_stmt | global_stmt | access_stmt | exec_stmt
328    # Tested below
329
330    def testExprStmt(self):
331        # (exprlist '=')* exprlist
332        1
333        1, 2, 3
334        x = 1
335        x = 1, 2, 3
336        x = y = z = 1, 2, 3
337        x, y, z = 1, 2, 3
338        abc = a, b, c = x, y, z = xyz = 1, 2, (3, 4)
339
340        check_syntax_error(self, "x + 1 = 1")
341        check_syntax_error(self, "a + 1 = b + 2")
342
343    def testPrintStmt(self):
344        # 'print' (test ',')* [test]
345        import StringIO
346
347        # Can't test printing to real stdout without comparing output
348        # which is not available in unittest.
349        save_stdout = sys.stdout
350        sys.stdout = StringIO.StringIO()
351
352        print 1, 2, 3
353        print 1, 2, 3,
354        print
355        print 0 or 1, 0 or 1,
356        print 0 or 1
357
358        # 'print' '>>' test ','
359        print >> sys.stdout, 1, 2, 3
360        print >> sys.stdout, 1, 2, 3,
361        print >> sys.stdout
362        print >> sys.stdout, 0 or 1, 0 or 1,
363        print >> sys.stdout, 0 or 1
364
365        # test printing to an instance
366        class Gulp:
367            def write(self, msg): pass
368
369        gulp = Gulp()
370        print >> gulp, 1, 2, 3
371        print >> gulp, 1, 2, 3,
372        print >> gulp
373        print >> gulp, 0 or 1, 0 or 1,
374        print >> gulp, 0 or 1
375
376        # test print >> None
377        def driver():
378            oldstdout = sys.stdout
379            sys.stdout = Gulp()
380            try:
381                tellme(Gulp())
382                tellme()
383            finally:
384                sys.stdout = oldstdout
385
386        # we should see this once
387        def tellme(file=sys.stdout):
388            print >> file, 'hello world'
389
390        driver()
391
392        # we should not see this at all
393        def tellme(file=None):
394            print >> file, 'goodbye universe'
395
396        driver()
397
398        self.assertEqual(sys.stdout.getvalue(), '''\
3991 2 3
4001 2 3
4011 1 1
4021 2 3
4031 2 3
4041 1 1
405hello world
406''')
407        sys.stdout = save_stdout
408
409        # syntax errors
410        check_syntax_error(self, 'print ,')
411        check_syntax_error(self, 'print >> x,')
412
413    def testDelStmt(self):
414        # 'del' exprlist
415        abc = [1,2,3]
416        x, y, z = abc
417        xyz = x, y, z
418
419        del abc
420        del x, y, (z, xyz)
421
422    def testPassStmt(self):
423        # 'pass'
424        pass
425
426    # flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
427    # Tested below
428
429    def testBreakStmt(self):
430        # 'break'
431        while 1: break
432
433    def testContinueStmt(self):
434        # 'continue'
435        i = 1
436        while i: i = 0; continue
437
438        msg = ""
439        while not msg:
440            msg = "ok"
441            try:
442                continue
443                msg = "continue failed to continue inside try"
444            except:
445                msg = "continue inside try called except block"
446        if msg != "ok":
447            self.fail(msg)
448
449        msg = ""
450        while not msg:
451            msg = "finally block not called"
452            try:
453                continue
454            finally:
455                msg = "ok"
456        if msg != "ok":
457            self.fail(msg)
458
459    def test_break_continue_loop(self):
460        # This test warrants an explanation. It is a test specifically for SF bugs
461        # #463359 and #462937. The bug is that a 'break' statement executed or
462        # exception raised inside a try/except inside a loop, *after* a continue
463        # statement has been executed in that loop, will cause the wrong number of
464        # arguments to be popped off the stack and the instruction pointer reset to
465        # a very small number (usually 0.) Because of this, the following test
466        # *must* written as a function, and the tracking vars *must* be function
467        # arguments with default values. Otherwise, the test will loop and loop.
468
469        def test_inner(extra_burning_oil = 1, count=0):
470            big_hippo = 2
471            while big_hippo:
472                count += 1
473                try:
474                    if extra_burning_oil and big_hippo == 1:
475                        extra_burning_oil -= 1
476                        break
477                    big_hippo -= 1
478                    continue
479                except:
480                    raise
481            if count > 2 or big_hippo != 1:
482                self.fail("continue then break in try/except in loop broken!")
483        test_inner()
484
485    def testReturn(self):
486        # 'return' [testlist]
487        def g1(): return
488        def g2(): return 1
489        g1()
490        x = g2()
491        check_syntax_error(self, "class foo:return 1")
492
493    def testYield(self):
494        check_syntax_error(self, "class foo:yield 1")
495
496    def testRaise(self):
497        # 'raise' test [',' test]
498        try: raise RuntimeError, 'just testing'
499        except RuntimeError: pass
500        try: raise KeyboardInterrupt
501        except KeyboardInterrupt: pass
502
503    def testImport(self):
504        # 'import' dotted_as_names
505        import sys
506        import time, sys
507        # 'from' dotted_name 'import' ('*' | '(' import_as_names ')' | import_as_names)
508        from time import time
509        from time import (time)
510        # not testable inside a function, but already done at top of the module
511        # from sys import *
512        from sys import path, argv
513        from sys import (path, argv)
514        from sys import (path, argv,)
515
516    def testGlobal(self):
517        # 'global' NAME (',' NAME)*
518        global a
519        global a, b
520        global one, two, three, four, five, six, seven, eight, nine, ten
521
522    def testExec(self):
523        # 'exec' expr ['in' expr [',' expr]]
524        z = None
525        del z
526        exec 'z=1+1\n'
527        if z != 2: self.fail('exec \'z=1+1\'\\n')
528        del z
529        exec 'z=1+1'
530        if z != 2: self.fail('exec \'z=1+1\'')
531        z = None
532        del z
533        import types
534        if hasattr(types, "UnicodeType"):
535            exec r"""if 1:
536            exec u'z=1+1\n'
537            if z != 2: self.fail('exec u\'z=1+1\'\\n')
538            del z
539            exec u'z=1+1'
540            if z != 2: self.fail('exec u\'z=1+1\'')"""
541        g = {}
542        exec 'z = 1' in g
543        if '__builtins__' in g: del g['__builtins__']
544        if g != {'z': 1}: self.fail('exec \'z = 1\' in g')
545        g = {}
546        l = {}
547
548        exec 'global a; a = 1; b = 2' in g, l
549        if '__builtins__' in g: del g['__builtins__']
550        if '__builtins__' in l: del l['__builtins__']
551        if (g, l) != ({'a':1}, {'b':2}):
552            self.fail('exec ... in g (%s), l (%s)' %(g,l))
553
554    def testAssert(self):
555        # assertTruestmt: 'assert' test [',' test]
556        assert 1
557        assert 1, 1
558        assert lambda x:x
559        assert 1, lambda x:x+1
560
561        try:
562            assert True
563        except AssertionError as e:
564            self.fail("'assert True' should not have raised an AssertionError")
565
566        try:
567            assert True, 'this should always pass'
568        except AssertionError as e:
569            self.fail("'assert True, msg' should not have "
570                      "raised an AssertionError")
571
572    # these tests fail if python is run with -O, so check __debug__
573    @unittest.skipUnless(__debug__, "Won't work if __debug__ is False")
574    def testAssert2(self):
575        try:
576            assert 0, "msg"
577        except AssertionError, e:
578            self.assertEqual(e.args[0], "msg")
579        else:
580            self.fail("AssertionError not raised by assert 0")
581
582        try:
583            assert False
584        except AssertionError as e:
585            self.assertEqual(len(e.args), 0)
586        else:
587            self.fail("AssertionError not raised by 'assert False'")
588
589
590    ### compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
591    # Tested below
592
593    def testIf(self):
594        # 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
595        if 1: pass
596        if 1: pass
597        else: pass
598        if 0: pass
599        elif 0: pass
600        if 0: pass
601        elif 0: pass
602        elif 0: pass
603        elif 0: pass
604        else: pass
605
606    def testWhile(self):
607        # 'while' test ':' suite ['else' ':' suite]
608        while 0: pass
609        while 0: pass
610        else: pass
611
612        # Issue1920: "while 0" is optimized away,
613        # ensure that the "else" clause is still present.
614        x = 0
615        while 0:
616            x = 1
617        else:
618            x = 2
619        self.assertEqual(x, 2)
620
621    def testFor(self):
622        # 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite]
623        for i in 1, 2, 3: pass
624        for i, j, k in (): pass
625        else: pass
626        class Squares:
627            def __init__(self, max):
628                self.max = max
629                self.sofar = []
630            def __len__(self): return len(self.sofar)
631            def __getitem__(self, i):
632                if not 0 <= i < self.max: raise IndexError
633                n = len(self.sofar)
634                while n <= i:
635                    self.sofar.append(n*n)
636                    n = n+1
637                return self.sofar[i]
638        n = 0
639        for x in Squares(10): n = n+x
640        if n != 285:
641            self.fail('for over growing sequence')
642
643        result = []
644        for x, in [(1,), (2,), (3,)]:
645            result.append(x)
646        self.assertEqual(result, [1, 2, 3])
647
648    def testTry(self):
649        ### try_stmt: 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
650        ###         | 'try' ':' suite 'finally' ':' suite
651        ### except_clause: 'except' [expr [('as' | ',') expr]]
652        try:
653            1/0
654        except ZeroDivisionError:
655            pass
656        else:
657            pass
658        try: 1/0
659        except EOFError: pass
660        except TypeError as msg: pass
661        except RuntimeError, msg: pass
662        except: pass
663        else: pass
664        try: 1/0
665        except (EOFError, TypeError, ZeroDivisionError): pass
666        try: 1/0
667        except (EOFError, TypeError, ZeroDivisionError), msg: pass
668        try: pass
669        finally: pass
670
671    def testSuite(self):
672        # simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT
673        if 1: pass
674        if 1:
675            pass
676        if 1:
677            #
678            #
679            #
680            pass
681            pass
682            #
683            pass
684            #
685
686    def testTest(self):
687        ### and_test ('or' and_test)*
688        ### and_test: not_test ('and' not_test)*
689        ### not_test: 'not' not_test | comparison
690        if not 1: pass
691        if 1 and 1: pass
692        if 1 or 1: pass
693        if not not not 1: pass
694        if not 1 and 1 and 1: pass
695        if 1 and 1 or 1 and 1 and 1 or not 1 and 1: pass
696
697    def testComparison(self):
698        ### comparison: expr (comp_op expr)*
699        ### comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
700        if 1: pass
701        x = (1 == 1)
702        if 1 == 1: pass
703        if 1 != 1: pass
704        if 1 < 1: pass
705        if 1 > 1: pass
706        if 1 <= 1: pass
707        if 1 >= 1: pass
708        if 1 is 1: pass
709        if 1 is not 1: pass
710        if 1 in (): pass
711        if 1 not in (): pass
712        if 1 < 1 > 1 == 1 >= 1 <= 1 != 1 in 1 not in 1 is 1 is not 1: pass
713        # Silence Py3k warning
714        if eval('1 <> 1'): pass
715        if eval('1 < 1 > 1 == 1 >= 1 <= 1 <> 1 != 1 in 1 not in 1 is 1 is not 1'): pass
716
717    def testBinaryMaskOps(self):
718        x = 1 & 1
719        x = 1 ^ 1
720        x = 1 | 1
721
722    def testShiftOps(self):
723        x = 1 << 1
724        x = 1 >> 1
725        x = 1 << 1 >> 1
726
727    def testAdditiveOps(self):
728        x = 1
729        x = 1 + 1
730        x = 1 - 1 - 1
731        x = 1 - 1 + 1 - 1 + 1
732
733    def testMultiplicativeOps(self):
734        x = 1 * 1
735        x = 1 / 1
736        x = 1 % 1
737        x = 1 / 1 * 1 % 1
738
739    def testUnaryOps(self):
740        x = +1
741        x = -1
742        x = ~1
743        x = ~1 ^ 1 & 1 | 1 & 1 ^ -1
744        x = -1*1/1 + 1*1 - ---1*1
745
746    def testSelectors(self):
747        ### trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME
748        ### subscript: expr | [expr] ':' [expr]
749
750        import sys, time
751        c = sys.path[0]
752        x = time.time()
753        x = sys.modules['time'].time()
754        a = '01234'
755        c = a[0]
756        c = a[-1]
757        s = a[0:5]
758        s = a[:5]
759        s = a[0:]
760        s = a[:]
761        s = a[-5:]
762        s = a[:-1]
763        s = a[-4:-3]
764        # A rough test of SF bug 1333982.  http://python.org/sf/1333982
765        # The testing here is fairly incomplete.
766        # Test cases should include: commas with 1 and 2 colons
767        d = {}
768        d[1] = 1
769        d[1,] = 2
770        d[1,2] = 3
771        d[1,2,3] = 4
772        L = list(d)
773        L.sort()
774        self.assertEqual(str(L), '[1, (1,), (1, 2), (1, 2, 3)]')
775
776    def testAtoms(self):
777        ### atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING
778        ### dictorsetmaker: (test ':' test (',' test ':' test)* [',']) | (test (',' test)* [','])
779
780        x = (1)
781        x = (1 or 2 or 3)
782        x = (1 or 2 or 3, 2, 3)
783
784        x = []
785        x = [1]
786        x = [1 or 2 or 3]
787        x = [1 or 2 or 3, 2, 3]
788        x = []
789
790        x = {}
791        x = {'one': 1}
792        x = {'one': 1,}
793        x = {'one' or 'two': 1 or 2}
794        x = {'one': 1, 'two': 2}
795        x = {'one': 1, 'two': 2,}
796        x = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6}
797
798        x = {'one'}
799        x = {'one', 1,}
800        x = {'one', 'two', 'three'}
801        x = {2, 3, 4,}
802
803        # Silence Py3k warning
804        x = eval('`x`')
805        x = eval('`1 or 2 or 3`')
806        self.assertEqual(eval('`1,2`'), '(1, 2)')
807
808        x = x
809        x = 'x'
810        x = 123
811
812    ### exprlist: expr (',' expr)* [',']
813    ### testlist: test (',' test)* [',']
814    # These have been exercised enough above
815
816    def testClassdef(self):
817        # 'class' NAME ['(' [testlist] ')'] ':' suite
818        class B: pass
819        class B2(): pass
820        class C1(B): pass
821        class C2(B): pass
822        class D(C1, C2, B): pass
823        class C:
824            def meth1(self): pass
825            def meth2(self, arg): pass
826            def meth3(self, a1, a2): pass
827        # decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
828        # decorators: decorator+
829        # decorated: decorators (classdef | funcdef)
830        def class_decorator(x):
831            x.decorated = True
832            return x
833        @class_decorator
834        class G:
835            pass
836        self.assertEqual(G.decorated, True)
837
838    def testDictcomps(self):
839        # dictorsetmaker: ( (test ':' test (comp_for |
840        #                                   (',' test ':' test)* [','])) |
841        #                   (test (comp_for | (',' test)* [','])) )
842        nums = [1, 2, 3]
843        self.assertEqual({i:i+1 for i in nums}, {1: 2, 2: 3, 3: 4})
844
845    def testListcomps(self):
846        # list comprehension tests
847        nums = [1, 2, 3, 4, 5]
848        strs = ["Apple", "Banana", "Coconut"]
849        spcs = ["  Apple", " Banana ", "Coco  nut  "]
850
851        self.assertEqual([s.strip() for s in spcs], ['Apple', 'Banana', 'Coco  nut'])
852        self.assertEqual([3 * x for x in nums], [3, 6, 9, 12, 15])
853        self.assertEqual([x for x in nums if x > 2], [3, 4, 5])
854        self.assertEqual([(i, s) for i in nums for s in strs],
855                         [(1, 'Apple'), (1, 'Banana'), (1, 'Coconut'),
856                          (2, 'Apple'), (2, 'Banana'), (2, 'Coconut'),
857                          (3, 'Apple'), (3, 'Banana'), (3, 'Coconut'),
858                          (4, 'Apple'), (4, 'Banana'), (4, 'Coconut'),
859                          (5, 'Apple'), (5, 'Banana'), (5, 'Coconut')])
860        self.assertEqual([(i, s) for i in nums for s in [f for f in strs if "n" in f]],
861                         [(1, 'Banana'), (1, 'Coconut'), (2, 'Banana'), (2, 'Coconut'),
862                          (3, 'Banana'), (3, 'Coconut'), (4, 'Banana'), (4, 'Coconut'),
863                          (5, 'Banana'), (5, 'Coconut')])
864        self.assertEqual([(lambda a:[a**i for i in range(a+1)])(j) for j in range(5)],
865                         [[1], [1, 1], [1, 2, 4], [1, 3, 9, 27], [1, 4, 16, 64, 256]])
866
867        def test_in_func(l):
868            return [None < x < 3 for x in l if x > 2]
869
870        self.assertEqual(test_in_func(nums), [False, False, False])
871
872        def test_nested_front():
873            self.assertEqual([[y for y in [x, x + 1]] for x in [1,3,5]],
874                             [[1, 2], [3, 4], [5, 6]])
875
876        test_nested_front()
877
878        check_syntax_error(self, "[i, s for i in nums for s in strs]")
879        check_syntax_error(self, "[x if y]")
880
881        suppliers = [
882          (1, "Boeing"),
883          (2, "Ford"),
884          (3, "Macdonalds")
885        ]
886
887        parts = [
888          (10, "Airliner"),
889          (20, "Engine"),
890          (30, "Cheeseburger")
891        ]
892
893        suppart = [
894          (1, 10), (1, 20), (2, 20), (3, 30)
895        ]
896
897        x = [
898          (sname, pname)
899            for (sno, sname) in suppliers
900              for (pno, pname) in parts
901                for (sp_sno, sp_pno) in suppart
902                  if sno == sp_sno and pno == sp_pno
903        ]
904
905        self.assertEqual(x, [('Boeing', 'Airliner'), ('Boeing', 'Engine'), ('Ford', 'Engine'),
906                             ('Macdonalds', 'Cheeseburger')])
907
908    def testGenexps(self):
909        # generator expression tests
910        g = ([x for x in range(10)] for x in range(1))
911        self.assertEqual(g.next(), [x for x in range(10)])
912        try:
913            g.next()
914            self.fail('should produce StopIteration exception')
915        except StopIteration:
916            pass
917
918        a = 1
919        try:
920            g = (a for d in a)
921            g.next()
922            self.fail('should produce TypeError')
923        except TypeError:
924            pass
925
926        self.assertEqual(list((x, y) for x in 'abcd' for y in 'abcd'), [(x, y) for x in 'abcd' for y in 'abcd'])
927        self.assertEqual(list((x, y) for x in 'ab' for y in 'xy'), [(x, y) for x in 'ab' for y in 'xy'])
928
929        a = [x for x in range(10)]
930        b = (x for x in (y for y in a))
931        self.assertEqual(sum(b), sum([x for x in range(10)]))
932
933        self.assertEqual(sum(x**2 for x in range(10)), sum([x**2 for x in range(10)]))
934        self.assertEqual(sum(x*x for x in range(10) if x%2), sum([x*x for x in range(10) if x%2]))
935        self.assertEqual(sum(x for x in (y for y in range(10))), sum([x for x in range(10)]))
936        self.assertEqual(sum(x for x in (y for y in (z for z in range(10)))), sum([x for x in range(10)]))
937        self.assertEqual(sum(x for x in [y for y in (z for z in range(10))]), sum([x for x in range(10)]))
938        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)]))
939        self.assertEqual(sum(x for x in (y for y in (z for z in range(10) if True) if False) if True), 0)
940        check_syntax_error(self, "foo(x for x in range(10), 100)")
941        check_syntax_error(self, "foo(100, x for x in range(10))")
942
943    def testComprehensionSpecials(self):
944        # test for outmost iterable precomputation
945        x = 10; g = (i for i in range(x)); x = 5
946        self.assertEqual(len(list(g)), 10)
947
948        # This should hold, since we're only precomputing outmost iterable.
949        x = 10; t = False; g = ((i,j) for i in range(x) if t for j in range(x))
950        x = 5; t = True;
951        self.assertEqual([(i,j) for i in range(10) for j in range(5)], list(g))
952
953        # Grammar allows multiple adjacent 'if's in listcomps and genexps,
954        # even though it's silly. Make sure it works (ifelse broke this.)
955        self.assertEqual([ x for x in range(10) if x % 2 if x % 3 ], [1, 5, 7])
956        self.assertEqual(list(x for x in range(10) if x % 2 if x % 3), [1, 5, 7])
957
958        # verify unpacking single element tuples in listcomp/genexp.
959        self.assertEqual([x for x, in [(4,), (5,), (6,)]], [4, 5, 6])
960        self.assertEqual(list(x for x, in [(7,), (8,), (9,)]), [7, 8, 9])
961
962    def test_with_statement(self):
963        class manager(object):
964            def __enter__(self):
965                return (1, 2)
966            def __exit__(self, *args):
967                pass
968
969        with manager():
970            pass
971        with manager() as x:
972            pass
973        with manager() as (x, y):
974            pass
975        with manager(), manager():
976            pass
977        with manager() as x, manager() as y:
978            pass
979        with manager() as x, manager():
980            pass
981
982    def testIfElseExpr(self):
983        # Test ifelse expressions in various cases
984        def _checkeval(msg, ret):
985            "helper to check that evaluation of expressions is done correctly"
986            print x
987            return ret
988
989        self.assertEqual([ x() for x in lambda: True, lambda: False if x() ], [True])
990        self.assertEqual([ x() for x in (lambda: True, lambda: False) if x() ], [True])
991        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])
992        self.assertEqual((5 if 1 else _checkeval("check 1", 0)), 5)
993        self.assertEqual((_checkeval("check 2", 0) if 0 else 5), 5)
994        self.assertEqual((5 and 6 if 0 else 1), 1)
995        self.assertEqual(((5 and 6) if 0 else 1), 1)
996        self.assertEqual((5 and (6 if 1 else 1)), 6)
997        self.assertEqual((0 or _checkeval("check 3", 2) if 0 else 3), 3)
998        self.assertEqual((1 or _checkeval("check 4", 2) if 1 else _checkeval("check 5", 3)), 1)
999        self.assertEqual((0 or 5 if 1 else _checkeval("check 6", 3)), 5)
1000        self.assertEqual((not 5 if 1 else 1), False)
1001        self.assertEqual((not 5 if 0 else 1), 1)
1002        self.assertEqual((6 + 1 if 1 else 2), 7)
1003        self.assertEqual((6 - 1 if 1 else 2), 5)
1004        self.assertEqual((6 * 2 if 1 else 4), 12)
1005        self.assertEqual((6 / 2 if 1 else 3), 3)
1006        self.assertEqual((6 < 4 if 0 else 2), 2)
1007
1008    def test_paren_evaluation(self):
1009        self.assertEqual(16 // (4 // 2), 8)
1010        self.assertEqual((16 // 4) // 2, 2)
1011        self.assertEqual(16 // 4 // 2, 2)
1012        self.assertTrue(False is (2 is 3))
1013        self.assertFalse((False is 2) is 3)
1014        self.assertFalse(False is 2 is 3)
1015
1016
1017def test_main():
1018    with check_py3k_warnings(
1019            ("backquote not supported", SyntaxWarning),
1020            ("tuple parameter unpacking has been removed", SyntaxWarning),
1021            ("parenthesized argument names are invalid", SyntaxWarning),
1022            ("classic int division", DeprecationWarning),
1023            (".+ not supported in 3.x", DeprecationWarning)):
1024        run_unittest(TokenTests, GrammarTests)
1025
1026if __name__ == '__main__':
1027    test_main()
1028