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