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