Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 1 | # Python test set -- part 1, grammar. |
| 2 | # This just tests whether the parser accepts them all. |
| 3 | |
Ezio Melotti | d80b4bf | 2010-03-17 13:52:48 +0000 | [diff] [blame] | 4 | from test.test_support import run_unittest, check_syntax_error, \ |
| 5 | check_py3k_warnings |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 6 | import unittest |
Jeremy Hylton | 7d3dff2 | 2001-10-10 01:45:02 +0000 | [diff] [blame] | 7 | import sys |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 8 | # testing import * |
| 9 | from sys import * |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 10 | |
Florent Xicluna | bc27c6a | 2010-03-19 18:34:55 +0000 | [diff] [blame] | 11 | |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 12 | class TokenTests(unittest.TestCase): |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 13 | |
Serhiy Storchaka | 0bcba37 | 2018-01-04 10:36:14 +0200 | [diff] [blame] | 14 | def test_backslash(self): |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 15 | # Backslash means line continuation: |
| 16 | x = 1 \ |
| 17 | + 1 |
Florent Xicluna | bc27c6a | 2010-03-19 18:34:55 +0000 | [diff] [blame] | 18 | self.assertEqual(x, 2, 'backslash for line continuation') |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 19 | |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 20 | # Backslash does not means continuation in comments :\ |
| 21 | x = 0 |
Florent Xicluna | bc27c6a | 2010-03-19 18:34:55 +0000 | [diff] [blame] | 22 | self.assertEqual(x, 0, 'backslash ending comment') |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 23 | |
Serhiy Storchaka | 0bcba37 | 2018-01-04 10:36:14 +0200 | [diff] [blame] | 24 | def test_plain_integers(self): |
| 25 | self.assertEqual(type(000), type(0)) |
Florent Xicluna | bc27c6a | 2010-03-19 18:34:55 +0000 | [diff] [blame] | 26 | self.assertEqual(0xff, 255) |
| 27 | self.assertEqual(0377, 255) |
Serhiy Storchaka | 0bcba37 | 2018-01-04 10:36:14 +0200 | [diff] [blame] | 28 | self.assertEqual(0o377, 255) |
Florent Xicluna | bc27c6a | 2010-03-19 18:34:55 +0000 | [diff] [blame] | 29 | self.assertEqual(2147483647, 017777777777) |
Serhiy Storchaka | 0bcba37 | 2018-01-04 10:36:14 +0200 | [diff] [blame] | 30 | self.assertEqual(2147483647, 0o17777777777) |
| 31 | self.assertEqual(0b1001, 9) |
Georg Brandl | 14404b6 | 2008-01-19 19:27:05 +0000 | [diff] [blame] | 32 | # "0x" is not a valid literal |
| 33 | self.assertRaises(SyntaxError, eval, "0x") |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 34 | from sys import maxint |
| 35 | if maxint == 2147483647: |
Serhiy Storchaka | 0bcba37 | 2018-01-04 10:36:14 +0200 | [diff] [blame] | 36 | self.assertEqual(-2147483647-1, -0o20000000000) |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 37 | # XXX -2147483648 |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 38 | self.assertTrue(037777777777 > 0) |
Serhiy Storchaka | 0bcba37 | 2018-01-04 10:36:14 +0200 | [diff] [blame] | 39 | self.assertTrue(0o37777777777 > 0) |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 40 | self.assertTrue(0xffffffff > 0) |
Serhiy Storchaka | 0bcba37 | 2018-01-04 10:36:14 +0200 | [diff] [blame] | 41 | self.assertTrue(0b1111111111111111111111111111111 > 0) |
| 42 | for s in ('2147483648', '040000000000', '0o40000000000', |
| 43 | '0x100000000', |
| 44 | '0b10000000000000000000000000000000'): |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 45 | try: |
| 46 | x = eval(s) |
| 47 | except OverflowError: |
| 48 | self.fail("OverflowError on huge integer literal %r" % s) |
| 49 | elif maxint == 9223372036854775807: |
Florent Xicluna | bc27c6a | 2010-03-19 18:34:55 +0000 | [diff] [blame] | 50 | self.assertEqual(-9223372036854775807-1, -01000000000000000000000) |
Serhiy Storchaka | 0bcba37 | 2018-01-04 10:36:14 +0200 | [diff] [blame] | 51 | self.assertEqual(-9223372036854775807-1, -0o1000000000000000000000) |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 52 | self.assertTrue(01777777777777777777777 > 0) |
Serhiy Storchaka | 0bcba37 | 2018-01-04 10:36:14 +0200 | [diff] [blame] | 53 | self.assertTrue(0o1777777777777777777777 > 0) |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 54 | self.assertTrue(0xffffffffffffffff > 0) |
Serhiy Storchaka | 0bcba37 | 2018-01-04 10:36:14 +0200 | [diff] [blame] | 55 | self.assertTrue(0b11111111111111111111111111111111111111111111111111111111111111 > 0) |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 56 | for s in '9223372036854775808', '02000000000000000000000', \ |
Serhiy Storchaka | 0bcba37 | 2018-01-04 10:36:14 +0200 | [diff] [blame] | 57 | '0o2000000000000000000000', \ |
| 58 | '0x10000000000000000', \ |
| 59 | '0b100000000000000000000000000000000000000000000000000000000000000': |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 60 | try: |
| 61 | x = eval(s) |
| 62 | except OverflowError: |
| 63 | self.fail("OverflowError on huge integer literal %r" % s) |
| 64 | else: |
| 65 | self.fail('Weird maxint value %r' % maxint) |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 66 | |
Serhiy Storchaka | 0bcba37 | 2018-01-04 10:36:14 +0200 | [diff] [blame] | 67 | def test_long_integers(self): |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 68 | x = 0L |
| 69 | x = 0l |
| 70 | x = 0xffffffffffffffffL |
| 71 | x = 0xffffffffffffffffl |
| 72 | x = 077777777777777777L |
| 73 | x = 077777777777777777l |
| 74 | x = 123456789012345678901234567890L |
| 75 | x = 123456789012345678901234567890l |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 76 | |
Serhiy Storchaka | 0bcba37 | 2018-01-04 10:36:14 +0200 | [diff] [blame] | 77 | def test_floats(self): |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 78 | x = 3.14 |
| 79 | x = 314. |
| 80 | x = 0.314 |
| 81 | # XXX x = 000.314 |
| 82 | x = .314 |
| 83 | x = 3e14 |
| 84 | x = 3E14 |
| 85 | x = 3e-14 |
| 86 | x = 3e+14 |
| 87 | x = 3.e14 |
| 88 | x = .3e14 |
| 89 | x = 3.1e4 |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 90 | |
Benjamin Peterson | 93e51aa | 2014-06-07 12:36:39 -0700 | [diff] [blame] | 91 | def test_float_exponent_tokenization(self): |
| 92 | # See issue 21642. |
| 93 | self.assertEqual(1 if 1else 0, 1) |
| 94 | self.assertEqual(1 if 0else 0, 0) |
| 95 | self.assertRaises(SyntaxError, eval, "0 if 1Else 0") |
| 96 | |
Serhiy Storchaka | 0bcba37 | 2018-01-04 10:36:14 +0200 | [diff] [blame] | 97 | def test_string_literals(self): |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 98 | x = ''; y = ""; self.assertTrue(len(x) == 0 and x == y) |
| 99 | x = '\''; y = "'"; self.assertTrue(len(x) == 1 and x == y and ord(x) == 39) |
| 100 | x = '"'; y = "\""; self.assertTrue(len(x) == 1 and x == y and ord(x) == 34) |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 101 | x = "doesn't \"shrink\" does it" |
| 102 | y = 'doesn\'t "shrink" does it' |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 103 | self.assertTrue(len(x) == 24 and x == y) |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 104 | x = "does \"shrink\" doesn't it" |
| 105 | y = 'does "shrink" doesn\'t it' |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 106 | self.assertTrue(len(x) == 24 and x == y) |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 107 | x = """ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 108 | The "quick" |
| 109 | brown fox |
| 110 | jumps over |
| 111 | the 'lazy' dog. |
| 112 | """ |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 113 | y = '\nThe "quick"\nbrown fox\njumps over\nthe \'lazy\' dog.\n' |
Florent Xicluna | bc27c6a | 2010-03-19 18:34:55 +0000 | [diff] [blame] | 114 | self.assertEqual(x, y) |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 115 | y = ''' |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 116 | The "quick" |
| 117 | brown fox |
| 118 | jumps over |
| 119 | the 'lazy' dog. |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 120 | ''' |
Florent Xicluna | bc27c6a | 2010-03-19 18:34:55 +0000 | [diff] [blame] | 121 | self.assertEqual(x, y) |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 122 | y = "\n\ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 123 | The \"quick\"\n\ |
| 124 | brown fox\n\ |
| 125 | jumps over\n\ |
| 126 | the 'lazy' dog.\n\ |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 127 | " |
Florent Xicluna | bc27c6a | 2010-03-19 18:34:55 +0000 | [diff] [blame] | 128 | self.assertEqual(x, y) |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 129 | y = '\n\ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 130 | The \"quick\"\n\ |
| 131 | brown fox\n\ |
| 132 | jumps over\n\ |
| 133 | the \'lazy\' dog.\n\ |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 134 | ' |
Florent Xicluna | bc27c6a | 2010-03-19 18:34:55 +0000 | [diff] [blame] | 135 | self.assertEqual(x, y) |
Guido van Rossum | b31c7f7 | 1993-11-11 10:31:23 +0000 | [diff] [blame] | 136 | |
| 137 | |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 138 | class GrammarTests(unittest.TestCase): |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 139 | |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 140 | # single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE |
| 141 | # XXX can't test in a script -- this rule is only used when interactive |
Tim Peters | abd8a33 | 2006-11-03 02:32:46 +0000 | [diff] [blame] | 142 | |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 143 | # file_input: (NEWLINE | stmt)* ENDMARKER |
| 144 | # Being tested as this very moment this very module |
Tim Peters | abd8a33 | 2006-11-03 02:32:46 +0000 | [diff] [blame] | 145 | |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 146 | # expr_input: testlist NEWLINE |
| 147 | # XXX Hard to test -- used only in calls to input() |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 148 | |
Serhiy Storchaka | 0bcba37 | 2018-01-04 10:36:14 +0200 | [diff] [blame] | 149 | def test_eval_input(self): |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 150 | # testlist ENDMARKER |
| 151 | x = eval('1, 0 or 1') |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 152 | |
Serhiy Storchaka | 0bcba37 | 2018-01-04 10:36:14 +0200 | [diff] [blame] | 153 | def test_funcdef(self): |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 154 | ### 'def' NAME parameters ':' suite |
| 155 | ### parameters: '(' [varargslist] ')' |
| 156 | ### varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' ('**'|'*' '*') NAME] |
| 157 | ### | ('**'|'*' '*') NAME) |
| 158 | ### | fpdef ['=' test] (',' fpdef ['=' test])* [','] |
| 159 | ### fpdef: NAME | '(' fplist ')' |
| 160 | ### fplist: fpdef (',' fpdef)* [','] |
| 161 | ### arglist: (argument ',')* (argument | *' test [',' '**' test] | '**' test) |
| 162 | ### argument: [test '='] test # Really [keyword '='] test |
| 163 | def f1(): pass |
| 164 | f1() |
| 165 | f1(*()) |
| 166 | f1(*(), **{}) |
| 167 | def f2(one_argument): pass |
| 168 | def f3(two, arguments): pass |
Serhiy Storchaka | 0bcba37 | 2018-01-04 10:36:14 +0200 | [diff] [blame] | 169 | with check_py3k_warnings(('tuple parameter unpacking has been removed', |
| 170 | SyntaxWarning)): |
| 171 | exec('def f4(two, (compound, (argument, list))): pass') |
| 172 | exec('def f5((compound, first), two): pass') |
Florent Xicluna | bc27c6a | 2010-03-19 18:34:55 +0000 | [diff] [blame] | 173 | self.assertEqual(f2.func_code.co_varnames, ('one_argument',)) |
| 174 | self.assertEqual(f3.func_code.co_varnames, ('two', 'arguments')) |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 175 | if sys.platform.startswith('java'): |
Florent Xicluna | bc27c6a | 2010-03-19 18:34:55 +0000 | [diff] [blame] | 176 | self.assertEqual(f4.func_code.co_varnames, |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 177 | ('two', '(compound, (argument, list))', 'compound', 'argument', |
| 178 | 'list',)) |
Florent Xicluna | bc27c6a | 2010-03-19 18:34:55 +0000 | [diff] [blame] | 179 | self.assertEqual(f5.func_code.co_varnames, |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 180 | ('(compound, first)', 'two', 'compound', 'first')) |
| 181 | else: |
Florent Xicluna | bc27c6a | 2010-03-19 18:34:55 +0000 | [diff] [blame] | 182 | self.assertEqual(f4.func_code.co_varnames, |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 183 | ('two', '.1', 'compound', 'argument', 'list')) |
Florent Xicluna | bc27c6a | 2010-03-19 18:34:55 +0000 | [diff] [blame] | 184 | self.assertEqual(f5.func_code.co_varnames, |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 185 | ('.0', 'two', 'compound', 'first')) |
| 186 | def a1(one_arg,): pass |
| 187 | def a2(two, args,): pass |
| 188 | def v0(*rest): pass |
| 189 | def v1(a, *rest): pass |
| 190 | def v2(a, b, *rest): pass |
Serhiy Storchaka | 0bcba37 | 2018-01-04 10:36:14 +0200 | [diff] [blame] | 191 | with check_py3k_warnings(('tuple parameter unpacking has been removed', |
| 192 | SyntaxWarning)): |
| 193 | exec('def v3(a, (b, c), *rest): return a, b, c, rest') |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 194 | |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 195 | f1() |
| 196 | f2(1) |
| 197 | f2(1,) |
| 198 | f3(1, 2) |
| 199 | f3(1, 2,) |
| 200 | f4(1, (2, (3, 4))) |
| 201 | v0() |
| 202 | v0(1) |
| 203 | v0(1,) |
| 204 | v0(1,2) |
| 205 | v0(1,2,3,4,5,6,7,8,9,0) |
| 206 | v1(1) |
| 207 | v1(1,) |
| 208 | v1(1,2) |
| 209 | v1(1,2,3) |
| 210 | v1(1,2,3,4,5,6,7,8,9,0) |
| 211 | v2(1,2) |
| 212 | v2(1,2,3) |
| 213 | v2(1,2,3,4) |
| 214 | v2(1,2,3,4,5,6,7,8,9,0) |
| 215 | v3(1,(2,3)) |
| 216 | v3(1,(2,3),4) |
| 217 | v3(1,(2,3),4,5,6,7,8,9,0) |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 218 | |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 219 | # ceval unpacks the formal arguments into the first argcount names; |
| 220 | # thus, the names nested inside tuples must appear after these names. |
| 221 | if sys.platform.startswith('java'): |
Florent Xicluna | bc27c6a | 2010-03-19 18:34:55 +0000 | [diff] [blame] | 222 | self.assertEqual(v3.func_code.co_varnames, ('a', '(b, c)', 'rest', 'b', 'c')) |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 223 | else: |
Florent Xicluna | bc27c6a | 2010-03-19 18:34:55 +0000 | [diff] [blame] | 224 | self.assertEqual(v3.func_code.co_varnames, ('a', '.1', 'rest', 'b', 'c')) |
| 225 | self.assertEqual(v3(1, (2, 3), 4), (1, 2, 3, (4,))) |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 226 | def d01(a=1): pass |
| 227 | d01() |
| 228 | d01(1) |
| 229 | d01(*(1,)) |
Serhiy Storchaka | 0bcba37 | 2018-01-04 10:36:14 +0200 | [diff] [blame] | 230 | d01(*[] or [2]) |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 231 | d01(**{'a':2}) |
Serhiy Storchaka | 0bcba37 | 2018-01-04 10:36:14 +0200 | [diff] [blame] | 232 | d01(**{'a':2} or {}) |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 233 | def d11(a, b=1): pass |
| 234 | d11(1) |
| 235 | d11(1, 2) |
| 236 | d11(1, **{'b':2}) |
| 237 | def d21(a, b, c=1): pass |
| 238 | d21(1, 2) |
| 239 | d21(1, 2, 3) |
| 240 | d21(*(1, 2, 3)) |
| 241 | d21(1, *(2, 3)) |
| 242 | d21(1, 2, *(3,)) |
| 243 | d21(1, 2, **{'c':3}) |
| 244 | def d02(a=1, b=2): pass |
| 245 | d02() |
| 246 | d02(1) |
| 247 | d02(1, 2) |
| 248 | d02(*(1, 2)) |
| 249 | d02(1, *(2,)) |
| 250 | d02(1, **{'b':2}) |
| 251 | d02(**{'a': 1, 'b': 2}) |
| 252 | def d12(a, b=1, c=2): pass |
| 253 | d12(1) |
| 254 | d12(1, 2) |
| 255 | d12(1, 2, 3) |
| 256 | def d22(a, b, c=1, d=2): pass |
| 257 | d22(1, 2) |
| 258 | d22(1, 2, 3) |
| 259 | d22(1, 2, 3, 4) |
| 260 | def d01v(a=1, *rest): pass |
| 261 | d01v() |
| 262 | d01v(1) |
| 263 | d01v(1, 2) |
| 264 | d01v(*(1, 2, 3, 4)) |
| 265 | d01v(*(1,)) |
| 266 | d01v(**{'a':2}) |
| 267 | def d11v(a, b=1, *rest): pass |
| 268 | d11v(1) |
| 269 | d11v(1, 2) |
| 270 | d11v(1, 2, 3) |
| 271 | def d21v(a, b, c=1, *rest): pass |
| 272 | d21v(1, 2) |
| 273 | d21v(1, 2, 3) |
| 274 | d21v(1, 2, 3, 4) |
| 275 | d21v(*(1, 2, 3, 4)) |
| 276 | d21v(1, 2, **{'c': 3}) |
| 277 | def d02v(a=1, b=2, *rest): pass |
| 278 | d02v() |
| 279 | d02v(1) |
| 280 | d02v(1, 2) |
| 281 | d02v(1, 2, 3) |
| 282 | d02v(1, *(2, 3, 4)) |
| 283 | d02v(**{'a': 1, 'b': 2}) |
| 284 | def d12v(a, b=1, c=2, *rest): pass |
| 285 | d12v(1) |
| 286 | d12v(1, 2) |
| 287 | d12v(1, 2, 3) |
| 288 | d12v(1, 2, 3, 4) |
| 289 | d12v(*(1, 2, 3, 4)) |
| 290 | d12v(1, 2, *(3, 4, 5)) |
| 291 | d12v(1, *(2,), **{'c': 3}) |
| 292 | def d22v(a, b, c=1, d=2, *rest): pass |
| 293 | d22v(1, 2) |
| 294 | d22v(1, 2, 3) |
| 295 | d22v(1, 2, 3, 4) |
| 296 | d22v(1, 2, 3, 4, 5) |
| 297 | d22v(*(1, 2, 3, 4)) |
| 298 | d22v(1, 2, *(3, 4, 5)) |
| 299 | d22v(1, *(2, 3), **{'d': 4}) |
Serhiy Storchaka | 0bcba37 | 2018-01-04 10:36:14 +0200 | [diff] [blame] | 300 | with check_py3k_warnings(('parenthesized argument names are invalid', |
| 301 | SyntaxWarning)): |
| 302 | exec('def d31v((x)): pass') |
| 303 | with check_py3k_warnings(('tuple parameter unpacking has been removed', |
| 304 | SyntaxWarning)): |
| 305 | exec('def d32v((x,)): pass') |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 306 | d31v(1) |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 307 | d32v((1,)) |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 308 | |
Benjamin Peterson | 80f0ed5 | 2008-08-19 19:52:46 +0000 | [diff] [blame] | 309 | # keyword arguments after *arglist |
| 310 | def f(*args, **kwargs): |
| 311 | return args, kwargs |
Florent Xicluna | bc27c6a | 2010-03-19 18:34:55 +0000 | [diff] [blame] | 312 | self.assertEqual(f(1, x=2, *[3, 4], y=5), ((1, 3, 4), |
Benjamin Peterson | 80f0ed5 | 2008-08-19 19:52:46 +0000 | [diff] [blame] | 313 | {'x':2, 'y':5})) |
| 314 | self.assertRaises(SyntaxError, eval, "f(1, *(2,3), 4)") |
| 315 | self.assertRaises(SyntaxError, eval, "f(1, x=2, *(3,4), x=5)") |
Serhiy Storchaka | 0bcba37 | 2018-01-04 10:36:14 +0200 | [diff] [blame] | 316 | self.assertEqual(f(**{'eggs':'scrambled', 'spam':'fried'}), |
| 317 | ((), {'eggs':'scrambled', 'spam':'fried'})) |
| 318 | self.assertEqual(f(spam='fried', **{'eggs':'scrambled'}), |
| 319 | ((), {'eggs':'scrambled', 'spam':'fried'})) |
Benjamin Peterson | 80f0ed5 | 2008-08-19 19:52:46 +0000 | [diff] [blame] | 320 | |
Amaury Forgeot d'Arc | d21fb4c | 2008-03-05 01:50:33 +0000 | [diff] [blame] | 321 | # Check ast errors in *args and *kwargs |
| 322 | check_syntax_error(self, "f(*g(1=2))") |
| 323 | check_syntax_error(self, "f(**g(1=2))") |
| 324 | |
Serhiy Storchaka | 0bcba37 | 2018-01-04 10:36:14 +0200 | [diff] [blame] | 325 | # Check trailing commas are permitted in funcdef argument list |
| 326 | def f(a,): pass |
| 327 | |
| 328 | def test_lambdef(self): |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 329 | ### lambdef: 'lambda' [varargslist] ':' test |
| 330 | l1 = lambda : 0 |
Florent Xicluna | bc27c6a | 2010-03-19 18:34:55 +0000 | [diff] [blame] | 331 | self.assertEqual(l1(), 0) |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 332 | l2 = lambda : a[d] # XXX just testing the expression |
| 333 | l3 = lambda : [2 < x for x in [-1, 3, 0L]] |
Florent Xicluna | bc27c6a | 2010-03-19 18:34:55 +0000 | [diff] [blame] | 334 | self.assertEqual(l3(), [0, 1, 0]) |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 335 | l4 = lambda x = lambda y = lambda z=1 : z : y() : x() |
Florent Xicluna | bc27c6a | 2010-03-19 18:34:55 +0000 | [diff] [blame] | 336 | self.assertEqual(l4(), 1) |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 337 | l5 = lambda x, y, z=2: x + y + z |
Florent Xicluna | bc27c6a | 2010-03-19 18:34:55 +0000 | [diff] [blame] | 338 | self.assertEqual(l5(1, 2), 5) |
| 339 | self.assertEqual(l5(1, 2, 3), 6) |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 340 | check_syntax_error(self, "lambda x: x = 2") |
Serhiy Storchaka | 0bcba37 | 2018-01-04 10:36:14 +0200 | [diff] [blame] | 341 | with check_py3k_warnings(('tuple parameter unpacking has been removed', |
| 342 | SyntaxWarning)): |
| 343 | check_syntax_error(self, "lambda (None,): None") |
| 344 | |
| 345 | # check that trailing commas are permitted |
| 346 | l10 = lambda a,: 0 |
| 347 | |
Jeremy Hylton | 619eea6 | 2001-01-25 20:12:27 +0000 | [diff] [blame] | 348 | |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 349 | ### stmt: simple_stmt | compound_stmt |
| 350 | # Tested below |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 351 | |
Serhiy Storchaka | 0bcba37 | 2018-01-04 10:36:14 +0200 | [diff] [blame] | 352 | def test_simple_stmt(self): |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 353 | ### simple_stmt: small_stmt (';' small_stmt)* [';'] |
| 354 | x = 1; pass; del x |
| 355 | def foo(): |
Ezio Melotti | c2077b0 | 2011-03-16 12:34:31 +0200 | [diff] [blame] | 356 | # verify statements that end with semi-colons |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 357 | x = 1; pass; del x; |
| 358 | foo() |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 359 | |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 360 | ### small_stmt: expr_stmt | print_stmt | pass_stmt | del_stmt | flow_stmt | import_stmt | global_stmt | access_stmt | exec_stmt |
| 361 | # Tested below |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 362 | |
Serhiy Storchaka | 0bcba37 | 2018-01-04 10:36:14 +0200 | [diff] [blame] | 363 | def test_expr_stmt(self): |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 364 | # (exprlist '=')* exprlist |
| 365 | 1 |
| 366 | 1, 2, 3 |
| 367 | x = 1 |
| 368 | x = 1, 2, 3 |
| 369 | x = y = z = 1, 2, 3 |
| 370 | x, y, z = 1, 2, 3 |
| 371 | abc = a, b, c = x, y, z = xyz = 1, 2, (3, 4) |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 372 | |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 373 | check_syntax_error(self, "x + 1 = 1") |
| 374 | check_syntax_error(self, "a + 1 = b + 2") |
Jeremy Hylton | 4779399 | 2001-02-19 15:35:26 +0000 | [diff] [blame] | 375 | |
Serhiy Storchaka | 0bcba37 | 2018-01-04 10:36:14 +0200 | [diff] [blame] | 376 | def test_print_stmt(self): |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 377 | # 'print' (test ',')* [test] |
| 378 | import StringIO |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 379 | |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 380 | # Can't test printing to real stdout without comparing output |
| 381 | # which is not available in unittest. |
| 382 | save_stdout = sys.stdout |
| 383 | sys.stdout = StringIO.StringIO() |
Tim Peters | abd8a33 | 2006-11-03 02:32:46 +0000 | [diff] [blame] | 384 | |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 385 | print 1, 2, 3 |
| 386 | print 1, 2, 3, |
| 387 | print |
| 388 | print 0 or 1, 0 or 1, |
| 389 | print 0 or 1 |
Barry Warsaw | efc92ee | 2000-08-21 15:46:50 +0000 | [diff] [blame] | 390 | |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 391 | # 'print' '>>' test ',' |
| 392 | print >> sys.stdout, 1, 2, 3 |
| 393 | print >> sys.stdout, 1, 2, 3, |
| 394 | print >> sys.stdout |
| 395 | print >> sys.stdout, 0 or 1, 0 or 1, |
| 396 | print >> sys.stdout, 0 or 1 |
Barry Warsaw | 9182b45 | 2000-08-29 04:57:10 +0000 | [diff] [blame] | 397 | |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 398 | # test printing to an instance |
| 399 | class Gulp: |
| 400 | def write(self, msg): pass |
Barry Warsaw | 7e3e1c1 | 2000-10-11 21:26:03 +0000 | [diff] [blame] | 401 | |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 402 | gulp = Gulp() |
| 403 | print >> gulp, 1, 2, 3 |
| 404 | print >> gulp, 1, 2, 3, |
| 405 | print >> gulp |
| 406 | print >> gulp, 0 or 1, 0 or 1, |
| 407 | print >> gulp, 0 or 1 |
Barry Warsaw | 9182b45 | 2000-08-29 04:57:10 +0000 | [diff] [blame] | 408 | |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 409 | # test print >> None |
| 410 | def driver(): |
| 411 | oldstdout = sys.stdout |
| 412 | sys.stdout = Gulp() |
| 413 | try: |
| 414 | tellme(Gulp()) |
| 415 | tellme() |
| 416 | finally: |
| 417 | sys.stdout = oldstdout |
Barry Warsaw | 9182b45 | 2000-08-29 04:57:10 +0000 | [diff] [blame] | 418 | |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 419 | # we should see this once |
| 420 | def tellme(file=sys.stdout): |
| 421 | print >> file, 'hello world' |
Barry Warsaw | 9182b45 | 2000-08-29 04:57:10 +0000 | [diff] [blame] | 422 | |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 423 | driver() |
Barry Warsaw | 9182b45 | 2000-08-29 04:57:10 +0000 | [diff] [blame] | 424 | |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 425 | # we should not see this at all |
| 426 | def tellme(file=None): |
| 427 | print >> file, 'goodbye universe' |
Barry Warsaw | 9182b45 | 2000-08-29 04:57:10 +0000 | [diff] [blame] | 428 | |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 429 | driver() |
Barry Warsaw | efc92ee | 2000-08-21 15:46:50 +0000 | [diff] [blame] | 430 | |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 431 | self.assertEqual(sys.stdout.getvalue(), '''\ |
| 432 | 1 2 3 |
| 433 | 1 2 3 |
| 434 | 1 1 1 |
| 435 | 1 2 3 |
| 436 | 1 2 3 |
| 437 | 1 1 1 |
| 438 | hello world |
| 439 | ''') |
| 440 | sys.stdout = save_stdout |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 441 | |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 442 | # syntax errors |
| 443 | check_syntax_error(self, 'print ,') |
| 444 | check_syntax_error(self, 'print >> x,') |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 445 | |
Serhiy Storchaka | 0bcba37 | 2018-01-04 10:36:14 +0200 | [diff] [blame] | 446 | def test_del_stmt(self): |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 447 | # 'del' exprlist |
| 448 | abc = [1,2,3] |
| 449 | x, y, z = abc |
| 450 | xyz = x, y, z |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 451 | |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 452 | del abc |
| 453 | del x, y, (z, xyz) |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 454 | |
Serhiy Storchaka | 0bcba37 | 2018-01-04 10:36:14 +0200 | [diff] [blame] | 455 | def test_pass_stmt(self): |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 456 | # 'pass' |
| 457 | pass |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 458 | |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 459 | # flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt |
| 460 | # Tested below |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 461 | |
Serhiy Storchaka | 0bcba37 | 2018-01-04 10:36:14 +0200 | [diff] [blame] | 462 | def test_break_stmt(self): |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 463 | # 'break' |
| 464 | while 1: break |
Tim Peters | 10fb386 | 2001-02-09 20:17:14 +0000 | [diff] [blame] | 465 | |
Serhiy Storchaka | 0bcba37 | 2018-01-04 10:36:14 +0200 | [diff] [blame] | 466 | def test_continue_stmt(self): |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 467 | # 'continue' |
| 468 | i = 1 |
| 469 | while i: i = 0; continue |
Thomas Wouters | 80d373c | 2001-09-26 12:43:39 +0000 | [diff] [blame] | 470 | |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 471 | msg = "" |
| 472 | while not msg: |
| 473 | msg = "ok" |
| 474 | try: |
| 475 | continue |
| 476 | msg = "continue failed to continue inside try" |
| 477 | except: |
| 478 | msg = "continue inside try called except block" |
| 479 | if msg != "ok": |
| 480 | self.fail(msg) |
Thomas Wouters | 80d373c | 2001-09-26 12:43:39 +0000 | [diff] [blame] | 481 | |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 482 | msg = "" |
| 483 | while not msg: |
| 484 | msg = "finally block not called" |
| 485 | try: |
| 486 | continue |
| 487 | finally: |
| 488 | msg = "ok" |
| 489 | if msg != "ok": |
| 490 | self.fail(msg) |
| 491 | |
| 492 | def test_break_continue_loop(self): |
| 493 | # This test warrants an explanation. It is a test specifically for SF bugs |
| 494 | # #463359 and #462937. The bug is that a 'break' statement executed or |
| 495 | # exception raised inside a try/except inside a loop, *after* a continue |
| 496 | # statement has been executed in that loop, will cause the wrong number of |
| 497 | # arguments to be popped off the stack and the instruction pointer reset to |
| 498 | # a very small number (usually 0.) Because of this, the following test |
| 499 | # *must* written as a function, and the tracking vars *must* be function |
| 500 | # arguments with default values. Otherwise, the test will loop and loop. |
| 501 | |
| 502 | def test_inner(extra_burning_oil = 1, count=0): |
| 503 | big_hippo = 2 |
| 504 | while big_hippo: |
| 505 | count += 1 |
| 506 | try: |
| 507 | if extra_burning_oil and big_hippo == 1: |
| 508 | extra_burning_oil -= 1 |
| 509 | break |
| 510 | big_hippo -= 1 |
| 511 | continue |
| 512 | except: |
| 513 | raise |
Ezio Melotti | d80b4bf | 2010-03-17 13:52:48 +0000 | [diff] [blame] | 514 | if count > 2 or big_hippo != 1: |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 515 | self.fail("continue then break in try/except in loop broken!") |
| 516 | test_inner() |
| 517 | |
Serhiy Storchaka | 0bcba37 | 2018-01-04 10:36:14 +0200 | [diff] [blame] | 518 | def test_return(self): |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 519 | # 'return' [testlist] |
| 520 | def g1(): return |
| 521 | def g2(): return 1 |
| 522 | g1() |
| 523 | x = g2() |
| 524 | check_syntax_error(self, "class foo:return 1") |
| 525 | |
Serhiy Storchaka | b495377 | 2018-01-02 10:56:40 +0200 | [diff] [blame] | 526 | def test_break_in_finally(self): |
| 527 | count = 0 |
| 528 | while count < 2: |
| 529 | count += 1 |
| 530 | try: |
| 531 | pass |
| 532 | finally: |
| 533 | break |
| 534 | self.assertEqual(count, 1) |
| 535 | |
| 536 | count = 0 |
| 537 | while count < 2: |
| 538 | count += 1 |
| 539 | try: |
| 540 | continue |
| 541 | finally: |
| 542 | break |
| 543 | self.assertEqual(count, 1) |
| 544 | |
| 545 | count = 0 |
| 546 | while count < 2: |
| 547 | count += 1 |
| 548 | try: |
Serhiy Storchaka | 0bcba37 | 2018-01-04 10:36:14 +0200 | [diff] [blame] | 549 | 1.0/0.0 |
Serhiy Storchaka | b495377 | 2018-01-02 10:56:40 +0200 | [diff] [blame] | 550 | finally: |
| 551 | break |
| 552 | self.assertEqual(count, 1) |
| 553 | |
| 554 | for count in [0, 1]: |
| 555 | self.assertEqual(count, 0) |
| 556 | try: |
| 557 | pass |
| 558 | finally: |
| 559 | break |
| 560 | self.assertEqual(count, 0) |
| 561 | |
| 562 | for count in [0, 1]: |
| 563 | self.assertEqual(count, 0) |
| 564 | try: |
| 565 | continue |
| 566 | finally: |
| 567 | break |
| 568 | self.assertEqual(count, 0) |
| 569 | |
| 570 | for count in [0, 1]: |
| 571 | self.assertEqual(count, 0) |
| 572 | try: |
Serhiy Storchaka | 0bcba37 | 2018-01-04 10:36:14 +0200 | [diff] [blame] | 573 | 1.0/0.0 |
Serhiy Storchaka | b495377 | 2018-01-02 10:56:40 +0200 | [diff] [blame] | 574 | finally: |
| 575 | break |
| 576 | self.assertEqual(count, 0) |
| 577 | |
| 578 | def test_return_in_finally(self): |
| 579 | def g1(): |
| 580 | try: |
| 581 | pass |
| 582 | finally: |
| 583 | return 1 |
| 584 | self.assertEqual(g1(), 1) |
| 585 | |
| 586 | def g2(): |
| 587 | try: |
| 588 | return 2 |
| 589 | finally: |
| 590 | return 3 |
| 591 | self.assertEqual(g2(), 3) |
| 592 | |
| 593 | def g3(): |
| 594 | try: |
Serhiy Storchaka | 0bcba37 | 2018-01-04 10:36:14 +0200 | [diff] [blame] | 595 | 1.0/0.0 |
Serhiy Storchaka | b495377 | 2018-01-02 10:56:40 +0200 | [diff] [blame] | 596 | finally: |
| 597 | return 4 |
| 598 | self.assertEqual(g3(), 4) |
| 599 | |
Serhiy Storchaka | 0bcba37 | 2018-01-04 10:36:14 +0200 | [diff] [blame] | 600 | def test_yield(self): |
| 601 | # Allowed as standalone statement |
| 602 | def g(): yield 1 |
| 603 | # Allowed as RHS of assignment |
| 604 | def g(): x = yield 1 |
| 605 | # Ordinary yield accepts implicit tuples |
| 606 | def g(): yield 1, 1 |
| 607 | def g(): x = yield 1, 1 |
| 608 | # Requires parentheses as subexpression |
| 609 | def g(): 1, (yield 1) |
| 610 | check_syntax_error(self, "def g(): 1, yield 1") |
| 611 | # Requires parentheses as call argument |
| 612 | def g(): f((yield 1)) |
| 613 | def g(): f((yield 1), 1) |
| 614 | check_syntax_error(self, "def g(): f(yield 1)") |
| 615 | check_syntax_error(self, "def g(): f(yield 1, 1)") |
| 616 | # Not allowed at top level |
| 617 | check_syntax_error(self, "yield") |
| 618 | # Not allowed at class scope |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 619 | check_syntax_error(self, "class foo:yield 1") |
Serhiy Storchaka | 0bcba37 | 2018-01-04 10:36:14 +0200 | [diff] [blame] | 620 | # Check annotation refleak on SyntaxError |
| 621 | check_syntax_error(self, "def g(a:(yield)): pass") |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 622 | |
Serhiy Storchaka | 65d1887 | 2017-12-02 21:00:09 +0200 | [diff] [blame] | 623 | def test_yield_in_comprehensions(self): |
| 624 | # Check yield in comprehensions |
| 625 | def g(): [x for x in [(yield 1)]] |
| 626 | |
| 627 | def check(code, warntext): |
| 628 | with check_py3k_warnings((warntext, DeprecationWarning)): |
| 629 | compile(code, '<test string>', 'exec') |
| 630 | if sys.py3kwarning: |
| 631 | import warnings |
| 632 | with warnings.catch_warnings(): |
| 633 | warnings.filterwarnings('error', category=DeprecationWarning) |
| 634 | with self.assertRaises(SyntaxError) as cm: |
| 635 | compile(code, '<test string>', 'exec') |
| 636 | self.assertIn(warntext, str(cm.exception)) |
| 637 | |
| 638 | check("def g(): [(yield x) for x in ()]", |
| 639 | "'yield' inside list comprehension") |
| 640 | check("def g(): [x for x in () if not (yield x)]", |
| 641 | "'yield' inside list comprehension") |
| 642 | check("def g(): [y for x in () for y in [(yield x)]]", |
| 643 | "'yield' inside list comprehension") |
| 644 | check("def g(): {(yield x) for x in ()}", |
| 645 | "'yield' inside set comprehension") |
| 646 | check("def g(): {(yield x): x for x in ()}", |
| 647 | "'yield' inside dict comprehension") |
| 648 | check("def g(): {x: (yield x) for x in ()}", |
| 649 | "'yield' inside dict comprehension") |
| 650 | check("def g(): ((yield x) for x in ())", |
| 651 | "'yield' inside generator expression") |
| 652 | with check_py3k_warnings(("'yield' inside list comprehension", |
| 653 | DeprecationWarning)): |
| 654 | check_syntax_error(self, "class C: [(yield x) for x in ()]") |
| 655 | check("class C: ((yield x) for x in ())", |
| 656 | "'yield' inside generator expression") |
| 657 | with check_py3k_warnings(("'yield' inside list comprehension", |
| 658 | DeprecationWarning)): |
| 659 | check_syntax_error(self, "[(yield x) for x in ()]") |
| 660 | check("((yield x) for x in ())", |
| 661 | "'yield' inside generator expression") |
| 662 | |
Serhiy Storchaka | 0bcba37 | 2018-01-04 10:36:14 +0200 | [diff] [blame] | 663 | def test_raise(self): |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 664 | # 'raise' test [',' test] |
| 665 | try: raise RuntimeError, 'just testing' |
| 666 | except RuntimeError: pass |
| 667 | try: raise KeyboardInterrupt |
| 668 | except KeyboardInterrupt: pass |
| 669 | |
Serhiy Storchaka | 0bcba37 | 2018-01-04 10:36:14 +0200 | [diff] [blame] | 670 | def test_import(self): |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 671 | # 'import' dotted_as_names |
| 672 | import sys |
| 673 | import time, sys |
| 674 | # 'from' dotted_name 'import' ('*' | '(' import_as_names ')' | import_as_names) |
| 675 | from time import time |
| 676 | from time import (time) |
| 677 | # not testable inside a function, but already done at top of the module |
| 678 | # from sys import * |
| 679 | from sys import path, argv |
| 680 | from sys import (path, argv) |
| 681 | from sys import (path, argv,) |
| 682 | |
Serhiy Storchaka | 0bcba37 | 2018-01-04 10:36:14 +0200 | [diff] [blame] | 683 | def test_global(self): |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 684 | # 'global' NAME (',' NAME)* |
| 685 | global a |
| 686 | global a, b |
| 687 | global one, two, three, four, five, six, seven, eight, nine, ten |
| 688 | |
Serhiy Storchaka | 0bcba37 | 2018-01-04 10:36:14 +0200 | [diff] [blame] | 689 | def test_exec(self): |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 690 | # 'exec' expr ['in' expr [',' expr]] |
| 691 | z = None |
| 692 | del z |
| 693 | exec 'z=1+1\n' |
| 694 | if z != 2: self.fail('exec \'z=1+1\'\\n') |
| 695 | del z |
| 696 | exec 'z=1+1' |
| 697 | if z != 2: self.fail('exec \'z=1+1\'') |
| 698 | z = None |
| 699 | del z |
| 700 | import types |
| 701 | if hasattr(types, "UnicodeType"): |
| 702 | exec r"""if 1: |
| 703 | exec u'z=1+1\n' |
| 704 | if z != 2: self.fail('exec u\'z=1+1\'\\n') |
| 705 | del z |
| 706 | exec u'z=1+1' |
| 707 | if z != 2: self.fail('exec u\'z=1+1\'')""" |
| 708 | g = {} |
| 709 | exec 'z = 1' in g |
Ezio Melotti | d80b4bf | 2010-03-17 13:52:48 +0000 | [diff] [blame] | 710 | if '__builtins__' in g: del g['__builtins__'] |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 711 | if g != {'z': 1}: self.fail('exec \'z = 1\' in g') |
| 712 | g = {} |
| 713 | l = {} |
| 714 | |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 715 | exec 'global a; a = 1; b = 2' in g, l |
Ezio Melotti | d80b4bf | 2010-03-17 13:52:48 +0000 | [diff] [blame] | 716 | if '__builtins__' in g: del g['__builtins__'] |
| 717 | if '__builtins__' in l: del l['__builtins__'] |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 718 | if (g, l) != ({'a':1}, {'b':2}): |
| 719 | self.fail('exec ... in g (%s), l (%s)' %(g,l)) |
| 720 | |
Serhiy Storchaka | 0bcba37 | 2018-01-04 10:36:14 +0200 | [diff] [blame] | 721 | def test_assert(self): |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 722 | # assertTruestmt: 'assert' test [',' test] |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 723 | assert 1 |
| 724 | assert 1, 1 |
| 725 | assert lambda x:x |
| 726 | assert 1, lambda x:x+1 |
Ezio Melotti | ab731a3 | 2011-12-02 18:17:30 +0200 | [diff] [blame] | 727 | |
| 728 | try: |
| 729 | assert True |
| 730 | except AssertionError as e: |
| 731 | self.fail("'assert True' should not have raised an AssertionError") |
| 732 | |
| 733 | try: |
| 734 | assert True, 'this should always pass' |
| 735 | except AssertionError as e: |
| 736 | self.fail("'assert True, msg' should not have " |
| 737 | "raised an AssertionError") |
| 738 | |
| 739 | # these tests fail if python is run with -O, so check __debug__ |
| 740 | @unittest.skipUnless(__debug__, "Won't work if __debug__ is False") |
| 741 | def testAssert2(self): |
Thomas Wouters | 80d373c | 2001-09-26 12:43:39 +0000 | [diff] [blame] | 742 | try: |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 743 | assert 0, "msg" |
| 744 | except AssertionError, e: |
Florent Xicluna | bc27c6a | 2010-03-19 18:34:55 +0000 | [diff] [blame] | 745 | self.assertEqual(e.args[0], "msg") |
Georg Brandl | facd273 | 2006-10-29 09:18:00 +0000 | [diff] [blame] | 746 | else: |
Ezio Melotti | ab731a3 | 2011-12-02 18:17:30 +0200 | [diff] [blame] | 747 | self.fail("AssertionError not raised by assert 0") |
| 748 | |
| 749 | try: |
| 750 | assert False |
| 751 | except AssertionError as e: |
| 752 | self.assertEqual(len(e.args), 0) |
| 753 | else: |
| 754 | self.fail("AssertionError not raised by 'assert False'") |
| 755 | |
Thomas Wouters | 80d373c | 2001-09-26 12:43:39 +0000 | [diff] [blame] | 756 | |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 757 | ### compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef |
| 758 | # Tested below |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 759 | |
Serhiy Storchaka | 0bcba37 | 2018-01-04 10:36:14 +0200 | [diff] [blame] | 760 | def test_if(self): |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 761 | # 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] |
| 762 | if 1: pass |
| 763 | if 1: pass |
| 764 | else: pass |
| 765 | if 0: pass |
| 766 | elif 0: pass |
| 767 | if 0: pass |
| 768 | elif 0: pass |
| 769 | elif 0: pass |
| 770 | elif 0: pass |
| 771 | else: pass |
Tim Peters | abd8a33 | 2006-11-03 02:32:46 +0000 | [diff] [blame] | 772 | |
Serhiy Storchaka | 0bcba37 | 2018-01-04 10:36:14 +0200 | [diff] [blame] | 773 | def test_while(self): |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 774 | # 'while' test ':' suite ['else' ':' suite] |
| 775 | while 0: pass |
| 776 | while 0: pass |
| 777 | else: pass |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 778 | |
Amaury Forgeot d'Arc | 16570f5 | 2008-01-24 22:51:18 +0000 | [diff] [blame] | 779 | # Issue1920: "while 0" is optimized away, |
| 780 | # ensure that the "else" clause is still present. |
| 781 | x = 0 |
| 782 | while 0: |
| 783 | x = 1 |
| 784 | else: |
| 785 | x = 2 |
Florent Xicluna | bc27c6a | 2010-03-19 18:34:55 +0000 | [diff] [blame] | 786 | self.assertEqual(x, 2) |
Amaury Forgeot d'Arc | 16570f5 | 2008-01-24 22:51:18 +0000 | [diff] [blame] | 787 | |
Serhiy Storchaka | 0bcba37 | 2018-01-04 10:36:14 +0200 | [diff] [blame] | 788 | def test_for(self): |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 789 | # 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite] |
| 790 | for i in 1, 2, 3: pass |
| 791 | for i, j, k in (): pass |
| 792 | else: pass |
| 793 | class Squares: |
| 794 | def __init__(self, max): |
| 795 | self.max = max |
| 796 | self.sofar = [] |
| 797 | def __len__(self): return len(self.sofar) |
| 798 | def __getitem__(self, i): |
| 799 | if not 0 <= i < self.max: raise IndexError |
| 800 | n = len(self.sofar) |
| 801 | while n <= i: |
| 802 | self.sofar.append(n*n) |
| 803 | n = n+1 |
| 804 | return self.sofar[i] |
| 805 | n = 0 |
| 806 | for x in Squares(10): n = n+x |
| 807 | if n != 285: |
| 808 | self.fail('for over growing sequence') |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 809 | |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 810 | result = [] |
| 811 | for x, in [(1,), (2,), (3,)]: |
| 812 | result.append(x) |
| 813 | self.assertEqual(result, [1, 2, 3]) |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 814 | |
Serhiy Storchaka | 0bcba37 | 2018-01-04 10:36:14 +0200 | [diff] [blame] | 815 | def test_try(self): |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 816 | ### try_stmt: 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite] |
| 817 | ### | 'try' ':' suite 'finally' ':' suite |
Collin Winter | 6290305 | 2007-05-18 23:11:24 +0000 | [diff] [blame] | 818 | ### except_clause: 'except' [expr [('as' | ',') expr]] |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 819 | try: |
Serhiy Storchaka | 0bcba37 | 2018-01-04 10:36:14 +0200 | [diff] [blame] | 820 | 1/0.0 |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 821 | except ZeroDivisionError: |
| 822 | pass |
| 823 | else: |
| 824 | pass |
Serhiy Storchaka | 0bcba37 | 2018-01-04 10:36:14 +0200 | [diff] [blame] | 825 | try: 1/0.0 |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 826 | except EOFError: pass |
Collin Winter | 6290305 | 2007-05-18 23:11:24 +0000 | [diff] [blame] | 827 | except TypeError as msg: pass |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 828 | except RuntimeError, msg: pass |
| 829 | except: pass |
| 830 | else: pass |
Serhiy Storchaka | 0bcba37 | 2018-01-04 10:36:14 +0200 | [diff] [blame] | 831 | try: 1/0.0 |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 832 | except (EOFError, TypeError, ZeroDivisionError): pass |
Serhiy Storchaka | 0bcba37 | 2018-01-04 10:36:14 +0200 | [diff] [blame] | 833 | try: 1/0.0 |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 834 | except (EOFError, TypeError, ZeroDivisionError), msg: pass |
| 835 | try: pass |
| 836 | finally: pass |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 837 | |
Serhiy Storchaka | 0bcba37 | 2018-01-04 10:36:14 +0200 | [diff] [blame] | 838 | def test_suite(self): |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 839 | # simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT |
| 840 | if 1: pass |
| 841 | if 1: |
| 842 | pass |
| 843 | if 1: |
| 844 | # |
| 845 | # |
| 846 | # |
| 847 | pass |
| 848 | pass |
| 849 | # |
| 850 | pass |
| 851 | # |
Jeremy Hylton | 2922ea8 | 2001-02-28 23:49:19 +0000 | [diff] [blame] | 852 | |
Serhiy Storchaka | 0bcba37 | 2018-01-04 10:36:14 +0200 | [diff] [blame] | 853 | def test_test(self): |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 854 | ### and_test ('or' and_test)* |
| 855 | ### and_test: not_test ('and' not_test)* |
| 856 | ### not_test: 'not' not_test | comparison |
| 857 | if not 1: pass |
| 858 | if 1 and 1: pass |
| 859 | if 1 or 1: pass |
| 860 | if not not not 1: pass |
| 861 | if not 1 and 1 and 1: pass |
| 862 | if 1 and 1 or 1 and 1 and 1 or not 1 and 1: pass |
| 863 | |
Serhiy Storchaka | 0bcba37 | 2018-01-04 10:36:14 +0200 | [diff] [blame] | 864 | def test_comparison(self): |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 865 | ### comparison: expr (comp_op expr)* |
| 866 | ### comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not' |
| 867 | if 1: pass |
| 868 | x = (1 == 1) |
| 869 | if 1 == 1: pass |
| 870 | if 1 != 1: pass |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 871 | if 1 < 1: pass |
| 872 | if 1 > 1: pass |
| 873 | if 1 <= 1: pass |
| 874 | if 1 >= 1: pass |
| 875 | if 1 is 1: pass |
| 876 | if 1 is not 1: pass |
| 877 | if 1 in (): pass |
| 878 | if 1 not in (): pass |
Ezio Melotti | d80b4bf | 2010-03-17 13:52:48 +0000 | [diff] [blame] | 879 | if 1 < 1 > 1 == 1 >= 1 <= 1 != 1 in 1 not in 1 is 1 is not 1: pass |
Serhiy Storchaka | 0bcba37 | 2018-01-04 10:36:14 +0200 | [diff] [blame] | 880 | with check_py3k_warnings(('<> not supported in 3.x; use !=', |
| 881 | DeprecationWarning)): |
| 882 | if eval('1 <> 1'): pass |
| 883 | with check_py3k_warnings(('<> not supported in 3.x; use !=', |
| 884 | DeprecationWarning)): |
| 885 | if eval('1 < 1 > 1 == 1 >= 1 <= 1 <> 1 != 1 in 1 not in 1 is 1 is not 1'): pass |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 886 | |
Serhiy Storchaka | 0bcba37 | 2018-01-04 10:36:14 +0200 | [diff] [blame] | 887 | def test_binary_mask_ops(self): |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 888 | x = 1 & 1 |
| 889 | x = 1 ^ 1 |
| 890 | x = 1 | 1 |
| 891 | |
Serhiy Storchaka | 0bcba37 | 2018-01-04 10:36:14 +0200 | [diff] [blame] | 892 | def test_shift_ops(self): |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 893 | x = 1 << 1 |
| 894 | x = 1 >> 1 |
| 895 | x = 1 << 1 >> 1 |
| 896 | |
Serhiy Storchaka | 0bcba37 | 2018-01-04 10:36:14 +0200 | [diff] [blame] | 897 | def test_additive_ops(self): |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 898 | x = 1 |
| 899 | x = 1 + 1 |
| 900 | x = 1 - 1 - 1 |
| 901 | x = 1 - 1 + 1 - 1 + 1 |
| 902 | |
Serhiy Storchaka | 0bcba37 | 2018-01-04 10:36:14 +0200 | [diff] [blame] | 903 | def test_multiplicative_ops(self): |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 904 | x = 1 * 1 |
Serhiy Storchaka | 0bcba37 | 2018-01-04 10:36:14 +0200 | [diff] [blame] | 905 | with check_py3k_warnings(('classic int division', DeprecationWarning)): |
| 906 | x = 1 / 1 |
| 907 | x = 1 / 1.0 |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 908 | x = 1 % 1 |
Serhiy Storchaka | 0bcba37 | 2018-01-04 10:36:14 +0200 | [diff] [blame] | 909 | with check_py3k_warnings(('classic int division', DeprecationWarning)): |
| 910 | x = 1 / 1 * 1 % 1 |
| 911 | x = 1 / 1.0 * 1 % 1 |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 912 | |
Serhiy Storchaka | 0bcba37 | 2018-01-04 10:36:14 +0200 | [diff] [blame] | 913 | def test_unary_ops(self): |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 914 | x = +1 |
| 915 | x = -1 |
| 916 | x = ~1 |
| 917 | x = ~1 ^ 1 & 1 | 1 & 1 ^ -1 |
Serhiy Storchaka | 0bcba37 | 2018-01-04 10:36:14 +0200 | [diff] [blame] | 918 | with check_py3k_warnings(('classic int division', DeprecationWarning)): |
| 919 | x = -1*1/1 + 1*1 - ---1*1 |
| 920 | x = -1*1/1.0 + 1*1 - ---1*1 |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 921 | |
Serhiy Storchaka | 0bcba37 | 2018-01-04 10:36:14 +0200 | [diff] [blame] | 922 | def test_selectors(self): |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 923 | ### trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME |
| 924 | ### subscript: expr | [expr] ':' [expr] |
Tim Peters | abd8a33 | 2006-11-03 02:32:46 +0000 | [diff] [blame] | 925 | |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 926 | import sys, time |
| 927 | c = sys.path[0] |
| 928 | x = time.time() |
| 929 | x = sys.modules['time'].time() |
| 930 | a = '01234' |
| 931 | c = a[0] |
| 932 | c = a[-1] |
| 933 | s = a[0:5] |
| 934 | s = a[:5] |
| 935 | s = a[0:] |
| 936 | s = a[:] |
| 937 | s = a[-5:] |
| 938 | s = a[:-1] |
| 939 | s = a[-4:-3] |
| 940 | # A rough test of SF bug 1333982. http://python.org/sf/1333982 |
| 941 | # The testing here is fairly incomplete. |
| 942 | # Test cases should include: commas with 1 and 2 colons |
| 943 | d = {} |
| 944 | d[1] = 1 |
| 945 | d[1,] = 2 |
| 946 | d[1,2] = 3 |
| 947 | d[1,2,3] = 4 |
| 948 | L = list(d) |
Serhiy Storchaka | 0bcba37 | 2018-01-04 10:36:14 +0200 | [diff] [blame] | 949 | L.sort(key=lambda x: (type(x).__name__, x)) |
Florent Xicluna | bc27c6a | 2010-03-19 18:34:55 +0000 | [diff] [blame] | 950 | self.assertEqual(str(L), '[1, (1,), (1, 2), (1, 2, 3)]') |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 951 | |
Serhiy Storchaka | 0bcba37 | 2018-01-04 10:36:14 +0200 | [diff] [blame] | 952 | def test_atoms(self): |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 953 | ### atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING |
Alexandre Vassalotti | ee936a2 | 2010-01-09 23:35:54 +0000 | [diff] [blame] | 954 | ### dictorsetmaker: (test ':' test (',' test ':' test)* [',']) | (test (',' test)* [',']) |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 955 | |
| 956 | x = (1) |
| 957 | x = (1 or 2 or 3) |
| 958 | x = (1 or 2 or 3, 2, 3) |
| 959 | |
| 960 | x = [] |
| 961 | x = [1] |
| 962 | x = [1 or 2 or 3] |
| 963 | x = [1 or 2 or 3, 2, 3] |
| 964 | x = [] |
| 965 | |
| 966 | x = {} |
| 967 | x = {'one': 1} |
| 968 | x = {'one': 1,} |
| 969 | x = {'one' or 'two': 1 or 2} |
| 970 | x = {'one': 1, 'two': 2} |
| 971 | x = {'one': 1, 'two': 2,} |
| 972 | x = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6} |
| 973 | |
Alexandre Vassalotti | ee936a2 | 2010-01-09 23:35:54 +0000 | [diff] [blame] | 974 | x = {'one'} |
| 975 | x = {'one', 1,} |
| 976 | x = {'one', 'two', 'three'} |
| 977 | x = {2, 3, 4,} |
| 978 | |
Serhiy Storchaka | 0bcba37 | 2018-01-04 10:36:14 +0200 | [diff] [blame] | 979 | with check_py3k_warnings(('backquote not supported', SyntaxWarning)): |
| 980 | x = eval('`x`') |
| 981 | x = eval('`1 or 2 or 3`') |
| 982 | self.assertEqual(eval('`1,2`'), '(1, 2)') |
Neal Norwitz | 85dbec6 | 2006-11-04 19:25:22 +0000 | [diff] [blame] | 983 | |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 984 | x = x |
| 985 | x = 'x' |
| 986 | x = 123 |
| 987 | |
| 988 | ### exprlist: expr (',' expr)* [','] |
| 989 | ### testlist: test (',' test)* [','] |
| 990 | # These have been exercised enough above |
| 991 | |
Serhiy Storchaka | 0bcba37 | 2018-01-04 10:36:14 +0200 | [diff] [blame] | 992 | def test_classdef(self): |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 993 | # 'class' NAME ['(' [testlist] ')'] ':' suite |
| 994 | class B: pass |
| 995 | class B2(): pass |
| 996 | class C1(B): pass |
| 997 | class C2(B): pass |
| 998 | class D(C1, C2, B): pass |
| 999 | class C: |
| 1000 | def meth1(self): pass |
| 1001 | def meth2(self, arg): pass |
| 1002 | def meth3(self, a1, a2): pass |
Serhiy Storchaka | 0bcba37 | 2018-01-04 10:36:14 +0200 | [diff] [blame] | 1003 | |
Christian Heimes | 5224d28 | 2008-02-23 15:01:05 +0000 | [diff] [blame] | 1004 | # decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE |
| 1005 | # decorators: decorator+ |
| 1006 | # decorated: decorators (classdef | funcdef) |
| 1007 | def class_decorator(x): |
| 1008 | x.decorated = True |
| 1009 | return x |
| 1010 | @class_decorator |
| 1011 | class G: |
| 1012 | pass |
| 1013 | self.assertEqual(G.decorated, True) |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 1014 | |
Serhiy Storchaka | 0bcba37 | 2018-01-04 10:36:14 +0200 | [diff] [blame] | 1015 | def test_dictcomps(self): |
Alexandre Vassalotti | b646547 | 2010-01-11 22:36:12 +0000 | [diff] [blame] | 1016 | # dictorsetmaker: ( (test ':' test (comp_for | |
| 1017 | # (',' test ':' test)* [','])) | |
| 1018 | # (test (comp_for | (',' test)* [','])) ) |
| 1019 | nums = [1, 2, 3] |
| 1020 | self.assertEqual({i:i+1 for i in nums}, {1: 2, 2: 3, 3: 4}) |
| 1021 | |
Serhiy Storchaka | 0bcba37 | 2018-01-04 10:36:14 +0200 | [diff] [blame] | 1022 | def test_listcomps(self): |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 1023 | # list comprehension tests |
| 1024 | nums = [1, 2, 3, 4, 5] |
| 1025 | strs = ["Apple", "Banana", "Coconut"] |
| 1026 | spcs = [" Apple", " Banana ", "Coco nut "] |
| 1027 | |
| 1028 | self.assertEqual([s.strip() for s in spcs], ['Apple', 'Banana', 'Coco nut']) |
| 1029 | self.assertEqual([3 * x for x in nums], [3, 6, 9, 12, 15]) |
| 1030 | self.assertEqual([x for x in nums if x > 2], [3, 4, 5]) |
| 1031 | self.assertEqual([(i, s) for i in nums for s in strs], |
| 1032 | [(1, 'Apple'), (1, 'Banana'), (1, 'Coconut'), |
| 1033 | (2, 'Apple'), (2, 'Banana'), (2, 'Coconut'), |
| 1034 | (3, 'Apple'), (3, 'Banana'), (3, 'Coconut'), |
| 1035 | (4, 'Apple'), (4, 'Banana'), (4, 'Coconut'), |
| 1036 | (5, 'Apple'), (5, 'Banana'), (5, 'Coconut')]) |
| 1037 | self.assertEqual([(i, s) for i in nums for s in [f for f in strs if "n" in f]], |
| 1038 | [(1, 'Banana'), (1, 'Coconut'), (2, 'Banana'), (2, 'Coconut'), |
| 1039 | (3, 'Banana'), (3, 'Coconut'), (4, 'Banana'), (4, 'Coconut'), |
| 1040 | (5, 'Banana'), (5, 'Coconut')]) |
| 1041 | self.assertEqual([(lambda a:[a**i for i in range(a+1)])(j) for j in range(5)], |
| 1042 | [[1], [1, 1], [1, 2, 4], [1, 3, 9, 27], [1, 4, 16, 64, 256]]) |
| 1043 | |
| 1044 | def test_in_func(l): |
Serhiy Storchaka | 0bcba37 | 2018-01-04 10:36:14 +0200 | [diff] [blame] | 1045 | return [0 < x < 3 for x in l if x > 2] |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 1046 | |
| 1047 | self.assertEqual(test_in_func(nums), [False, False, False]) |
| 1048 | |
| 1049 | def test_nested_front(): |
| 1050 | self.assertEqual([[y for y in [x, x + 1]] for x in [1,3,5]], |
| 1051 | [[1, 2], [3, 4], [5, 6]]) |
| 1052 | |
| 1053 | test_nested_front() |
| 1054 | |
| 1055 | check_syntax_error(self, "[i, s for i in nums for s in strs]") |
| 1056 | check_syntax_error(self, "[x if y]") |
| 1057 | |
| 1058 | suppliers = [ |
| 1059 | (1, "Boeing"), |
| 1060 | (2, "Ford"), |
| 1061 | (3, "Macdonalds") |
| 1062 | ] |
| 1063 | |
| 1064 | parts = [ |
| 1065 | (10, "Airliner"), |
| 1066 | (20, "Engine"), |
| 1067 | (30, "Cheeseburger") |
| 1068 | ] |
| 1069 | |
| 1070 | suppart = [ |
| 1071 | (1, 10), (1, 20), (2, 20), (3, 30) |
| 1072 | ] |
| 1073 | |
| 1074 | x = [ |
| 1075 | (sname, pname) |
| 1076 | for (sno, sname) in suppliers |
| 1077 | for (pno, pname) in parts |
| 1078 | for (sp_sno, sp_pno) in suppart |
| 1079 | if sno == sp_sno and pno == sp_pno |
| 1080 | ] |
| 1081 | |
| 1082 | self.assertEqual(x, [('Boeing', 'Airliner'), ('Boeing', 'Engine'), ('Ford', 'Engine'), |
| 1083 | ('Macdonalds', 'Cheeseburger')]) |
| 1084 | |
Serhiy Storchaka | 0bcba37 | 2018-01-04 10:36:14 +0200 | [diff] [blame] | 1085 | def test_genexps(self): |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 1086 | # generator expression tests |
| 1087 | g = ([x for x in range(10)] for x in range(1)) |
| 1088 | self.assertEqual(g.next(), [x for x in range(10)]) |
| 1089 | try: |
| 1090 | g.next() |
| 1091 | self.fail('should produce StopIteration exception') |
| 1092 | except StopIteration: |
| 1093 | pass |
| 1094 | |
| 1095 | a = 1 |
| 1096 | try: |
| 1097 | g = (a for d in a) |
| 1098 | g.next() |
| 1099 | self.fail('should produce TypeError') |
| 1100 | except TypeError: |
| 1101 | pass |
| 1102 | |
| 1103 | self.assertEqual(list((x, y) for x in 'abcd' for y in 'abcd'), [(x, y) for x in 'abcd' for y in 'abcd']) |
| 1104 | self.assertEqual(list((x, y) for x in 'ab' for y in 'xy'), [(x, y) for x in 'ab' for y in 'xy']) |
| 1105 | |
| 1106 | a = [x for x in range(10)] |
| 1107 | b = (x for x in (y for y in a)) |
| 1108 | self.assertEqual(sum(b), sum([x for x in range(10)])) |
| 1109 | |
| 1110 | self.assertEqual(sum(x**2 for x in range(10)), sum([x**2 for x in range(10)])) |
| 1111 | self.assertEqual(sum(x*x for x in range(10) if x%2), sum([x*x for x in range(10) if x%2])) |
| 1112 | self.assertEqual(sum(x for x in (y for y in range(10))), sum([x for x in range(10)])) |
| 1113 | self.assertEqual(sum(x for x in (y for y in (z for z in range(10)))), sum([x for x in range(10)])) |
| 1114 | self.assertEqual(sum(x for x in [y for y in (z for z in range(10))]), sum([x for x in range(10)])) |
| 1115 | 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)])) |
| 1116 | self.assertEqual(sum(x for x in (y for y in (z for z in range(10) if True) if False) if True), 0) |
| 1117 | check_syntax_error(self, "foo(x for x in range(10), 100)") |
| 1118 | check_syntax_error(self, "foo(100, x for x in range(10))") |
| 1119 | |
Serhiy Storchaka | 0bcba37 | 2018-01-04 10:36:14 +0200 | [diff] [blame] | 1120 | def test_comprehension_specials(self): |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 1121 | # test for outmost iterable precomputation |
| 1122 | x = 10; g = (i for i in range(x)); x = 5 |
| 1123 | self.assertEqual(len(list(g)), 10) |
| 1124 | |
| 1125 | # This should hold, since we're only precomputing outmost iterable. |
| 1126 | x = 10; t = False; g = ((i,j) for i in range(x) if t for j in range(x)) |
| 1127 | x = 5; t = True; |
| 1128 | self.assertEqual([(i,j) for i in range(10) for j in range(5)], list(g)) |
| 1129 | |
| 1130 | # Grammar allows multiple adjacent 'if's in listcomps and genexps, |
| 1131 | # even though it's silly. Make sure it works (ifelse broke this.) |
| 1132 | self.assertEqual([ x for x in range(10) if x % 2 if x % 3 ], [1, 5, 7]) |
| 1133 | self.assertEqual(list(x for x in range(10) if x % 2 if x % 3), [1, 5, 7]) |
| 1134 | |
| 1135 | # verify unpacking single element tuples in listcomp/genexp. |
| 1136 | self.assertEqual([x for x, in [(4,), (5,), (6,)]], [4, 5, 6]) |
| 1137 | self.assertEqual(list(x for x, in [(7,), (8,), (9,)]), [7, 8, 9]) |
| 1138 | |
Benjamin Peterson | 4650892 | 2009-05-29 21:48:19 +0000 | [diff] [blame] | 1139 | def test_with_statement(self): |
| 1140 | class manager(object): |
| 1141 | def __enter__(self): |
| 1142 | return (1, 2) |
| 1143 | def __exit__(self, *args): |
| 1144 | pass |
| 1145 | |
| 1146 | with manager(): |
| 1147 | pass |
| 1148 | with manager() as x: |
| 1149 | pass |
| 1150 | with manager() as (x, y): |
| 1151 | pass |
| 1152 | with manager(), manager(): |
| 1153 | pass |
| 1154 | with manager() as x, manager() as y: |
| 1155 | pass |
| 1156 | with manager() as x, manager(): |
| 1157 | pass |
| 1158 | |
Serhiy Storchaka | 0bcba37 | 2018-01-04 10:36:14 +0200 | [diff] [blame] | 1159 | def test_if_else_expr(self): |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 1160 | # Test ifelse expressions in various cases |
| 1161 | def _checkeval(msg, ret): |
| 1162 | "helper to check that evaluation of expressions is done correctly" |
Serhiy Storchaka | 0bcba37 | 2018-01-04 10:36:14 +0200 | [diff] [blame] | 1163 | print(msg) |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 1164 | return ret |
| 1165 | |
| 1166 | self.assertEqual([ x() for x in lambda: True, lambda: False if x() ], [True]) |
| 1167 | self.assertEqual([ x() for x in (lambda: True, lambda: False) if x() ], [True]) |
| 1168 | 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]) |
| 1169 | self.assertEqual((5 if 1 else _checkeval("check 1", 0)), 5) |
| 1170 | self.assertEqual((_checkeval("check 2", 0) if 0 else 5), 5) |
| 1171 | self.assertEqual((5 and 6 if 0 else 1), 1) |
| 1172 | self.assertEqual(((5 and 6) if 0 else 1), 1) |
| 1173 | self.assertEqual((5 and (6 if 1 else 1)), 6) |
| 1174 | self.assertEqual((0 or _checkeval("check 3", 2) if 0 else 3), 3) |
| 1175 | self.assertEqual((1 or _checkeval("check 4", 2) if 1 else _checkeval("check 5", 3)), 1) |
| 1176 | self.assertEqual((0 or 5 if 1 else _checkeval("check 6", 3)), 5) |
| 1177 | self.assertEqual((not 5 if 1 else 1), False) |
| 1178 | self.assertEqual((not 5 if 0 else 1), 1) |
| 1179 | self.assertEqual((6 + 1 if 1 else 2), 7) |
| 1180 | self.assertEqual((6 - 1 if 1 else 2), 5) |
| 1181 | self.assertEqual((6 * 2 if 1 else 4), 12) |
Serhiy Storchaka | 0bcba37 | 2018-01-04 10:36:14 +0200 | [diff] [blame] | 1182 | with check_py3k_warnings(('classic int division', DeprecationWarning)): |
| 1183 | self.assertEqual((6 / 2 if 1 else 3), 3) |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 1184 | self.assertEqual((6 < 4 if 0 else 2), 2) |
Guido van Rossum | b3b09c9 | 1993-10-22 14:24:22 +0000 | [diff] [blame] | 1185 | |
Benjamin Peterson | b2e31a1 | 2009-10-31 03:56:15 +0000 | [diff] [blame] | 1186 | def test_paren_evaluation(self): |
| 1187 | self.assertEqual(16 // (4 // 2), 8) |
| 1188 | self.assertEqual((16 // 4) // 2, 2) |
| 1189 | self.assertEqual(16 // 4 // 2, 2) |
| 1190 | self.assertTrue(False is (2 is 3)) |
| 1191 | self.assertFalse((False is 2) is 3) |
| 1192 | self.assertFalse(False is 2 is 3) |
| 1193 | |
Guido van Rossum | b3b09c9 | 1993-10-22 14:24:22 +0000 | [diff] [blame] | 1194 | |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 1195 | def test_main(): |
Serhiy Storchaka | 0bcba37 | 2018-01-04 10:36:14 +0200 | [diff] [blame] | 1196 | run_unittest(TokenTests, GrammarTests) |
Jeremy Hylton | f828e2d | 2001-02-19 15:54:52 +0000 | [diff] [blame] | 1197 | |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 1198 | if __name__ == '__main__': |
| 1199 | test_main() |