0001 # Python test set -- part 1, grammar. 0002 # This just tests whether the parser accepts them all. 0003 0004 # NOTE: When you run this test as a script from the command line, you 0005 # get warnings about certain hex/oct constants. Since those are 0006 # issued by the parser, you can't suppress them by adding a 0007 # filterwarnings() call to this module. Therefore, to shut up the 0008 # regression test, the filterwarnings() call has been added to 0009 # regrtest.py. 0010 0011 from test.test_support import TestFailed, verify, check_syntax 0012 import sys 0013 0014 print '1. Parser' 0015 0016 print '1.1 Tokens' 0017 0018 print '1.1.1 Backslashes' 0019 0020 # Backslash means line continuation: 0021 x = 1 \ 0022 + 1 0023 if x != 2: raise TestFailed, 'backslash for line continuation' 0024 0025 # Backslash does not means continuation in comments :\ 0026 x = 0 0027 if x != 0: raise TestFailed, 'backslash ending comment' 0028 0029 print '1.1.2 Numeric literals' 0030 0031 print '1.1.2.1 Plain integers' 0032 if 0xff != 255: raise TestFailed, 'hex int' 0033 if 0377 != 255: raise TestFailed, 'octal int' 0034 if 2147483647 != 017777777777: raise TestFailed, 'large positive int' 0035 try: 0036 from sys import maxint 0037 except ImportError: 0038 maxint = 2147483647 0039 if maxint == 2147483647: 0040 # The following test will start to fail in Python 2.4; 0041 # change the 020000000000 to -020000000000 0042 if -2147483647-1 != -020000000000: raise TestFailed, 'max negative int' 0043 # XXX -2147483648 0044 if 037777777777 < 0: raise TestFailed, 'large oct' 0045 if 0xffffffff < 0: raise TestFailed, 'large hex' 0046 for s in '2147483648', '040000000000', '0x100000000': 0047 try: 0048 x = eval(s) 0049 except OverflowError: 0050 print "OverflowError on huge integer literal " + repr(s) 0051 elif eval('maxint == 9223372036854775807'): 0052 if eval('-9223372036854775807-1 != -01000000000000000000000'): 0053 raise TestFailed, 'max negative int' 0054 if eval('01777777777777777777777') < 0: raise TestFailed, 'large oct' 0055 if eval('0xffffffffffffffff') < 0: raise TestFailed, 'large hex' 0056 for s in '9223372036854775808', '02000000000000000000000', \ 0057 '0x10000000000000000': 0058 try: 0059 x = eval(s) 0060 except OverflowError: 0061 print "OverflowError on huge integer literal " + repr(s) 0062 else: 0063 print 'Weird maxint value', maxint 0064 0065 print '1.1.2.2 Long integers' 0066 x = 0L 0067 x = 0l 0068 x = 0xffffffffffffffffL 0069 x = 0xffffffffffffffffl 0070 x = 077777777777777777L 0071 x = 077777777777777777l 0072 x = 123456789012345678901234567890L 0073 x = 123456789012345678901234567890l 0074 0075 print '1.1.2.3 Floating point' 0076 x = 3.14 0077 x = 314. 0078 x = 0.314 0079 # XXX x = 000.314 0080 x = .314 0081 x = 3e14 0082 x = 3E14 0083 x = 3e-14 0084 x = 3e+14 0085 x = 3.e14 0086 x = .3e14 0087 x = 3.1e4 0088 0089 print '1.1.3 String literals' 0090 0091 x = ''; y = ""; verify(len(x) == 0 and x == y) 0092 x = '\''; y = "'"; verify(len(x) == 1 and x == y and ord(x) == 39) 0093 x = '"'; y = "\""; verify(len(x) == 1 and x == y and ord(x) == 34) 0094 x = "doesn't \"shrink\" does it" 0095 y = 'doesn\'t "shrink" does it' 0096 verify(len(x) == 24 and x == y) 0097 x = "does \"shrink\" doesn't it" 0098 y = 'does "shrink" doesn\'t it' 0099 verify(len(x) == 24 and x == y) 0100 x = """ 0101 The "quick" 0102 brown fox 0103 jumps over 0104 the 'lazy' dog. 0105 """ 0106 y = '\nThe "quick"\nbrown fox\njumps over\nthe \'lazy\' dog.\n' 0107 verify(x == y) 0108 y = ''' 0109 The "quick" 0110 brown fox 0111 jumps over 0112 the 'lazy' dog. 0113 '''; verify(x == y) 0114 y = "\n\ 0115 The \"quick\"\n\ 0116 brown fox\n\ 0117 jumps over\n\ 0118 the 'lazy' dog.\n\ 0119 "; verify(x == y) 0120 y = '\n\ 0121 The \"quick\"\n\ 0122 brown fox\n\ 0123 jumps over\n\ 0124 the \'lazy\' dog.\n\ 0125 '; verify(x == y) 0126 0127 0128 print '1.2 Grammar' 0129 0130 print 'single_input' # NEWLINE | simple_stmt | compound_stmt NEWLINE 0131 # XXX can't test in a script -- this rule is only used when interactive 0132 0133 print 'file_input' # (NEWLINE | stmt)* ENDMARKER 0134 # Being tested as this very moment this very module 0135 0136 print 'expr_input' # testlist NEWLINE 0137 # XXX Hard to test -- used only in calls to input() 0138 0139 print 'eval_input' # testlist ENDMARKER 0140 x = eval('1, 0 or 1') 0141 0142 print 'funcdef' 0143 ### 'def' NAME parameters ':' suite 0144 ### parameters: '(' [varargslist] ')' 0145 ### varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' ('**'|'*' '*') NAME] 0146 ### | ('**'|'*' '*') NAME) 0147 ### | fpdef ['=' test] (',' fpdef ['=' test])* [','] 0148 ### fpdef: NAME | '(' fplist ')' 0149 ### fplist: fpdef (',' fpdef)* [','] 0150 ### arglist: (argument ',')* (argument | *' test [',' '**' test] | '**' test) 0151 ### argument: [test '='] test # Really [keyword '='] test 0152 def f1(): pass 0153 f1() 0154 f1(*()) 0155 f1(*(), **{}) 0156 def f2(one_argument): pass 0157 def f3(two, arguments): pass 0158 def f4(two, (compound, (argument, list))): pass 0159 def f5((compound, first), two): pass 0160 verify(f2.func_code.co_varnames == ('one_argument',)) 0161 verify(f3.func_code.co_varnames == ('two', 'arguments')) 0162 if sys.platform.startswith('java'): 0163 verify(f4.func_code.co_varnames == 0164 ('two', '(compound, (argument, list))', 'compound', 'argument', 0165 'list',)) 0166 verify(f5.func_code.co_varnames == 0167 ('(compound, first)', 'two', 'compound', 'first')) 0168 else: 0169 verify(f4.func_code.co_varnames == ('two', '.2', 'compound', 0170 'argument', 'list')) 0171 verify(f5.func_code.co_varnames == ('.0', 'two', 'compound', 'first')) 0172 def a1(one_arg,): pass 0173 def a2(two, args,): pass 0174 def v0(*rest): pass 0175 def v1(a, *rest): pass 0176 def v2(a, b, *rest): pass 0177 def v3(a, (b, c), *rest): return a, b, c, rest 0178 if sys.platform.startswith('java'): 0179 verify(v3.func_code.co_varnames == ('a', '(b, c)', 'rest', 'b', 'c')) 0180 else: 0181 verify(v3.func_code.co_varnames == ('a', '.2', 'rest', 'b', 'c')) 0182 verify(v3(1, (2, 3), 4) == (1, 2, 3, (4,))) 0183 def d01(a=1): pass 0184 d01() 0185 d01(1) 0186 d01(*(1,)) 0187 d01(**{'a':2}) 0188 def d11(a, b=1): pass 0189 d11(1) 0190 d11(1, 2) 0191 d11(1, **{'b':2}) 0192 def d21(a, b, c=1): pass 0193 d21(1, 2) 0194 d21(1, 2, 3) 0195 d21(*(1, 2, 3)) 0196 d21(1, *(2, 3)) 0197 d21(1, 2, *(3,)) 0198 d21(1, 2, **{'c':3}) 0199 def d02(a=1, b=2): pass 0200 d02() 0201 d02(1) 0202 d02(1, 2) 0203 d02(*(1, 2)) 0204 d02(1, *(2,)) 0205 d02(1, **{'b':2}) 0206 d02(**{'a': 1, 'b': 2}) 0207 def d12(a, b=1, c=2): pass 0208 d12(1) 0209 d12(1, 2) 0210 d12(1, 2, 3) 0211 def d22(a, b, c=1, d=2): pass 0212 d22(1, 2) 0213 d22(1, 2, 3) 0214 d22(1, 2, 3, 4) 0215 def d01v(a=1, *rest): pass 0216 d01v() 0217 d01v(1) 0218 d01v(1, 2) 0219 d01v(*(1, 2, 3, 4)) 0220 d01v(*(1,)) 0221 d01v(**{'a':2}) 0222 def d11v(a, b=1, *rest): pass 0223 d11v(1) 0224 d11v(1, 2) 0225 d11v(1, 2, 3) 0226 def d21v(a, b, c=1, *rest): pass 0227 d21v(1, 2) 0228 d21v(1, 2, 3) 0229 d21v(1, 2, 3, 4) 0230 d21v(*(1, 2, 3, 4)) 0231 d21v(1, 2, **{'c': 3}) 0232 def d02v(a=1, b=2, *rest): pass 0233 d02v() 0234 d02v(1) 0235 d02v(1, 2) 0236 d02v(1, 2, 3) 0237 d02v(1, *(2, 3, 4)) 0238 d02v(**{'a': 1, 'b': 2}) 0239 def d12v(a, b=1, c=2, *rest): pass 0240 d12v(1) 0241 d12v(1, 2) 0242 d12v(1, 2, 3) 0243 d12v(1, 2, 3, 4) 0244 d12v(*(1, 2, 3, 4)) 0245 d12v(1, 2, *(3, 4, 5)) 0246 d12v(1, *(2,), **{'c': 3}) 0247 def d22v(a, b, c=1, d=2, *rest): pass 0248 d22v(1, 2) 0249 d22v(1, 2, 3) 0250 d22v(1, 2, 3, 4) 0251 d22v(1, 2, 3, 4, 5) 0252 d22v(*(1, 2, 3, 4)) 0253 d22v(1, 2, *(3, 4, 5)) 0254 d22v(1, *(2, 3), **{'d': 4}) 0255 0256 ### lambdef: 'lambda' [varargslist] ':' test 0257 print 'lambdef' 0258 l1 = lambda : 0 0259 verify(l1() == 0) 0260 l2 = lambda : a[d] # XXX just testing the expression 0261 l3 = lambda : [2 < x for x in [-1, 3, 0L]] 0262 verify(l3() == [0, 1, 0]) 0263 l4 = lambda x = lambda y = lambda z=1 : z : y() : x() 0264 verify(l4() == 1) 0265 l5 = lambda x, y, z=2: x + y + z 0266 verify(l5(1, 2) == 5) 0267 verify(l5(1, 2, 3) == 6) 0268 check_syntax("lambda x: x = 2") 0269 0270 ### stmt: simple_stmt | compound_stmt 0271 # Tested below 0272 0273 ### simple_stmt: small_stmt (';' small_stmt)* [';'] 0274 print 'simple_stmt' 0275 x = 1; pass; del x 0276 0277 ### small_stmt: expr_stmt | print_stmt | pass_stmt | del_stmt | flow_stmt | import_stmt | global_stmt | access_stmt | exec_stmt 0278 # Tested below 0279 0280 print 'expr_stmt' # (exprlist '=')* exprlist 0281 1 0282 1, 2, 3 0283 x = 1 0284 x = 1, 2, 3 0285 x = y = z = 1, 2, 3 0286 x, y, z = 1, 2, 3 0287 abc = a, b, c = x, y, z = xyz = 1, 2, (3, 4) 0288 # NB these variables are deleted below 0289 0290 check_syntax("x + 1 = 1") 0291 check_syntax("a + 1 = b + 2") 0292 0293 print 'print_stmt' # 'print' (test ',')* [test] 0294 print 1, 2, 3 0295 print 1, 2, 3, 0296 print 0297 print 0 or 1, 0 or 1, 0298 print 0 or 1 0299 0300 print 'extended print_stmt' # 'print' '>>' test ',' 0301 import sys 0302 print >> sys.stdout, 1, 2, 3 0303 print >> sys.stdout, 1, 2, 3, 0304 print >> sys.stdout 0305 print >> sys.stdout, 0 or 1, 0 or 1, 0306 print >> sys.stdout, 0 or 1 0307 0308 # test printing to an instance 0309 class Gulp: 0310 def write(self, msg): pass 0311 0312 gulp = Gulp() 0313 print >> gulp, 1, 2, 3 0314 print >> gulp, 1, 2, 3, 0315 print >> gulp 0316 print >> gulp, 0 or 1, 0 or 1, 0317 print >> gulp, 0 or 1 0318 0319 # test print >> None 0320 def driver(): 0321 oldstdout = sys.stdout 0322 sys.stdout = Gulp() 0323 try: 0324 tellme(Gulp()) 0325 tellme() 0326 finally: 0327 sys.stdout = oldstdout 0328 0329 # we should see this once 0330 def tellme(file=sys.stdout): 0331 print >> file, 'hello world' 0332 0333 driver() 0334 0335 # we should not see this at all 0336 def tellme(file=None): 0337 print >> file, 'goodbye universe' 0338 0339 driver() 0340 0341 # syntax errors 0342 check_syntax('print ,') 0343 check_syntax('print >> x,') 0344 0345 print 'del_stmt' # 'del' exprlist 0346 del abc 0347 del x, y, (z, xyz) 0348 0349 print 'pass_stmt' # 'pass' 0350 pass 0351 0352 print 'flow_stmt' # break_stmt | continue_stmt | return_stmt | raise_stmt 0353 # Tested below 0354 0355 print 'break_stmt' # 'break' 0356 while 1: break 0357 0358 print 'continue_stmt' # 'continue' 0359 i = 1 0360 while i: i = 0; continue 0361 0362 msg = "" 0363 while not msg: 0364 msg = "continue + try/except ok" 0365 try: 0366 continue 0367 msg = "continue failed to continue inside try" 0368 except: 0369 msg = "continue inside try called except block" 0370 print msg 0371 0372 msg = "" 0373 while not msg: 0374 msg = "finally block not called" 0375 try: 0376 continue 0377 finally: 0378 msg = "continue + try/finally ok" 0379 print msg 0380 0381 0382 # This test warrants an explanation. It is a test specifically for SF bugs 0383 # #463359 and #462937. The bug is that a 'break' statement executed or 0384 # exception raised inside a try/except inside a loop, *after* a continue 0385 # statement has been executed in that loop, will cause the wrong number of 0386 # arguments to be popped off the stack and the instruction pointer reset to 0387 # a very small number (usually 0.) Because of this, the following test 0388 # *must* written as a function, and the tracking vars *must* be function 0389 # arguments with default values. Otherwise, the test will loop and loop. 0390 0391 print "testing continue and break in try/except in loop" 0392 def test_break_continue_loop(extra_burning_oil = 1, count=0): 0393 big_hippo = 2 0394 while big_hippo: 0395 count += 1 0396 try: 0397 if extra_burning_oil and big_hippo == 1: 0398 extra_burning_oil -= 1 0399 break 0400 big_hippo -= 1 0401 continue 0402 except: 0403 raise 0404 if count > 2 or big_hippo <> 1: 0405 print "continue then break in try/except in loop broken!" 0406 test_break_continue_loop() 0407 0408 print 'return_stmt' # 'return' [testlist] 0409 def g1(): return 0410 def g2(): return 1 0411 g1() 0412 x = g2() 0413 0414 print 'raise_stmt' # 'raise' test [',' test] 0415 try: raise RuntimeError, 'just testing' 0416 except RuntimeError: pass 0417 try: raise KeyboardInterrupt 0418 except KeyboardInterrupt: pass 0419 0420 print 'import_name' # 'import' dotted_as_names 0421 import sys 0422 import time, sys 0423 print 'import_from' # 'from' dotted_name 'import' ('*' | '(' import_as_names ')' | import_as_names) 0424 from time import time 0425 from time import (time) 0426 from sys import * 0427 from sys import path, argv 0428 from sys import (path, argv) 0429 from sys import (path, argv,) 0430 0431 print 'global_stmt' # 'global' NAME (',' NAME)* 0432 def f(): 0433 global a 0434 global a, b 0435 global one, two, three, four, five, six, seven, eight, nine, ten 0436 0437 print 'exec_stmt' # 'exec' expr ['in' expr [',' expr]] 0438 def f(): 0439 z = None 0440 del z 0441 exec 'z=1+1\n' 0442 if z != 2: raise TestFailed, 'exec \'z=1+1\'\\n' 0443 del z 0444 exec 'z=1+1' 0445 if z != 2: raise TestFailed, 'exec \'z=1+1\'' 0446 z = None 0447 del z 0448 import types 0449 if hasattr(types, "UnicodeType"): 0450 exec r"""if 1: 0451 exec u'z=1+1\n' 0452 if z != 2: raise TestFailed, 'exec u\'z=1+1\'\\n' 0453 del z 0454 exec u'z=1+1' 0455 if z != 2: raise TestFailed, 'exec u\'z=1+1\'' 0456 """ 0457 f() 0458 g = {} 0459 exec 'z = 1' in g 0460 if g.has_key('__builtins__'): del g['__builtins__'] 0461 if g != {'z': 1}: raise TestFailed, 'exec \'z = 1\' in g' 0462 g = {} 0463 l = {} 0464 0465 import warnings 0466 warnings.filterwarnings("ignore", "global statement", module="<string>") 0467 exec 'global a; a = 1; b = 2' in g, l 0468 if g.has_key('__builtins__'): del g['__builtins__'] 0469 if l.has_key('__builtins__'): del l['__builtins__'] 0470 if (g, l) != ({'a':1}, {'b':2}): raise TestFailed, 'exec ... in g (%s), l (%s)' %(g,l) 0471 0472 0473 print "assert_stmt" # assert_stmt: 'assert' test [',' test] 0474 assert 1 0475 assert 1, 1 0476 assert lambda x:x 0477 assert 1, lambda x:x+1 0478 0479 ### compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef 0480 # Tested below 0481 0482 print 'if_stmt' # 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] 0483 if 1: pass 0484 if 1: pass 0485 else: pass 0486 if 0: pass 0487 elif 0: pass 0488 if 0: pass 0489 elif 0: pass 0490 elif 0: pass 0491 elif 0: pass 0492 else: pass 0493 0494 print 'while_stmt' # 'while' test ':' suite ['else' ':' suite] 0495 while 0: pass 0496 while 0: pass 0497 else: pass 0498 0499 print 'for_stmt' # 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite] 0500 for i in 1, 2, 3: pass 0501 for i, j, k in (): pass 0502 else: pass 0503 class Squares: 0504 def __init__(self, max): 0505 self.max = max 0506 self.sofar = [] 0507 def __len__(self): return len(self.sofar) 0508 def __getitem__(self, i): 0509 if not 0 <= i < self.max: raise IndexError 0510 n = len(self.sofar) 0511 while n <= i: 0512 self.sofar.append(n*n) 0513 n = n+1 0514 return self.sofar[i] 0515 n = 0 0516 for x in Squares(10): n = n+x 0517 if n != 285: raise TestFailed, 'for over growing sequence' 0518 0519 print 'try_stmt' 0520 ### try_stmt: 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite] 0521 ### | 'try' ':' suite 'finally' ':' suite 0522 ### except_clause: 'except' [expr [',' expr]] 0523 try: 0524 1/0 0525 except ZeroDivisionError: 0526 pass 0527 else: 0528 pass 0529 try: 1/0 0530 except EOFError: pass 0531 except TypeError, msg: pass 0532 except RuntimeError, msg: pass 0533 except: pass 0534 else: pass 0535 try: 1/0 0536 except (EOFError, TypeError, ZeroDivisionError): pass 0537 try: 1/0 0538 except (EOFError, TypeError, ZeroDivisionError), msg: pass 0539 try: pass 0540 finally: pass 0541 0542 print 'suite' # simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT 0543 if 1: pass 0544 if 1: 0545 pass 0546 if 1: 0547 # 0548 # 0549 # 0550 pass 0551 pass 0552 # 0553 pass 0554 # 0555 0556 print 'test' 0557 ### and_test ('or' and_test)* 0558 ### and_test: not_test ('and' not_test)* 0559 ### not_test: 'not' not_test | comparison 0560 if not 1: pass 0561 if 1 and 1: pass 0562 if 1 or 1: pass 0563 if not not not 1: pass 0564 if not 1 and 1 and 1: pass 0565 if 1 and 1 or 1 and 1 and 1 or not 1 and 1: pass 0566 0567 print 'comparison' 0568 ### comparison: expr (comp_op expr)* 0569 ### comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not' 0570 if 1: pass 0571 x = (1 == 1) 0572 if 1 == 1: pass 0573 if 1 != 1: pass 0574 if 1 <> 1: pass 0575 if 1 < 1: pass 0576 if 1 > 1: pass 0577 if 1 <= 1: pass 0578 if 1 >= 1: pass 0579 if 1 is 1: pass 0580 if 1 is not 1: pass 0581 if 1 in (): pass 0582 if 1 not in (): pass 0583 if 1 < 1 > 1 == 1 >= 1 <= 1 <> 1 != 1 in 1 not in 1 is 1 is not 1: pass 0584 0585 print 'binary mask ops' 0586 x = 1 & 1 0587 x = 1 ^ 1 0588 x = 1 | 1 0589 0590 print 'shift ops' 0591 x = 1 << 1 0592 x = 1 >> 1 0593 x = 1 << 1 >> 1 0594 0595 print 'additive ops' 0596 x = 1 0597 x = 1 + 1 0598 x = 1 - 1 - 1 0599 x = 1 - 1 + 1 - 1 + 1 0600 0601 print 'multiplicative ops' 0602 x = 1 * 1 0603 x = 1 / 1 0604 x = 1 % 1 0605 x = 1 / 1 * 1 % 1 0606 0607 print 'unary ops' 0608 x = +1 0609 x = -1 0610 x = ~1 0611 x = ~1 ^ 1 & 1 | 1 & 1 ^ -1 0612 x = -1*1/1 + 1*1 - ---1*1 0613 0614 print 'selectors' 0615 ### trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME 0616 ### subscript: expr | [expr] ':' [expr] 0617 f1() 0618 f2(1) 0619 f2(1,) 0620 f3(1, 2) 0621 f3(1, 2,) 0622 f4(1, (2, (3, 4))) 0623 v0() 0624 v0(1) 0625 v0(1,) 0626 v0(1,2) 0627 v0(1,2,3,4,5,6,7,8,9,0) 0628 v1(1) 0629 v1(1,) 0630 v1(1,2) 0631 v1(1,2,3) 0632 v1(1,2,3,4,5,6,7,8,9,0) 0633 v2(1,2) 0634 v2(1,2,3) 0635 v2(1,2,3,4) 0636 v2(1,2,3,4,5,6,7,8,9,0) 0637 v3(1,(2,3)) 0638 v3(1,(2,3),4) 0639 v3(1,(2,3),4,5,6,7,8,9,0) 0640 print 0641 import sys, time 0642 c = sys.path[0] 0643 x = time.time() 0644 x = sys.modules['time'].time() 0645 a = '01234' 0646 c = a[0] 0647 c = a[-1] 0648 s = a[0:5] 0649 s = a[:5] 0650 s = a[0:] 0651 s = a[:] 0652 s = a[-5:] 0653 s = a[:-1] 0654 s = a[-4:-3] 0655 0656 print 'atoms' 0657 ### atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING 0658 ### dictmaker: test ':' test (',' test ':' test)* [','] 0659 0660 x = (1) 0661 x = (1 or 2 or 3) 0662 x = (1 or 2 or 3, 2, 3) 0663 0664 x = [] 0665 x = [1] 0666 x = [1 or 2 or 3] 0667 x = [1 or 2 or 3, 2, 3] 0668 x = [] 0669 0670 x = {} 0671 x = {'one': 1} 0672 x = {'one': 1,} 0673 x = {'one' or 'two': 1 or 2} 0674 x = {'one': 1, 'two': 2} 0675 x = {'one': 1, 'two': 2,} 0676 x = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6} 0677 0678 x = `x` 0679 x = `1 or 2 or 3` 0680 x = x 0681 x = 'x' 0682 x = 123 0683 0684 ### exprlist: expr (',' expr)* [','] 0685 ### testlist: test (',' test)* [','] 0686 # These have been exercised enough above 0687 0688 print 'classdef' # 'class' NAME ['(' testlist ')'] ':' suite 0689 class B: pass 0690 class C1(B): pass 0691 class C2(B): pass 0692 class D(C1, C2, B): pass 0693 class C: 0694 def meth1(self): pass 0695 def meth2(self, arg): pass 0696 def meth3(self, a1, a2): pass 0697 0698 # list comprehension tests 0699 nums = [1, 2, 3, 4, 5] 0700 strs = ["Apple", "Banana", "Coconut"] 0701 spcs = [" Apple", " Banana ", "Coco nut "] 0702 0703 print [s.strip() for s in spcs] 0704 print [3 * x for x in nums] 0705 print [x for x in nums if x > 2] 0706 print [(i, s) for i in nums for s in strs] 0707 print [(i, s) for i in nums for s in [f for f in strs if "n" in f]] 0708 print [(lambda a:[a**i for i in range(a+1)])(j) for j in range(5)] 0709 0710 def test_in_func(l): 0711 return [None < x < 3 for x in l if x > 2] 0712 0713 print test_in_func(nums) 0714 0715 def test_nested_front(): 0716 print [[y for y in [x, x + 1]] for x in [1,3,5]] 0717 0718 test_nested_front() 0719 0720 check_syntax("[i, s for i in nums for s in strs]") 0721 check_syntax("[x if y]") 0722 0723 suppliers = [ 0724 (1, "Boeing"), 0725 (2, "Ford"), 0726 (3, "Macdonalds") 0727 ] 0728 0729 parts = [ 0730 (10, "Airliner"), 0731 (20, "Engine"), 0732 (30, "Cheeseburger") 0733 ] 0734 0735 suppart = [ 0736 (1, 10), (1, 20), (2, 20), (3, 30) 0737 ] 0738 0739 print [ 0740 (sname, pname) 0741 for (sno, sname) in suppliers 0742 for (pno, pname) in parts 0743 for (sp_sno, sp_pno) in suppart 0744 if sno == sp_sno and pno == sp_pno 0745 ] 0746 0747 # generator expression tests 0748 g = ([x for x in range(10)] for x in range(1)) 0749 verify(g.next() == [x for x in range(10)]) 0750 try: 0751 g.next() 0752 raise TestFailed, 'should produce StopIteration exception' 0753 except StopIteration: 0754 pass 0755 0756 a = 1 0757 try: 0758 g = (a for d in a) 0759 g.next() 0760 raise TestFailed, 'should produce TypeError' 0761 except TypeError: 0762 pass 0763 0764 verify(list((x, y) for x in 'abcd' for y in 'abcd') == [(x, y) for x in 'abcd' for y in 'abcd']) 0765 verify(list((x, y) for x in 'ab' for y in 'xy') == [(x, y) for x in 'ab' for y in 'xy']) 0766 0767 a = [x for x in range(10)] 0768 b = (x for x in (y for y in a)) 0769 verify(sum(b) == sum([x for x in range(10)])) 0770 0771 verify(sum(x**2 for x in range(10)) == sum([x**2 for x in range(10)])) 0772 verify(sum(x*x for x in range(10) if x%2) == sum([x*x for x in range(10) if x%2])) 0773 verify(sum(x for x in (y for y in range(10))) == sum([x for x in range(10)])) 0774 verify(sum(x for x in (y for y in (z for z in range(10)))) == sum([x for x in range(10)])) 0775 verify(sum(x for x in [y for y in (z for z in range(10))]) == sum([x for x in range(10)])) 0776 verify(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)])) 0777 verify(sum(x for x in (y for y in (z for z in range(10) if True) if False) if True) == 0) 0778 check_syntax("foo(x for x in range(10), 100)") 0779 check_syntax("foo(100, x for x in range(10))") 0780 0781 # test for outmost iterable precomputation 0782 x = 10; g = (i for i in range(x)); x = 5 0783 verify(len(list(g)) == 10) 0784 0785 # This should hold, since we're only precomputing outmost iterable. 0786 x = 10; t = False; g = ((i,j) for i in range(x) if t for j in range(x)) 0787 x = 5; t = True; 0788 verify([(i,j) for i in range(10) for j in range(5)] == list(g)) 0789
Generated by PyXR 0.9.4