CoolFace
Apppublic

aravagarwal/CodeCloakPII

sourceHugging Faceupdated 3y agoView on Hugging Face
0likes
python3-grammar-crlf.py946 linesDownload Raw Back to examples
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, you5# get warnings about certain hex/oct constants.  Since those are6# issued by the parser, you can't suppress them by adding a7# filterwarnings() call to this module.  Therefore, to shut up the8# regression test, the filterwarnings() call has been added to9# regrtest.py.10 11from test.support import run_unittest, check_syntax_error12import unittest13import sys14# testing import *15from sys import *16 17class TokenTests(unittest.TestCase):18 19    def testBackslash(self):20        # Backslash means line continuation:21        x = 1 \22        + 123        self.assertEquals(x, 2, 'backslash for line continuation')24 25        # Backslash does not means continuation in comments :\26        x = 027        self.assertEquals(x, 0, 'backslash ending comment')28 29    def testPlainIntegers(self):30        self.assertEquals(type(000), type(0))31        self.assertEquals(0xff, 255)32        self.assertEquals(0o377, 255)33        self.assertEquals(2147483647, 0o17777777777)34        self.assertEquals(0b1001, 9)35        # "0x" is not a valid literal36        self.assertRaises(SyntaxError, eval, "0x")37        from sys import maxsize38        if maxsize == 2147483647:39            self.assertEquals(-2147483647-1, -0o20000000000)40            # XXX -214748364841            self.assert_(0o37777777777 > 0)42            self.assert_(0xffffffff > 0)43            self.assert_(0b1111111111111111111111111111111 > 0)44            for s in ('2147483648', '0o40000000000', '0x100000000',45                      '0b10000000000000000000000000000000'):46                try:47                    x = eval(s)48                except OverflowError:49                    self.fail("OverflowError on huge integer literal %r" % s)50        elif maxsize == 9223372036854775807:51            self.assertEquals(-9223372036854775807-1, -0o1000000000000000000000)52            self.assert_(0o1777777777777777777777 > 0)53            self.assert_(0xffffffffffffffff > 0)54            self.assert_(0b11111111111111111111111111111111111111111111111111111111111111 > 0)55            for s in '9223372036854775808', '0o2000000000000000000000', \56                     '0x10000000000000000', \57                     '0b100000000000000000000000000000000000000000000000000000000000000':58                try:59                    x = eval(s)60                except OverflowError:61                    self.fail("OverflowError on huge integer literal %r" % s)62        else:63            self.fail('Weird maxsize value %r' % maxsize)64 65    def testLongIntegers(self):66        x = 067        x = 0xffffffffffffffff68        x = 0Xffffffffffffffff69        x = 0o7777777777777777770        x = 0O7777777777777777771        x = 12345678901234567890123456789072        x = 0b10000000000000000000000000000000000000000000000000000000000000000000073        x = 0B11111111111111111111111111111111111111111111111111111111111111111111174 75    def testUnderscoresInNumbers(self):76        # Integers77        x = 1_078        x = 123_456_7_8979        x = 0xabc_123_4_580        x = 0X_abc_12381        x = 0B11_0182        x = 0b_11_0183        x = 0o45_6784        x = 0O_45_6785 86        # Floats87        x = 3_1.488        x = 03_1.489        x = 3_1.90        x = .3_191        x = 3.1_492        x = 0_3.1_493        x = 3e1_494        x = 3_1e+4_195        x = 3_1E-4_196 97    def testFloats(self):98        x = 3.1499        x = 314.100        x = 0.314101        # XXX x = 000.314102        x = .314103        x = 3e14104        x = 3E14105        x = 3e-14106        x = 3e+14107        x = 3.e14108        x = .3e14109        x = 3.1e4110 111    def testEllipsis(self):112        x = ...113        self.assert_(x is Ellipsis)114        self.assertRaises(SyntaxError, eval, ".. .")115 116class GrammarTests(unittest.TestCase):117 118    # single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE119    # XXX can't test in a script -- this rule is only used when interactive120 121    # file_input: (NEWLINE | stmt)* ENDMARKER122    # Being tested as this very moment this very module123 124    # expr_input: testlist NEWLINE125    # XXX Hard to test -- used only in calls to input()126 127    def testEvalInput(self):128        # testlist ENDMARKER129        x = eval('1, 0 or 1')130 131    def testFuncdef(self):132        ### [decorators] 'def' NAME parameters ['->' test] ':' suite133        ### decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE134        ### decorators: decorator+135        ### parameters: '(' [typedargslist] ')'136        ### typedargslist: ((tfpdef ['=' test] ',')*137        ###                ('*' [tfpdef] (',' tfpdef ['=' test])* [',' '**' tfpdef] | '**' tfpdef)138        ###                | tfpdef ['=' test] (',' tfpdef ['=' test])* [','])139        ### tfpdef: NAME [':' test]140        ### varargslist: ((vfpdef ['=' test] ',')*141        ###              ('*' [vfpdef] (',' vfpdef ['=' test])*  [',' '**' vfpdef] | '**' vfpdef)142        ###              | vfpdef ['=' test] (',' vfpdef ['=' test])* [','])143        ### vfpdef: NAME144        def f1(): pass145        f1()146        f1(*())147        f1(*(), **{})148        def f2(one_argument): pass149        def f3(two, arguments): pass150        self.assertEquals(f2.__code__.co_varnames, ('one_argument',))151        self.assertEquals(f3.__code__.co_varnames, ('two', 'arguments'))152        def a1(one_arg,): pass153        def a2(two, args,): pass154        def v0(*rest): pass155        def v1(a, *rest): pass156        def v2(a, b, *rest): pass157 158        f1()159        f2(1)160        f2(1,)161        f3(1, 2)162        f3(1, 2,)163        v0()164        v0(1)165        v0(1,)166        v0(1,2)167        v0(1,2,3,4,5,6,7,8,9,0)168        v1(1)169        v1(1,)170        v1(1,2)171        v1(1,2,3)172        v1(1,2,3,4,5,6,7,8,9,0)173        v2(1,2)174        v2(1,2,3)175        v2(1,2,3,4)176        v2(1,2,3,4,5,6,7,8,9,0)177 178        def d01(a=1): pass179        d01()180        d01(1)181        d01(*(1,))182        d01(**{'a':2})183        def d11(a, b=1): pass184        d11(1)185        d11(1, 2)186        d11(1, **{'b':2})187        def d21(a, b, c=1): pass188        d21(1, 2)189        d21(1, 2, 3)190        d21(*(1, 2, 3))191        d21(1, *(2, 3))192        d21(1, 2, *(3,))193        d21(1, 2, **{'c':3})194        def d02(a=1, b=2): pass195        d02()196        d02(1)197        d02(1, 2)198        d02(*(1, 2))199        d02(1, *(2,))200        d02(1, **{'b':2})201        d02(**{'a': 1, 'b': 2})202        def d12(a, b=1, c=2): pass203        d12(1)204        d12(1, 2)205        d12(1, 2, 3)206        def d22(a, b, c=1, d=2): pass207        d22(1, 2)208        d22(1, 2, 3)209        d22(1, 2, 3, 4)210        def d01v(a=1, *rest): pass211        d01v()212        d01v(1)213        d01v(1, 2)214        d01v(*(1, 2, 3, 4))215        d01v(*(1,))216        d01v(**{'a':2})217        def d11v(a, b=1, *rest): pass218        d11v(1)219        d11v(1, 2)220        d11v(1, 2, 3)221        def d21v(a, b, c=1, *rest): pass222        d21v(1, 2)223        d21v(1, 2, 3)224        d21v(1, 2, 3, 4)225        d21v(*(1, 2, 3, 4))226        d21v(1, 2, **{'c': 3})227        def d02v(a=1, b=2, *rest): pass228        d02v()229        d02v(1)230        d02v(1, 2)231        d02v(1, 2, 3)232        d02v(1, *(2, 3, 4))233        d02v(**{'a': 1, 'b': 2})234        def d12v(a, b=1, c=2, *rest): pass235        d12v(1)236        d12v(1, 2)237        d12v(1, 2, 3)238        d12v(1, 2, 3, 4)239        d12v(*(1, 2, 3, 4))240        d12v(1, 2, *(3, 4, 5))241        d12v(1, *(2,), **{'c': 3})242        def d22v(a, b, c=1, d=2, *rest): pass243        d22v(1, 2)244        d22v(1, 2, 3)245        d22v(1, 2, 3, 4)246        d22v(1, 2, 3, 4, 5)247        d22v(*(1, 2, 3, 4))248        d22v(1, 2, *(3, 4, 5))249        d22v(1, *(2, 3), **{'d': 4})250 251        # keyword argument type tests252        try:253            str('x', **{b'foo':1 })254        except TypeError:255            pass256        else:257            self.fail('Bytes should not work as keyword argument names')258        # keyword only argument tests259        def pos0key1(*, key): return key260        pos0key1(key=100)261        def pos2key2(p1, p2, *, k1, k2=100): return p1,p2,k1,k2262        pos2key2(1, 2, k1=100)263        pos2key2(1, 2, k1=100, k2=200)264        pos2key2(1, 2, k2=100, k1=200)265        def pos2key2dict(p1, p2, *, k1=100, k2, **kwarg): return p1,p2,k1,k2,kwarg266        pos2key2dict(1,2,k2=100,tokwarg1=100,tokwarg2=200)267        pos2key2dict(1,2,tokwarg1=100,tokwarg2=200, k2=100)268 269        # keyword arguments after *arglist270        def f(*args, **kwargs):271            return args, kwargs272        self.assertEquals(f(1, x=2, *[3, 4], y=5), ((1, 3, 4),273                                                    {'x':2, 'y':5}))274        self.assertRaises(SyntaxError, eval, "f(1, *(2,3), 4)")275        self.assertRaises(SyntaxError, eval, "f(1, x=2, *(3,4), x=5)")276 277        # argument annotation tests278        def f(x) -> list: pass279        self.assertEquals(f.__annotations__, {'return': list})280        def f(x:int): pass281        self.assertEquals(f.__annotations__, {'x': int})282        def f(*x:str): pass283        self.assertEquals(f.__annotations__, {'x': str})284        def f(**x:float): pass285        self.assertEquals(f.__annotations__, {'x': float})286        def f(x, y:1+2): pass287        self.assertEquals(f.__annotations__, {'y': 3})288        def f(a, b:1, c:2, d): pass289        self.assertEquals(f.__annotations__, {'b': 1, 'c': 2})290        def f(a, b:1, c:2, d, e:3=4, f=5, *g:6): pass291        self.assertEquals(f.__annotations__,292                          {'b': 1, 'c': 2, 'e': 3, 'g': 6})293        def f(a, b:1, c:2, d, e:3=4, f=5, *g:6, h:7, i=8, j:9=10,294              **k:11) -> 12: pass295        self.assertEquals(f.__annotations__,296                          {'b': 1, 'c': 2, 'e': 3, 'g': 6, 'h': 7, 'j': 9,297                           'k': 11, 'return': 12})298        # Check for SF Bug #1697248 - mixing decorators and a return annotation299        def null(x): return x300        @null301        def f(x) -> list: pass302        self.assertEquals(f.__annotations__, {'return': list})303 304        # test closures with a variety of oparg's305        closure = 1306        def f(): return closure307        def f(x=1): return closure308        def f(*, k=1): return closure309        def f() -> int: return closure310 311        # Check ast errors in *args and *kwargs312        check_syntax_error(self, "f(*g(1=2))")313        check_syntax_error(self, "f(**g(1=2))")314 315    def testLambdef(self):316        ### lambdef: 'lambda' [varargslist] ':' test317        l1 = lambda : 0318        self.assertEquals(l1(), 0)319        l2 = lambda : a[d] # XXX just testing the expression320        l3 = lambda : [2 < x for x in [-1, 3, 0]]321        self.assertEquals(l3(), [0, 1, 0])322        l4 = lambda x = lambda y = lambda z=1 : z : y() : x()323        self.assertEquals(l4(), 1)324        l5 = lambda x, y, z=2: x + y + z325        self.assertEquals(l5(1, 2), 5)326        self.assertEquals(l5(1, 2, 3), 6)327        check_syntax_error(self, "lambda x: x = 2")328        check_syntax_error(self, "lambda (None,): None")329        l6 = lambda x, y, *, k=20: x+y+k330        self.assertEquals(l6(1,2), 1+2+20)331        self.assertEquals(l6(1,2,k=10), 1+2+10)332 333 334    ### stmt: simple_stmt | compound_stmt335    # Tested below336 337    def testSimpleStmt(self):338        ### simple_stmt: small_stmt (';' small_stmt)* [';']339        x = 1; pass; del x340        def foo():341            # verify statements that end with semi-colons342            x = 1; pass; del x;343        foo()344 345    ### small_stmt: expr_stmt | pass_stmt | del_stmt | flow_stmt | import_stmt | global_stmt | access_stmt346    # Tested below347 348    def testExprStmt(self):349        # (exprlist '=')* exprlist350        1351        1, 2, 3352        x = 1353        x = 1, 2, 3354        x = y = z = 1, 2, 3355        x, y, z = 1, 2, 3356        abc = a, b, c = x, y, z = xyz = 1, 2, (3, 4)357 358        check_syntax_error(self, "x + 1 = 1")359        check_syntax_error(self, "a + 1 = b + 2")360 361    def testDelStmt(self):362        # 'del' exprlist363        abc = [1,2,3]364        x, y, z = abc365        xyz = x, y, z366 367        del abc368        del x, y, (z, xyz)369 370    def testPassStmt(self):371        # 'pass'372        pass373 374    # flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt375    # Tested below376 377    def testBreakStmt(self):378        # 'break'379        while 1: break380 381    def testContinueStmt(self):382        # 'continue'383        i = 1384        while i: i = 0; continue385 386        msg = ""387        while not msg:388            msg = "ok"389            try:390                continue391                msg = "continue failed to continue inside try"392            except:393                msg = "continue inside try called except block"394        if msg != "ok":395            self.fail(msg)396 397        msg = ""398        while not msg:399            msg = "finally block not called"400            try:401                continue402            finally:403                msg = "ok"404        if msg != "ok":405            self.fail(msg)406 407    def test_break_continue_loop(self):408        # This test warrants an explanation. It is a test specifically for SF bugs409        # #463359 and #462937. The bug is that a 'break' statement executed or410        # exception raised inside a try/except inside a loop, *after* a continue411        # statement has been executed in that loop, will cause the wrong number of412        # arguments to be popped off the stack and the instruction pointer reset to413        # a very small number (usually 0.) Because of this, the following test414        # *must* written as a function, and the tracking vars *must* be function415        # arguments with default values. Otherwise, the test will loop and loop.416 417        def test_inner(extra_burning_oil = 1, count=0):418            big_hippo = 2419            while big_hippo:420                count += 1421                try:422                    if extra_burning_oil and big_hippo == 1:423                        extra_burning_oil -= 1424                        break425                    big_hippo -= 1426                    continue427                except:428                    raise429            if count > 2 or big_hippo != 1:430                self.fail("continue then break in try/except in loop broken!")431        test_inner()432 433    def testReturn(self):434        # 'return' [testlist]435        def g1(): return436        def g2(): return 1437        g1()438        x = g2()439        check_syntax_error(self, "class foo:return 1")440 441    def testYield(self):442        check_syntax_error(self, "class foo:yield 1")443 444    def testRaise(self):445        # 'raise' test [',' test]446        try: raise RuntimeError('just testing')447        except RuntimeError: pass448        try: raise KeyboardInterrupt449        except KeyboardInterrupt: pass450 451    def testImport(self):452        # 'import' dotted_as_names453        import sys454        import time, sys455        # 'from' dotted_name 'import' ('*' | '(' import_as_names ')' | import_as_names)456        from time import time457        from time import (time)458        # not testable inside a function, but already done at top of the module459        # from sys import *460        from sys import path, argv461        from sys import (path, argv)462        from sys import (path, argv,)463 464    def testGlobal(self):465        # 'global' NAME (',' NAME)*466        global a467        global a, b468        global one, two, three, four, five, six, seven, eight, nine, ten469 470    def testNonlocal(self):471        # 'nonlocal' NAME (',' NAME)*472        x = 0473        y = 0474        def f():475            nonlocal x476            nonlocal x, y477 478    def testAssert(self):479        # assert_stmt: 'assert' test [',' test]480        assert 1481        assert 1, 1482        assert lambda x:x483        assert 1, lambda x:x+1484        try:485            assert 0, "msg"486        except AssertionError as e:487            self.assertEquals(e.args[0], "msg")488        else:489            if __debug__:490                self.fail("AssertionError not raised by assert 0")491 492    ### compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef493    # Tested below494 495    def testIf(self):496        # 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]497        if 1: pass498        if 1: pass499        else: pass500        if 0: pass501        elif 0: pass502        if 0: pass503        elif 0: pass504        elif 0: pass505        elif 0: pass506        else: pass507 508    def testWhile(self):509        # 'while' test ':' suite ['else' ':' suite]510        while 0: pass511        while 0: pass512        else: pass513 514        # Issue1920: "while 0" is optimized away,515        # ensure that the "else" clause is still present.516        x = 0517        while 0:518            x = 1519        else:520            x = 2521        self.assertEquals(x, 2)522 523    def testFor(self):524        # 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite]525        for i in 1, 2, 3: pass526        for i, j, k in (): pass527        else: pass528        class Squares:529            def __init__(self, max):530                self.max = max531                self.sofar = []532            def __len__(self): return len(self.sofar)533            def __getitem__(self, i):534                if not 0 <= i < self.max: raise IndexError535                n = len(self.sofar)536                while n <= i:537                    self.sofar.append(n*n)538                    n = n+1539                return self.sofar[i]540        n = 0541        for x in Squares(10): n = n+x542        if n != 285:543            self.fail('for over growing sequence')544 545        result = []546        for x, in [(1,), (2,), (3,)]:547            result.append(x)548        self.assertEqual(result, [1, 2, 3])549 550    def testTry(self):551        ### try_stmt: 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]552        ###         | 'try' ':' suite 'finally' ':' suite553        ### except_clause: 'except' [expr ['as' expr]]554        try:555            1/0556        except ZeroDivisionError:557            pass558        else:559            pass560        try: 1/0561        except EOFError: pass562        except TypeError as msg: pass563        except RuntimeError as msg: pass564        except: pass565        else: pass566        try: 1/0567        except (EOFError, TypeError, ZeroDivisionError): pass568        try: 1/0569        except (EOFError, TypeError, ZeroDivisionError) as msg: pass570        try: pass571        finally: pass572 573    def testSuite(self):574        # simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT575        if 1: pass576        if 1:577            pass578        if 1:579            #580            #581            #582            pass583            pass584            #585            pass586            #587 588    def testTest(self):589        ### and_test ('or' and_test)*590        ### and_test: not_test ('and' not_test)*591        ### not_test: 'not' not_test | comparison592        if not 1: pass593        if 1 and 1: pass594        if 1 or 1: pass595        if not not not 1: pass596        if not 1 and 1 and 1: pass597        if 1 and 1 or 1 and 1 and 1 or not 1 and 1: pass598 599    def testComparison(self):600        ### comparison: expr (comp_op expr)*601        ### comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'|'is' 'not'602        if 1: pass603        x = (1 == 1)604        if 1 == 1: pass605        if 1 != 1: pass606        if 1 < 1: pass607        if 1 > 1: pass608        if 1 <= 1: pass609        if 1 >= 1: pass610        if 1 is 1: pass611        if 1 is not 1: pass612        if 1 in (): pass613        if 1 not in (): pass614        if 1 < 1 > 1 == 1 >= 1 <= 1 != 1 in 1 not in 1 is 1 is not 1: pass615 616    def testBinaryMaskOps(self):617        x = 1 & 1618        x = 1 ^ 1619        x = 1 | 1620 621    def testShiftOps(self):622        x = 1 << 1623        x = 1 >> 1624        x = 1 << 1 >> 1625 626    def testAdditiveOps(self):627        x = 1628        x = 1 + 1629        x = 1 - 1 - 1630        x = 1 - 1 + 1 - 1 + 1631 632    def testMultiplicativeOps(self):633        x = 1 * 1634        x = 1 / 1635        x = 1 % 1636        x = 1 / 1 * 1 % 1637 638    def testUnaryOps(self):639        x = +1640        x = -1641        x = ~1642        x = ~1 ^ 1 & 1 | 1 & 1 ^ -1643        x = -1*1/1 + 1*1 - ---1*1644 645    def testSelectors(self):646        ### trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME647        ### subscript: expr | [expr] ':' [expr]648 649        import sys, time650        c = sys.path[0]651        x = time.time()652        x = sys.modules['time'].time()653        a = '01234'654        c = a[0]655        c = a[-1]656        s = a[0:5]657        s = a[:5]658        s = a[0:]659        s = a[:]660        s = a[-5:]661        s = a[:-1]662        s = a[-4:-3]663        # A rough test of SF bug 1333982.  http://python.org/sf/1333982664        # The testing here is fairly incomplete.665        # Test cases should include: commas with 1 and 2 colons666        d = {}667        d[1] = 1668        d[1,] = 2669        d[1,2] = 3670        d[1,2,3] = 4671        L = list(d)672        L.sort(key=lambda x: x if isinstance(x, tuple) else ())673        self.assertEquals(str(L), '[1, (1,), (1, 2), (1, 2, 3)]')674 675    def testAtoms(self):676        ### atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictsetmaker] '}' | NAME | NUMBER | STRING677        ### dictsetmaker: (test ':' test (',' test ':' test)* [',']) | (test (',' test)* [','])678 679        x = (1)680        x = (1 or 2 or 3)681        x = (1 or 2 or 3, 2, 3)682 683        x = []684        x = [1]685        x = [1 or 2 or 3]686        x = [1 or 2 or 3, 2, 3]687        x = []688 689        x = {}690        x = {'one': 1}691        x = {'one': 1,}692        x = {'one' or 'two': 1 or 2}693        x = {'one': 1, 'two': 2}694        x = {'one': 1, 'two': 2,}695        x = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6}696 697        x = {'one'}698        x = {'one', 1,}699        x = {'one', 'two', 'three'}700        x = {2, 3, 4,}701 702        x = x703        x = 'x'704        x = 123705 706    ### exprlist: expr (',' expr)* [',']707    ### testlist: test (',' test)* [',']708    # These have been exercised enough above709 710    def testClassdef(self):711        # 'class' NAME ['(' [testlist] ')'] ':' suite712        class B: pass713        class B2(): pass714        class C1(B): pass715        class C2(B): pass716        class D(C1, C2, B): pass717        class C:718            def meth1(self): pass719            def meth2(self, arg): pass720            def meth3(self, a1, a2): pass721 722        # decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE723        # decorators: decorator+724        # decorated: decorators (classdef | funcdef)725        def class_decorator(x): return x726        @class_decorator727        class G: pass728 729    def testDictcomps(self):730        # dictorsetmaker: ( (test ':' test (comp_for |731        #                                   (',' test ':' test)* [','])) |732        #                   (test (comp_for | (',' test)* [','])) )733        nums = [1, 2, 3]734        self.assertEqual({i:i+1 for i in nums}, {1: 2, 2: 3, 3: 4})735 736    def testListcomps(self):737        # list comprehension tests738        nums = [1, 2, 3, 4, 5]739        strs = ["Apple", "Banana", "Coconut"]740        spcs = ["  Apple", " Banana ", "Coco  nut  "]741 742        self.assertEqual([s.strip() for s in spcs], ['Apple', 'Banana', 'Coco  nut'])743        self.assertEqual([3 * x for x in nums], [3, 6, 9, 12, 15])744        self.assertEqual([x for x in nums if x > 2], [3, 4, 5])745        self.assertEqual([(i, s) for i in nums for s in strs],746                         [(1, 'Apple'), (1, 'Banana'), (1, 'Coconut'),747                          (2, 'Apple'), (2, 'Banana'), (2, 'Coconut'),748                          (3, 'Apple'), (3, 'Banana'), (3, 'Coconut'),749                          (4, 'Apple'), (4, 'Banana'), (4, 'Coconut'),750                          (5, 'Apple'), (5, 'Banana'), (5, 'Coconut')])751        self.assertEqual([(i, s) for i in nums for s in [f for f in strs if "n" in f]],752                         [(1, 'Banana'), (1, 'Coconut'), (2, 'Banana'), (2, 'Coconut'),753                          (3, 'Banana'), (3, 'Coconut'), (4, 'Banana'), (4, 'Coconut'),754                          (5, 'Banana'), (5, 'Coconut')])755        self.assertEqual([(lambda a:[a**i for i in range(a+1)])(j) for j in range(5)],756                         [[1], [1, 1], [1, 2, 4], [1, 3, 9, 27], [1, 4, 16, 64, 256]])757 758        def test_in_func(l):759            return [0 < x < 3 for x in l if x > 2]760 761        self.assertEqual(test_in_func(nums), [False, False, False])762 763        def test_nested_front():764            self.assertEqual([[y for y in [x, x + 1]] for x in [1,3,5]],765                             [[1, 2], [3, 4], [5, 6]])766 767        test_nested_front()768 769        check_syntax_error(self, "[i, s for i in nums for s in strs]")770        check_syntax_error(self, "[x if y]")771 772        suppliers = [773          (1, "Boeing"),774          (2, "Ford"),775          (3, "Macdonalds")776        ]777 778        parts = [779          (10, "Airliner"),780          (20, "Engine"),781          (30, "Cheeseburger")782        ]783 784        suppart = [785          (1, 10), (1, 20), (2, 20), (3, 30)786        ]787 788        x = [789          (sname, pname)790            for (sno, sname) in suppliers791              for (pno, pname) in parts792                for (sp_sno, sp_pno) in suppart793                  if sno == sp_sno and pno == sp_pno794        ]795 796        self.assertEqual(x, [('Boeing', 'Airliner'), ('Boeing', 'Engine'), ('Ford', 'Engine'),797                             ('Macdonalds', 'Cheeseburger')])798 799    def testGenexps(self):800        # generator expression tests801        g = ([x for x in range(10)] for x in range(1))802        self.assertEqual(next(g), [x for x in range(10)])803        try:804            next(g)805            self.fail('should produce StopIteration exception')806        except StopIteration:807            pass808 809        a = 1810        try:811            g = (a for d in a)812            next(g)813            self.fail('should produce TypeError')814        except TypeError:815            pass816 817        self.assertEqual(list((x, y) for x in 'abcd' for y in 'abcd'), [(x, y) for x in 'abcd' for y in 'abcd'])818        self.assertEqual(list((x, y) for x in 'ab' for y in 'xy'), [(x, y) for x in 'ab' for y in 'xy'])819 820        a = [x for x in range(10)]821        b = (x for x in (y for y in a))822        self.assertEqual(sum(b), sum([x for x in range(10)]))823 824        self.assertEqual(sum(x**2 for x in range(10)), sum([x**2 for x in range(10)]))825        self.assertEqual(sum(x*x for x in range(10) if x%2), sum([x*x for x in range(10) if x%2]))826        self.assertEqual(sum(x for x in (y for y in range(10))), sum([x for x in range(10)]))827        self.assertEqual(sum(x for x in (y for y in (z for z in range(10)))), sum([x for x in range(10)]))828        self.assertEqual(sum(x for x in [y for y in (z for z in range(10))]), sum([x for x in range(10)]))829        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)]))830        self.assertEqual(sum(x for x in (y for y in (z for z in range(10) if True) if False) if True), 0)831        check_syntax_error(self, "foo(x for x in range(10), 100)")832        check_syntax_error(self, "foo(100, x for x in range(10))")833 834    def testComprehensionSpecials(self):835        # test for outmost iterable precomputation836        x = 10; g = (i for i in range(x)); x = 5837        self.assertEqual(len(list(g)), 10)838 839        # This should hold, since we're only precomputing outmost iterable.840        x = 10; t = False; g = ((i,j) for i in range(x) if t for j in range(x))841        x = 5; t = True;842        self.assertEqual([(i,j) for i in range(10) for j in range(5)], list(g))843 844        # Grammar allows multiple adjacent 'if's in listcomps and genexps,845        # even though it's silly. Make sure it works (ifelse broke this.)846        self.assertEqual([ x for x in range(10) if x % 2 if x % 3 ], [1, 5, 7])847        self.assertEqual(list(x for x in range(10) if x % 2 if x % 3), [1, 5, 7])848 849        # verify unpacking single element tuples in listcomp/genexp.850        self.assertEqual([x for x, in [(4,), (5,), (6,)]], [4, 5, 6])851        self.assertEqual(list(x for x, in [(7,), (8,), (9,)]), [7, 8, 9])852 853    def test_with_statement(self):854        class manager(object):855            def __enter__(self):856                return (1, 2)857            def __exit__(self, *args):858                pass859 860        with manager():861            pass862        with manager() as x:863            pass864        with manager() as (x, y):865            pass866        with manager(), manager():867            pass868        with manager() as x, manager() as y:869            pass870        with manager() as x, manager():871            pass872 873    def testIfElseExpr(self):874        # Test ifelse expressions in various cases875        def _checkeval(msg, ret):876            "helper to check that evaluation of expressions is done correctly"877            print(x)878            return ret879 880        # the next line is not allowed anymore881        #self.assertEqual([ x() for x in lambda: True, lambda: False if x() ], [True])882        self.assertEqual([ x() for x in (lambda: True, lambda: False) if x() ], [True])883        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])884        self.assertEqual((5 if 1 else _checkeval("check 1", 0)), 5)885        self.assertEqual((_checkeval("check 2", 0) if 0 else 5), 5)886        self.assertEqual((5 and 6 if 0 else 1), 1)887        self.assertEqual(((5 and 6) if 0 else 1), 1)888        self.assertEqual((5 and (6 if 1 else 1)), 6)889        self.assertEqual((0 or _checkeval("check 3", 2) if 0 else 3), 3)890        self.assertEqual((1 or _checkeval("check 4", 2) if 1 else _checkeval("check 5", 3)), 1)891        self.assertEqual((0 or 5 if 1 else _checkeval("check 6", 3)), 5)892        self.assertEqual((not 5 if 1 else 1), False)893        self.assertEqual((not 5 if 0 else 1), 1)894        self.assertEqual((6 + 1 if 1 else 2), 7)895        self.assertEqual((6 - 1 if 1 else 2), 5)896        self.assertEqual((6 * 2 if 1 else 4), 12)897        self.assertEqual((6 / 2 if 1 else 3), 3)898        self.assertEqual((6 < 4 if 0 else 2), 2)899 900    def testStringLiterals(self):901        x = ''; y = ""; self.assert_(len(x) == 0 and x == y)902        x = '\''; y = "'"; self.assert_(len(x) == 1 and x == y and ord(x) == 39)903        x = '"'; y = "\""; self.assert_(len(x) == 1 and x == y and ord(x) == 34)904        x = "doesn't \"shrink\" does it"905        y = 'doesn\'t "shrink" does it'906        self.assert_(len(x) == 24 and x == y)907        x = "does \"shrink\" doesn't it"908        y = 'does "shrink" doesn\'t it'909        self.assert_(len(x) == 24 and x == y)910        x = """911The "quick"912brown fox913jumps over914the 'lazy' dog.915"""916        y = '\nThe "quick"\nbrown fox\njumps over\nthe \'lazy\' dog.\n'917        self.assertEquals(x, y)918        y = '''919The "quick"920brown fox921jumps over922the 'lazy' dog.923'''924        self.assertEquals(x, y)925        y = "\n\926The \"quick\"\n\927brown fox\n\928jumps over\n\929the 'lazy' dog.\n\930"931        self.assertEquals(x, y)932        y = '\n\933The \"quick\"\n\934brown fox\n\935jumps over\n\936the \'lazy\' dog.\n\937'938        self.assertEquals(x, y)939 940 941def test_main():942    run_unittest(TokenTests, GrammarTests)943 944if __name__ == '__main__':945    test_main()946