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