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