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_star_expr] 477 def g1(): return 478 def g2(): return 1 479 return_list = [2, 3] 480 def g3(): return 1, *return_list 481 g1() 482 x = g2() 483 x3 = g3() 484 check_syntax_error(self, "class foo:return 1") 485 486 def testYield(self): 487 # 'yield' [yield_arg] 488 def g1(): yield 1 489 yield_list = [2, 3] 490 def g2(): yield 1, *yield_list 491 def g3(): yield from iter(yield_list) 492 x1 = g1() 493 x2 = g2() 494 x3 = g3() 495 check_syntax_error(self, "class foo:yield 1") 496 check_syntax_error(self, "def g4(): yield from *a") 497 498 def testRaise(self): 499 # 'raise' test [',' test] 500 try: raise RuntimeError('just testing') 501 except RuntimeError: pass 502 try: raise KeyboardInterrupt 503 except KeyboardInterrupt: pass 504 505 def testImport(self): 506 # 'import' dotted_as_names 507 import sys 508 import time, sys 509 # 'from' dotted_name 'import' ('*' | '(' import_as_names ')' | import_as_names) 510 from time import time 511 from time import (time) 512 # not testable inside a function, but already done at top of the module 513 # from sys import * 514 from sys import path, argv 515 from sys import (path, argv) 516 from sys import (path, argv,) 517 518 def testGlobal(self): 519 # 'global' NAME (',' NAME)* 520 global a 521 global a, b 522 global one, two, three, four, five, six, seven, eight, nine, ten 523 524 def testNonlocal(self): 525 # 'nonlocal' NAME (',' NAME)* 526 x = 0 527 y = 0 528 def f(): 529 nonlocal x 530 nonlocal x, y 531 532 def testAssert(self): 533 # assert_stmt: 'assert' test [',' test] 534 assert 1 535 assert 1, 1 536 assert lambda x:x 537 assert 1, lambda x:x+1 538 try: 539 assert 0, "msg" 540 except AssertionError as e: 541 self.assertEquals(e.args[0], "msg") 542 else: 543 if __debug__: 544 self.fail("AssertionError not raised by assert 0") 545 546 ### compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef 547 # Tested below 548 549 def testIf(self): 550 # 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] 551 if 1: pass 552 if 1: pass 553 else: pass 554 if 0: pass 555 elif 0: pass 556 if 0: pass 557 elif 0: pass 558 elif 0: pass 559 elif 0: pass 560 else: pass 561 562 def testWhile(self): 563 # 'while' test ':' suite ['else' ':' suite] 564 while 0: pass 565 while 0: pass 566 else: pass 567 568 # Issue1920: "while 0" is optimized away, 569 # ensure that the "else" clause is still present. 570 x = 0 571 while 0: 572 x = 1 573 else: 574 x = 2 575 self.assertEquals(x, 2) 576 577 def testFor(self): 578 # 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite] 579 for i in 1, 2, 3: pass 580 for i, j, k in (): pass 581 else: pass 582 class Squares: 583 def __init__(self, max): 584 self.max = max 585 self.sofar = [] 586 def __len__(self): return len(self.sofar) 587 def __getitem__(self, i): 588 if not 0 <= i < self.max: raise IndexError 589 n = len(self.sofar) 590 while n <= i: 591 self.sofar.append(n*n) 592 n = n+1 593 return self.sofar[i] 594 n = 0 595 for x in Squares(10): n = n+x 596 if n != 285: 597 self.fail('for over growing sequence') 598 599 result = [] 600 for x, in [(1,), (2,), (3,)]: 601 result.append(x) 602 self.assertEqual(result, [1, 2, 3]) 603 604 def testTry(self): 605 ### try_stmt: 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite] 606 ### | 'try' ':' suite 'finally' ':' suite 607 ### except_clause: 'except' [expr ['as' expr]] 608 try: 609 1/0 610 except ZeroDivisionError: 611 pass 612 else: 613 pass 614 try: 1/0 615 except EOFError: pass 616 except TypeError as msg: pass 617 except RuntimeError as msg: pass 618 except: pass 619 else: pass 620 try: 1/0 621 except (EOFError, TypeError, ZeroDivisionError): pass 622 try: 1/0 623 except (EOFError, TypeError, ZeroDivisionError) as msg: pass 624 try: pass 625 finally: pass 626 627 def testSuite(self): 628 # simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT 629 if 1: pass 630 if 1: 631 pass 632 if 1: 633 # 634 # 635 # 636 pass 637 pass 638 # 639 pass 640 # 641 642 def testTest(self): 643 ### and_test ('or' and_test)* 644 ### and_test: not_test ('and' not_test)* 645 ### not_test: 'not' not_test | comparison 646 if not 1: pass 647 if 1 and 1: pass 648 if 1 or 1: pass 649 if not not not 1: pass 650 if not 1 and 1 and 1: pass 651 if 1 and 1 or 1 and 1 and 1 or not 1 and 1: pass 652 653 def testComparison(self): 654 ### comparison: expr (comp_op expr)* 655 ### comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'|'is' 'not' 656 if 1: pass 657 x = (1 == 1) 658 if 1 == 1: pass 659 if 1 != 1: pass 660 if 1 < 1: pass 661 if 1 > 1: pass 662 if 1 <= 1: pass 663 if 1 >= 1: pass 664 if 1 is 1: pass 665 if 1 is not 1: pass 666 if 1 in (): pass 667 if 1 not in (): pass 668 if 1 < 1 > 1 == 1 >= 1 <= 1 != 1 in 1 not in 1 is 1 is not 1: pass 669 670 def testBinaryMaskOps(self): 671 x = 1 & 1 672 x = 1 ^ 1 673 x = 1 | 1 674 675 def testShiftOps(self): 676 x = 1 << 1 677 x = 1 >> 1 678 x = 1 << 1 >> 1 679 680 def testAdditiveOps(self): 681 x = 1 682 x = 1 + 1 683 x = 1 - 1 - 1 684 x = 1 - 1 + 1 - 1 + 1 685 686 def testMultiplicativeOps(self): 687 x = 1 * 1 688 x = 1 / 1 689 x = 1 % 1 690 x = 1 / 1 * 1 % 1 691 692 def testUnaryOps(self): 693 x = +1 694 x = -1 695 x = ~1 696 x = ~1 ^ 1 & 1 | 1 & 1 ^ -1 697 x = -1*1/1 + 1*1 - ---1*1 698 699 def testSelectors(self): 700 ### trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME 701 ### subscript: expr | [expr] ':' [expr] 702 703 import sys, time 704 c = sys.path[0] 705 x = time.time() 706 x = sys.modules['time'].time() 707 a = '01234' 708 c = a[0] 709 c = a[-1] 710 s = a[0:5] 711 s = a[:5] 712 s = a[0:] 713 s = a[:] 714 s = a[-5:] 715 s = a[:-1] 716 s = a[-4:-3] 717 # A rough test of SF bug 1333982. http://python.org/sf/1333982 718 # The testing here is fairly incomplete. 719 # Test cases should include: commas with 1 and 2 colons 720 d = {} 721 d[1] = 1 722 d[1,] = 2 723 d[1,2] = 3 724 d[1,2,3] = 4 725 L = list(d) 726 L.sort(key=lambda x: x if isinstance(x, tuple) else ()) 727 self.assertEquals(str(L), '[1, (1,), (1, 2), (1, 2, 3)]') 728 729 def testAtoms(self): 730 ### atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictsetmaker] '}' | NAME | NUMBER | STRING 731 ### dictsetmaker: (test ':' test (',' test ':' test)* [',']) | (test (',' test)* [',']) 732 733 x = (1) 734 x = (1 or 2 or 3) 735 x = (1 or 2 or 3, 2, 3) 736 737 x = [] 738 x = [1] 739 x = [1 or 2 or 3] 740 x = [1 or 2 or 3, 2, 3] 741 x = [] 742 743 x = {} 744 x = {'one': 1} 745 x = {'one': 1,} 746 x = {'one' or 'two': 1 or 2} 747 x = {'one': 1, 'two': 2} 748 x = {'one': 1, 'two': 2,} 749 x = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6} 750 751 x = {'one'} 752 x = {'one', 1,} 753 x = {'one', 'two', 'three'} 754 x = {2, 3, 4,} 755 756 x = x 757 x = 'x' 758 x = 123 759 760 ### exprlist: expr (',' expr)* [','] 761 ### testlist: test (',' test)* [','] 762 # These have been exercised enough above 763 764 def testClassdef(self): 765 # 'class' NAME ['(' [testlist] ')'] ':' suite 766 class B: pass 767 class B2(): pass 768 class C1(B): pass 769 class C2(B): pass 770 class D(C1, C2, B): pass 771 class C: 772 def meth1(self): pass 773 def meth2(self, arg): pass 774 def meth3(self, a1, a2): pass 775 776 # decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE 777 # decorators: decorator+ 778 # decorated: decorators (classdef | funcdef) 779 def class_decorator(x): return x 780 @class_decorator 781 class G: pass 782 783 def testDictcomps(self): 784 # dictorsetmaker: ( (test ':' test (comp_for | 785 # (',' test ':' test)* [','])) | 786 # (test (comp_for | (',' test)* [','])) ) 787 nums = [1, 2, 3] 788 self.assertEqual({i:i+1 for i in nums}, {1: 2, 2: 3, 3: 4}) 789 790 def testListcomps(self): 791 # list comprehension tests 792 nums = [1, 2, 3, 4, 5] 793 strs = ["Apple", "Banana", "Coconut"] 794 spcs = [" Apple", " Banana ", "Coco nut "] 795 796 self.assertEqual([s.strip() for s in spcs], ['Apple', 'Banana', 'Coco nut']) 797 self.assertEqual([3 * x for x in nums], [3, 6, 9, 12, 15]) 798 self.assertEqual([x for x in nums if x > 2], [3, 4, 5]) 799 self.assertEqual([(i, s) for i in nums for s in strs], 800 [(1, 'Apple'), (1, 'Banana'), (1, 'Coconut'), 801 (2, 'Apple'), (2, 'Banana'), (2, 'Coconut'), 802 (3, 'Apple'), (3, 'Banana'), (3, 'Coconut'), 803 (4, 'Apple'), (4, 'Banana'), (4, 'Coconut'), 804 (5, 'Apple'), (5, 'Banana'), (5, 'Coconut')]) 805 self.assertEqual([(i, s) for i in nums for s in [f for f in strs if "n" in f]], 806 [(1, 'Banana'), (1, 'Coconut'), (2, 'Banana'), (2, 'Coconut'), 807 (3, 'Banana'), (3, 'Coconut'), (4, 'Banana'), (4, 'Coconut'), 808 (5, 'Banana'), (5, 'Coconut')]) 809 self.assertEqual([(lambda a:[a**i for i in range(a+1)])(j) for j in range(5)], 810 [[1], [1, 1], [1, 2, 4], [1, 3, 9, 27], [1, 4, 16, 64, 256]]) 811 812 def test_in_func(l): 813 return [0 < x < 3 for x in l if x > 2] 814 815 self.assertEqual(test_in_func(nums), [False, False, False]) 816 817 def test_nested_front(): 818 self.assertEqual([[y for y in [x, x + 1]] for x in [1,3,5]], 819 [[1, 2], [3, 4], [5, 6]]) 820 821 test_nested_front() 822 823 check_syntax_error(self, "[i, s for i in nums for s in strs]") 824 check_syntax_error(self, "[x if y]") 825 826 suppliers = [ 827 (1, "Boeing"), 828 (2, "Ford"), 829 (3, "Macdonalds") 830 ] 831 832 parts = [ 833 (10, "Airliner"), 834 (20, "Engine"), 835 (30, "Cheeseburger") 836 ] 837 838 suppart = [ 839 (1, 10), (1, 20), (2, 20), (3, 30) 840 ] 841 842 x = [ 843 (sname, pname) 844 for (sno, sname) in suppliers 845 for (pno, pname) in parts 846 for (sp_sno, sp_pno) in suppart 847 if sno == sp_sno and pno == sp_pno 848 ] 849 850 self.assertEqual(x, [('Boeing', 'Airliner'), ('Boeing', 'Engine'), ('Ford', 'Engine'), 851 ('Macdonalds', 'Cheeseburger')]) 852 853 def testGenexps(self): 854 # generator expression tests 855 g = ([x for x in range(10)] for x in range(1)) 856 self.assertEqual(next(g), [x for x in range(10)]) 857 try: 858 next(g) 859 self.fail('should produce StopIteration exception') 860 except StopIteration: 861 pass 862 863 a = 1 864 try: 865 g = (a for d in a) 866 next(g) 867 self.fail('should produce TypeError') 868 except TypeError: 869 pass 870 871 self.assertEqual(list((x, y) for x in 'abcd' for y in 'abcd'), [(x, y) for x in 'abcd' for y in 'abcd']) 872 self.assertEqual(list((x, y) for x in 'ab' for y in 'xy'), [(x, y) for x in 'ab' for y in 'xy']) 873 874 a = [x for x in range(10)] 875 b = (x for x in (y for y in a)) 876 self.assertEqual(sum(b), sum([x for x in range(10)])) 877 878 self.assertEqual(sum(x**2 for x in range(10)), sum([x**2 for x in range(10)])) 879 self.assertEqual(sum(x*x for x in range(10) if x%2), sum([x*x for x in range(10) if x%2])) 880 self.assertEqual(sum(x for x in (y for y in range(10))), sum([x for x in range(10)])) 881 self.assertEqual(sum(x for x in (y for y in (z for z in range(10)))), sum([x for x in range(10)])) 882 self.assertEqual(sum(x for x in [y for y in (z for z in range(10))]), sum([x for x in range(10)])) 883 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)])) 884 self.assertEqual(sum(x for x in (y for y in (z for z in range(10) if True) if False) if True), 0) 885 check_syntax_error(self, "foo(x for x in range(10), 100)") 886 check_syntax_error(self, "foo(100, x for x in range(10))") 887 888 def testComprehensionSpecials(self): 889 # test for outmost iterable precomputation 890 x = 10; g = (i for i in range(x)); x = 5 891 self.assertEqual(len(list(g)), 10) 892 893 # This should hold, since we're only precomputing outmost iterable. 894 x = 10; t = False; g = ((i,j) for i in range(x) if t for j in range(x)) 895 x = 5; t = True; 896 self.assertEqual([(i,j) for i in range(10) for j in range(5)], list(g)) 897 898 # Grammar allows multiple adjacent 'if's in listcomps and genexps, 899 # even though it's silly. Make sure it works (ifelse broke this.) 900 self.assertEqual([ x for x in range(10) if x % 2 if x % 3 ], [1, 5, 7]) 901 self.assertEqual(list(x for x in range(10) if x % 2 if x % 3), [1, 5, 7]) 902 903 # verify unpacking single element tuples in listcomp/genexp. 904 self.assertEqual([x for x, in [(4,), (5,), (6,)]], [4, 5, 6]) 905 self.assertEqual(list(x for x, in [(7,), (8,), (9,)]), [7, 8, 9]) 906 907 def test_with_statement(self): 908 class manager(object): 909 def __enter__(self): 910 return (1, 2) 911 def __exit__(self, *args): 912 pass 913 914 with manager(): 915 pass 916 with manager() as x: 917 pass 918 with manager() as (x, y): 919 pass 920 with manager(), manager(): 921 pass 922 with manager() as x, manager() as y: 923 pass 924 with manager() as x, manager(): 925 pass 926 927 def testIfElseExpr(self): 928 # Test ifelse expressions in various cases 929 def _checkeval(msg, ret): 930 "helper to check that evaluation of expressions is done correctly" 931 print(x) 932 return ret 933 934 # the next line is not allowed anymore 935 #self.assertEqual([ x() for x in lambda: True, lambda: False if x() ], [True]) 936 self.assertEqual([ x() for x in (lambda: True, lambda: False) if x() ], [True]) 937 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]) 938 self.assertEqual((5 if 1 else _checkeval("check 1", 0)), 5) 939 self.assertEqual((_checkeval("check 2", 0) if 0 else 5), 5) 940 self.assertEqual((5 and 6 if 0 else 1), 1) 941 self.assertEqual(((5 and 6) if 0 else 1), 1) 942 self.assertEqual((5 and (6 if 1 else 1)), 6) 943 self.assertEqual((0 or _checkeval("check 3", 2) if 0 else 3), 3) 944 self.assertEqual((1 or _checkeval("check 4", 2) if 1 else _checkeval("check 5", 3)), 1) 945 self.assertEqual((0 or 5 if 1 else _checkeval("check 6", 3)), 5) 946 self.assertEqual((not 5 if 1 else 1), False) 947 self.assertEqual((not 5 if 0 else 1), 1) 948 self.assertEqual((6 + 1 if 1 else 2), 7) 949 self.assertEqual((6 - 1 if 1 else 2), 5) 950 self.assertEqual((6 * 2 if 1 else 4), 12) 951 self.assertEqual((6 / 2 if 1 else 3), 3) 952 self.assertEqual((6 < 4 if 0 else 2), 2) 953 954 955def test_main(): 956 run_unittest(TokenTests, GrammarTests) 957 958if __name__ == '__main__': 959 test_main() 960