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','0x10000000000000000':51 try:52 x = eval(s)53 except OverflowError:54 self.fail("OverflowError on huge integer literal %r" % s)55 else:56 self.fail('Weird maxint value %r' % maxint)57 58 def testLongIntegers(self):59 x = 0L60 x = 0l61 x = 0xffffffffffffffffL62 x = 0xffffffffffffffffl63 x = 077777777777777777L64 x = 077777777777777777l65 x = 123456789012345678901234567890L66 x = 123456789012345678901234567890l67 68 def testFloats(self):69 x = 3.1470 x = 314.71 x = 0.31472 # XXX x = 000.31473 x = .31474 x = 3e1475 x = 3E1476 x = 3e-1477 x = 3e+1478 x = 3.e1479 x = .3e1480 x = 3.1e481 82class GrammarTests(unittest.TestCase):83 84 # single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE85 # XXX can't test in a script -- this rule is only used when interactive86 87 # file_input: (NEWLINE | stmt)* ENDMARKER88 # Being tested as this very moment this very module89 90 # expr_input: testlist NEWLINE91 # XXX Hard to test -- used only in calls to input()92 93 def testEvalInput(self):94 # testlist ENDMARKER95 x = eval('1, 0 or 1')96 97 def testFuncdef(self):98 ### 'def' NAME parameters ':' suite99 ### parameters: '(' [varargslist] ')'100 ### varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' ('**'|'*' '*') NAME]101 ### | ('**'|'*' '*') NAME)102 ### | fpdef ['=' test] (',' fpdef ['=' test])* [',']103 ### fpdef: NAME | '(' fplist ')'104 ### fplist: fpdef (',' fpdef)* [',']105 ### arglist: (argument ',')* (argument | *' test [',' '**' test] | '**' test)106 ### argument: [test '='] test # Really [keyword '='] test107 def f1(): pass108 f1()109 f1(*())110 f1(*(), **{})111 def f2(one_argument): pass112 def f3(two, arguments): pass113 def f4(two, (compound, (argument, list))): pass114 def f5((compound, first), two): pass115 self.assertEquals(f2.func_code.co_varnames, ('one_argument',))116 self.assertEquals(f3.func_code.co_varnames, ('two', 'arguments'))117 if sys.platform.startswith('java'):118 self.assertEquals(f4.func_code.co_varnames,119 ('two', '(compound, (argument, list))', 'compound', 'argument',120 'list',))121 self.assertEquals(f5.func_code.co_varnames,122 ('(compound, first)', 'two', 'compound', 'first'))123 else:124 self.assertEquals(f4.func_code.co_varnames,125 ('two', '.1', 'compound', 'argument', 'list'))126 self.assertEquals(f5.func_code.co_varnames,127 ('.0', 'two', 'compound', 'first'))128 def a1(one_arg,): pass129 def a2(two, args,): pass130 def v0(*rest): pass131 def v1(a, *rest): pass132 def v2(a, b, *rest): pass133 def v3(a, (b, c), *rest): return a, b, c, rest134 135 f1()136 f2(1)137 f2(1,)138 f3(1, 2)139 f3(1, 2,)140 f4(1, (2, (3, 4)))141 v0()142 v0(1)143 v0(1,)144 v0(1,2)145 v0(1,2,3,4,5,6,7,8,9,0)146 v1(1)147 v1(1,)148 v1(1,2)149 v1(1,2,3)150 v1(1,2,3,4,5,6,7,8,9,0)151 v2(1,2)152 v2(1,2,3)153 v2(1,2,3,4)154 v2(1,2,3,4,5,6,7,8,9,0)155 v3(1,(2,3))156 v3(1,(2,3),4)157 v3(1,(2,3),4,5,6,7,8,9,0)158 159 # ceval unpacks the formal arguments into the first argcount names;160 # thus, the names nested inside tuples must appear after these names.161 if sys.platform.startswith('java'):162 self.assertEquals(v3.func_code.co_varnames, ('a', '(b, c)', 'rest', 'b', 'c'))163 else:164 self.assertEquals(v3.func_code.co_varnames, ('a', '.1', 'rest', 'b', 'c'))165 self.assertEquals(v3(1, (2, 3), 4), (1, 2, 3, (4,)))166 def d01(a=1): pass167 d01()168 d01(1)169 d01(*(1,))170 d01(**{'a':2})171 def d11(a, b=1): pass172 d11(1)173 d11(1, 2)174 d11(1, **{'b':2})175 def d21(a, b, c=1): pass176 d21(1, 2)177 d21(1, 2, 3)178 d21(*(1, 2, 3))179 d21(1, *(2, 3))180 d21(1, 2, *(3,))181 d21(1, 2, **{'c':3})182 def d02(a=1, b=2): pass183 d02()184 d02(1)185 d02(1, 2)186 d02(*(1, 2))187 d02(1, *(2,))188 d02(1, **{'b':2})189 d02(**{'a': 1, 'b': 2})190 def d12(a, b=1, c=2): pass191 d12(1)192 d12(1, 2)193 d12(1, 2, 3)194 def d22(a, b, c=1, d=2): pass195 d22(1, 2)196 d22(1, 2, 3)197 d22(1, 2, 3, 4)198 def d01v(a=1, *rest): pass199 d01v()200 d01v(1)201 d01v(1, 2)202 d01v(*(1, 2, 3, 4))203 d01v(*(1,))204 d01v(**{'a':2})205 def d11v(a, b=1, *rest): pass206 d11v(1)207 d11v(1, 2)208 d11v(1, 2, 3)209 def d21v(a, b, c=1, *rest): pass210 d21v(1, 2)211 d21v(1, 2, 3)212 d21v(1, 2, 3, 4)213 d21v(*(1, 2, 3, 4))214 d21v(1, 2, **{'c': 3})215 def d02v(a=1, b=2, *rest): pass216 d02v()217 d02v(1)218 d02v(1, 2)219 d02v(1, 2, 3)220 d02v(1, *(2, 3, 4))221 d02v(**{'a': 1, 'b': 2})222 def d12v(a, b=1, c=2, *rest): pass223 d12v(1)224 d12v(1, 2)225 d12v(1, 2, 3)226 d12v(1, 2, 3, 4)227 d12v(*(1, 2, 3, 4))228 d12v(1, 2, *(3, 4, 5))229 d12v(1, *(2,), **{'c': 3})230 def d22v(a, b, c=1, d=2, *rest): pass231 d22v(1, 2)232 d22v(1, 2, 3)233 d22v(1, 2, 3, 4)234 d22v(1, 2, 3, 4, 5)235 d22v(*(1, 2, 3, 4))236 d22v(1, 2, *(3, 4, 5))237 d22v(1, *(2, 3), **{'d': 4})238 def d31v((x)): pass239 d31v(1)240 def d32v((x,)): pass241 d32v((1,))242 243 # keyword arguments after *arglist244 def f(*args, **kwargs):245 return args, kwargs246 self.assertEquals(f(1, x=2, *[3, 4], y=5), ((1, 3, 4),247 {'x':2, 'y':5}))248 self.assertRaises(SyntaxError, eval, "f(1, *(2,3), 4)")249 self.assertRaises(SyntaxError, eval, "f(1, x=2, *(3,4), x=5)")250 251 # Check ast errors in *args and *kwargs252 check_syntax_error(self, "f(*g(1=2))")253 check_syntax_error(self, "f(**g(1=2))")254 255 def testLambdef(self):256 ### lambdef: 'lambda' [varargslist] ':' test257 l1 = lambda : 0258 self.assertEquals(l1(), 0)259 l2 = lambda : a[d] # XXX just testing the expression260 l3 = lambda : [2 < x for x in [-1, 3, 0L]]261 self.assertEquals(l3(), [0, 1, 0])262 l4 = lambda x = lambda y = lambda z=1 : z : y() : x()263 self.assertEquals(l4(), 1)264 l5 = lambda x, y, z=2: x + y + z265 self.assertEquals(l5(1, 2), 5)266 self.assertEquals(l5(1, 2, 3), 6)267 check_syntax_error(self, "lambda x: x = 2")268 check_syntax_error(self, "lambda (None,): None")269 270 ### stmt: simple_stmt | compound_stmt271 # Tested below272 273 def testSimpleStmt(self):274 ### simple_stmt: small_stmt (';' small_stmt)* [';']275 x = 1; pass; del x276 def foo():277 # verify statements that end with semi-colons278 x = 1; pass; del x;279 foo()280 281 ### small_stmt: expr_stmt | print_stmt | pass_stmt | del_stmt | flow_stmt | import_stmt | global_stmt | access_stmt | exec_stmt282 # Tested below283 284 def testExprStmt(self):285 # (exprlist '=')* exprlist286 1287 1, 2, 3288 x = 1289 x = 1, 2, 3290 x = y = z = 1, 2, 3291 x, y, z = 1, 2, 3292 abc = a, b, c = x, y, z = xyz = 1, 2, (3, 4)293 294 check_syntax_error(self, "x + 1 = 1")295 check_syntax_error(self, "a + 1 = b + 2")296 297 def testPrintStmt(self):298 # 'print' (test ',')* [test]299 import StringIO300 301 # Can't test printing to real stdout without comparing output302 # which is not available in unittest.303 save_stdout = sys.stdout304 sys.stdout = StringIO.StringIO()305 306 print 1, 2, 3307 print 1, 2, 3,308 print309 print 0 or 1, 0 or 1,310 print 0 or 1311 312 # 'print' '>>' test ','313 print >> sys.stdout, 1, 2, 3314 print >> sys.stdout, 1, 2, 3,315 print >> sys.stdout316 print >> sys.stdout, 0 or 1, 0 or 1,317 print >> sys.stdout, 0 or 1318 319 # test printing to an instance320 class Gulp:321 def write(self, msg): pass322 323 gulp = Gulp()324 print >> gulp, 1, 2, 3325 print >> gulp, 1, 2, 3,326 print >> gulp327 print >> gulp, 0 or 1, 0 or 1,328 print >> gulp, 0 or 1329 330 # test print >> None331 def driver():332 oldstdout = sys.stdout333 sys.stdout = Gulp()334 try:335 tellme(Gulp())336 tellme()337 finally:338 sys.stdout = oldstdout339 340 # we should see this once341 def tellme(file=sys.stdout):342 print >> file, 'hello world'343 344 driver()345 346 # we should not see this at all347 def tellme(file=None):348 print >> file, 'goodbye universe'349 350 driver()351 352 self.assertEqual(sys.stdout.getvalue(), '''\3531 2 33541 2 33551 1 13561 2 33571 2 33581 1 1359hello world360''')361 sys.stdout = save_stdout362 363 # syntax errors364 check_syntax_error(self, 'print ,')365 check_syntax_error(self, 'print >> x,')366 367 def testDelStmt(self):368 # 'del' exprlist369 abc = [1,2,3]370 x, y, z = abc371 xyz = x, y, z372 373 del abc374 del x, y, (z, xyz)375 376 def testPassStmt(self):377 # 'pass'378 pass379 380 # flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt381 # Tested below382 383 def testBreakStmt(self):384 # 'break'385 while 1: break386 387 def testContinueStmt(self):388 # 'continue'389 i = 1390 while i: i = 0; continue391 392 msg = ""393 while not msg:394 msg = "ok"395 try:396 continue397 msg = "continue failed to continue inside try"398 except:399 msg = "continue inside try called except block"400 if msg != "ok":401 self.fail(msg)402 403 msg = ""404 while not msg:405 msg = "finally block not called"406 try:407 continue408 finally:409 msg = "ok"410 if msg != "ok":411 self.fail(msg)412 413 def test_break_continue_loop(self):414 # This test warrants an explanation. It is a test specifically for SF bugs415 # #463359 and #462937. The bug is that a 'break' statement executed or416 # exception raised inside a try/except inside a loop, *after* a continue417 # statement has been executed in that loop, will cause the wrong number of418 # arguments to be popped off the stack and the instruction pointer reset to419 # a very small number (usually 0.) Because of this, the following test420 # *must* written as a function, and the tracking vars *must* be function421 # arguments with default values. Otherwise, the test will loop and loop.422 423 def test_inner(extra_burning_oil = 1, count=0):424 big_hippo = 2425 while big_hippo:426 count += 1427 try:428 if extra_burning_oil and big_hippo == 1:429 extra_burning_oil -= 1430 break431 big_hippo -= 1432 continue433 except:434 raise435 if count > 2 or big_hippo <> 1:436 self.fail("continue then break in try/except in loop broken!")437 test_inner()438 439 def testReturn(self):440 # 'return' [testlist]441 def g1(): return442 def g2(): return 1443 g1()444 x = g2()445 check_syntax_error(self, "class foo:return 1")446 447 def testYield(self):448 check_syntax_error(self, "class foo:yield 1")449 450 def testRaise(self):451 # 'raise' test [',' test]452 try: raise RuntimeError, 'just testing'453 except RuntimeError: pass454 try: raise KeyboardInterrupt455 except KeyboardInterrupt: pass456 457 def testImport(self):458 # 'import' dotted_as_names459 import sys460 import time, sys461 # 'from' dotted_name 'import' ('*' | '(' import_as_names ')' | import_as_names)462 from time import time463 from time import (time)464 # not testable inside a function, but already done at top of the module465 # from sys import *466 from sys import path, argv467 from sys import (path, argv)468 from sys import (path, argv,)469 470 def testGlobal(self):471 # 'global' NAME (',' NAME)*472 global a473 global a, b474 global one, two, three, four, five, six, seven, eight, nine, ten475 476 def testExec(self):477 # 'exec' expr ['in' expr [',' expr]]478 z = None479 del z480 exec 'z=1+1\n'481 if z != 2: self.fail('exec \'z=1+1\'\\n')482 del z483 exec 'z=1+1'484 if z != 2: self.fail('exec \'z=1+1\'')485 z = None486 del z487 import types488 if hasattr(types, "UnicodeType"):489 exec r"""if 1:490 exec u'z=1+1\n'491 if z != 2: self.fail('exec u\'z=1+1\'\\n')492 del z493 exec u'z=1+1'494 if z != 2: self.fail('exec u\'z=1+1\'')"""495 g = {}496 exec 'z = 1' in g497 if g.has_key('__builtins__'): del g['__builtins__']498 if g != {'z': 1}: self.fail('exec \'z = 1\' in g')499 g = {}500 l = {}501 502 import warnings503 warnings.filterwarnings("ignore", "global statement", module="<string>")504 exec 'global a; a = 1; b = 2' in g, l505 if g.has_key('__builtins__'): del g['__builtins__']506 if l.has_key('__builtins__'): del l['__builtins__']507 if (g, l) != ({'a':1}, {'b':2}):508 self.fail('exec ... in g (%s), l (%s)' %(g,l))509 510 def testAssert(self):511 # assert_stmt: 'assert' test [',' test]512 assert 1513 assert 1, 1514 assert lambda x:x515 assert 1, lambda x:x+1516 try:517 assert 0, "msg"518 except AssertionError, e:519 self.assertEquals(e.args[0], "msg")520 else:521 if __debug__:522 self.fail("AssertionError not raised by assert 0")523 524 ### compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef525 # Tested below526 527 def testIf(self):528 # 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]529 if 1: pass530 if 1: pass531 else: pass532 if 0: pass533 elif 0: pass534 if 0: pass535 elif 0: pass536 elif 0: pass537 elif 0: pass538 else: pass539 540 def testWhile(self):541 # 'while' test ':' suite ['else' ':' suite]542 while 0: pass543 while 0: pass544 else: pass545 546 # Issue1920: "while 0" is optimized away,547 # ensure that the "else" clause is still present.548 x = 0549 while 0:550 x = 1551 else:552 x = 2553 self.assertEquals(x, 2)554 555 def testFor(self):556 # 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite]557 for i in 1, 2, 3: pass558 for i, j, k in (): pass559 else: pass560 class Squares:561 def __init__(self, max):562 self.max = max563 self.sofar = []564 def __len__(self): return len(self.sofar)565 def __getitem__(self, i):566 if not 0 <= i < self.max: raise IndexError567 n = len(self.sofar)568 while n <= i:569 self.sofar.append(n*n)570 n = n+1571 return self.sofar[i]572 n = 0573 for x in Squares(10): n = n+x574 if n != 285:575 self.fail('for over growing sequence')576 577 result = []578 for x, in [(1,), (2,), (3,)]:579 result.append(x)580 self.assertEqual(result, [1, 2, 3])581 582 def testTry(self):583 ### try_stmt: 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]584 ### | 'try' ':' suite 'finally' ':' suite585 ### except_clause: 'except' [expr [('as' | ',') expr]]586 try:587 1/0588 except ZeroDivisionError:589 pass590 else:591 pass592 try: 1/0593 except EOFError: pass594 except TypeError as msg: pass595 except RuntimeError, msg: pass596 except: pass597 else: pass598 try: 1/0599 except (EOFError, TypeError, ZeroDivisionError): pass600 try: 1/0601 except (EOFError, TypeError, ZeroDivisionError), msg: pass602 try: pass603 finally: pass604 605 def testSuite(self):606 # simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT607 if 1: pass608 if 1:609 pass610 if 1:611 #612 #613 #614 pass615 pass616 #617 pass618 #619 620 def testTest(self):621 ### and_test ('or' and_test)*622 ### and_test: not_test ('and' not_test)*623 ### not_test: 'not' not_test | comparison624 if not 1: pass625 if 1 and 1: pass626 if 1 or 1: pass627 if not not not 1: pass628 if not 1 and 1 and 1: pass629 if 1 and 1 or 1 and 1 and 1 or not 1 and 1: pass630 631 def testComparison(self):632 ### comparison: expr (comp_op expr)*633 ### comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'634 if 1: pass635 x = (1 == 1)636 if 1 == 1: pass637 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 is 1: pass644 if 1 is not 1: pass645 if 1 in (): pass646 if 1 not in (): pass647 if 1 < 1 > 1 == 1 >= 1 <= 1 <> 1 != 1 in 1 not in 1 is 1 is not 1: pass648 649 def testBinaryMaskOps(self):650 x = 1 & 1651 x = 1 ^ 1652 x = 1 | 1653 654 def testShiftOps(self):655 x = 1 << 1656 x = 1 >> 1657 x = 1 << 1 >> 1658 659 def testAdditiveOps(self):660 x = 1661 x = 1 + 1662 x = 1 - 1 - 1663 x = 1 - 1 + 1 - 1 + 1664 665 def testMultiplicativeOps(self):666 x = 1 * 1667 x = 1 / 1668 x = 1 % 1669 x = 1 / 1 * 1 % 1670 671 def testUnaryOps(self):672 x = +1673 x = -1674 x = ~1675 x = ~1 ^ 1 & 1 | 1 & 1 ^ -1676 x = -1*1/1 + 1*1 - ---1*1677 678 def testSelectors(self):679 ### trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME680 ### subscript: expr | [expr] ':' [expr]681 682 import sys, time683 c = sys.path[0]684 x = time.time()685 x = sys.modules['time'].time()686 a = '01234'687 c = a[0]688 c = a[-1]689 s = a[0:5]690 s = a[:5]691 s = a[0:]692 s = a[:]693 s = a[-5:]694 s = a[:-1]695 s = a[-4:-3]696 # A rough test of SF bug 1333982. http://python.org/sf/1333982697 # The testing here is fairly incomplete.698 # Test cases should include: commas with 1 and 2 colons699 d = {}700 d[1] = 1701 d[1,] = 2702 d[1,2] = 3703 d[1,2,3] = 4704 L = list(d)705 L.sort()706 self.assertEquals(str(L), '[1, (1,), (1, 2), (1, 2, 3)]')707 708 def testAtoms(self):709 ### atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING710 ### dictmaker: test ':' test (',' test ':' test)* [',']711 712 x = (1)713 x = (1 or 2 or 3)714 x = (1 or 2 or 3, 2, 3)715 716 x = []717 x = [1]718 x = [1 or 2 or 3]719 x = [1 or 2 or 3, 2, 3]720 x = []721 722 x = {}723 x = {'one': 1}724 x = {'one': 1,}725 x = {'one' or 'two': 1 or 2}726 x = {'one': 1, 'two': 2}727 x = {'one': 1, 'two': 2,}728 x = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6}729 730 x = `x`731 x = `1 or 2 or 3`732 self.assertEqual(`1,2`, '(1, 2)')733 734 x = x735 x = 'x'736 x = 123737 738 ### exprlist: expr (',' expr)* [',']739 ### testlist: test (',' test)* [',']740 # These have been exercised enough above741 742 def testClassdef(self):743 # 'class' NAME ['(' [testlist] ')'] ':' suite744 class B: pass745 class B2(): pass746 class C1(B): pass747 class C2(B): pass748 class D(C1, C2, B): pass749 class C:750 def meth1(self): pass751 def meth2(self, arg): pass752 def meth3(self, a1, a2): pass753 # decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE754 # decorators: decorator+755 # decorated: decorators (classdef | funcdef)756 def class_decorator(x):757 x.decorated = True758 return x759 @class_decorator760 class G:761 pass762 self.assertEqual(G.decorated, True)763 764 def testListcomps(self):765 # list comprehension tests766 nums = [1, 2, 3, 4, 5]767 strs = ["Apple", "Banana", "Coconut"]768 spcs = [" Apple", " Banana ", "Coco nut "]769 770 self.assertEqual([s.strip() for s in spcs], ['Apple', 'Banana', 'Coco nut'])771 self.assertEqual([3 * x for x in nums], [3, 6, 9, 12, 15])772 self.assertEqual([x for x in nums if x > 2], [3, 4, 5])773 self.assertEqual([(i, s) for i in nums for s in strs],774 [(1, 'Apple'), (1, 'Banana'), (1, 'Coconut'),775 (2, 'Apple'), (2, 'Banana'), (2, 'Coconut'),776 (3, 'Apple'), (3, 'Banana'), (3, 'Coconut'),777 (4, 'Apple'), (4, 'Banana'), (4, 'Coconut'),778 (5, 'Apple'), (5, 'Banana'), (5, 'Coconut')])779 self.assertEqual([(i, s) for i in nums for s in [f for f in strs if "n" in f]],780 [(1, 'Banana'), (1, 'Coconut'), (2, 'Banana'), (2, 'Coconut'),781 (3, 'Banana'), (3, 'Coconut'), (4, 'Banana'), (4, 'Coconut'),782 (5, 'Banana'), (5, 'Coconut')])783 self.assertEqual([(lambda a:[a**i for i in range(a+1)])(j) for j in range(5)],784 [[1], [1, 1], [1, 2, 4], [1, 3, 9, 27], [1, 4, 16, 64, 256]])785 786 def test_in_func(l):787 return [None < x < 3 for x in l if x > 2]788 789 self.assertEqual(test_in_func(nums), [False, False, False])790 791 def test_nested_front():792 self.assertEqual([[y for y in [x, x + 1]] for x in [1,3,5]],793 [[1, 2], [3, 4], [5, 6]])794 795 test_nested_front()796 797 check_syntax_error(self, "[i, s for i in nums for s in strs]")798 check_syntax_error(self, "[x if y]")799 800 suppliers = [801 (1, "Boeing"),802 (2, "Ford"),803 (3, "Macdonalds")804 ]805 806 parts = [807 (10, "Airliner"),808 (20, "Engine"),809 (30, "Cheeseburger")810 ]811 812 suppart = [813 (1, 10), (1, 20), (2, 20), (3, 30)814 ]815 816 x = [817 (sname, pname)818 for (sno, sname) in suppliers819 for (pno, pname) in parts820 for (sp_sno, sp_pno) in suppart821 if sno == sp_sno and pno == sp_pno822 ]823 824 self.assertEqual(x, [('Boeing', 'Airliner'), ('Boeing', 'Engine'), ('Ford', 'Engine'),825 ('Macdonalds', 'Cheeseburger')])826 827 def testGenexps(self):828 # generator expression tests829 g = ([x for x in range(10)] for x in range(1))830 self.assertEqual(g.next(), [x for x in range(10)])831 try:832 g.next()833 self.fail('should produce StopIteration exception')834 except StopIteration:835 pass836 837 a = 1838 try:839 g = (a for d in a)840 g.next()841 self.fail('should produce TypeError')842 except TypeError:843 pass844 845 self.assertEqual(list((x, y) for x in 'abcd' for y in 'abcd'), [(x, y) for x in 'abcd' for y in 'abcd'])846 self.assertEqual(list((x, y) for x in 'ab' for y in 'xy'), [(x, y) for x in 'ab' for y in 'xy'])847 848 a = [x for x in range(10)]849 b = (x for x in (y for y in a))850 self.assertEqual(sum(b), sum([x for x in range(10)]))851 852 self.assertEqual(sum(x**2 for x in range(10)), sum([x**2 for x in range(10)]))853 self.assertEqual(sum(x*x for x in range(10) if x%2), sum([x*x for x in range(10) if x%2]))854 self.assertEqual(sum(x for x in (y for y in range(10))), sum([x for x in range(10)]))855 self.assertEqual(sum(x for x in (y for y in (z for z 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) if True)) if True), 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 False) if True), 0)859 check_syntax_error(self, "foo(x for x in range(10), 100)")860 check_syntax_error(self, "foo(100, x for x in range(10))")861 862 def testComprehensionSpecials(self):863 # test for outmost iterable precomputation864 x = 10; g = (i for i in range(x)); x = 5865 self.assertEqual(len(list(g)), 10)866 867 # This should hold, since we're only precomputing outmost iterable.868 x = 10; t = False; g = ((i,j) for i in range(x) if t for j in range(x))869 x = 5; t = True;870 self.assertEqual([(i,j) for i in range(10) for j in range(5)], list(g))871 872 # Grammar allows multiple adjacent 'if's in listcomps and genexps,873 # even though it's silly. Make sure it works (ifelse broke this.)874 self.assertEqual([ x for x in range(10) if x % 2 if x % 3 ], [1, 5, 7])875 self.assertEqual(list(x for x in range(10) if x % 2 if x % 3), [1, 5, 7])876 877 # verify unpacking single element tuples in listcomp/genexp.878 self.assertEqual([x for x, in [(4,), (5,), (6,)]], [4, 5, 6])879 self.assertEqual(list(x for x, in [(7,), (8,), (9,)]), [7, 8, 9])880 881 def test_with_statement(self):882 class manager(object):883 def __enter__(self):884 return (1, 2)885 def __exit__(self, *args):886 pass887 888 with manager():889 pass890 with manager() as x:891 pass892 with manager() as (x, y):893 pass894 with manager(), manager():895 pass896 with manager() as x, manager() as y:897 pass898 with manager() as x, manager():899 pass900 901 def testIfElseExpr(self):902 # Test ifelse expressions in various cases903 def _checkeval(msg, ret):904 "helper to check that evaluation of expressions is done correctly"905 print x906 return ret907 908 self.assertEqual([ x() for x in lambda: True, lambda: False if x() ], [True])909 self.assertEqual([ x() for x in (lambda: True, lambda: False) if x() ], [True])910 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])911 self.assertEqual((5 if 1 else _checkeval("check 1", 0)), 5)912 self.assertEqual((_checkeval("check 2", 0) if 0 else 5), 5)913 self.assertEqual((5 and 6 if 0 else 1), 1)914 self.assertEqual(((5 and 6) if 0 else 1), 1)915 self.assertEqual((5 and (6 if 1 else 1)), 6)916 self.assertEqual((0 or _checkeval("check 3", 2) if 0 else 3), 3)917 self.assertEqual((1 or _checkeval("check 4", 2) if 1 else _checkeval("check 5", 3)), 1)918 self.assertEqual((0 or 5 if 1 else _checkeval("check 6", 3)), 5)919 self.assertEqual((not 5 if 1 else 1), False)920 self.assertEqual((not 5 if 0 else 1), 1)921 self.assertEqual((6 + 1 if 1 else 2), 7)922 self.assertEqual((6 - 1 if 1 else 2), 5)923 self.assertEqual((6 * 2 if 1 else 4), 12)924 self.assertEqual((6 / 2 if 1 else 3), 3)925 self.assertEqual((6 < 4 if 0 else 2), 2)926 927 def testStringLiterals(self):928 x = ''; y = ""; self.assert_(len(x) == 0 and x == y)929 x = '\''; y = "'"; self.assert_(len(x) == 1 and x == y and ord(x) == 39)930 x = '"'; y = "\""; self.assert_(len(x) == 1 and x == y and ord(x) == 34)931 x = "doesn't \"shrink\" does it"932 y = 'doesn\'t "shrink" does it'933 self.assert_(len(x) == 24 and x == y)934 x = "does \"shrink\" doesn't it"935 y = 'does "shrink" doesn\'t it'936 self.assert_(len(x) == 24 and x == y)937 x = """938The "quick"939brown fox940jumps over941the 'lazy' dog.942"""943 y = '\nThe "quick"\nbrown fox\njumps over\nthe \'lazy\' dog.\n'944 self.assertEquals(x, y)945 y = '''946The "quick"947brown fox948jumps over949the 'lazy' dog.950'''951 self.assertEquals(x, y)952 y = "\n\953The \"quick\"\n\954brown fox\n\955jumps over\n\956the 'lazy' dog.\n\957"958 self.assertEquals(x, y)959 y = '\n\960The \"quick\"\n\961brown fox\n\962jumps over\n\963the \'lazy\' dog.\n\964'965 self.assertEquals(x, y)966 967 968 969def test_main():970 run_unittest(TokenTests, GrammarTests)971 972if __name__ == '__main__':973 test_main()974 