aravagarwal/CodeCloakPII
0
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.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(0xff, 255)31 self.assertEquals(0377, 255)32 self.assertEquals(2147483647, 017777777777)33 # "0x" is not a valid literal34 self.assertRaises(SyntaxError, eval, "0x")35 from sys import maxint36 if maxint == 2147483647:37 self.assertEquals(-2147483647-1, -020000000000)38 # XXX -214748364839 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 = 0L61 x = 0l62 x = 0xffffffffffffffffL63 x = 0xffffffffffffffffl64 x = 077777777777777777L65 x = 077777777777777777l66 x = 123456789012345678901234567890L67 x = 123456789012345678901234567890l68 69 def testFloats(self):70 x = 3.1471 x = 314.72 x = 0.31473 # XXX x = 000.31474 x = .31475 x = 3e1476 x = 3E1477 x = 3e-1478 x = 3e+1479 x = 3.e1480 x = .3e1481 x = 3.1e482 83class GrammarTests(unittest.TestCase):84 85 # single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE86 # XXX can't test in a script -- this rule is only used when interactive87 88 # file_input: (NEWLINE | stmt)* ENDMARKER89 # Being tested as this very moment this very module90 91 # expr_input: testlist NEWLINE92 # XXX Hard to test -- used only in calls to input()93 94 def testEvalInput(self):95 # testlist ENDMARKER96 x = eval('1, 0 or 1')97 98 def testFuncdef(self):99 ### 'def' NAME parameters ':' suite100 ### parameters: '(' [varargslist] ')'101 ### varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' ('**'|'*' '*') NAME]102 ### | ('**'|'*' '*') NAME)103 ### | fpdef ['=' test] (',' fpdef ['=' test])* [',']104 ### fpdef: NAME | '(' fplist ')'105 ### fplist: fpdef (',' fpdef)* [',']106 ### arglist: (argument ',')* (argument | *' test [',' '**' test] | '**' test)107 ### argument: [test '='] test # Really [keyword '='] test108 def f1(): pass109 f1()110 f1(*())111 f1(*(), **{})112 def f2(one_argument): pass113 def f3(two, arguments): pass114 def f4(two, (compound, (argument, list))): pass115 def f5((compound, first), two): pass116 self.assertEquals(f2.func_code.co_varnames, ('one_argument',))117 self.assertEquals(f3.func_code.co_varnames, ('two', 'arguments'))118 if sys.platform.startswith('java'):119 self.assertEquals(f4.func_code.co_varnames,120 ('two', '(compound, (argument, list))', 'compound', 'argument',121 'list',))122 self.assertEquals(f5.func_code.co_varnames,123 ('(compound, first)', 'two', 'compound', 'first'))124 else:125 self.assertEquals(f4.func_code.co_varnames,126 ('two', '.1', 'compound', 'argument', 'list'))127 self.assertEquals(f5.func_code.co_varnames,128 ('.0', 'two', 'compound', 'first'))129 def a1(one_arg,): pass130 def a2(two, args,): pass131 def v0(*rest): pass132 def v1(a, *rest): pass133 def v2(a, b, *rest): pass134 def v3(a, (b, c), *rest): return a, b, c, rest135 136 f1()137 f2(1)138 f2(1,)139 f3(1, 2)140 f3(1, 2,)141 f4(1, (2, (3, 4)))142 v0()143 v0(1)144 v0(1,)145 v0(1,2)146 v0(1,2,3,4,5,6,7,8,9,0)147 v1(1)148 v1(1,)149 v1(1,2)150 v1(1,2,3)151 v1(1,2,3,4,5,6,7,8,9,0)152 v2(1,2)153 v2(1,2,3)154 v2(1,2,3,4)155 v2(1,2,3,4,5,6,7,8,9,0)156 v3(1,(2,3))157 v3(1,(2,3),4)158 v3(1,(2,3),4,5,6,7,8,9,0)159 160 # ceval unpacks the formal arguments into the first argcount names;161 # thus, the names nested inside tuples must appear after these names.162 if sys.platform.startswith('java'):163 self.assertEquals(v3.func_code.co_varnames, ('a', '(b, c)', 'rest', 'b', 'c'))164 else:165 self.assertEquals(v3.func_code.co_varnames, ('a', '.1', 'rest', 'b', 'c'))166 self.assertEquals(v3(1, (2, 3), 4), (1, 2, 3, (4,)))167 def d01(a=1): pass168 d01()169 d01(1)170 d01(*(1,))171 d01(**{'a':2})172 def d11(a, b=1): pass173 d11(1)174 d11(1, 2)175 d11(1, **{'b':2})176 def d21(a, b, c=1): pass177 d21(1, 2)178 d21(1, 2, 3)179 d21(*(1, 2, 3))180 d21(1, *(2, 3))181 d21(1, 2, *(3,))182 d21(1, 2, **{'c':3})183 def d02(a=1, b=2): pass184 d02()185 d02(1)186 d02(1, 2)187 d02(*(1, 2))188 d02(1, *(2,))189 d02(1, **{'b':2})190 d02(**{'a': 1, 'b': 2})191 def d12(a, b=1, c=2): pass192 d12(1)193 d12(1, 2)194 d12(1, 2, 3)195 def d22(a, b, c=1, d=2): pass196 d22(1, 2)197 d22(1, 2, 3)198 d22(1, 2, 3, 4)199 def d01v(a=1, *rest): pass200 d01v()201 d01v(1)202 d01v(1, 2)203 d01v(*(1, 2, 3, 4))204 d01v(*(1,))205 d01v(**{'a':2})206 def d11v(a, b=1, *rest): pass207 d11v(1)208 d11v(1, 2)209 d11v(1, 2, 3)210 def d21v(a, b, c=1, *rest): pass211 d21v(1, 2)212 d21v(1, 2, 3)213 d21v(1, 2, 3, 4)214 d21v(*(1, 2, 3, 4))215 d21v(1, 2, **{'c': 3})216 def d02v(a=1, b=2, *rest): pass217 d02v()218 d02v(1)219 d02v(1, 2)220 d02v(1, 2, 3)221 d02v(1, *(2, 3, 4))222 d02v(**{'a': 1, 'b': 2})223 def d12v(a, b=1, c=2, *rest): pass224 d12v(1)225 d12v(1, 2)226 d12v(1, 2, 3)227 d12v(1, 2, 3, 4)228 d12v(*(1, 2, 3, 4))229 d12v(1, 2, *(3, 4, 5))230 d12v(1, *(2,), **{'c': 3})231 def d22v(a, b, c=1, d=2, *rest): pass232 d22v(1, 2)233 d22v(1, 2, 3)234 d22v(1, 2, 3, 4)235 d22v(1, 2, 3, 4, 5)236 d22v(*(1, 2, 3, 4))237 d22v(1, 2, *(3, 4, 5))238 d22v(1, *(2, 3), **{'d': 4})239 def d31v((x)): pass240 d31v(1)241 def d32v((x,)): pass242 d32v((1,))243 244 # keyword arguments after *arglist245 def f(*args, **kwargs):246 return args, kwargs247 self.assertEquals(f(1, x=2, *[3, 4], y=5), ((1, 3, 4),248 {'x':2, 'y':5}))249 self.assertRaises(SyntaxError, eval, "f(1, *(2,3), 4)")250 self.assertRaises(SyntaxError, eval, "f(1, x=2, *(3,4), x=5)")251 252 # Check ast errors in *args and *kwargs253 check_syntax_error(self, "f(*g(1=2))")254 check_syntax_error(self, "f(**g(1=2))")255 256 def testLambdef(self):257 ### lambdef: 'lambda' [varargslist] ':' test258 l1 = lambda : 0259 self.assertEquals(l1(), 0)260 l2 = lambda : a[d] # XXX just testing the expression261 l3 = lambda : [2 < x for x in [-1, 3, 0L]]262 self.assertEquals(l3(), [0, 1, 0])263 l4 = lambda x = lambda y = lambda z=1 : z : y() : x()264 self.assertEquals(l4(), 1)265 l5 = lambda x, y, z=2: x + y + z266 self.assertEquals(l5(1, 2), 5)267 self.assertEquals(l5(1, 2, 3), 6)268 check_syntax_error(self, "lambda x: x = 2")269 check_syntax_error(self, "lambda (None,): None")270 271 ### stmt: simple_stmt | compound_stmt272 # Tested below273 274 def testSimpleStmt(self):275 ### simple_stmt: small_stmt (';' small_stmt)* [';']276 x = 1; pass; del x277 def foo():278 # verify statements that end with semi-colons279 x = 1; pass; del x;280 foo()281 282 ### small_stmt: expr_stmt | print_stmt | pass_stmt | del_stmt | flow_stmt | import_stmt | global_stmt | access_stmt | exec_stmt283 # Tested below284 285 def testExprStmt(self):286 # (exprlist '=')* exprlist287 1288 1, 2, 3289 x = 1290 x = 1, 2, 3291 x = y = z = 1, 2, 3292 x, y, z = 1, 2, 3293 abc = a, b, c = x, y, z = xyz = 1, 2, (3, 4)294 295 check_syntax_error(self, "x + 1 = 1")296 check_syntax_error(self, "a + 1 = b + 2")297 298 def testPrintStmt(self):299 # 'print' (test ',')* [test]300 import StringIO301 302 # Can't test printing to real stdout without comparing output303 # which is not available in unittest.304 save_stdout = sys.stdout305 sys.stdout = StringIO.StringIO()306 307 print 1, 2, 3308 print 1, 2, 3,309 print310 print 0 or 1, 0 or 1,311 print 0 or 1312 313 # 'print' '>>' test ','314 print >> sys.stdout, 1, 2, 3315 print >> sys.stdout, 1, 2, 3,316 print >> sys.stdout317 print >> sys.stdout, 0 or 1, 0 or 1,318 print >> sys.stdout, 0 or 1319 320 # test printing to an instance321 class Gulp:322 def write(self, msg): pass323 324 gulp = Gulp()325 print >> gulp, 1, 2, 3326 print >> gulp, 1, 2, 3,327 print >> gulp328 print >> gulp, 0 or 1, 0 or 1,329 print >> gulp, 0 or 1330 331 # test print >> None332 def driver():333 oldstdout = sys.stdout334 sys.stdout = Gulp()335 try:336 tellme(Gulp())337 tellme()338 finally:339 sys.stdout = oldstdout340 341 # we should see this once342 def tellme(file=sys.stdout):343 print >> file, 'hello world'344 345 driver()346 347 # we should not see this at all348 def tellme(file=None):349 print >> file, 'goodbye universe'350 351 driver()352 353 self.assertEqual(sys.stdout.getvalue(), '''\3541 2 33551 2 33561 1 13571 2 33581 2 33591 1 1360hello world361''')362 sys.stdout = save_stdout363 364 # syntax errors365 check_syntax_error(self, 'print ,')366 check_syntax_error(self, 'print >> x,')367 368 def testDelStmt(self):369 # 'del' exprlist370 abc = [1,2,3]371 x, y, z = abc372 xyz = x, y, z373 374 del abc375 del x, y, (z, xyz)376 377 def testPassStmt(self):378 # 'pass'379 pass380 381 # flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt382 # Tested below383 384 def testBreakStmt(self):385 # 'break'386 while 1: break387 388 def testContinueStmt(self):389 # 'continue'390 i = 1391 while i: i = 0; continue392 393 msg = ""394 while not msg:395 msg = "ok"396 try:397 continue398 msg = "continue failed to continue inside try"399 except:400 msg = "continue inside try called except block"401 if msg != "ok":402 self.fail(msg)403 404 msg = ""405 while not msg:406 msg = "finally block not called"407 try:408 continue409 finally:410 msg = "ok"411 if msg != "ok":412 self.fail(msg)413 414 def test_break_continue_loop(self):415 # This test warrants an explanation. It is a test specifically for SF bugs416 # #463359 and #462937. The bug is that a 'break' statement executed or417 # exception raised inside a try/except inside a loop, *after* a continue418 # statement has been executed in that loop, will cause the wrong number of419 # arguments to be popped off the stack and the instruction pointer reset to420 # a very small number (usually 0.) Because of this, the following test421 # *must* written as a function, and the tracking vars *must* be function422 # arguments with default values. Otherwise, the test will loop and loop.423 424 def test_inner(extra_burning_oil = 1, count=0):425 big_hippo = 2426 while big_hippo:427 count += 1428 try:429 if extra_burning_oil and big_hippo == 1:430 extra_burning_oil -= 1431 break432 big_hippo -= 1433 continue434 except:435 raise436 if count > 2 or big_hippo <> 1:437 self.fail("continue then break in try/except in loop broken!")438 test_inner()439 440 def testReturn(self):441 # 'return' [testlist]442 def g1(): return443 def g2(): return 1444 g1()445 x = g2()446 check_syntax_error(self, "class foo:return 1")447 448 def testYield(self):449 check_syntax_error(self, "class foo:yield 1")450 451 def testRaise(self):452 # 'raise' test [',' test]453 try: raise RuntimeError, 'just testing'454 except RuntimeError: pass455 try: raise KeyboardInterrupt456 except KeyboardInterrupt: pass457 458 def testImport(self):459 # 'import' dotted_as_names460 import sys461 import time, sys462 # 'from' dotted_name 'import' ('*' | '(' import_as_names ')' | import_as_names)463 from time import time464 from time import (time)465 # not testable inside a function, but already done at top of the module466 # from sys import *467 from sys import path, argv468 from sys import (path, argv)469 from sys import (path, argv,)470 471 def testGlobal(self):472 # 'global' NAME (',' NAME)*473 global a474 global a, b475 global one, two, three, four, five, six, seven, eight, nine, ten476 477 def testExec(self):478 # 'exec' expr ['in' expr [',' expr]]479 z = None480 del z481 exec 'z=1+1\n'482 if z != 2: self.fail('exec \'z=1+1\'\\n')483 del z484 exec 'z=1+1'485 if z != 2: self.fail('exec \'z=1+1\'')486 z = None487 del z488 import types489 if hasattr(types, "UnicodeType"):490 exec r"""if 1:491 exec u'z=1+1\n'492 if z != 2: self.fail('exec u\'z=1+1\'\\n')493 del z494 exec u'z=1+1'495 if z != 2: self.fail('exec u\'z=1+1\'')"""496 g = {}497 exec 'z = 1' in g498 if g.has_key('__builtins__'): del g['__builtins__']499 if g != {'z': 1}: self.fail('exec \'z = 1\' in g')500 g = {}501 l = {}502 503 import warnings504 warnings.filterwarnings("ignore", "global statement", module="<string>")505 exec 'global a; a = 1; b = 2' in g, l506 if g.has_key('__builtins__'): del g['__builtins__']507 if l.has_key('__builtins__'): del l['__builtins__']508 if (g, l) != ({'a':1}, {'b':2}):509 self.fail('exec ... in g (%s), l (%s)' %(g,l))510 511 def testAssert(self):512 # assert_stmt: 'assert' test [',' test]513 assert 1514 assert 1, 1515 assert lambda x:x516 assert 1, lambda x:x+1517 try:518 assert 0, "msg"519 except AssertionError, e:520 self.assertEquals(e.args[0], "msg")521 else:522 if __debug__:523 self.fail("AssertionError not raised by assert 0")524 525 ### compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef526 # Tested below527 528 def testIf(self):529 # 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]530 if 1: pass531 if 1: pass532 else: pass533 if 0: pass534 elif 0: pass535 if 0: pass536 elif 0: pass537 elif 0: pass538 elif 0: pass539 else: pass540 541 def testWhile(self):542 # 'while' test ':' suite ['else' ':' suite]543 while 0: pass544 while 0: pass545 else: pass546 547 # Issue1920: "while 0" is optimized away,548 # ensure that the "else" clause is still present.549 x = 0550 while 0:551 x = 1552 else:553 x = 2554 self.assertEquals(x, 2)555 556 def testFor(self):557 # 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite]558 for i in 1, 2, 3: pass559 for i, j, k in (): pass560 else: pass561 class Squares:562 def __init__(self, max):563 self.max = max564 self.sofar = []565 def __len__(self): return len(self.sofar)566 def __getitem__(self, i):567 if not 0 <= i < self.max: raise IndexError568 n = len(self.sofar)569 while n <= i:570 self.sofar.append(n*n)571 n = n+1572 return self.sofar[i]573 n = 0574 for x in Squares(10): n = n+x575 if n != 285:576 self.fail('for over growing sequence')577 578 result = []579 for x, in [(1,), (2,), (3,)]:580 result.append(x)581 self.assertEqual(result, [1, 2, 3])582 583 def testTry(self):584 ### try_stmt: 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]585 ### | 'try' ':' suite 'finally' ':' suite586 ### except_clause: 'except' [expr [('as' | ',') expr]]587 try:588 1/0589 except ZeroDivisionError:590 pass591 else:592 pass593 try: 1/0594 except EOFError: pass595 except TypeError as msg: pass596 except RuntimeError, msg: pass597 except: pass598 else: pass599 try: 1/0600 except (EOFError, TypeError, ZeroDivisionError): pass601 try: 1/0602 except (EOFError, TypeError, ZeroDivisionError), msg: pass603 try: pass604 finally: pass605 606 def testSuite(self):607 # simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT608 if 1: pass609 if 1:610 pass611 if 1:612 #613 #614 #615 pass616 pass617 #618 pass619 #620 621 def testTest(self):622 ### and_test ('or' and_test)*623 ### and_test: not_test ('and' not_test)*624 ### not_test: 'not' not_test | comparison625 if not 1: pass626 if 1 and 1: pass627 if 1 or 1: pass628 if not not not 1: pass629 if not 1 and 1 and 1: pass630 if 1 and 1 or 1 and 1 and 1 or not 1 and 1: pass631 632 def testComparison(self):633 ### comparison: expr (comp_op expr)*634 ### comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'635 if 1: pass636 x = (1 == 1)637 if 1 == 1: pass638 if 1 != 1: pass639 if 1 <> 1: pass640 if 1 < 1: pass641 if 1 > 1: pass642 if 1 <= 1: pass643 if 1 >= 1: pass644 if 1 is 1: pass645 if 1 is not 1: pass646 if 1 in (): pass647 if 1 not in (): pass648 if 1 < 1 > 1 == 1 >= 1 <= 1 <> 1 != 1 in 1 not in 1 is 1 is not 1: pass649 650 def testBinaryMaskOps(self):651 x = 1 & 1652 x = 1 ^ 1653 x = 1 | 1654 655 def testShiftOps(self):656 x = 1 << 1657 x = 1 >> 1658 x = 1 << 1 >> 1659 660 def testAdditiveOps(self):661 x = 1662 x = 1 + 1663 x = 1 - 1 - 1664 x = 1 - 1 + 1 - 1 + 1665 666 def testMultiplicativeOps(self):667 x = 1 * 1668 x = 1 / 1669 x = 1 % 1670 x = 1 / 1 * 1 % 1671 672 def testUnaryOps(self):673 x = +1674 x = -1675 x = ~1676 x = ~1 ^ 1 & 1 | 1 & 1 ^ -1677 x = -1*1/1 + 1*1 - ---1*1678 679 def testSelectors(self):680 ### trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME681 ### subscript: expr | [expr] ':' [expr]682 683 import sys, time684 c = sys.path[0]685 x = time.time()686 x = sys.modules['time'].time()687 a = '01234'688 c = a[0]689 c = a[-1]690 s = a[0:5]691 s = a[:5]692 s = a[0:]693 s = a[:]694 s = a[-5:]695 s = a[:-1]696 s = a[-4:-3]697 # A rough test of SF bug 1333982. http://python.org/sf/1333982698 # The testing here is fairly incomplete.699 # Test cases should include: commas with 1 and 2 colons700 d = {}701 d[1] = 1702 d[1,] = 2703 d[1,2] = 3704 d[1,2,3] = 4705 L = list(d)706 L.sort()707 self.assertEquals(str(L), '[1, (1,), (1, 2), (1, 2, 3)]')708 709 def testAtoms(self):710 ### atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING711 ### dictmaker: test ':' test (',' test ':' test)* [',']712 713 x = (1)714 x = (1 or 2 or 3)715 x = (1 or 2 or 3, 2, 3)716 717 x = []718 x = [1]719 x = [1 or 2 or 3]720 x = [1 or 2 or 3, 2, 3]721 x = []722 723 x = {}724 x = {'one': 1}725 x = {'one': 1,}726 x = {'one' or 'two': 1 or 2}727 x = {'one': 1, 'two': 2}728 x = {'one': 1, 'two': 2,}729 x = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6}730 731 x = `x`732 x = `1 or 2 or 3`733 self.assertEqual(`1,2`, '(1, 2)')734 735 x = x736 x = 'x'737 x = 123738 739 ### exprlist: expr (',' expr)* [',']740 ### testlist: test (',' test)* [',']741 # These have been exercised enough above742 743 def testClassdef(self):744 # 'class' NAME ['(' [testlist] ')'] ':' suite745 class B: pass746 class B2(): pass747 class C1(B): pass748 class C2(B): pass749 class D(C1, C2, B): pass750 class C:751 def meth1(self): pass752 def meth2(self, arg): pass753 def meth3(self, a1, a2): pass754 # decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE755 # decorators: decorator+756 # decorated: decorators (classdef | funcdef)757 def class_decorator(x):758 x.decorated = True759 return x760 @class_decorator761 class G:762 pass763 self.assertEqual(G.decorated, True)764 765 def testListcomps(self):766 # list comprehension tests767 nums = [1, 2, 3, 4, 5]768 strs = ["Apple", "Banana", "Coconut"]769 spcs = [" Apple", " Banana ", "Coco nut "]770 771 self.assertEqual([s.strip() for s in spcs], ['Apple', 'Banana', 'Coco nut'])772 self.assertEqual([3 * x for x in nums], [3, 6, 9, 12, 15])773 self.assertEqual([x for x in nums if x > 2], [3, 4, 5])774 self.assertEqual([(i, s) for i in nums for s in strs],775 [(1, 'Apple'), (1, 'Banana'), (1, 'Coconut'),776 (2, 'Apple'), (2, 'Banana'), (2, 'Coconut'),777 (3, 'Apple'), (3, 'Banana'), (3, 'Coconut'),778 (4, 'Apple'), (4, 'Banana'), (4, 'Coconut'),779 (5, 'Apple'), (5, 'Banana'), (5, 'Coconut')])780 self.assertEqual([(i, s) for i in nums for s in [f for f in strs if "n" in f]],781 [(1, 'Banana'), (1, 'Coconut'), (2, 'Banana'), (2, 'Coconut'),782 (3, 'Banana'), (3, 'Coconut'), (4, 'Banana'), (4, 'Coconut'),783 (5, 'Banana'), (5, 'Coconut')])784 self.assertEqual([(lambda a:[a**i for i in range(a+1)])(j) for j in range(5)],785 [[1], [1, 1], [1, 2, 4], [1, 3, 9, 27], [1, 4, 16, 64, 256]])786 787 def test_in_func(l):788 return [None < x < 3 for x in l if x > 2]789 790 self.assertEqual(test_in_func(nums), [False, False, False])791 792 def test_nested_front():793 self.assertEqual([[y for y in [x, x + 1]] for x in [1,3,5]],794 [[1, 2], [3, 4], [5, 6]])795 796 test_nested_front()797 798 check_syntax_error(self, "[i, s for i in nums for s in strs]")799 check_syntax_error(self, "[x if y]")800 801 suppliers = [802 (1, "Boeing"),803 (2, "Ford"),804 (3, "Macdonalds")805 ]806 807 parts = [808 (10, "Airliner"),809 (20, "Engine"),810 (30, "Cheeseburger")811 ]812 813 suppart = [814 (1, 10), (1, 20), (2, 20), (3, 30)815 ]816 817 x = [818 (sname, pname)819 for (sno, sname) in suppliers820 for (pno, pname) in parts821 for (sp_sno, sp_pno) in suppart822 if sno == sp_sno and pno == sp_pno823 ]824 825 self.assertEqual(x, [('Boeing', 'Airliner'), ('Boeing', 'Engine'), ('Ford', 'Engine'),826 ('Macdonalds', 'Cheeseburger')])827 828 def testGenexps(self):829 # generator expression tests830 g = ([x for x in range(10)] for x in range(1))831 self.assertEqual(g.next(), [x for x in range(10)])832 try:833 g.next()834 self.fail('should produce StopIteration exception')835 except StopIteration:836 pass837 838 a = 1839 try:840 g = (a for d in a)841 g.next()842 self.fail('should produce TypeError')843 except TypeError:844 pass845 846 self.assertEqual(list((x, y) for x in 'abcd' for y in 'abcd'), [(x, y) for x in 'abcd' for y in 'abcd'])847 self.assertEqual(list((x, y) for x in 'ab' for y in 'xy'), [(x, y) for x in 'ab' for y in 'xy'])848 849 a = [x for x in range(10)]850 b = (x for x in (y for y in a))851 self.assertEqual(sum(b), sum([x for x in range(10)]))852 853 self.assertEqual(sum(x**2 for x in range(10)), sum([x**2 for x in range(10)]))854 self.assertEqual(sum(x*x for x in range(10) if x%2), sum([x*x for x in range(10) if x%2]))855 self.assertEqual(sum(x for x in (y for y in range(10))), sum([x for x in range(10)]))856 self.assertEqual(sum(x for x in (y for y in (z for z in range(10)))), sum([x for x in range(10)]))857 self.assertEqual(sum(x for x in [y for y in (z for z in range(10))]), sum([x for x in range(10)]))858 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)]))859 self.assertEqual(sum(x for x in (y for y in (z for z in range(10) if True) if False) if True), 0)860 check_syntax_error(self, "foo(x for x in range(10), 100)")861 check_syntax_error(self, "foo(100, x for x in range(10))")862 863 def testComprehensionSpecials(self):864 # test for outmost iterable precomputation865 x = 10; g = (i for i in range(x)); x = 5866 self.assertEqual(len(list(g)), 10)867 868 # This should hold, since we're only precomputing outmost iterable.869 x = 10; t = False; g = ((i,j) for i in range(x) if t for j in range(x))870 x = 5; t = True;871 self.assertEqual([(i,j) for i in range(10) for j in range(5)], list(g))872 873 # Grammar allows multiple adjacent 'if's in listcomps and genexps,874 # even though it's silly. Make sure it works (ifelse broke this.)875 self.assertEqual([ x for x in range(10) if x % 2 if x % 3 ], [1, 5, 7])876 self.assertEqual(list(x for x in range(10) if x % 2 if x % 3), [1, 5, 7])877 878 # verify unpacking single element tuples in listcomp/genexp.879 self.assertEqual([x for x, in [(4,), (5,), (6,)]], [4, 5, 6])880 self.assertEqual(list(x for x, in [(7,), (8,), (9,)]), [7, 8, 9])881 882 def test_with_statement(self):883 class manager(object):884 def __enter__(self):885 return (1, 2)886 def __exit__(self, *args):887 pass888 889 with manager():890 pass891 with manager() as x:892 pass893 with manager() as (x, y):894 pass895 with manager(), manager():896 pass897 with manager() as x, manager() as y:898 pass899 with manager() as x, manager():900 pass901 902 def testIfElseExpr(self):903 # Test ifelse expressions in various cases904 def _checkeval(msg, ret):905 "helper to check that evaluation of expressions is done correctly"906 print x907 return ret908 909 self.assertEqual([ x() for x in lambda: True, lambda: False if x() ], [True])910 self.assertEqual([ x() for x in (lambda: True, lambda: False) if x() ], [True])911 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])912 self.assertEqual((5 if 1 else _checkeval("check 1", 0)), 5)913 self.assertEqual((_checkeval("check 2", 0) if 0 else 5), 5)914 self.assertEqual((5 and 6 if 0 else 1), 1)915 self.assertEqual(((5 and 6) if 0 else 1), 1)916 self.assertEqual((5 and (6 if 1 else 1)), 6)917 self.assertEqual((0 or _checkeval("check 3", 2) if 0 else 3), 3)918 self.assertEqual((1 or _checkeval("check 4", 2) if 1 else _checkeval("check 5", 3)), 1)919 self.assertEqual((0 or 5 if 1 else _checkeval("check 6", 3)), 5)920 self.assertEqual((not 5 if 1 else 1), False)921 self.assertEqual((not 5 if 0 else 1), 1)922 self.assertEqual((6 + 1 if 1 else 2), 7)923 self.assertEqual((6 - 1 if 1 else 2), 5)924 self.assertEqual((6 * 2 if 1 else 4), 12)925 self.assertEqual((6 / 2 if 1 else 3), 3)926 self.assertEqual((6 < 4 if 0 else 2), 2)927 928 def testStringLiterals(self):929 x = ''; y = ""; self.assert_(len(x) == 0 and x == y)930 x = '\''; y = "'"; self.assert_(len(x) == 1 and x == y and ord(x) == 39)931 x = '"'; y = "\""; self.assert_(len(x) == 1 and x == y and ord(x) == 34)932 x = "doesn't \"shrink\" does it"933 y = 'doesn\'t "shrink" does it'934 self.assert_(len(x) == 24 and x == y)935 x = "does \"shrink\" doesn't it"936 y = 'does "shrink" doesn\'t it'937 self.assert_(len(x) == 24 and x == y)938 x = """939The "quick"940brown fox941jumps over942the 'lazy' dog.943"""944 y = '\nThe "quick"\nbrown fox\njumps over\nthe \'lazy\' dog.\n'945 self.assertEquals(x, y)946 y = '''947The "quick"948brown fox949jumps over950the 'lazy' dog.951'''952 self.assertEquals(x, y)953 y = "\n\954The \"quick\"\n\955brown fox\n\956jumps over\n\957the 'lazy' dog.\n\958"959 self.assertEquals(x, y)960 y = '\n\961The \"quick\"\n\962brown fox\n\963jumps over\n\964the \'lazy\' dog.\n\965'966 self.assertEquals(x, y)967 968 969 970def test_main():971 run_unittest(TokenTests, GrammarTests)972 973if __name__ == '__main__':974 test_main()975 976 