PageRenderTime 166ms CodeModel.GetById 99ms app.highlight 58ms RepoModel.GetById 1ms app.codeStats 1ms

/Lib/test/test_grammar.py

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