10c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# Python test set -- part 1, grammar. 20c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# This just tests whether the parser accepts them all. 30c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 40c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yifrom test.test_support import run_unittest, check_syntax_error, \ 50c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi check_py3k_warnings 60c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yiimport unittest 70c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yiimport sys 80c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi# testing import * 90c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yifrom sys import * 100c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 110c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 120c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yiclass TokenTests(unittest.TestCase): 130c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 140c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def testBackslash(self): 150c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # Backslash means line continuation: 160c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = 1 \ 170c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi + 1 180c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual(x, 2, 'backslash for line continuation') 190c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 200c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # Backslash does not means continuation in comments :\ 210c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = 0 220c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual(x, 0, 'backslash ending comment') 230c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 240c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def testPlainIntegers(self): 250c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual(0xff, 255) 260c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual(0377, 255) 270c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual(2147483647, 017777777777) 280c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # "0x" is not a valid literal 290c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertRaises(SyntaxError, eval, "0x") 300c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi from sys import maxint 310c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi if maxint == 2147483647: 320c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual(-2147483647-1, -020000000000) 330c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # XXX -2147483648 340c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertTrue(037777777777 > 0) 350c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertTrue(0xffffffff > 0) 360c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi for s in '2147483648', '040000000000', '0x100000000': 370c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi try: 380c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = eval(s) 390c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi except OverflowError: 400c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.fail("OverflowError on huge integer literal %r" % s) 410c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi elif maxint == 9223372036854775807: 420c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual(-9223372036854775807-1, -01000000000000000000000) 430c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertTrue(01777777777777777777777 > 0) 440c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertTrue(0xffffffffffffffff > 0) 450c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi for s in '9223372036854775808', '02000000000000000000000', \ 460c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi '0x10000000000000000': 470c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi try: 480c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = eval(s) 490c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi except OverflowError: 500c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.fail("OverflowError on huge integer literal %r" % s) 510c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi else: 520c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.fail('Weird maxint value %r' % maxint) 530c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 540c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def testLongIntegers(self): 550c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = 0L 560c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = 0l 570c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = 0xffffffffffffffffL 580c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = 0xffffffffffffffffl 590c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = 077777777777777777L 600c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = 077777777777777777l 610c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = 123456789012345678901234567890L 620c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = 123456789012345678901234567890l 630c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 640c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def testFloats(self): 650c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = 3.14 660c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = 314. 670c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = 0.314 680c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # XXX x = 000.314 690c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = .314 700c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = 3e14 710c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = 3E14 720c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = 3e-14 730c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = 3e+14 740c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = 3.e14 750c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = .3e14 760c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = 3.1e4 770c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 780c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def testStringLiterals(self): 790c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = ''; y = ""; self.assertTrue(len(x) == 0 and x == y) 800c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = '\''; y = "'"; self.assertTrue(len(x) == 1 and x == y and ord(x) == 39) 810c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = '"'; y = "\""; self.assertTrue(len(x) == 1 and x == y and ord(x) == 34) 820c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = "doesn't \"shrink\" does it" 830c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi y = 'doesn\'t "shrink" does it' 840c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertTrue(len(x) == 24 and x == y) 850c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = "does \"shrink\" doesn't it" 860c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi y = 'does "shrink" doesn\'t it' 870c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertTrue(len(x) == 24 and x == y) 880c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = """ 890c5958b1636c47ed7c284f859c8e805fd06a0e6Bill YiThe "quick" 900c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yibrown fox 910c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yijumps over 920c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yithe 'lazy' dog. 930c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi""" 940c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi y = '\nThe "quick"\nbrown fox\njumps over\nthe \'lazy\' dog.\n' 950c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual(x, y) 960c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi y = ''' 970c5958b1636c47ed7c284f859c8e805fd06a0e6Bill YiThe "quick" 980c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yibrown fox 990c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yijumps over 1000c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yithe 'lazy' dog. 1010c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi''' 1020c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual(x, y) 1030c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi y = "\n\ 1040c5958b1636c47ed7c284f859c8e805fd06a0e6Bill YiThe \"quick\"\n\ 1050c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yibrown fox\n\ 1060c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yijumps over\n\ 1070c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yithe 'lazy' dog.\n\ 1080c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi" 1090c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual(x, y) 1100c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi y = '\n\ 1110c5958b1636c47ed7c284f859c8e805fd06a0e6Bill YiThe \"quick\"\n\ 1120c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yibrown fox\n\ 1130c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yijumps over\n\ 1140c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yithe \'lazy\' dog.\n\ 1150c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi' 1160c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual(x, y) 1170c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 1180c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 1190c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yiclass GrammarTests(unittest.TestCase): 1200c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 1210c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE 1220c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # XXX can't test in a script -- this rule is only used when interactive 1230c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 1240c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # file_input: (NEWLINE | stmt)* ENDMARKER 1250c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # Being tested as this very moment this very module 1260c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 1270c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # expr_input: testlist NEWLINE 1280c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # XXX Hard to test -- used only in calls to input() 1290c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 1300c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def testEvalInput(self): 1310c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # testlist ENDMARKER 1320c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = eval('1, 0 or 1') 1330c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 1340c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def testFuncdef(self): 1350c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi ### 'def' NAME parameters ':' suite 1360c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi ### parameters: '(' [varargslist] ')' 1370c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi ### varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' ('**'|'*' '*') NAME] 1380c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi ### | ('**'|'*' '*') NAME) 1390c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi ### | fpdef ['=' test] (',' fpdef ['=' test])* [','] 1400c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi ### fpdef: NAME | '(' fplist ')' 1410c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi ### fplist: fpdef (',' fpdef)* [','] 1420c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi ### arglist: (argument ',')* (argument | *' test [',' '**' test] | '**' test) 1430c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi ### argument: [test '='] test # Really [keyword '='] test 1440c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def f1(): pass 1450c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi f1() 1460c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi f1(*()) 1470c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi f1(*(), **{}) 1480c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def f2(one_argument): pass 1490c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def f3(two, arguments): pass 1500c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # Silence Py3k warning 1510c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi exec('def f4(two, (compound, (argument, list))): pass') 1520c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi exec('def f5((compound, first), two): pass') 1530c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual(f2.func_code.co_varnames, ('one_argument',)) 1540c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual(f3.func_code.co_varnames, ('two', 'arguments')) 1550c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi if sys.platform.startswith('java'): 1560c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual(f4.func_code.co_varnames, 1570c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi ('two', '(compound, (argument, list))', 'compound', 'argument', 1580c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 'list',)) 1590c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual(f5.func_code.co_varnames, 1600c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi ('(compound, first)', 'two', 'compound', 'first')) 1610c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi else: 1620c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual(f4.func_code.co_varnames, 1630c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi ('two', '.1', 'compound', 'argument', 'list')) 1640c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual(f5.func_code.co_varnames, 1650c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi ('.0', 'two', 'compound', 'first')) 1660c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def a1(one_arg,): pass 1670c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def a2(two, args,): pass 1680c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def v0(*rest): pass 1690c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def v1(a, *rest): pass 1700c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def v2(a, b, *rest): pass 1710c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # Silence Py3k warning 1720c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi exec('def v3(a, (b, c), *rest): return a, b, c, rest') 1730c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 1740c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi f1() 1750c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi f2(1) 1760c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi f2(1,) 1770c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi f3(1, 2) 1780c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi f3(1, 2,) 1790c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi f4(1, (2, (3, 4))) 1800c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi v0() 1810c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi v0(1) 1820c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi v0(1,) 1830c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi v0(1,2) 1840c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi v0(1,2,3,4,5,6,7,8,9,0) 1850c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi v1(1) 1860c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi v1(1,) 1870c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi v1(1,2) 1880c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi v1(1,2,3) 1890c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi v1(1,2,3,4,5,6,7,8,9,0) 1900c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi v2(1,2) 1910c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi v2(1,2,3) 1920c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi v2(1,2,3,4) 1930c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi v2(1,2,3,4,5,6,7,8,9,0) 1940c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi v3(1,(2,3)) 1950c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi v3(1,(2,3),4) 1960c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi v3(1,(2,3),4,5,6,7,8,9,0) 1970c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 1980c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # ceval unpacks the formal arguments into the first argcount names; 1990c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # thus, the names nested inside tuples must appear after these names. 2000c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi if sys.platform.startswith('java'): 2010c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual(v3.func_code.co_varnames, ('a', '(b, c)', 'rest', 'b', 'c')) 2020c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi else: 2030c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual(v3.func_code.co_varnames, ('a', '.1', 'rest', 'b', 'c')) 2040c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual(v3(1, (2, 3), 4), (1, 2, 3, (4,))) 2050c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def d01(a=1): pass 2060c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi d01() 2070c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi d01(1) 2080c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi d01(*(1,)) 2090c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi d01(**{'a':2}) 2100c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def d11(a, b=1): pass 2110c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi d11(1) 2120c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi d11(1, 2) 2130c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi d11(1, **{'b':2}) 2140c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def d21(a, b, c=1): pass 2150c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi d21(1, 2) 2160c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi d21(1, 2, 3) 2170c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi d21(*(1, 2, 3)) 2180c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi d21(1, *(2, 3)) 2190c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi d21(1, 2, *(3,)) 2200c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi d21(1, 2, **{'c':3}) 2210c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def d02(a=1, b=2): pass 2220c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi d02() 2230c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi d02(1) 2240c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi d02(1, 2) 2250c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi d02(*(1, 2)) 2260c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi d02(1, *(2,)) 2270c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi d02(1, **{'b':2}) 2280c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi d02(**{'a': 1, 'b': 2}) 2290c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def d12(a, b=1, c=2): pass 2300c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi d12(1) 2310c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi d12(1, 2) 2320c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi d12(1, 2, 3) 2330c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def d22(a, b, c=1, d=2): pass 2340c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi d22(1, 2) 2350c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi d22(1, 2, 3) 2360c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi d22(1, 2, 3, 4) 2370c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def d01v(a=1, *rest): pass 2380c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi d01v() 2390c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi d01v(1) 2400c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi d01v(1, 2) 2410c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi d01v(*(1, 2, 3, 4)) 2420c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi d01v(*(1,)) 2430c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi d01v(**{'a':2}) 2440c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def d11v(a, b=1, *rest): pass 2450c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi d11v(1) 2460c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi d11v(1, 2) 2470c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi d11v(1, 2, 3) 2480c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def d21v(a, b, c=1, *rest): pass 2490c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi d21v(1, 2) 2500c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi d21v(1, 2, 3) 2510c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi d21v(1, 2, 3, 4) 2520c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi d21v(*(1, 2, 3, 4)) 2530c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi d21v(1, 2, **{'c': 3}) 2540c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def d02v(a=1, b=2, *rest): pass 2550c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi d02v() 2560c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi d02v(1) 2570c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi d02v(1, 2) 2580c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi d02v(1, 2, 3) 2590c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi d02v(1, *(2, 3, 4)) 2600c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi d02v(**{'a': 1, 'b': 2}) 2610c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def d12v(a, b=1, c=2, *rest): pass 2620c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi d12v(1) 2630c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi d12v(1, 2) 2640c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi d12v(1, 2, 3) 2650c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi d12v(1, 2, 3, 4) 2660c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi d12v(*(1, 2, 3, 4)) 2670c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi d12v(1, 2, *(3, 4, 5)) 2680c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi d12v(1, *(2,), **{'c': 3}) 2690c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def d22v(a, b, c=1, d=2, *rest): pass 2700c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi d22v(1, 2) 2710c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi d22v(1, 2, 3) 2720c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi d22v(1, 2, 3, 4) 2730c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi d22v(1, 2, 3, 4, 5) 2740c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi d22v(*(1, 2, 3, 4)) 2750c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi d22v(1, 2, *(3, 4, 5)) 2760c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi d22v(1, *(2, 3), **{'d': 4}) 2770c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # Silence Py3k warning 2780c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi exec('def d31v((x)): pass') 2790c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi exec('def d32v((x,)): pass') 2800c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi d31v(1) 2810c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi d32v((1,)) 2820c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 2830c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # keyword arguments after *arglist 2840c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def f(*args, **kwargs): 2850c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi return args, kwargs 2860c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual(f(1, x=2, *[3, 4], y=5), ((1, 3, 4), 2870c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi {'x':2, 'y':5})) 2880c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertRaises(SyntaxError, eval, "f(1, *(2,3), 4)") 2890c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertRaises(SyntaxError, eval, "f(1, x=2, *(3,4), x=5)") 2900c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 2910c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # Check ast errors in *args and *kwargs 2920c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi check_syntax_error(self, "f(*g(1=2))") 2930c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi check_syntax_error(self, "f(**g(1=2))") 2940c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 2950c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def testLambdef(self): 2960c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi ### lambdef: 'lambda' [varargslist] ':' test 2970c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi l1 = lambda : 0 2980c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual(l1(), 0) 2990c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi l2 = lambda : a[d] # XXX just testing the expression 3000c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi l3 = lambda : [2 < x for x in [-1, 3, 0L]] 3010c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual(l3(), [0, 1, 0]) 3020c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi l4 = lambda x = lambda y = lambda z=1 : z : y() : x() 3030c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual(l4(), 1) 3040c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi l5 = lambda x, y, z=2: x + y + z 3050c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual(l5(1, 2), 5) 3060c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual(l5(1, 2, 3), 6) 3070c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi check_syntax_error(self, "lambda x: x = 2") 3080c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi check_syntax_error(self, "lambda (None,): None") 3090c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 3100c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi ### stmt: simple_stmt | compound_stmt 3110c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # Tested below 3120c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 3130c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def testSimpleStmt(self): 3140c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi ### simple_stmt: small_stmt (';' small_stmt)* [';'] 3150c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = 1; pass; del x 3160c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def foo(): 3170c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # verify statements that end with semi-colons 3180c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = 1; pass; del x; 3190c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi foo() 3200c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 3210c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi ### small_stmt: expr_stmt | print_stmt | pass_stmt | del_stmt | flow_stmt | import_stmt | global_stmt | access_stmt | exec_stmt 3220c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # Tested below 3230c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 3240c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def testExprStmt(self): 3250c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # (exprlist '=')* exprlist 3260c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 1 3270c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 1, 2, 3 3280c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = 1 3290c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = 1, 2, 3 3300c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = y = z = 1, 2, 3 3310c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x, y, z = 1, 2, 3 3320c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi abc = a, b, c = x, y, z = xyz = 1, 2, (3, 4) 3330c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 3340c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi check_syntax_error(self, "x + 1 = 1") 3350c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi check_syntax_error(self, "a + 1 = b + 2") 3360c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 3370c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def testPrintStmt(self): 3380c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # 'print' (test ',')* [test] 3390c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi import StringIO 3400c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 3410c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # Can't test printing to real stdout without comparing output 3420c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # which is not available in unittest. 3430c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi save_stdout = sys.stdout 3440c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi sys.stdout = StringIO.StringIO() 3450c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 3460c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi print 1, 2, 3 3470c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi print 1, 2, 3, 3480c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi print 3490c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi print 0 or 1, 0 or 1, 3500c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi print 0 or 1 3510c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 3520c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # 'print' '>>' test ',' 3530c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi print >> sys.stdout, 1, 2, 3 3540c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi print >> sys.stdout, 1, 2, 3, 3550c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi print >> sys.stdout 3560c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi print >> sys.stdout, 0 or 1, 0 or 1, 3570c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi print >> sys.stdout, 0 or 1 3580c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 3590c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # test printing to an instance 3600c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi class Gulp: 3610c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def write(self, msg): pass 3620c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 3630c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi gulp = Gulp() 3640c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi print >> gulp, 1, 2, 3 3650c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi print >> gulp, 1, 2, 3, 3660c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi print >> gulp 3670c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi print >> gulp, 0 or 1, 0 or 1, 3680c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi print >> gulp, 0 or 1 3690c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 3700c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # test print >> None 3710c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def driver(): 3720c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi oldstdout = sys.stdout 3730c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi sys.stdout = Gulp() 3740c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi try: 3750c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi tellme(Gulp()) 3760c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi tellme() 3770c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi finally: 3780c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi sys.stdout = oldstdout 3790c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 3800c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # we should see this once 3810c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def tellme(file=sys.stdout): 3820c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi print >> file, 'hello world' 3830c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 3840c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi driver() 3850c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 3860c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # we should not see this at all 3870c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def tellme(file=None): 3880c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi print >> file, 'goodbye universe' 3890c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 3900c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi driver() 3910c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 3920c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual(sys.stdout.getvalue(), '''\ 3930c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi1 2 3 3940c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi1 2 3 3950c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi1 1 1 3960c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi1 2 3 3970c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi1 2 3 3980c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi1 1 1 3990c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yihello world 4000c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi''') 4010c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi sys.stdout = save_stdout 4020c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 4030c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # syntax errors 4040c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi check_syntax_error(self, 'print ,') 4050c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi check_syntax_error(self, 'print >> x,') 4060c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 4070c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def testDelStmt(self): 4080c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # 'del' exprlist 4090c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi abc = [1,2,3] 4100c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x, y, z = abc 4110c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi xyz = x, y, z 4120c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 4130c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi del abc 4140c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi del x, y, (z, xyz) 4150c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 4160c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def testPassStmt(self): 4170c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # 'pass' 4180c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi pass 4190c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 4200c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt 4210c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # Tested below 4220c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 4230c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def testBreakStmt(self): 4240c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # 'break' 4250c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi while 1: break 4260c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 4270c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def testContinueStmt(self): 4280c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # 'continue' 4290c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi i = 1 4300c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi while i: i = 0; continue 4310c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 4320c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi msg = "" 4330c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi while not msg: 4340c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi msg = "ok" 4350c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi try: 4360c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi continue 4370c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi msg = "continue failed to continue inside try" 4380c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi except: 4390c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi msg = "continue inside try called except block" 4400c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi if msg != "ok": 4410c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.fail(msg) 4420c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 4430c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi msg = "" 4440c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi while not msg: 4450c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi msg = "finally block not called" 4460c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi try: 4470c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi continue 4480c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi finally: 4490c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi msg = "ok" 4500c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi if msg != "ok": 4510c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.fail(msg) 4520c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 4530c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def test_break_continue_loop(self): 4540c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # This test warrants an explanation. It is a test specifically for SF bugs 4550c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # #463359 and #462937. The bug is that a 'break' statement executed or 4560c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # exception raised inside a try/except inside a loop, *after* a continue 4570c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # statement has been executed in that loop, will cause the wrong number of 4580c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # arguments to be popped off the stack and the instruction pointer reset to 4590c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # a very small number (usually 0.) Because of this, the following test 4600c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # *must* written as a function, and the tracking vars *must* be function 4610c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # arguments with default values. Otherwise, the test will loop and loop. 4620c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 4630c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def test_inner(extra_burning_oil = 1, count=0): 4640c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi big_hippo = 2 4650c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi while big_hippo: 4660c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi count += 1 4670c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi try: 4680c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi if extra_burning_oil and big_hippo == 1: 4690c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi extra_burning_oil -= 1 4700c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi break 4710c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi big_hippo -= 1 4720c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi continue 4730c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi except: 4740c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi raise 4750c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi if count > 2 or big_hippo != 1: 4760c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.fail("continue then break in try/except in loop broken!") 4770c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi test_inner() 4780c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 4790c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def testReturn(self): 4800c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # 'return' [testlist] 4810c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def g1(): return 4820c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def g2(): return 1 4830c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi g1() 4840c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = g2() 4850c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi check_syntax_error(self, "class foo:return 1") 4860c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 4870c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def testYield(self): 4880c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi check_syntax_error(self, "class foo:yield 1") 4890c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 4900c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def testRaise(self): 4910c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # 'raise' test [',' test] 4920c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi try: raise RuntimeError, 'just testing' 4930c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi except RuntimeError: pass 4940c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi try: raise KeyboardInterrupt 4950c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi except KeyboardInterrupt: pass 4960c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 4970c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def testImport(self): 4980c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # 'import' dotted_as_names 4990c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi import sys 5000c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi import time, sys 5010c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # 'from' dotted_name 'import' ('*' | '(' import_as_names ')' | import_as_names) 5020c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi from time import time 5030c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi from time import (time) 5040c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # not testable inside a function, but already done at top of the module 5050c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # from sys import * 5060c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi from sys import path, argv 5070c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi from sys import (path, argv) 5080c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi from sys import (path, argv,) 5090c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 5100c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def testGlobal(self): 5110c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # 'global' NAME (',' NAME)* 5120c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi global a 5130c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi global a, b 5140c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi global one, two, three, four, five, six, seven, eight, nine, ten 5150c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 5160c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def testExec(self): 5170c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # 'exec' expr ['in' expr [',' expr]] 5180c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi z = None 5190c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi del z 5200c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi exec 'z=1+1\n' 5210c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi if z != 2: self.fail('exec \'z=1+1\'\\n') 5220c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi del z 5230c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi exec 'z=1+1' 5240c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi if z != 2: self.fail('exec \'z=1+1\'') 5250c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi z = None 5260c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi del z 5270c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi import types 5280c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi if hasattr(types, "UnicodeType"): 5290c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi exec r"""if 1: 5300c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi exec u'z=1+1\n' 5310c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi if z != 2: self.fail('exec u\'z=1+1\'\\n') 5320c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi del z 5330c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi exec u'z=1+1' 5340c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi if z != 2: self.fail('exec u\'z=1+1\'')""" 5350c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi g = {} 5360c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi exec 'z = 1' in g 5370c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi if '__builtins__' in g: del g['__builtins__'] 5380c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi if g != {'z': 1}: self.fail('exec \'z = 1\' in g') 5390c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi g = {} 5400c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi l = {} 5410c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 5420c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi exec 'global a; a = 1; b = 2' in g, l 5430c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi if '__builtins__' in g: del g['__builtins__'] 5440c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi if '__builtins__' in l: del l['__builtins__'] 5450c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi if (g, l) != ({'a':1}, {'b':2}): 5460c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.fail('exec ... in g (%s), l (%s)' %(g,l)) 5470c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 5480c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def testAssert(self): 5490c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # assertTruestmt: 'assert' test [',' test] 5500c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi assert 1 5510c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi assert 1, 1 5520c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi assert lambda x:x 5530c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi assert 1, lambda x:x+1 5540c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 5550c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi try: 5560c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi assert True 5570c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi except AssertionError as e: 5580c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.fail("'assert True' should not have raised an AssertionError") 5590c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 5600c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi try: 5610c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi assert True, 'this should always pass' 5620c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi except AssertionError as e: 5630c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.fail("'assert True, msg' should not have " 5640c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi "raised an AssertionError") 5650c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 5660c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # these tests fail if python is run with -O, so check __debug__ 5670c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi @unittest.skipUnless(__debug__, "Won't work if __debug__ is False") 5680c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def testAssert2(self): 5690c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi try: 5700c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi assert 0, "msg" 5710c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi except AssertionError, e: 5720c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual(e.args[0], "msg") 5730c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi else: 5740c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.fail("AssertionError not raised by assert 0") 5750c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 5760c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi try: 5770c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi assert False 5780c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi except AssertionError as e: 5790c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual(len(e.args), 0) 5800c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi else: 5810c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.fail("AssertionError not raised by 'assert False'") 5820c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 5830c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 5840c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi ### compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef 5850c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # Tested below 5860c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 5870c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def testIf(self): 5880c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] 5890c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi if 1: pass 5900c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi if 1: pass 5910c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi else: pass 5920c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi if 0: pass 5930c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi elif 0: pass 5940c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi if 0: pass 5950c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi elif 0: pass 5960c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi elif 0: pass 5970c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi elif 0: pass 5980c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi else: pass 5990c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 6000c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def testWhile(self): 6010c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # 'while' test ':' suite ['else' ':' suite] 6020c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi while 0: pass 6030c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi while 0: pass 6040c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi else: pass 6050c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 6060c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # Issue1920: "while 0" is optimized away, 6070c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # ensure that the "else" clause is still present. 6080c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = 0 6090c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi while 0: 6100c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = 1 6110c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi else: 6120c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = 2 6130c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual(x, 2) 6140c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 6150c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def testFor(self): 6160c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite] 6170c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi for i in 1, 2, 3: pass 6180c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi for i, j, k in (): pass 6190c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi else: pass 6200c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi class Squares: 6210c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def __init__(self, max): 6220c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.max = max 6230c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.sofar = [] 6240c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def __len__(self): return len(self.sofar) 6250c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def __getitem__(self, i): 6260c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi if not 0 <= i < self.max: raise IndexError 6270c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi n = len(self.sofar) 6280c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi while n <= i: 6290c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.sofar.append(n*n) 6300c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi n = n+1 6310c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi return self.sofar[i] 6320c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi n = 0 6330c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi for x in Squares(10): n = n+x 6340c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi if n != 285: 6350c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.fail('for over growing sequence') 6360c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 6370c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi result = [] 6380c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi for x, in [(1,), (2,), (3,)]: 6390c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi result.append(x) 6400c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual(result, [1, 2, 3]) 6410c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 6420c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def testTry(self): 6430c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi ### try_stmt: 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite] 6440c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi ### | 'try' ':' suite 'finally' ':' suite 6450c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi ### except_clause: 'except' [expr [('as' | ',') expr]] 6460c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi try: 6470c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 1/0 6480c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi except ZeroDivisionError: 6490c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi pass 6500c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi else: 6510c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi pass 6520c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi try: 1/0 6530c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi except EOFError: pass 6540c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi except TypeError as msg: pass 6550c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi except RuntimeError, msg: pass 6560c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi except: pass 6570c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi else: pass 6580c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi try: 1/0 6590c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi except (EOFError, TypeError, ZeroDivisionError): pass 6600c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi try: 1/0 6610c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi except (EOFError, TypeError, ZeroDivisionError), msg: pass 6620c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi try: pass 6630c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi finally: pass 6640c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 6650c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def testSuite(self): 6660c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT 6670c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi if 1: pass 6680c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi if 1: 6690c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi pass 6700c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi if 1: 6710c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # 6720c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # 6730c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # 6740c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi pass 6750c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi pass 6760c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # 6770c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi pass 6780c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # 6790c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 6800c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def testTest(self): 6810c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi ### and_test ('or' and_test)* 6820c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi ### and_test: not_test ('and' not_test)* 6830c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi ### not_test: 'not' not_test | comparison 6840c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi if not 1: pass 6850c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi if 1 and 1: pass 6860c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi if 1 or 1: pass 6870c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi if not not not 1: pass 6880c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi if not 1 and 1 and 1: pass 6890c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi if 1 and 1 or 1 and 1 and 1 or not 1 and 1: pass 6900c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 6910c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def testComparison(self): 6920c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi ### comparison: expr (comp_op expr)* 6930c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi ### comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not' 6940c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi if 1: pass 6950c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = (1 == 1) 6960c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi if 1 == 1: pass 6970c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi if 1 != 1: pass 6980c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi if 1 < 1: pass 6990c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi if 1 > 1: pass 7000c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi if 1 <= 1: pass 7010c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi if 1 >= 1: pass 7020c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi if 1 is 1: pass 7030c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi if 1 is not 1: pass 7040c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi if 1 in (): pass 7050c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi if 1 not in (): pass 7060c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi if 1 < 1 > 1 == 1 >= 1 <= 1 != 1 in 1 not in 1 is 1 is not 1: pass 7070c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # Silence Py3k warning 7080c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi if eval('1 <> 1'): pass 7090c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi if eval('1 < 1 > 1 == 1 >= 1 <= 1 <> 1 != 1 in 1 not in 1 is 1 is not 1'): pass 7100c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 7110c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def testBinaryMaskOps(self): 7120c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = 1 & 1 7130c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = 1 ^ 1 7140c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = 1 | 1 7150c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 7160c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def testShiftOps(self): 7170c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = 1 << 1 7180c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = 1 >> 1 7190c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = 1 << 1 >> 1 7200c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 7210c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def testAdditiveOps(self): 7220c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = 1 7230c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = 1 + 1 7240c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = 1 - 1 - 1 7250c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = 1 - 1 + 1 - 1 + 1 7260c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 7270c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def testMultiplicativeOps(self): 7280c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = 1 * 1 7290c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = 1 / 1 7300c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = 1 % 1 7310c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = 1 / 1 * 1 % 1 7320c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 7330c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def testUnaryOps(self): 7340c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = +1 7350c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = -1 7360c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = ~1 7370c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = ~1 ^ 1 & 1 | 1 & 1 ^ -1 7380c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = -1*1/1 + 1*1 - ---1*1 7390c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 7400c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def testSelectors(self): 7410c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi ### trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME 7420c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi ### subscript: expr | [expr] ':' [expr] 7430c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 7440c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi import sys, time 7450c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi c = sys.path[0] 7460c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = time.time() 7470c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = sys.modules['time'].time() 7480c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi a = '01234' 7490c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi c = a[0] 7500c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi c = a[-1] 7510c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi s = a[0:5] 7520c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi s = a[:5] 7530c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi s = a[0:] 7540c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi s = a[:] 7550c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi s = a[-5:] 7560c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi s = a[:-1] 7570c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi s = a[-4:-3] 7580c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # A rough test of SF bug 1333982. http://python.org/sf/1333982 7590c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # The testing here is fairly incomplete. 7600c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # Test cases should include: commas with 1 and 2 colons 7610c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi d = {} 7620c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi d[1] = 1 7630c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi d[1,] = 2 7640c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi d[1,2] = 3 7650c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi d[1,2,3] = 4 7660c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi L = list(d) 7670c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi L.sort() 7680c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual(str(L), '[1, (1,), (1, 2), (1, 2, 3)]') 7690c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 7700c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def testAtoms(self): 7710c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi ### atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING 7720c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi ### dictorsetmaker: (test ':' test (',' test ':' test)* [',']) | (test (',' test)* [',']) 7730c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 7740c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = (1) 7750c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = (1 or 2 or 3) 7760c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = (1 or 2 or 3, 2, 3) 7770c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 7780c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = [] 7790c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = [1] 7800c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = [1 or 2 or 3] 7810c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = [1 or 2 or 3, 2, 3] 7820c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = [] 7830c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 7840c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = {} 7850c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = {'one': 1} 7860c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = {'one': 1,} 7870c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = {'one' or 'two': 1 or 2} 7880c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = {'one': 1, 'two': 2} 7890c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = {'one': 1, 'two': 2,} 7900c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6} 7910c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 7920c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = {'one'} 7930c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = {'one', 1,} 7940c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = {'one', 'two', 'three'} 7950c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = {2, 3, 4,} 7960c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 7970c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # Silence Py3k warning 7980c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = eval('`x`') 7990c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = eval('`1 or 2 or 3`') 8000c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual(eval('`1,2`'), '(1, 2)') 8010c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 8020c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = x 8030c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = 'x' 8040c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = 123 8050c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 8060c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi ### exprlist: expr (',' expr)* [','] 8070c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi ### testlist: test (',' test)* [','] 8080c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # These have been exercised enough above 8090c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 8100c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def testClassdef(self): 8110c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # 'class' NAME ['(' [testlist] ')'] ':' suite 8120c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi class B: pass 8130c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi class B2(): pass 8140c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi class C1(B): pass 8150c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi class C2(B): pass 8160c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi class D(C1, C2, B): pass 8170c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi class C: 8180c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def meth1(self): pass 8190c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def meth2(self, arg): pass 8200c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def meth3(self, a1, a2): pass 8210c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE 8220c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # decorators: decorator+ 8230c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # decorated: decorators (classdef | funcdef) 8240c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def class_decorator(x): 8250c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x.decorated = True 8260c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi return x 8270c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi @class_decorator 8280c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi class G: 8290c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi pass 8300c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual(G.decorated, True) 8310c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 8320c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def testDictcomps(self): 8330c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # dictorsetmaker: ( (test ':' test (comp_for | 8340c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # (',' test ':' test)* [','])) | 8350c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # (test (comp_for | (',' test)* [','])) ) 8360c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi nums = [1, 2, 3] 8370c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual({i:i+1 for i in nums}, {1: 2, 2: 3, 3: 4}) 8380c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 8390c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def testListcomps(self): 8400c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # list comprehension tests 8410c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi nums = [1, 2, 3, 4, 5] 8420c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi strs = ["Apple", "Banana", "Coconut"] 8430c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi spcs = [" Apple", " Banana ", "Coco nut "] 8440c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 8450c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual([s.strip() for s in spcs], ['Apple', 'Banana', 'Coco nut']) 8460c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual([3 * x for x in nums], [3, 6, 9, 12, 15]) 8470c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual([x for x in nums if x > 2], [3, 4, 5]) 8480c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual([(i, s) for i in nums for s in strs], 8490c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi [(1, 'Apple'), (1, 'Banana'), (1, 'Coconut'), 8500c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi (2, 'Apple'), (2, 'Banana'), (2, 'Coconut'), 8510c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi (3, 'Apple'), (3, 'Banana'), (3, 'Coconut'), 8520c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi (4, 'Apple'), (4, 'Banana'), (4, 'Coconut'), 8530c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi (5, 'Apple'), (5, 'Banana'), (5, 'Coconut')]) 8540c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual([(i, s) for i in nums for s in [f for f in strs if "n" in f]], 8550c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi [(1, 'Banana'), (1, 'Coconut'), (2, 'Banana'), (2, 'Coconut'), 8560c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi (3, 'Banana'), (3, 'Coconut'), (4, 'Banana'), (4, 'Coconut'), 8570c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi (5, 'Banana'), (5, 'Coconut')]) 8580c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual([(lambda a:[a**i for i in range(a+1)])(j) for j in range(5)], 8590c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi [[1], [1, 1], [1, 2, 4], [1, 3, 9, 27], [1, 4, 16, 64, 256]]) 8600c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 8610c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def test_in_func(l): 8620c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi return [None < x < 3 for x in l if x > 2] 8630c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 8640c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual(test_in_func(nums), [False, False, False]) 8650c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 8660c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def test_nested_front(): 8670c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual([[y for y in [x, x + 1]] for x in [1,3,5]], 8680c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi [[1, 2], [3, 4], [5, 6]]) 8690c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 8700c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi test_nested_front() 8710c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 8720c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi check_syntax_error(self, "[i, s for i in nums for s in strs]") 8730c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi check_syntax_error(self, "[x if y]") 8740c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 8750c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi suppliers = [ 8760c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi (1, "Boeing"), 8770c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi (2, "Ford"), 8780c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi (3, "Macdonalds") 8790c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi ] 8800c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 8810c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi parts = [ 8820c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi (10, "Airliner"), 8830c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi (20, "Engine"), 8840c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi (30, "Cheeseburger") 8850c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi ] 8860c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 8870c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi suppart = [ 8880c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi (1, 10), (1, 20), (2, 20), (3, 30) 8890c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi ] 8900c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 8910c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = [ 8920c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi (sname, pname) 8930c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi for (sno, sname) in suppliers 8940c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi for (pno, pname) in parts 8950c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi for (sp_sno, sp_pno) in suppart 8960c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi if sno == sp_sno and pno == sp_pno 8970c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi ] 8980c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 8990c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual(x, [('Boeing', 'Airliner'), ('Boeing', 'Engine'), ('Ford', 'Engine'), 9000c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi ('Macdonalds', 'Cheeseburger')]) 9010c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 9020c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def testGenexps(self): 9030c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # generator expression tests 9040c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi g = ([x for x in range(10)] for x in range(1)) 9050c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual(g.next(), [x for x in range(10)]) 9060c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi try: 9070c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi g.next() 9080c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.fail('should produce StopIteration exception') 9090c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi except StopIteration: 9100c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi pass 9110c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 9120c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi a = 1 9130c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi try: 9140c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi g = (a for d in a) 9150c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi g.next() 9160c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.fail('should produce TypeError') 9170c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi except TypeError: 9180c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi pass 9190c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 9200c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual(list((x, y) for x in 'abcd' for y in 'abcd'), [(x, y) for x in 'abcd' for y in 'abcd']) 9210c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual(list((x, y) for x in 'ab' for y in 'xy'), [(x, y) for x in 'ab' for y in 'xy']) 9220c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 9230c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi a = [x for x in range(10)] 9240c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi b = (x for x in (y for y in a)) 9250c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual(sum(b), sum([x for x in range(10)])) 9260c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 9270c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual(sum(x**2 for x in range(10)), sum([x**2 for x in range(10)])) 9280c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual(sum(x*x for x in range(10) if x%2), sum([x*x for x in range(10) if x%2])) 9290c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual(sum(x for x in (y for y in range(10))), sum([x for x in range(10)])) 9300c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual(sum(x for x in (y for y in (z for z in range(10)))), sum([x for x in range(10)])) 9310c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual(sum(x for x in [y for y in (z for z in range(10))]), sum([x for x in range(10)])) 9320c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 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)])) 9330c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual(sum(x for x in (y for y in (z for z in range(10) if True) if False) if True), 0) 9340c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi check_syntax_error(self, "foo(x for x in range(10), 100)") 9350c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi check_syntax_error(self, "foo(100, x for x in range(10))") 9360c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 9370c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def testComprehensionSpecials(self): 9380c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # test for outmost iterable precomputation 9390c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = 10; g = (i for i in range(x)); x = 5 9400c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual(len(list(g)), 10) 9410c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 9420c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # This should hold, since we're only precomputing outmost iterable. 9430c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = 10; t = False; g = ((i,j) for i in range(x) if t for j in range(x)) 9440c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi x = 5; t = True; 9450c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual([(i,j) for i in range(10) for j in range(5)], list(g)) 9460c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 9470c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # Grammar allows multiple adjacent 'if's in listcomps and genexps, 9480c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # even though it's silly. Make sure it works (ifelse broke this.) 9490c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual([ x for x in range(10) if x % 2 if x % 3 ], [1, 5, 7]) 9500c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual(list(x for x in range(10) if x % 2 if x % 3), [1, 5, 7]) 9510c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 9520c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # verify unpacking single element tuples in listcomp/genexp. 9530c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual([x for x, in [(4,), (5,), (6,)]], [4, 5, 6]) 9540c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual(list(x for x, in [(7,), (8,), (9,)]), [7, 8, 9]) 9550c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 9560c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def test_with_statement(self): 9570c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi class manager(object): 9580c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def __enter__(self): 9590c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi return (1, 2) 9600c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def __exit__(self, *args): 9610c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi pass 9620c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 9630c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi with manager(): 9640c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi pass 9650c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi with manager() as x: 9660c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi pass 9670c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi with manager() as (x, y): 9680c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi pass 9690c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi with manager(), manager(): 9700c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi pass 9710c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi with manager() as x, manager() as y: 9720c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi pass 9730c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi with manager() as x, manager(): 9740c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi pass 9750c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 9760c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def testIfElseExpr(self): 9770c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # Test ifelse expressions in various cases 9780c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def _checkeval(msg, ret): 9790c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi "helper to check that evaluation of expressions is done correctly" 9800c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi print x 9810c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi return ret 9820c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 9830c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual([ x() for x in lambda: True, lambda: False if x() ], [True]) 9840c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual([ x() for x in (lambda: True, lambda: False) if x() ], [True]) 9850c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 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]) 9860c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual((5 if 1 else _checkeval("check 1", 0)), 5) 9870c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual((_checkeval("check 2", 0) if 0 else 5), 5) 9880c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual((5 and 6 if 0 else 1), 1) 9890c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual(((5 and 6) if 0 else 1), 1) 9900c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual((5 and (6 if 1 else 1)), 6) 9910c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual((0 or _checkeval("check 3", 2) if 0 else 3), 3) 9920c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual((1 or _checkeval("check 4", 2) if 1 else _checkeval("check 5", 3)), 1) 9930c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual((0 or 5 if 1 else _checkeval("check 6", 3)), 5) 9940c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual((not 5 if 1 else 1), False) 9950c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual((not 5 if 0 else 1), 1) 9960c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual((6 + 1 if 1 else 2), 7) 9970c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual((6 - 1 if 1 else 2), 5) 9980c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual((6 * 2 if 1 else 4), 12) 9990c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual((6 / 2 if 1 else 3), 3) 10000c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual((6 < 4 if 0 else 2), 2) 10010c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 10020c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def test_paren_evaluation(self): 10030c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual(16 // (4 // 2), 8) 10040c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual((16 // 4) // 2, 2) 10050c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertEqual(16 // 4 // 2, 2) 10060c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertTrue(False is (2 is 3)) 10070c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertFalse((False is 2) is 3) 10080c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.assertFalse(False is 2 is 3) 10090c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 10100c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 10110c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yidef test_main(): 10120c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi with check_py3k_warnings( 10130c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi ("backquote not supported", SyntaxWarning), 10140c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi ("tuple parameter unpacking has been removed", SyntaxWarning), 10150c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi ("parenthesized argument names are invalid", SyntaxWarning), 10160c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi ("classic int division", DeprecationWarning), 10170c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi (".+ not supported in 3.x", DeprecationWarning)): 10180c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi run_unittest(TokenTests, GrammarTests) 10190c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 10200c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yiif __name__ == '__main__': 10210c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi test_main() 1022