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