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 | |
Serhiy Storchaka | e7a4bb5 | 2019-02-19 08:30:15 +0200 | [diff] [blame] | 4 | from test.support import check_syntax_error, check_syntax_warning |
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 |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 8 | import warnings |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 9 | # testing import * |
| 10 | from sys import * |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 11 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 12 | # different import patterns to check that __annotations__ does not interfere |
| 13 | # with import machinery |
| 14 | import test.ann_module as ann_module |
| 15 | import typing |
| 16 | from collections import ChainMap |
| 17 | from test import ann_module2 |
| 18 | import test |
| 19 | |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 20 | # These are shared with test_tokenize and other test modules. |
| 21 | # |
| 22 | # Note: since several test cases filter out floats by looking for "e" and ".", |
| 23 | # don't add hexadecimal literals that contain "e" or "E". |
| 24 | VALID_UNDERSCORE_LITERALS = [ |
| 25 | '0_0_0', |
| 26 | '4_2', |
| 27 | '1_0000_0000', |
| 28 | '0b1001_0100', |
| 29 | '0xffff_ffff', |
| 30 | '0o5_7_7', |
| 31 | '1_00_00.5', |
| 32 | '1_00_00.5e5', |
| 33 | '1_00_00e5_1', |
| 34 | '1e1_0', |
| 35 | '.1_4', |
| 36 | '.1_4e1', |
| 37 | '0b_0', |
| 38 | '0x_f', |
| 39 | '0o_5', |
| 40 | '1_00_00j', |
| 41 | '1_00_00.5j', |
| 42 | '1_00_00e5_1j', |
| 43 | '.1_4j', |
| 44 | '(1_2.5+3_3j)', |
| 45 | '(.5_6j)', |
| 46 | ] |
| 47 | INVALID_UNDERSCORE_LITERALS = [ |
| 48 | # Trailing underscores: |
| 49 | '0_', |
| 50 | '42_', |
| 51 | '1.4j_', |
| 52 | '0x_', |
| 53 | '0b1_', |
| 54 | '0xf_', |
| 55 | '0o5_', |
| 56 | '0 if 1_Else 1', |
| 57 | # Underscores in the base selector: |
| 58 | '0_b0', |
| 59 | '0_xf', |
| 60 | '0_o5', |
| 61 | # Old-style octal, still disallowed: |
| 62 | '0_7', |
| 63 | '09_99', |
| 64 | # Multiple consecutive underscores: |
| 65 | '4_______2', |
| 66 | '0.1__4', |
| 67 | '0.1__4j', |
| 68 | '0b1001__0100', |
| 69 | '0xffff__ffff', |
| 70 | '0x___', |
| 71 | '0o5__77', |
| 72 | '1e1__0', |
| 73 | '1e1__0j', |
| 74 | # Underscore right before a dot: |
| 75 | '1_.4', |
| 76 | '1_.4j', |
| 77 | # Underscore right after a dot: |
| 78 | '1._4', |
| 79 | '1._4j', |
| 80 | '._5', |
| 81 | '._5j', |
| 82 | # Underscore right after a sign: |
| 83 | '1.0e+_1', |
| 84 | '1.0e+_1j', |
| 85 | # Underscore right before j: |
| 86 | '1.4_j', |
| 87 | '1.4e5_j', |
| 88 | # Underscore right before e: |
| 89 | '1_e1', |
| 90 | '1.4_e1', |
| 91 | '1.4_e1j', |
| 92 | # Underscore right after e: |
| 93 | '1e_1', |
| 94 | '1.4e_1', |
| 95 | '1.4e_1j', |
| 96 | # Complex cases with parens: |
| 97 | '(1+1.5_j_)', |
| 98 | '(1+1.5_j)', |
| 99 | ] |
| 100 | |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 101 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 102 | class TokenTests(unittest.TestCase): |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 103 | |
Serhiy Storchaka | e7a4bb5 | 2019-02-19 08:30:15 +0200 | [diff] [blame] | 104 | from test.support import check_syntax_error |
Serhiy Storchaka | cf7303e | 2018-07-09 15:09:35 +0300 | [diff] [blame] | 105 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 106 | def test_backslash(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 107 | # Backslash means line continuation: |
| 108 | x = 1 \ |
| 109 | + 1 |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 110 | self.assertEqual(x, 2, 'backslash for line continuation') |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 111 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 112 | # Backslash does not means continuation in comments :\ |
| 113 | x = 0 |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 114 | self.assertEqual(x, 0, 'backslash ending comment') |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 115 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 116 | def test_plain_integers(self): |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 117 | self.assertEqual(type(000), type(0)) |
| 118 | self.assertEqual(0xff, 255) |
| 119 | self.assertEqual(0o377, 255) |
| 120 | self.assertEqual(2147483647, 0o17777777777) |
| 121 | self.assertEqual(0b1001, 9) |
Georg Brandl | fceab5a | 2008-01-19 20:08:23 +0000 | [diff] [blame] | 122 | # "0x" is not a valid literal |
| 123 | self.assertRaises(SyntaxError, eval, "0x") |
Christian Heimes | a37d4c6 | 2007-12-04 23:02:19 +0000 | [diff] [blame] | 124 | from sys import maxsize |
| 125 | if maxsize == 2147483647: |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 126 | self.assertEqual(-2147483647-1, -0o20000000000) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 127 | # XXX -2147483648 |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 128 | self.assertTrue(0o37777777777 > 0) |
| 129 | self.assertTrue(0xffffffff > 0) |
| 130 | self.assertTrue(0b1111111111111111111111111111111 > 0) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 131 | for s in ('2147483648', '0o40000000000', '0x100000000', |
| 132 | '0b10000000000000000000000000000000'): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 133 | try: |
| 134 | x = eval(s) |
| 135 | except OverflowError: |
| 136 | self.fail("OverflowError on huge integer literal %r" % s) |
Christian Heimes | a37d4c6 | 2007-12-04 23:02:19 +0000 | [diff] [blame] | 137 | elif maxsize == 9223372036854775807: |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 138 | self.assertEqual(-9223372036854775807-1, -0o1000000000000000000000) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 139 | self.assertTrue(0o1777777777777777777777 > 0) |
| 140 | self.assertTrue(0xffffffffffffffff > 0) |
| 141 | self.assertTrue(0b11111111111111111111111111111111111111111111111111111111111111 > 0) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 142 | for s in '9223372036854775808', '0o2000000000000000000000', \ |
| 143 | '0x10000000000000000', \ |
| 144 | '0b100000000000000000000000000000000000000000000000000000000000000': |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 145 | try: |
| 146 | x = eval(s) |
| 147 | except OverflowError: |
| 148 | self.fail("OverflowError on huge integer literal %r" % s) |
| 149 | else: |
Christian Heimes | a37d4c6 | 2007-12-04 23:02:19 +0000 | [diff] [blame] | 150 | self.fail('Weird maxsize value %r' % maxsize) |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 151 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 152 | def test_long_integers(self): |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 153 | x = 0 |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 154 | x = 0xffffffffffffffff |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 155 | x = 0Xffffffffffffffff |
| 156 | x = 0o77777777777777777 |
| 157 | x = 0O77777777777777777 |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 158 | x = 123456789012345678901234567890 |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 159 | x = 0b100000000000000000000000000000000000000000000000000000000000000000000 |
| 160 | x = 0B111111111111111111111111111111111111111111111111111111111111111111111 |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 161 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 162 | def test_floats(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 163 | x = 3.14 |
| 164 | x = 314. |
| 165 | x = 0.314 |
| 166 | # XXX x = 000.314 |
| 167 | x = .314 |
| 168 | x = 3e14 |
| 169 | x = 3E14 |
| 170 | x = 3e-14 |
| 171 | x = 3e+14 |
| 172 | x = 3.e14 |
| 173 | x = .3e14 |
| 174 | x = 3.1e4 |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 175 | |
Benjamin Peterson | c416162 | 2014-06-07 12:36:39 -0700 | [diff] [blame] | 176 | def test_float_exponent_tokenization(self): |
| 177 | # See issue 21642. |
| 178 | self.assertEqual(1 if 1else 0, 1) |
| 179 | self.assertEqual(1 if 0else 0, 0) |
| 180 | self.assertRaises(SyntaxError, eval, "0 if 1Else 0") |
| 181 | |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 182 | def test_underscore_literals(self): |
| 183 | for lit in VALID_UNDERSCORE_LITERALS: |
| 184 | self.assertEqual(eval(lit), eval(lit.replace('_', ''))) |
| 185 | for lit in INVALID_UNDERSCORE_LITERALS: |
| 186 | self.assertRaises(SyntaxError, eval, lit) |
| 187 | # Sanity check: no literal begins with an underscore |
| 188 | self.assertRaises(NameError, eval, "_0") |
| 189 | |
Serhiy Storchaka | cf7303e | 2018-07-09 15:09:35 +0300 | [diff] [blame] | 190 | def test_bad_numerical_literals(self): |
| 191 | check = self.check_syntax_error |
| 192 | check("0b12", "invalid digit '2' in binary literal") |
| 193 | check("0b1_2", "invalid digit '2' in binary literal") |
| 194 | check("0b2", "invalid digit '2' in binary literal") |
| 195 | check("0b1_", "invalid binary literal") |
| 196 | check("0b", "invalid binary literal") |
| 197 | check("0o18", "invalid digit '8' in octal literal") |
| 198 | check("0o1_8", "invalid digit '8' in octal literal") |
| 199 | check("0o8", "invalid digit '8' in octal literal") |
| 200 | check("0o1_", "invalid octal literal") |
| 201 | check("0o", "invalid octal literal") |
| 202 | check("0x1_", "invalid hexadecimal literal") |
| 203 | check("0x", "invalid hexadecimal literal") |
| 204 | check("1_", "invalid decimal literal") |
| 205 | check("012", |
| 206 | "leading zeros in decimal integer literals are not permitted; " |
| 207 | "use an 0o prefix for octal integers") |
| 208 | check("1.2_", "invalid decimal literal") |
| 209 | check("1e2_", "invalid decimal literal") |
| 210 | check("1e+", "invalid decimal literal") |
| 211 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 212 | def test_string_literals(self): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 213 | x = ''; y = ""; self.assertTrue(len(x) == 0 and x == y) |
| 214 | x = '\''; y = "'"; self.assertTrue(len(x) == 1 and x == y and ord(x) == 39) |
| 215 | 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] | 216 | x = "doesn't \"shrink\" does it" |
| 217 | y = 'doesn\'t "shrink" does it' |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 218 | self.assertTrue(len(x) == 24 and x == y) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 219 | x = "does \"shrink\" doesn't it" |
| 220 | y = 'does "shrink" doesn\'t it' |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 221 | self.assertTrue(len(x) == 24 and x == y) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 222 | x = """ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 223 | The "quick" |
| 224 | brown fox |
| 225 | jumps over |
| 226 | the 'lazy' dog. |
| 227 | """ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 228 | y = '\nThe "quick"\nbrown fox\njumps over\nthe \'lazy\' dog.\n' |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 229 | self.assertEqual(x, y) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 230 | y = ''' |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 231 | The "quick" |
| 232 | brown fox |
| 233 | jumps over |
| 234 | the 'lazy' dog. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 235 | ''' |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 236 | self.assertEqual(x, y) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 237 | y = "\n\ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 238 | The \"quick\"\n\ |
| 239 | brown fox\n\ |
| 240 | jumps over\n\ |
| 241 | the 'lazy' dog.\n\ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 242 | " |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 243 | self.assertEqual(x, y) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 244 | y = '\n\ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 245 | The \"quick\"\n\ |
| 246 | brown fox\n\ |
| 247 | jumps over\n\ |
| 248 | the \'lazy\' dog.\n\ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 249 | ' |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 250 | self.assertEqual(x, y) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 251 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 252 | def test_ellipsis(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 253 | x = ... |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 254 | self.assertTrue(x is Ellipsis) |
Georg Brandl | dde0028 | 2007-03-18 19:01:53 +0000 | [diff] [blame] | 255 | self.assertRaises(SyntaxError, eval, ".. .") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 256 | |
Benjamin Peterson | 758888d | 2011-05-30 11:12:38 -0500 | [diff] [blame] | 257 | def test_eof_error(self): |
| 258 | samples = ("def foo(", "\ndef foo(", "def foo(\n") |
| 259 | for s in samples: |
| 260 | with self.assertRaises(SyntaxError) as cm: |
| 261 | compile(s, "<test>", "exec") |
| 262 | self.assertIn("unexpected EOF", str(cm.exception)) |
| 263 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 264 | var_annot_global: int # a global annotated is necessary for test_var_annot |
| 265 | |
| 266 | # custom namespace for testing __annotations__ |
| 267 | |
| 268 | class CNS: |
| 269 | def __init__(self): |
| 270 | self._dct = {} |
| 271 | def __setitem__(self, item, value): |
| 272 | self._dct[item.lower()] = value |
| 273 | def __getitem__(self, item): |
| 274 | return self._dct[item] |
| 275 | |
| 276 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 277 | class GrammarTests(unittest.TestCase): |
| 278 | |
Serhiy Storchaka | e7a4bb5 | 2019-02-19 08:30:15 +0200 | [diff] [blame] | 279 | from test.support import check_syntax_error, check_syntax_warning |
Serhiy Storchaka | 07ca9af | 2018-02-04 10:53:48 +0200 | [diff] [blame] | 280 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 281 | # single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE |
| 282 | # XXX can't test in a script -- this rule is only used when interactive |
| 283 | |
| 284 | # file_input: (NEWLINE | stmt)* ENDMARKER |
| 285 | # Being tested as this very moment this very module |
| 286 | |
| 287 | # expr_input: testlist NEWLINE |
| 288 | # XXX Hard to test -- used only in calls to input() |
| 289 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 290 | def test_eval_input(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 291 | # testlist ENDMARKER |
| 292 | x = eval('1, 0 or 1') |
| 293 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 294 | def test_var_annot_basics(self): |
| 295 | # all these should be allowed |
| 296 | var1: int = 5 |
| 297 | var2: [int, str] |
| 298 | my_lst = [42] |
| 299 | def one(): |
| 300 | return 1 |
| 301 | int.new_attr: int |
| 302 | [list][0]: type |
| 303 | my_lst[one()-1]: int = 5 |
| 304 | self.assertEqual(my_lst, [5]) |
| 305 | |
| 306 | def test_var_annot_syntax_errors(self): |
| 307 | # parser pass |
| 308 | check_syntax_error(self, "def f: int") |
| 309 | check_syntax_error(self, "x: int: str") |
| 310 | check_syntax_error(self, "def f():\n" |
| 311 | " nonlocal x: int\n") |
| 312 | # AST pass |
| 313 | check_syntax_error(self, "[x, 0]: int\n") |
| 314 | check_syntax_error(self, "f(): int\n") |
| 315 | check_syntax_error(self, "(x,): int") |
| 316 | check_syntax_error(self, "def f():\n" |
| 317 | " (x, y): int = (1, 2)\n") |
| 318 | # symtable pass |
| 319 | check_syntax_error(self, "def f():\n" |
| 320 | " x: int\n" |
| 321 | " global x\n") |
| 322 | check_syntax_error(self, "def f():\n" |
| 323 | " global x\n" |
| 324 | " x: int\n") |
| 325 | |
| 326 | def test_var_annot_basic_semantics(self): |
| 327 | # execution order |
| 328 | with self.assertRaises(ZeroDivisionError): |
| 329 | no_name[does_not_exist]: no_name_again = 1/0 |
| 330 | with self.assertRaises(NameError): |
| 331 | no_name[does_not_exist]: 1/0 = 0 |
| 332 | global var_annot_global |
| 333 | |
| 334 | # function semantics |
| 335 | def f(): |
| 336 | st: str = "Hello" |
| 337 | a.b: int = (1, 2) |
| 338 | return st |
| 339 | self.assertEqual(f.__annotations__, {}) |
| 340 | def f_OK(): |
| 341 | x: 1/0 |
| 342 | f_OK() |
| 343 | def fbad(): |
| 344 | x: int |
| 345 | print(x) |
| 346 | with self.assertRaises(UnboundLocalError): |
| 347 | fbad() |
| 348 | def f2bad(): |
| 349 | (no_such_global): int |
| 350 | print(no_such_global) |
| 351 | try: |
| 352 | f2bad() |
| 353 | except Exception as e: |
| 354 | self.assertIs(type(e), NameError) |
| 355 | |
| 356 | # class semantics |
| 357 | class C: |
Guido van Rossum | 015d874 | 2016-09-11 09:45:24 -0700 | [diff] [blame] | 358 | __foo: int |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 359 | s: str = "attr" |
| 360 | z = 2 |
| 361 | def __init__(self, x): |
| 362 | self.x: int = x |
Guido van Rossum | 015d874 | 2016-09-11 09:45:24 -0700 | [diff] [blame] | 363 | self.assertEqual(C.__annotations__, {'_C__foo': int, 's': str}) |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 364 | with self.assertRaises(NameError): |
| 365 | class CBad: |
| 366 | no_such_name_defined.attr: int = 0 |
| 367 | with self.assertRaises(NameError): |
| 368 | class Cbad2(C): |
| 369 | x: int |
| 370 | x.y: list = [] |
| 371 | |
| 372 | def test_var_annot_metaclass_semantics(self): |
| 373 | class CMeta(type): |
| 374 | @classmethod |
| 375 | def __prepare__(metacls, name, bases, **kwds): |
| 376 | return {'__annotations__': CNS()} |
| 377 | class CC(metaclass=CMeta): |
| 378 | XX: 'ANNOT' |
| 379 | self.assertEqual(CC.__annotations__['xx'], 'ANNOT') |
| 380 | |
| 381 | def test_var_annot_module_semantics(self): |
| 382 | with self.assertRaises(AttributeError): |
| 383 | print(test.__annotations__) |
| 384 | self.assertEqual(ann_module.__annotations__, |
| 385 | {1: 2, 'x': int, 'y': str, 'f': typing.Tuple[int, int]}) |
| 386 | self.assertEqual(ann_module.M.__annotations__, |
| 387 | {'123': 123, 'o': type}) |
| 388 | self.assertEqual(ann_module2.__annotations__, {}) |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 389 | |
| 390 | def test_var_annot_in_module(self): |
| 391 | # check that functions fail the same way when executed |
| 392 | # outside of module where they were defined |
| 393 | from test.ann_module3 import f_bad_ann, g_bad_ann, D_bad_ann |
| 394 | with self.assertRaises(NameError): |
| 395 | f_bad_ann() |
| 396 | with self.assertRaises(NameError): |
| 397 | g_bad_ann() |
| 398 | with self.assertRaises(NameError): |
| 399 | D_bad_ann(5) |
| 400 | |
| 401 | def test_var_annot_simple_exec(self): |
| 402 | gns = {}; lns= {} |
| 403 | exec("'docstring'\n" |
| 404 | "__annotations__[1] = 2\n" |
| 405 | "x: int = 5\n", gns, lns) |
| 406 | self.assertEqual(lns["__annotations__"], {1: 2, 'x': int}) |
| 407 | with self.assertRaises(KeyError): |
| 408 | gns['__annotations__'] |
| 409 | |
| 410 | def test_var_annot_custom_maps(self): |
| 411 | # tests with custom locals() and __annotations__ |
| 412 | ns = {'__annotations__': CNS()} |
| 413 | exec('X: int; Z: str = "Z"; (w): complex = 1j', ns) |
| 414 | self.assertEqual(ns['__annotations__']['x'], int) |
| 415 | self.assertEqual(ns['__annotations__']['z'], str) |
| 416 | with self.assertRaises(KeyError): |
| 417 | ns['__annotations__']['w'] |
| 418 | nonloc_ns = {} |
| 419 | class CNS2: |
| 420 | def __init__(self): |
| 421 | self._dct = {} |
| 422 | def __setitem__(self, item, value): |
| 423 | nonlocal nonloc_ns |
| 424 | self._dct[item] = value |
| 425 | nonloc_ns[item] = value |
| 426 | def __getitem__(self, item): |
| 427 | return self._dct[item] |
| 428 | exec('x: int = 1', {}, CNS2()) |
| 429 | self.assertEqual(nonloc_ns['__annotations__']['x'], int) |
| 430 | |
| 431 | def test_var_annot_refleak(self): |
| 432 | # complex case: custom locals plus custom __annotations__ |
| 433 | # this was causing refleak |
| 434 | cns = CNS() |
| 435 | nonloc_ns = {'__annotations__': cns} |
| 436 | class CNS2: |
| 437 | def __init__(self): |
| 438 | self._dct = {'__annotations__': cns} |
| 439 | def __setitem__(self, item, value): |
| 440 | nonlocal nonloc_ns |
| 441 | self._dct[item] = value |
| 442 | nonloc_ns[item] = value |
| 443 | def __getitem__(self, item): |
| 444 | return self._dct[item] |
| 445 | exec('X: str', {}, CNS2()) |
| 446 | self.assertEqual(nonloc_ns['__annotations__']['x'], str) |
| 447 | |
Ivan Levkivskyi | 62c35a8 | 2019-01-25 01:39:19 +0000 | [diff] [blame] | 448 | def test_var_annot_rhs(self): |
| 449 | ns = {} |
| 450 | exec('x: tuple = 1, 2', ns) |
| 451 | self.assertEqual(ns['x'], (1, 2)) |
| 452 | stmt = ('def f():\n' |
| 453 | ' x: int = yield') |
| 454 | exec(stmt, ns) |
| 455 | self.assertEqual(list(ns['f']()), [None]) |
| 456 | |
Pablo Galindo | 8565f6b | 2019-06-03 08:34:20 +0100 | [diff] [blame] | 457 | ns = {"a": 1, 'b': (2, 3, 4), "c":5, "Tuple": typing.Tuple} |
| 458 | exec('x: Tuple[int, ...] = a,*b,c', ns) |
| 459 | self.assertEqual(ns['x'], (1, 2, 3, 4, 5)) |
| 460 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 461 | def test_funcdef(self): |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 462 | ### [decorators] 'def' NAME parameters ['->' test] ':' suite |
| 463 | ### decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE |
| 464 | ### decorators: decorator+ |
| 465 | ### parameters: '(' [typedargslist] ')' |
| 466 | ### typedargslist: ((tfpdef ['=' test] ',')* |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 467 | ### ('*' [tfpdef] (',' tfpdef ['=' test])* [',' '**' tfpdef] | '**' tfpdef) |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 468 | ### | tfpdef ['=' test] (',' tfpdef ['=' test])* [',']) |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 469 | ### tfpdef: NAME [':' test] |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 470 | ### varargslist: ((vfpdef ['=' test] ',')* |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 471 | ### ('*' [vfpdef] (',' vfpdef ['=' test])* [',' '**' vfpdef] | '**' vfpdef) |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 472 | ### | vfpdef ['=' test] (',' vfpdef ['=' test])* [',']) |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 473 | ### vfpdef: NAME |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 474 | def f1(): pass |
| 475 | f1() |
| 476 | f1(*()) |
| 477 | f1(*(), **{}) |
| 478 | def f2(one_argument): pass |
| 479 | def f3(two, arguments): pass |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 480 | self.assertEqual(f2.__code__.co_varnames, ('one_argument',)) |
| 481 | self.assertEqual(f3.__code__.co_varnames, ('two', 'arguments')) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 482 | def a1(one_arg,): pass |
| 483 | def a2(two, args,): pass |
| 484 | def v0(*rest): pass |
| 485 | def v1(a, *rest): pass |
| 486 | def v2(a, b, *rest): pass |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 487 | |
| 488 | f1() |
| 489 | f2(1) |
| 490 | f2(1,) |
| 491 | f3(1, 2) |
| 492 | f3(1, 2,) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 493 | v0() |
| 494 | v0(1) |
| 495 | v0(1,) |
| 496 | v0(1,2) |
| 497 | v0(1,2,3,4,5,6,7,8,9,0) |
| 498 | v1(1) |
| 499 | v1(1,) |
| 500 | v1(1,2) |
| 501 | v1(1,2,3) |
| 502 | v1(1,2,3,4,5,6,7,8,9,0) |
| 503 | v2(1,2) |
| 504 | v2(1,2,3) |
| 505 | v2(1,2,3,4) |
| 506 | v2(1,2,3,4,5,6,7,8,9,0) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 507 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 508 | def d01(a=1): pass |
| 509 | d01() |
| 510 | d01(1) |
| 511 | d01(*(1,)) |
Yury Selivanov | 14acf5f | 2015-08-05 17:54:10 -0400 | [diff] [blame] | 512 | d01(*[] or [2]) |
| 513 | d01(*() or (), *{} and (), **() or {}) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 514 | d01(**{'a':2}) |
Benjamin Peterson | de12b79 | 2015-05-16 09:44:45 -0400 | [diff] [blame] | 515 | d01(**{'a':2} or {}) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 516 | def d11(a, b=1): pass |
| 517 | d11(1) |
| 518 | d11(1, 2) |
| 519 | d11(1, **{'b':2}) |
| 520 | def d21(a, b, c=1): pass |
| 521 | d21(1, 2) |
| 522 | d21(1, 2, 3) |
| 523 | d21(*(1, 2, 3)) |
| 524 | d21(1, *(2, 3)) |
| 525 | d21(1, 2, *(3,)) |
| 526 | d21(1, 2, **{'c':3}) |
| 527 | def d02(a=1, b=2): pass |
| 528 | d02() |
| 529 | d02(1) |
| 530 | d02(1, 2) |
| 531 | d02(*(1, 2)) |
| 532 | d02(1, *(2,)) |
| 533 | d02(1, **{'b':2}) |
| 534 | d02(**{'a': 1, 'b': 2}) |
| 535 | def d12(a, b=1, c=2): pass |
| 536 | d12(1) |
| 537 | d12(1, 2) |
| 538 | d12(1, 2, 3) |
| 539 | def d22(a, b, c=1, d=2): pass |
| 540 | d22(1, 2) |
| 541 | d22(1, 2, 3) |
| 542 | d22(1, 2, 3, 4) |
| 543 | def d01v(a=1, *rest): pass |
| 544 | d01v() |
| 545 | d01v(1) |
| 546 | d01v(1, 2) |
| 547 | d01v(*(1, 2, 3, 4)) |
| 548 | d01v(*(1,)) |
| 549 | d01v(**{'a':2}) |
| 550 | def d11v(a, b=1, *rest): pass |
| 551 | d11v(1) |
| 552 | d11v(1, 2) |
| 553 | d11v(1, 2, 3) |
| 554 | def d21v(a, b, c=1, *rest): pass |
| 555 | d21v(1, 2) |
| 556 | d21v(1, 2, 3) |
| 557 | d21v(1, 2, 3, 4) |
| 558 | d21v(*(1, 2, 3, 4)) |
| 559 | d21v(1, 2, **{'c': 3}) |
| 560 | def d02v(a=1, b=2, *rest): pass |
| 561 | d02v() |
| 562 | d02v(1) |
| 563 | d02v(1, 2) |
| 564 | d02v(1, 2, 3) |
| 565 | d02v(1, *(2, 3, 4)) |
| 566 | d02v(**{'a': 1, 'b': 2}) |
| 567 | def d12v(a, b=1, c=2, *rest): pass |
| 568 | d12v(1) |
| 569 | d12v(1, 2) |
| 570 | d12v(1, 2, 3) |
| 571 | d12v(1, 2, 3, 4) |
| 572 | d12v(*(1, 2, 3, 4)) |
| 573 | d12v(1, 2, *(3, 4, 5)) |
| 574 | d12v(1, *(2,), **{'c': 3}) |
| 575 | def d22v(a, b, c=1, d=2, *rest): pass |
| 576 | d22v(1, 2) |
| 577 | d22v(1, 2, 3) |
| 578 | d22v(1, 2, 3, 4) |
| 579 | d22v(1, 2, 3, 4, 5) |
| 580 | d22v(*(1, 2, 3, 4)) |
| 581 | d22v(1, 2, *(3, 4, 5)) |
| 582 | d22v(1, *(2, 3), **{'d': 4}) |
Georg Brandl | d8b690f | 2008-05-16 17:28:50 +0000 | [diff] [blame] | 583 | |
| 584 | # keyword argument type tests |
| 585 | try: |
| 586 | str('x', **{b'foo':1 }) |
| 587 | except TypeError: |
| 588 | pass |
| 589 | else: |
| 590 | self.fail('Bytes should not work as keyword argument names') |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 591 | # keyword only argument tests |
| 592 | def pos0key1(*, key): return key |
| 593 | pos0key1(key=100) |
| 594 | def pos2key2(p1, p2, *, k1, k2=100): return p1,p2,k1,k2 |
| 595 | pos2key2(1, 2, k1=100) |
| 596 | pos2key2(1, 2, k1=100, k2=200) |
| 597 | pos2key2(1, 2, k2=100, k1=200) |
| 598 | def pos2key2dict(p1, p2, *, k1=100, k2, **kwarg): return p1,p2,k1,k2,kwarg |
| 599 | pos2key2dict(1,2,k2=100,tokwarg1=100,tokwarg2=200) |
| 600 | pos2key2dict(1,2,tokwarg1=100,tokwarg2=200, k2=100) |
| 601 | |
Robert Collins | df39599 | 2015-08-12 08:00:06 +1200 | [diff] [blame] | 602 | self.assertRaises(SyntaxError, eval, "def f(*): pass") |
| 603 | self.assertRaises(SyntaxError, eval, "def f(*,): pass") |
| 604 | self.assertRaises(SyntaxError, eval, "def f(*, **kwds): pass") |
| 605 | |
Benjamin Peterson | 2d735bc | 2008-08-19 20:57:10 +0000 | [diff] [blame] | 606 | # keyword arguments after *arglist |
| 607 | def f(*args, **kwargs): |
| 608 | return args, kwargs |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 609 | 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] | 610 | {'x':2, 'y':5})) |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 611 | self.assertEqual(f(1, *(2,3), 4), ((1, 2, 3, 4), {})) |
Benjamin Peterson | 2d735bc | 2008-08-19 20:57:10 +0000 | [diff] [blame] | 612 | self.assertRaises(SyntaxError, eval, "f(1, x=2, *(3,4), x=5)") |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 613 | self.assertEqual(f(**{'eggs':'scrambled', 'spam':'fried'}), |
| 614 | ((), {'eggs':'scrambled', 'spam':'fried'})) |
| 615 | self.assertEqual(f(spam='fried', **{'eggs':'scrambled'}), |
| 616 | ((), {'eggs':'scrambled', 'spam':'fried'})) |
Benjamin Peterson | 2d735bc | 2008-08-19 20:57:10 +0000 | [diff] [blame] | 617 | |
Serhiy Storchaka | 0cc99c8 | 2018-01-04 10:36:35 +0200 | [diff] [blame] | 618 | # Check ast errors in *args and *kwargs |
| 619 | check_syntax_error(self, "f(*g(1=2))") |
| 620 | check_syntax_error(self, "f(**g(1=2))") |
| 621 | |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 622 | # argument annotation tests |
| 623 | def f(x) -> list: pass |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 624 | self.assertEqual(f.__annotations__, {'return': list}) |
Zachary Ware | ce17f76 | 2015-08-01 21:55:36 -0500 | [diff] [blame] | 625 | def f(x: int): pass |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 626 | self.assertEqual(f.__annotations__, {'x': int}) |
Pablo Galindo | a0c01bf | 2019-05-31 15:19:50 +0100 | [diff] [blame] | 627 | def f(x: int, /): pass |
| 628 | self.assertEqual(f.__annotations__, {'x': int}) |
| 629 | def f(x: int = 34, /): pass |
| 630 | self.assertEqual(f.__annotations__, {'x': int}) |
Zachary Ware | ce17f76 | 2015-08-01 21:55:36 -0500 | [diff] [blame] | 631 | def f(*x: str): pass |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 632 | self.assertEqual(f.__annotations__, {'x': str}) |
Zachary Ware | ce17f76 | 2015-08-01 21:55:36 -0500 | [diff] [blame] | 633 | def f(**x: float): pass |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 634 | self.assertEqual(f.__annotations__, {'x': float}) |
Zachary Ware | ce17f76 | 2015-08-01 21:55:36 -0500 | [diff] [blame] | 635 | def f(x, y: 1+2): pass |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 636 | self.assertEqual(f.__annotations__, {'y': 3}) |
Pablo Galindo | a0c01bf | 2019-05-31 15:19:50 +0100 | [diff] [blame] | 637 | def f(x, y: 1+2, /): pass |
| 638 | self.assertEqual(f.__annotations__, {'y': 3}) |
Zachary Ware | ce17f76 | 2015-08-01 21:55:36 -0500 | [diff] [blame] | 639 | def f(a, b: 1, c: 2, d): pass |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 640 | self.assertEqual(f.__annotations__, {'b': 1, 'c': 2}) |
Pablo Galindo | a0c01bf | 2019-05-31 15:19:50 +0100 | [diff] [blame] | 641 | def f(a, b: 1, /, c: 2, d): pass |
| 642 | self.assertEqual(f.__annotations__, {'b': 1, 'c': 2}) |
Zachary Ware | ce17f76 | 2015-08-01 21:55:36 -0500 | [diff] [blame] | 643 | 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] | 644 | self.assertEqual(f.__annotations__, |
Zachary Ware | ce17f76 | 2015-08-01 21:55:36 -0500 | [diff] [blame] | 645 | {'b': 1, 'c': 2, 'e': 3, 'g': 6}) |
| 646 | def f(a, b: 1, c: 2, d, e: 3 = 4, f=5, *g: 6, h: 7, i=8, j: 9 = 10, |
| 647 | **k: 11) -> 12: pass |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 648 | self.assertEqual(f.__annotations__, |
Zachary Ware | ce17f76 | 2015-08-01 21:55:36 -0500 | [diff] [blame] | 649 | {'b': 1, 'c': 2, 'e': 3, 'g': 6, 'h': 7, 'j': 9, |
| 650 | 'k': 11, 'return': 12}) |
Pablo Galindo | a0c01bf | 2019-05-31 15:19:50 +0100 | [diff] [blame] | 651 | def f(a, b: 1, c: 2, d, e: 3 = 4, f: int = 5, /, *g: 6, h: 7, i=8, j: 9 = 10, |
| 652 | **k: 11) -> 12: pass |
| 653 | self.assertEqual(f.__annotations__, |
| 654 | {'b': 1, 'c': 2, 'e': 3, 'f': int, 'g': 6, 'h': 7, 'j': 9, |
| 655 | 'k': 11, 'return': 12}) |
Yury Selivanov | 34ce99f | 2014-02-18 12:49:41 -0500 | [diff] [blame] | 656 | # Check for issue #20625 -- annotations mangling |
| 657 | class Spam: |
Zachary Ware | ce17f76 | 2015-08-01 21:55:36 -0500 | [diff] [blame] | 658 | def f(self, *, __kw: 1): |
Yury Selivanov | 34ce99f | 2014-02-18 12:49:41 -0500 | [diff] [blame] | 659 | pass |
| 660 | class Ham(Spam): pass |
Benjamin Peterson | bcfcfc5 | 2014-03-09 20:59:24 -0500 | [diff] [blame] | 661 | self.assertEqual(Spam.f.__annotations__, {'_Spam__kw': 1}) |
| 662 | self.assertEqual(Ham.f.__annotations__, {'_Spam__kw': 1}) |
Nick Coghlan | 71011e2 | 2007-04-23 11:05:01 +0000 | [diff] [blame] | 663 | # Check for SF Bug #1697248 - mixing decorators and a return annotation |
| 664 | def null(x): return x |
| 665 | @null |
| 666 | def f(x) -> list: pass |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 667 | self.assertEqual(f.__annotations__, {'return': list}) |
Nick Coghlan | 71011e2 | 2007-04-23 11:05:01 +0000 | [diff] [blame] | 668 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 669 | # test closures with a variety of opargs |
Guido van Rossum | 0240b92 | 2007-02-26 21:23:50 +0000 | [diff] [blame] | 670 | closure = 1 |
| 671 | def f(): return closure |
| 672 | def f(x=1): return closure |
| 673 | def f(*, k=1): return closure |
| 674 | def f() -> int: return closure |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 675 | |
Robert Collins | df39599 | 2015-08-12 08:00:06 +1200 | [diff] [blame] | 676 | # Check trailing commas are permitted in funcdef argument list |
| 677 | def f(a,): pass |
| 678 | def f(*args,): pass |
| 679 | def f(**kwds,): pass |
| 680 | def f(a, *args,): pass |
| 681 | def f(a, **kwds,): pass |
| 682 | def f(*args, b,): pass |
| 683 | def f(*, b,): pass |
| 684 | def f(*args, **kwds,): pass |
| 685 | def f(a, *args, b,): pass |
| 686 | def f(a, *, b,): pass |
| 687 | def f(a, *args, **kwds,): pass |
| 688 | def f(*args, b, **kwds,): pass |
| 689 | def f(*, b, **kwds,): pass |
| 690 | def f(a, *args, b, **kwds,): pass |
| 691 | def f(a, *, b, **kwds,): pass |
| 692 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 693 | def test_lambdef(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 694 | ### lambdef: 'lambda' [varargslist] ':' test |
| 695 | l1 = lambda : 0 |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 696 | self.assertEqual(l1(), 0) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 697 | l2 = lambda : a[d] # XXX just testing the expression |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 698 | l3 = lambda : [2 < x for x in [-1, 3, 0]] |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 699 | self.assertEqual(l3(), [0, 1, 0]) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 700 | l4 = lambda x = lambda y = lambda z=1 : z : y() : x() |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 701 | self.assertEqual(l4(), 1) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 702 | l5 = lambda x, y, z=2: x + y + z |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 703 | self.assertEqual(l5(1, 2), 5) |
| 704 | self.assertEqual(l5(1, 2, 3), 6) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 705 | check_syntax_error(self, "lambda x: x = 2") |
Amaury Forgeot d'Arc | 35c8658 | 2008-06-17 21:11:29 +0000 | [diff] [blame] | 706 | check_syntax_error(self, "lambda (None,): None") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 707 | l6 = lambda x, y, *, k=20: x+y+k |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 708 | self.assertEqual(l6(1,2), 1+2+20) |
| 709 | self.assertEqual(l6(1,2,k=10), 1+2+10) |
Guido van Rossum | b31c7f7 | 1993-11-11 10:31:23 +0000 | [diff] [blame] | 710 | |
Robert Collins | df39599 | 2015-08-12 08:00:06 +1200 | [diff] [blame] | 711 | # check that trailing commas are permitted |
| 712 | l10 = lambda a,: 0 |
| 713 | l11 = lambda *args,: 0 |
| 714 | l12 = lambda **kwds,: 0 |
| 715 | l13 = lambda a, *args,: 0 |
| 716 | l14 = lambda a, **kwds,: 0 |
| 717 | l15 = lambda *args, b,: 0 |
| 718 | l16 = lambda *, b,: 0 |
| 719 | l17 = lambda *args, **kwds,: 0 |
| 720 | l18 = lambda a, *args, b,: 0 |
| 721 | l19 = lambda a, *, b,: 0 |
| 722 | l20 = lambda a, *args, **kwds,: 0 |
| 723 | l21 = lambda *args, b, **kwds,: 0 |
| 724 | l22 = lambda *, b, **kwds,: 0 |
| 725 | l23 = lambda a, *args, b, **kwds,: 0 |
| 726 | l24 = lambda a, *, b, **kwds,: 0 |
| 727 | |
Guido van Rossum | b31c7f7 | 1993-11-11 10:31:23 +0000 | [diff] [blame] | 728 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 729 | ### stmt: simple_stmt | compound_stmt |
| 730 | # Tested below |
Georg Brandl | 52318d6 | 2006-09-06 07:06:08 +0000 | [diff] [blame] | 731 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 732 | def test_simple_stmt(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 733 | ### simple_stmt: small_stmt (';' small_stmt)* [';'] |
| 734 | x = 1; pass; del x |
| 735 | def foo(): |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 736 | # verify statements that end with semi-colons |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 737 | x = 1; pass; del x; |
| 738 | foo() |
Georg Brandl | 52318d6 | 2006-09-06 07:06:08 +0000 | [diff] [blame] | 739 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 740 | ### 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] | 741 | # Tested below |
Georg Brandl | 52318d6 | 2006-09-06 07:06:08 +0000 | [diff] [blame] | 742 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 743 | def test_expr_stmt(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 744 | # (exprlist '=')* exprlist |
Victor Stinner | 15a3095 | 2016-02-08 22:45:06 +0100 | [diff] [blame] | 745 | 1 |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 746 | 1, 2, 3 |
| 747 | x = 1 |
| 748 | x = 1, 2, 3 |
| 749 | x = y = z = 1, 2, 3 |
| 750 | x, y, z = 1, 2, 3 |
| 751 | 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] | 752 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 753 | check_syntax_error(self, "x + 1 = 1") |
| 754 | check_syntax_error(self, "a + 1 = b + 2") |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 755 | |
Nick Coghlan | 5b1fdc1 | 2014-06-16 19:48:02 +1000 | [diff] [blame] | 756 | # Check the heuristic for print & exec covers significant cases |
| 757 | # As well as placing some limits on false positives |
| 758 | def test_former_statements_refer_to_builtins(self): |
| 759 | keywords = "print", "exec" |
| 760 | # Cases where we want the custom error |
| 761 | cases = [ |
| 762 | "{} foo", |
| 763 | "{} {{1:foo}}", |
| 764 | "if 1: {} foo", |
| 765 | "if 1: {} {{1:foo}}", |
| 766 | "if 1:\n {} foo", |
| 767 | "if 1:\n {} {{1:foo}}", |
| 768 | ] |
| 769 | for keyword in keywords: |
| 770 | custom_msg = "call to '{}'".format(keyword) |
| 771 | for case in cases: |
| 772 | source = case.format(keyword) |
| 773 | with self.subTest(source=source): |
| 774 | with self.assertRaisesRegex(SyntaxError, custom_msg): |
| 775 | exec(source) |
| 776 | source = source.replace("foo", "(foo.)") |
| 777 | with self.subTest(source=source): |
| 778 | with self.assertRaisesRegex(SyntaxError, "invalid syntax"): |
| 779 | exec(source) |
| 780 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 781 | def test_del_stmt(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 782 | # 'del' exprlist |
| 783 | abc = [1,2,3] |
| 784 | x, y, z = abc |
| 785 | xyz = x, y, z |
Barry Warsaw | 7e3e1c1 | 2000-10-11 21:26:03 +0000 | [diff] [blame] | 786 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 787 | del abc |
| 788 | del x, y, (z, xyz) |
Barry Warsaw | 9182b45 | 2000-08-29 04:57:10 +0000 | [diff] [blame] | 789 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 790 | def test_pass_stmt(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 791 | # 'pass' |
| 792 | pass |
Barry Warsaw | 9182b45 | 2000-08-29 04:57:10 +0000 | [diff] [blame] | 793 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 794 | # flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt |
| 795 | # Tested below |
Barry Warsaw | 9182b45 | 2000-08-29 04:57:10 +0000 | [diff] [blame] | 796 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 797 | def test_break_stmt(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 798 | # 'break' |
| 799 | while 1: break |
Barry Warsaw | 9182b45 | 2000-08-29 04:57:10 +0000 | [diff] [blame] | 800 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 801 | def test_continue_stmt(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 802 | # 'continue' |
| 803 | i = 1 |
| 804 | while i: i = 0; continue |
Barry Warsaw | 9182b45 | 2000-08-29 04:57:10 +0000 | [diff] [blame] | 805 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 806 | msg = "" |
| 807 | while not msg: |
| 808 | msg = "ok" |
| 809 | try: |
| 810 | continue |
| 811 | msg = "continue failed to continue inside try" |
| 812 | except: |
| 813 | msg = "continue inside try called except block" |
| 814 | if msg != "ok": |
| 815 | self.fail(msg) |
Barry Warsaw | efc92ee | 2000-08-21 15:46:50 +0000 | [diff] [blame] | 816 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 817 | msg = "" |
| 818 | while not msg: |
| 819 | msg = "finally block not called" |
| 820 | try: |
| 821 | continue |
| 822 | finally: |
| 823 | msg = "ok" |
| 824 | if msg != "ok": |
| 825 | self.fail(msg) |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 826 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 827 | def test_break_continue_loop(self): |
| 828 | # This test warrants an explanation. It is a test specifically for SF bugs |
| 829 | # #463359 and #462937. The bug is that a 'break' statement executed or |
| 830 | # exception raised inside a try/except inside a loop, *after* a continue |
| 831 | # statement has been executed in that loop, will cause the wrong number of |
| 832 | # arguments to be popped off the stack and the instruction pointer reset to |
| 833 | # a very small number (usually 0.) Because of this, the following test |
| 834 | # *must* written as a function, and the tracking vars *must* be function |
| 835 | # arguments with default values. Otherwise, the test will loop and loop. |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 836 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 837 | def test_inner(extra_burning_oil = 1, count=0): |
| 838 | big_hippo = 2 |
| 839 | while big_hippo: |
| 840 | count += 1 |
| 841 | try: |
| 842 | if extra_burning_oil and big_hippo == 1: |
| 843 | extra_burning_oil -= 1 |
| 844 | break |
| 845 | big_hippo -= 1 |
| 846 | continue |
| 847 | except: |
| 848 | raise |
| 849 | if count > 2 or big_hippo != 1: |
| 850 | self.fail("continue then break in try/except in loop broken!") |
| 851 | test_inner() |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 852 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 853 | def test_return(self): |
David Cuthbert | fd97d1f | 2018-09-21 18:31:15 -0700 | [diff] [blame] | 854 | # 'return' [testlist_star_expr] |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 855 | def g1(): return |
| 856 | def g2(): return 1 |
David Cuthbert | fd97d1f | 2018-09-21 18:31:15 -0700 | [diff] [blame] | 857 | def g3(): |
| 858 | z = [2, 3] |
| 859 | return 1, *z |
| 860 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 861 | g1() |
| 862 | x = g2() |
David Cuthbert | fd97d1f | 2018-09-21 18:31:15 -0700 | [diff] [blame] | 863 | y = g3() |
| 864 | self.assertEqual(y, (1, 2, 3), "unparenthesized star expr return") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 865 | check_syntax_error(self, "class foo:return 1") |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 866 | |
Serhiy Storchaka | 7cc42c3 | 2018-01-02 02:38:35 +0200 | [diff] [blame] | 867 | def test_break_in_finally(self): |
| 868 | count = 0 |
| 869 | while count < 2: |
| 870 | count += 1 |
| 871 | try: |
| 872 | pass |
| 873 | finally: |
| 874 | break |
| 875 | self.assertEqual(count, 1) |
| 876 | |
| 877 | count = 0 |
| 878 | while count < 2: |
| 879 | count += 1 |
| 880 | try: |
| 881 | continue |
| 882 | finally: |
| 883 | break |
| 884 | self.assertEqual(count, 1) |
| 885 | |
| 886 | count = 0 |
| 887 | while count < 2: |
| 888 | count += 1 |
| 889 | try: |
| 890 | 1/0 |
| 891 | finally: |
| 892 | break |
| 893 | self.assertEqual(count, 1) |
| 894 | |
| 895 | for count in [0, 1]: |
| 896 | self.assertEqual(count, 0) |
| 897 | try: |
| 898 | pass |
| 899 | finally: |
| 900 | break |
| 901 | self.assertEqual(count, 0) |
| 902 | |
| 903 | for count in [0, 1]: |
| 904 | self.assertEqual(count, 0) |
| 905 | try: |
| 906 | continue |
| 907 | finally: |
| 908 | break |
| 909 | self.assertEqual(count, 0) |
| 910 | |
| 911 | for count in [0, 1]: |
| 912 | self.assertEqual(count, 0) |
| 913 | try: |
| 914 | 1/0 |
| 915 | finally: |
| 916 | break |
| 917 | self.assertEqual(count, 0) |
| 918 | |
Serhiy Storchaka | fe2bbb1 | 2018-03-18 09:56:52 +0200 | [diff] [blame] | 919 | def test_continue_in_finally(self): |
| 920 | count = 0 |
| 921 | while count < 2: |
| 922 | count += 1 |
| 923 | try: |
| 924 | pass |
| 925 | finally: |
| 926 | continue |
| 927 | break |
| 928 | self.assertEqual(count, 2) |
| 929 | |
| 930 | count = 0 |
| 931 | while count < 2: |
| 932 | count += 1 |
| 933 | try: |
| 934 | break |
| 935 | finally: |
| 936 | continue |
| 937 | self.assertEqual(count, 2) |
| 938 | |
| 939 | count = 0 |
| 940 | while count < 2: |
| 941 | count += 1 |
| 942 | try: |
| 943 | 1/0 |
| 944 | finally: |
| 945 | continue |
| 946 | break |
| 947 | self.assertEqual(count, 2) |
| 948 | |
| 949 | for count in [0, 1]: |
| 950 | try: |
| 951 | pass |
| 952 | finally: |
| 953 | continue |
| 954 | break |
| 955 | self.assertEqual(count, 1) |
| 956 | |
| 957 | for count in [0, 1]: |
| 958 | try: |
| 959 | break |
| 960 | finally: |
| 961 | continue |
| 962 | self.assertEqual(count, 1) |
| 963 | |
| 964 | for count in [0, 1]: |
| 965 | try: |
| 966 | 1/0 |
| 967 | finally: |
| 968 | continue |
| 969 | break |
| 970 | self.assertEqual(count, 1) |
| 971 | |
Serhiy Storchaka | 7cc42c3 | 2018-01-02 02:38:35 +0200 | [diff] [blame] | 972 | def test_return_in_finally(self): |
| 973 | def g1(): |
| 974 | try: |
| 975 | pass |
| 976 | finally: |
| 977 | return 1 |
| 978 | self.assertEqual(g1(), 1) |
| 979 | |
| 980 | def g2(): |
| 981 | try: |
| 982 | return 2 |
| 983 | finally: |
| 984 | return 3 |
| 985 | self.assertEqual(g2(), 3) |
| 986 | |
| 987 | def g3(): |
| 988 | try: |
| 989 | 1/0 |
| 990 | finally: |
| 991 | return 4 |
| 992 | self.assertEqual(g3(), 4) |
| 993 | |
Serhiy Storchaka | ed146b5 | 2019-08-24 13:41:53 +0300 | [diff] [blame] | 994 | def test_break_in_finally_after_return(self): |
| 995 | # See issue #37830 |
| 996 | def g1(x): |
| 997 | for count in [0, 1]: |
| 998 | count2 = 0 |
| 999 | while count2 < 20: |
| 1000 | count2 += 10 |
| 1001 | try: |
| 1002 | return count + count2 |
| 1003 | finally: |
| 1004 | if x: |
| 1005 | break |
| 1006 | return 'end', count, count2 |
| 1007 | self.assertEqual(g1(False), 10) |
| 1008 | self.assertEqual(g1(True), ('end', 1, 10)) |
| 1009 | |
| 1010 | def g2(x): |
| 1011 | for count in [0, 1]: |
| 1012 | for count2 in [10, 20]: |
| 1013 | try: |
| 1014 | return count + count2 |
| 1015 | finally: |
| 1016 | if x: |
| 1017 | break |
| 1018 | return 'end', count, count2 |
| 1019 | self.assertEqual(g2(False), 10) |
| 1020 | self.assertEqual(g2(True), ('end', 1, 10)) |
| 1021 | |
| 1022 | def test_continue_in_finally_after_return(self): |
| 1023 | # See issue #37830 |
| 1024 | def g1(x): |
| 1025 | count = 0 |
| 1026 | while count < 100: |
| 1027 | count += 1 |
| 1028 | try: |
| 1029 | return count |
| 1030 | finally: |
| 1031 | if x: |
| 1032 | continue |
| 1033 | return 'end', count |
| 1034 | self.assertEqual(g1(False), 1) |
| 1035 | self.assertEqual(g1(True), ('end', 100)) |
| 1036 | |
| 1037 | def g2(x): |
| 1038 | for count in [0, 1]: |
| 1039 | try: |
| 1040 | return count |
| 1041 | finally: |
| 1042 | if x: |
| 1043 | continue |
| 1044 | return 'end', count |
| 1045 | self.assertEqual(g2(False), 0) |
| 1046 | self.assertEqual(g2(True), ('end', 1)) |
| 1047 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 1048 | def test_yield(self): |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 1049 | # Allowed as standalone statement |
| 1050 | def g(): yield 1 |
| 1051 | def g(): yield from () |
| 1052 | # Allowed as RHS of assignment |
| 1053 | def g(): x = yield 1 |
| 1054 | def g(): x = yield from () |
| 1055 | # Ordinary yield accepts implicit tuples |
| 1056 | def g(): yield 1, 1 |
| 1057 | def g(): x = yield 1, 1 |
| 1058 | # 'yield from' does not |
| 1059 | check_syntax_error(self, "def g(): yield from (), 1") |
| 1060 | check_syntax_error(self, "def g(): x = yield from (), 1") |
| 1061 | # Requires parentheses as subexpression |
| 1062 | def g(): 1, (yield 1) |
| 1063 | def g(): 1, (yield from ()) |
| 1064 | check_syntax_error(self, "def g(): 1, yield 1") |
| 1065 | check_syntax_error(self, "def g(): 1, yield from ()") |
| 1066 | # Requires parentheses as call argument |
| 1067 | def g(): f((yield 1)) |
| 1068 | def g(): f((yield 1), 1) |
| 1069 | def g(): f((yield from ())) |
| 1070 | def g(): f((yield from ()), 1) |
David Cuthbert | fd97d1f | 2018-09-21 18:31:15 -0700 | [diff] [blame] | 1071 | # Do not require parenthesis for tuple unpacking |
| 1072 | def g(): rest = 4, 5, 6; yield 1, 2, 3, *rest |
Serhiy Storchaka | 4642d5f | 2018-10-05 21:09:56 +0300 | [diff] [blame] | 1073 | self.assertEqual(list(g()), [(1, 2, 3, 4, 5, 6)]) |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 1074 | check_syntax_error(self, "def g(): f(yield 1)") |
| 1075 | check_syntax_error(self, "def g(): f(yield 1, 1)") |
| 1076 | check_syntax_error(self, "def g(): f(yield from ())") |
| 1077 | check_syntax_error(self, "def g(): f(yield from (), 1)") |
| 1078 | # Not allowed at top level |
| 1079 | check_syntax_error(self, "yield") |
| 1080 | check_syntax_error(self, "yield from") |
| 1081 | # Not allowed at class scope |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1082 | check_syntax_error(self, "class foo:yield 1") |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 1083 | check_syntax_error(self, "class foo:yield from ()") |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 1084 | # Check annotation refleak on SyntaxError |
| 1085 | check_syntax_error(self, "def g(a:(yield)): pass") |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 1086 | |
Serhiy Storchaka | 73a7e9b | 2017-12-01 06:54:17 +0200 | [diff] [blame] | 1087 | def test_yield_in_comprehensions(self): |
| 1088 | # Check yield in comprehensions |
| 1089 | def g(): [x for x in [(yield 1)]] |
| 1090 | def g(): [x for x in [(yield from ())]] |
| 1091 | |
Serhiy Storchaka | 07ca9af | 2018-02-04 10:53:48 +0200 | [diff] [blame] | 1092 | check = self.check_syntax_error |
Serhiy Storchaka | 73a7e9b | 2017-12-01 06:54:17 +0200 | [diff] [blame] | 1093 | check("def g(): [(yield x) for x in ()]", |
| 1094 | "'yield' inside list comprehension") |
| 1095 | check("def g(): [x for x in () if not (yield x)]", |
| 1096 | "'yield' inside list comprehension") |
| 1097 | check("def g(): [y for x in () for y in [(yield x)]]", |
| 1098 | "'yield' inside list comprehension") |
| 1099 | check("def g(): {(yield x) for x in ()}", |
| 1100 | "'yield' inside set comprehension") |
| 1101 | check("def g(): {(yield x): x for x in ()}", |
| 1102 | "'yield' inside dict comprehension") |
| 1103 | check("def g(): {x: (yield x) for x in ()}", |
| 1104 | "'yield' inside dict comprehension") |
| 1105 | check("def g(): ((yield x) for x in ())", |
| 1106 | "'yield' inside generator expression") |
| 1107 | check("def g(): [(yield from x) for x in ()]", |
| 1108 | "'yield' inside list comprehension") |
| 1109 | check("class C: [(yield x) for x in ()]", |
| 1110 | "'yield' inside list comprehension") |
| 1111 | check("[(yield x) for x in ()]", |
| 1112 | "'yield' inside list comprehension") |
| 1113 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 1114 | def test_raise(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1115 | # 'raise' test [',' test] |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 1116 | try: raise RuntimeError('just testing') |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1117 | except RuntimeError: pass |
| 1118 | try: raise KeyboardInterrupt |
| 1119 | except KeyboardInterrupt: pass |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 1120 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 1121 | def test_import(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1122 | # 'import' dotted_as_names |
| 1123 | import sys |
| 1124 | import time, sys |
| 1125 | # 'from' dotted_name 'import' ('*' | '(' import_as_names ')' | import_as_names) |
| 1126 | from time import time |
| 1127 | from time import (time) |
| 1128 | # not testable inside a function, but already done at top of the module |
| 1129 | # from sys import * |
| 1130 | from sys import path, argv |
| 1131 | from sys import (path, argv) |
| 1132 | from sys import (path, argv,) |
Tim Peters | 10fb386 | 2001-02-09 20:17:14 +0000 | [diff] [blame] | 1133 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 1134 | def test_global(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1135 | # 'global' NAME (',' NAME)* |
| 1136 | global a |
| 1137 | global a, b |
| 1138 | global one, two, three, four, five, six, seven, eight, nine, ten |
Thomas Wouters | 80d373c | 2001-09-26 12:43:39 +0000 | [diff] [blame] | 1139 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 1140 | def test_nonlocal(self): |
Benjamin Peterson | a933e52 | 2008-10-24 22:16:39 +0000 | [diff] [blame] | 1141 | # 'nonlocal' NAME (',' NAME)* |
| 1142 | x = 0 |
| 1143 | y = 0 |
| 1144 | def f(): |
| 1145 | nonlocal x |
| 1146 | nonlocal x, y |
| 1147 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 1148 | def test_assert(self): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1149 | # assertTruestmt: 'assert' test [',' test] |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1150 | assert 1 |
| 1151 | assert 1, 1 |
| 1152 | assert lambda x:x |
| 1153 | assert 1, lambda x:x+1 |
Ezio Melotti | 6cc5bf7 | 2011-12-02 18:22:52 +0200 | [diff] [blame] | 1154 | |
| 1155 | try: |
| 1156 | assert True |
| 1157 | except AssertionError as e: |
| 1158 | self.fail("'assert True' should not have raised an AssertionError") |
| 1159 | |
| 1160 | try: |
| 1161 | assert True, 'this should always pass' |
| 1162 | except AssertionError as e: |
| 1163 | self.fail("'assert True, msg' should not have " |
| 1164 | "raised an AssertionError") |
| 1165 | |
| 1166 | # these tests fail if python is run with -O, so check __debug__ |
| 1167 | @unittest.skipUnless(__debug__, "Won't work if __debug__ is False") |
| 1168 | def testAssert2(self): |
Thomas Wouters | 80d373c | 2001-09-26 12:43:39 +0000 | [diff] [blame] | 1169 | try: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1170 | assert 0, "msg" |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 1171 | except AssertionError as e: |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 1172 | self.assertEqual(e.args[0], "msg") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1173 | else: |
Ezio Melotti | 6cc5bf7 | 2011-12-02 18:22:52 +0200 | [diff] [blame] | 1174 | self.fail("AssertionError not raised by assert 0") |
| 1175 | |
| 1176 | try: |
| 1177 | assert False |
| 1178 | except AssertionError as e: |
| 1179 | self.assertEqual(len(e.args), 0) |
| 1180 | else: |
| 1181 | self.fail("AssertionError not raised by 'assert False'") |
| 1182 | |
Serhiy Storchaka | e7a4bb5 | 2019-02-19 08:30:15 +0200 | [diff] [blame] | 1183 | self.check_syntax_warning('assert(x, "msg")', |
| 1184 | 'assertion is always true') |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 1185 | with warnings.catch_warnings(): |
Serhiy Storchaka | e7a4bb5 | 2019-02-19 08:30:15 +0200 | [diff] [blame] | 1186 | warnings.simplefilter('error', SyntaxWarning) |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 1187 | compile('assert x, "msg"', '<testcase>', 'exec') |
| 1188 | |
Thomas Wouters | 80d373c | 2001-09-26 12:43:39 +0000 | [diff] [blame] | 1189 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1190 | ### compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef |
| 1191 | # Tested below |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1192 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 1193 | def test_if(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1194 | # 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] |
| 1195 | if 1: pass |
| 1196 | if 1: pass |
| 1197 | else: pass |
| 1198 | if 0: pass |
| 1199 | elif 0: pass |
| 1200 | if 0: pass |
| 1201 | elif 0: pass |
| 1202 | elif 0: pass |
| 1203 | elif 0: pass |
| 1204 | else: pass |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 1205 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 1206 | def test_while(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1207 | # 'while' test ':' suite ['else' ':' suite] |
| 1208 | while 0: pass |
| 1209 | while 0: pass |
| 1210 | else: pass |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 1211 | |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 1212 | # Issue1920: "while 0" is optimized away, |
| 1213 | # ensure that the "else" clause is still present. |
| 1214 | x = 0 |
| 1215 | while 0: |
| 1216 | x = 1 |
| 1217 | else: |
| 1218 | x = 2 |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 1219 | self.assertEqual(x, 2) |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 1220 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 1221 | def test_for(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1222 | # 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite] |
| 1223 | for i in 1, 2, 3: pass |
| 1224 | for i, j, k in (): pass |
| 1225 | else: pass |
| 1226 | class Squares: |
| 1227 | def __init__(self, max): |
| 1228 | self.max = max |
| 1229 | self.sofar = [] |
| 1230 | def __len__(self): return len(self.sofar) |
| 1231 | def __getitem__(self, i): |
| 1232 | if not 0 <= i < self.max: raise IndexError |
| 1233 | n = len(self.sofar) |
| 1234 | while n <= i: |
| 1235 | self.sofar.append(n*n) |
| 1236 | n = n+1 |
| 1237 | return self.sofar[i] |
| 1238 | n = 0 |
| 1239 | for x in Squares(10): n = n+x |
| 1240 | if n != 285: |
| 1241 | self.fail('for over growing sequence') |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 1242 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1243 | result = [] |
| 1244 | for x, in [(1,), (2,), (3,)]: |
| 1245 | result.append(x) |
| 1246 | self.assertEqual(result, [1, 2, 3]) |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 1247 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 1248 | def test_try(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1249 | ### try_stmt: 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite] |
| 1250 | ### | 'try' ':' suite 'finally' ':' suite |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 1251 | ### except_clause: 'except' [expr ['as' expr]] |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1252 | try: |
| 1253 | 1/0 |
| 1254 | except ZeroDivisionError: |
| 1255 | pass |
| 1256 | else: |
| 1257 | pass |
| 1258 | try: 1/0 |
| 1259 | except EOFError: pass |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 1260 | except TypeError as msg: pass |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1261 | except: pass |
| 1262 | else: pass |
| 1263 | try: 1/0 |
| 1264 | except (EOFError, TypeError, ZeroDivisionError): pass |
| 1265 | try: 1/0 |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 1266 | except (EOFError, TypeError, ZeroDivisionError) as msg: pass |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1267 | try: pass |
| 1268 | finally: pass |
Jeremy Hylton | f828e2d | 2001-02-19 15:54:52 +0000 | [diff] [blame] | 1269 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 1270 | def test_suite(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1271 | # simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT |
| 1272 | if 1: pass |
| 1273 | if 1: |
| 1274 | pass |
| 1275 | if 1: |
| 1276 | # |
| 1277 | # |
| 1278 | # |
| 1279 | pass |
| 1280 | pass |
| 1281 | # |
| 1282 | pass |
| 1283 | # |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 1284 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 1285 | def test_test(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1286 | ### and_test ('or' and_test)* |
| 1287 | ### and_test: not_test ('and' not_test)* |
| 1288 | ### not_test: 'not' not_test | comparison |
| 1289 | if not 1: pass |
| 1290 | if 1 and 1: pass |
| 1291 | if 1 or 1: pass |
| 1292 | if not not not 1: pass |
| 1293 | if not 1 and 1 and 1: pass |
| 1294 | 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] | 1295 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 1296 | def test_comparison(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1297 | ### comparison: expr (comp_op expr)* |
| 1298 | ### comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'|'is' 'not' |
| 1299 | if 1: pass |
| 1300 | x = (1 == 1) |
| 1301 | if 1 == 1: pass |
| 1302 | if 1 != 1: pass |
| 1303 | if 1 < 1: pass |
| 1304 | if 1 > 1: pass |
| 1305 | if 1 <= 1: pass |
| 1306 | if 1 >= 1: pass |
Serhiy Storchaka | 3bcbedc | 2019-01-18 07:47:48 +0200 | [diff] [blame] | 1307 | if x is x: pass |
| 1308 | if x is not x: pass |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1309 | if 1 in (): pass |
| 1310 | if 1 not in (): pass |
Serhiy Storchaka | 3bcbedc | 2019-01-18 07:47:48 +0200 | [diff] [blame] | 1311 | if 1 < 1 > 1 == 1 >= 1 <= 1 != 1 in 1 not in x is x is not x: pass |
| 1312 | |
| 1313 | def test_comparison_is_literal(self): |
| 1314 | def check(test, msg='"is" with a literal'): |
Serhiy Storchaka | e7a4bb5 | 2019-02-19 08:30:15 +0200 | [diff] [blame] | 1315 | self.check_syntax_warning(test, msg) |
Serhiy Storchaka | 3bcbedc | 2019-01-18 07:47:48 +0200 | [diff] [blame] | 1316 | |
| 1317 | check('x is 1') |
| 1318 | check('x is "thing"') |
| 1319 | check('1 is x') |
| 1320 | check('x is y is 1') |
| 1321 | check('x is not 1', '"is not" with a literal') |
| 1322 | |
| 1323 | with warnings.catch_warnings(): |
Serhiy Storchaka | e7a4bb5 | 2019-02-19 08:30:15 +0200 | [diff] [blame] | 1324 | warnings.simplefilter('error', SyntaxWarning) |
Serhiy Storchaka | 3bcbedc | 2019-01-18 07:47:48 +0200 | [diff] [blame] | 1325 | compile('x is None', '<testcase>', 'exec') |
| 1326 | compile('x is False', '<testcase>', 'exec') |
| 1327 | compile('x is True', '<testcase>', 'exec') |
| 1328 | compile('x is ...', '<testcase>', 'exec') |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 1329 | |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 1330 | def test_warn_missed_comma(self): |
| 1331 | def check(test): |
Serhiy Storchaka | e7a4bb5 | 2019-02-19 08:30:15 +0200 | [diff] [blame] | 1332 | self.check_syntax_warning(test, msg) |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 1333 | |
| 1334 | msg=r'is not callable; perhaps you missed a comma\?' |
| 1335 | check('[(1, 2) (3, 4)]') |
| 1336 | check('[(x, y) (3, 4)]') |
| 1337 | check('[[1, 2] (3, 4)]') |
| 1338 | check('[{1, 2} (3, 4)]') |
| 1339 | check('[{1: 2} (3, 4)]') |
| 1340 | check('[[i for i in range(5)] (3, 4)]') |
| 1341 | check('[{i for i in range(5)} (3, 4)]') |
| 1342 | check('[(i for i in range(5)) (3, 4)]') |
| 1343 | check('[{i: i for i in range(5)} (3, 4)]') |
| 1344 | check('[f"{x}" (3, 4)]') |
| 1345 | check('[f"x={x}" (3, 4)]') |
| 1346 | check('["abc" (3, 4)]') |
| 1347 | check('[b"abc" (3, 4)]') |
| 1348 | check('[123 (3, 4)]') |
| 1349 | check('[12.3 (3, 4)]') |
| 1350 | check('[12.3j (3, 4)]') |
| 1351 | check('[None (3, 4)]') |
| 1352 | check('[True (3, 4)]') |
| 1353 | check('[... (3, 4)]') |
| 1354 | |
| 1355 | msg=r'is not subscriptable; perhaps you missed a comma\?' |
| 1356 | check('[{1, 2} [i, j]]') |
| 1357 | check('[{i for i in range(5)} [i, j]]') |
| 1358 | check('[(i for i in range(5)) [i, j]]') |
| 1359 | check('[(lambda x, y: x) [i, j]]') |
| 1360 | check('[123 [i, j]]') |
| 1361 | check('[12.3 [i, j]]') |
| 1362 | check('[12.3j [i, j]]') |
| 1363 | check('[None [i, j]]') |
| 1364 | check('[True [i, j]]') |
| 1365 | check('[... [i, j]]') |
| 1366 | |
| 1367 | msg=r'indices must be integers or slices, not tuple; perhaps you missed a comma\?' |
| 1368 | check('[(1, 2) [i, j]]') |
| 1369 | check('[(x, y) [i, j]]') |
| 1370 | check('[[1, 2] [i, j]]') |
| 1371 | check('[[i for i in range(5)] [i, j]]') |
| 1372 | check('[f"{x}" [i, j]]') |
| 1373 | check('[f"x={x}" [i, j]]') |
| 1374 | check('["abc" [i, j]]') |
| 1375 | check('[b"abc" [i, j]]') |
| 1376 | |
| 1377 | msg=r'indices must be integers or slices, not tuple;' |
| 1378 | check('[[1, 2] [3, 4]]') |
| 1379 | msg=r'indices must be integers or slices, not list;' |
| 1380 | check('[[1, 2] [[3, 4]]]') |
| 1381 | check('[[1, 2] [[i for i in range(5)]]]') |
| 1382 | msg=r'indices must be integers or slices, not set;' |
| 1383 | check('[[1, 2] [{3, 4}]]') |
| 1384 | check('[[1, 2] [{i for i in range(5)}]]') |
| 1385 | msg=r'indices must be integers or slices, not dict;' |
| 1386 | check('[[1, 2] [{3: 4}]]') |
| 1387 | check('[[1, 2] [{i: i for i in range(5)}]]') |
| 1388 | msg=r'indices must be integers or slices, not generator;' |
| 1389 | check('[[1, 2] [(i for i in range(5))]]') |
| 1390 | msg=r'indices must be integers or slices, not function;' |
| 1391 | check('[[1, 2] [(lambda x, y: x)]]') |
| 1392 | msg=r'indices must be integers or slices, not str;' |
| 1393 | check('[[1, 2] [f"{x}"]]') |
| 1394 | check('[[1, 2] [f"x={x}"]]') |
| 1395 | check('[[1, 2] ["abc"]]') |
| 1396 | msg=r'indices must be integers or slices, not' |
| 1397 | check('[[1, 2] [b"abc"]]') |
| 1398 | check('[[1, 2] [12.3]]') |
| 1399 | check('[[1, 2] [12.3j]]') |
| 1400 | check('[[1, 2] [None]]') |
| 1401 | check('[[1, 2] [...]]') |
| 1402 | |
| 1403 | with warnings.catch_warnings(): |
Serhiy Storchaka | e7a4bb5 | 2019-02-19 08:30:15 +0200 | [diff] [blame] | 1404 | warnings.simplefilter('error', SyntaxWarning) |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 1405 | compile('[(lambda x, y: x) (3, 4)]', '<testcase>', 'exec') |
| 1406 | compile('[[1, 2] [i]]', '<testcase>', 'exec') |
| 1407 | compile('[[1, 2] [0]]', '<testcase>', 'exec') |
| 1408 | compile('[[1, 2] [True]]', '<testcase>', 'exec') |
| 1409 | compile('[[1, 2] [1:2]]', '<testcase>', 'exec') |
| 1410 | compile('[{(1, 2): 3} [i, j]]', '<testcase>', 'exec') |
| 1411 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 1412 | def test_binary_mask_ops(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1413 | x = 1 & 1 |
| 1414 | x = 1 ^ 1 |
| 1415 | x = 1 | 1 |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 1416 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 1417 | def test_shift_ops(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1418 | x = 1 << 1 |
| 1419 | x = 1 >> 1 |
| 1420 | x = 1 << 1 >> 1 |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1421 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 1422 | def test_additive_ops(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1423 | x = 1 |
| 1424 | x = 1 + 1 |
| 1425 | x = 1 - 1 - 1 |
| 1426 | x = 1 - 1 + 1 - 1 + 1 |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 1427 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 1428 | def test_multiplicative_ops(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1429 | x = 1 * 1 |
| 1430 | x = 1 / 1 |
| 1431 | x = 1 % 1 |
| 1432 | x = 1 / 1 * 1 % 1 |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 1433 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 1434 | def test_unary_ops(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1435 | x = +1 |
| 1436 | x = -1 |
| 1437 | x = ~1 |
| 1438 | x = ~1 ^ 1 & 1 | 1 & 1 ^ -1 |
| 1439 | x = -1*1/1 + 1*1 - ---1*1 |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 1440 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 1441 | def test_selectors(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1442 | ### trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME |
| 1443 | ### subscript: expr | [expr] ':' [expr] |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 1444 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1445 | import sys, time |
| 1446 | c = sys.path[0] |
| 1447 | x = time.time() |
| 1448 | x = sys.modules['time'].time() |
| 1449 | a = '01234' |
| 1450 | c = a[0] |
| 1451 | c = a[-1] |
| 1452 | s = a[0:5] |
| 1453 | s = a[:5] |
| 1454 | s = a[0:] |
| 1455 | s = a[:] |
| 1456 | s = a[-5:] |
| 1457 | s = a[:-1] |
| 1458 | s = a[-4:-3] |
| 1459 | # A rough test of SF bug 1333982. http://python.org/sf/1333982 |
| 1460 | # The testing here is fairly incomplete. |
| 1461 | # Test cases should include: commas with 1 and 2 colons |
| 1462 | d = {} |
| 1463 | d[1] = 1 |
| 1464 | d[1,] = 2 |
| 1465 | d[1,2] = 3 |
| 1466 | d[1,2,3] = 4 |
| 1467 | L = list(d) |
Serhiy Storchaka | 0cc99c8 | 2018-01-04 10:36:35 +0200 | [diff] [blame] | 1468 | L.sort(key=lambda x: (type(x).__name__, x)) |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 1469 | self.assertEqual(str(L), '[1, (1,), (1, 2), (1, 2, 3)]') |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 1470 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 1471 | def test_atoms(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1472 | ### atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictsetmaker] '}' | NAME | NUMBER | STRING |
| 1473 | ### dictsetmaker: (test ':' test (',' test ':' test)* [',']) | (test (',' test)* [',']) |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 1474 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1475 | x = (1) |
| 1476 | x = (1 or 2 or 3) |
| 1477 | x = (1 or 2 or 3, 2, 3) |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 1478 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1479 | x = [] |
| 1480 | x = [1] |
| 1481 | x = [1 or 2 or 3] |
| 1482 | x = [1 or 2 or 3, 2, 3] |
| 1483 | x = [] |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 1484 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1485 | x = {} |
| 1486 | x = {'one': 1} |
| 1487 | x = {'one': 1,} |
| 1488 | x = {'one' or 'two': 1 or 2} |
| 1489 | x = {'one': 1, 'two': 2} |
| 1490 | x = {'one': 1, 'two': 2,} |
| 1491 | 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] | 1492 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1493 | x = {'one'} |
| 1494 | x = {'one', 1,} |
| 1495 | x = {'one', 'two', 'three'} |
| 1496 | x = {2, 3, 4,} |
| 1497 | |
| 1498 | x = x |
| 1499 | x = 'x' |
| 1500 | x = 123 |
| 1501 | |
| 1502 | ### exprlist: expr (',' expr)* [','] |
| 1503 | ### testlist: test (',' test)* [','] |
| 1504 | # These have been exercised enough above |
| 1505 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 1506 | def test_classdef(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1507 | # 'class' NAME ['(' [testlist] ')'] ':' suite |
| 1508 | class B: pass |
| 1509 | class B2(): pass |
| 1510 | class C1(B): pass |
| 1511 | class C2(B): pass |
| 1512 | class D(C1, C2, B): pass |
| 1513 | class C: |
| 1514 | def meth1(self): pass |
| 1515 | def meth2(self, arg): pass |
| 1516 | def meth3(self, a1, a2): pass |
| 1517 | |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 1518 | # decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE |
| 1519 | # decorators: decorator+ |
| 1520 | # decorated: decorators (classdef | funcdef) |
| 1521 | def class_decorator(x): return x |
| 1522 | @class_decorator |
| 1523 | class G: pass |
| 1524 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 1525 | def test_dictcomps(self): |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 1526 | # dictorsetmaker: ( (test ':' test (comp_for | |
| 1527 | # (',' test ':' test)* [','])) | |
| 1528 | # (test (comp_for | (',' test)* [','])) ) |
| 1529 | nums = [1, 2, 3] |
| 1530 | self.assertEqual({i:i+1 for i in nums}, {1: 2, 2: 3, 3: 4}) |
| 1531 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 1532 | def test_listcomps(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1533 | # list comprehension tests |
| 1534 | nums = [1, 2, 3, 4, 5] |
| 1535 | strs = ["Apple", "Banana", "Coconut"] |
| 1536 | spcs = [" Apple", " Banana ", "Coco nut "] |
| 1537 | |
| 1538 | self.assertEqual([s.strip() for s in spcs], ['Apple', 'Banana', 'Coco nut']) |
| 1539 | self.assertEqual([3 * x for x in nums], [3, 6, 9, 12, 15]) |
| 1540 | self.assertEqual([x for x in nums if x > 2], [3, 4, 5]) |
| 1541 | self.assertEqual([(i, s) for i in nums for s in strs], |
| 1542 | [(1, 'Apple'), (1, 'Banana'), (1, 'Coconut'), |
| 1543 | (2, 'Apple'), (2, 'Banana'), (2, 'Coconut'), |
| 1544 | (3, 'Apple'), (3, 'Banana'), (3, 'Coconut'), |
| 1545 | (4, 'Apple'), (4, 'Banana'), (4, 'Coconut'), |
| 1546 | (5, 'Apple'), (5, 'Banana'), (5, 'Coconut')]) |
| 1547 | self.assertEqual([(i, s) for i in nums for s in [f for f in strs if "n" in f]], |
| 1548 | [(1, 'Banana'), (1, 'Coconut'), (2, 'Banana'), (2, 'Coconut'), |
| 1549 | (3, 'Banana'), (3, 'Coconut'), (4, 'Banana'), (4, 'Coconut'), |
| 1550 | (5, 'Banana'), (5, 'Coconut')]) |
| 1551 | self.assertEqual([(lambda a:[a**i for i in range(a+1)])(j) for j in range(5)], |
| 1552 | [[1], [1, 1], [1, 2, 4], [1, 3, 9, 27], [1, 4, 16, 64, 256]]) |
| 1553 | |
| 1554 | def test_in_func(l): |
| 1555 | return [0 < x < 3 for x in l if x > 2] |
| 1556 | |
| 1557 | self.assertEqual(test_in_func(nums), [False, False, False]) |
| 1558 | |
| 1559 | def test_nested_front(): |
| 1560 | self.assertEqual([[y for y in [x, x + 1]] for x in [1,3,5]], |
| 1561 | [[1, 2], [3, 4], [5, 6]]) |
| 1562 | |
| 1563 | test_nested_front() |
| 1564 | |
| 1565 | check_syntax_error(self, "[i, s for i in nums for s in strs]") |
| 1566 | check_syntax_error(self, "[x if y]") |
| 1567 | |
| 1568 | suppliers = [ |
| 1569 | (1, "Boeing"), |
| 1570 | (2, "Ford"), |
| 1571 | (3, "Macdonalds") |
| 1572 | ] |
| 1573 | |
| 1574 | parts = [ |
| 1575 | (10, "Airliner"), |
| 1576 | (20, "Engine"), |
| 1577 | (30, "Cheeseburger") |
| 1578 | ] |
| 1579 | |
| 1580 | suppart = [ |
| 1581 | (1, 10), (1, 20), (2, 20), (3, 30) |
| 1582 | ] |
| 1583 | |
| 1584 | x = [ |
| 1585 | (sname, pname) |
| 1586 | for (sno, sname) in suppliers |
| 1587 | for (pno, pname) in parts |
| 1588 | for (sp_sno, sp_pno) in suppart |
| 1589 | if sno == sp_sno and pno == sp_pno |
| 1590 | ] |
| 1591 | |
| 1592 | self.assertEqual(x, [('Boeing', 'Airliner'), ('Boeing', 'Engine'), ('Ford', 'Engine'), |
| 1593 | ('Macdonalds', 'Cheeseburger')]) |
| 1594 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 1595 | def test_genexps(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1596 | # generator expression tests |
| 1597 | g = ([x for x in range(10)] for x in range(1)) |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 1598 | self.assertEqual(next(g), [x for x in range(10)]) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1599 | try: |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 1600 | next(g) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1601 | self.fail('should produce StopIteration exception') |
| 1602 | except StopIteration: |
| 1603 | pass |
| 1604 | |
| 1605 | a = 1 |
| 1606 | try: |
| 1607 | g = (a for d in a) |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 1608 | next(g) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1609 | self.fail('should produce TypeError') |
| 1610 | except TypeError: |
| 1611 | pass |
| 1612 | |
| 1613 | self.assertEqual(list((x, y) for x in 'abcd' for y in 'abcd'), [(x, y) for x in 'abcd' for y in 'abcd']) |
| 1614 | self.assertEqual(list((x, y) for x in 'ab' for y in 'xy'), [(x, y) for x in 'ab' for y in 'xy']) |
| 1615 | |
| 1616 | a = [x for x in range(10)] |
| 1617 | b = (x for x in (y for y in a)) |
| 1618 | self.assertEqual(sum(b), sum([x for x in range(10)])) |
| 1619 | |
| 1620 | self.assertEqual(sum(x**2 for x in range(10)), sum([x**2 for x in range(10)])) |
| 1621 | self.assertEqual(sum(x*x for x in range(10) if x%2), sum([x*x for x in range(10) if x%2])) |
| 1622 | self.assertEqual(sum(x for x in (y for y in range(10))), sum([x for x in range(10)])) |
| 1623 | self.assertEqual(sum(x for x in (y for y in (z for z in range(10)))), sum([x for x in range(10)])) |
| 1624 | self.assertEqual(sum(x for x in [y for y in (z for z in range(10))]), sum([x for x in range(10)])) |
| 1625 | 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)])) |
| 1626 | self.assertEqual(sum(x for x in (y for y in (z for z in range(10) if True) if False) if True), 0) |
| 1627 | check_syntax_error(self, "foo(x for x in range(10), 100)") |
| 1628 | check_syntax_error(self, "foo(100, x for x in range(10))") |
| 1629 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 1630 | def test_comprehension_specials(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1631 | # test for outmost iterable precomputation |
| 1632 | x = 10; g = (i for i in range(x)); x = 5 |
| 1633 | self.assertEqual(len(list(g)), 10) |
| 1634 | |
| 1635 | # This should hold, since we're only precomputing outmost iterable. |
| 1636 | x = 10; t = False; g = ((i,j) for i in range(x) if t for j in range(x)) |
| 1637 | x = 5; t = True; |
| 1638 | self.assertEqual([(i,j) for i in range(10) for j in range(5)], list(g)) |
| 1639 | |
| 1640 | # Grammar allows multiple adjacent 'if's in listcomps and genexps, |
| 1641 | # even though it's silly. Make sure it works (ifelse broke this.) |
| 1642 | self.assertEqual([ x for x in range(10) if x % 2 if x % 3 ], [1, 5, 7]) |
| 1643 | self.assertEqual(list(x for x in range(10) if x % 2 if x % 3), [1, 5, 7]) |
| 1644 | |
| 1645 | # verify unpacking single element tuples in listcomp/genexp. |
| 1646 | self.assertEqual([x for x, in [(4,), (5,), (6,)]], [4, 5, 6]) |
| 1647 | self.assertEqual(list(x for x, in [(7,), (8,), (9,)]), [7, 8, 9]) |
| 1648 | |
Benjamin Peterson | f17ab89 | 2009-05-29 21:55:57 +0000 | [diff] [blame] | 1649 | def test_with_statement(self): |
| 1650 | class manager(object): |
| 1651 | def __enter__(self): |
| 1652 | return (1, 2) |
| 1653 | def __exit__(self, *args): |
| 1654 | pass |
| 1655 | |
| 1656 | with manager(): |
| 1657 | pass |
| 1658 | with manager() as x: |
| 1659 | pass |
| 1660 | with manager() as (x, y): |
| 1661 | pass |
| 1662 | with manager(), manager(): |
| 1663 | pass |
| 1664 | with manager() as x, manager() as y: |
| 1665 | pass |
| 1666 | with manager() as x, manager(): |
| 1667 | pass |
| 1668 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 1669 | def test_if_else_expr(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1670 | # Test ifelse expressions in various cases |
| 1671 | def _checkeval(msg, ret): |
| 1672 | "helper to check that evaluation of expressions is done correctly" |
Victor Stinner | c6ec54d | 2016-04-12 18:33:41 +0200 | [diff] [blame] | 1673 | print(msg) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1674 | return ret |
| 1675 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1676 | # the next line is not allowed anymore |
| 1677 | #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] | 1678 | self.assertEqual([ x() for x in (lambda: True, lambda: False) if x() ], [True]) |
| 1679 | 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]) |
| 1680 | self.assertEqual((5 if 1 else _checkeval("check 1", 0)), 5) |
| 1681 | self.assertEqual((_checkeval("check 2", 0) if 0 else 5), 5) |
| 1682 | self.assertEqual((5 and 6 if 0 else 1), 1) |
| 1683 | self.assertEqual(((5 and 6) if 0 else 1), 1) |
| 1684 | self.assertEqual((5 and (6 if 1 else 1)), 6) |
| 1685 | self.assertEqual((0 or _checkeval("check 3", 2) if 0 else 3), 3) |
| 1686 | self.assertEqual((1 or _checkeval("check 4", 2) if 1 else _checkeval("check 5", 3)), 1) |
| 1687 | self.assertEqual((0 or 5 if 1 else _checkeval("check 6", 3)), 5) |
| 1688 | self.assertEqual((not 5 if 1 else 1), False) |
| 1689 | self.assertEqual((not 5 if 0 else 1), 1) |
| 1690 | self.assertEqual((6 + 1 if 1 else 2), 7) |
| 1691 | self.assertEqual((6 - 1 if 1 else 2), 5) |
| 1692 | self.assertEqual((6 * 2 if 1 else 4), 12) |
| 1693 | self.assertEqual((6 / 2 if 1 else 3), 3) |
| 1694 | self.assertEqual((6 < 4 if 0 else 2), 2) |
Jeremy Hylton | 7b03bad | 2006-02-28 17:46:23 +0000 | [diff] [blame] | 1695 | |
Benjamin Peterson | f6489f9 | 2009-11-25 17:46:26 +0000 | [diff] [blame] | 1696 | def test_paren_evaluation(self): |
| 1697 | self.assertEqual(16 // (4 // 2), 8) |
| 1698 | self.assertEqual((16 // 4) // 2, 2) |
| 1699 | self.assertEqual(16 // 4 // 2, 2) |
Serhiy Storchaka | 3bcbedc | 2019-01-18 07:47:48 +0200 | [diff] [blame] | 1700 | x = 2 |
| 1701 | y = 3 |
| 1702 | self.assertTrue(False is (x is y)) |
| 1703 | self.assertFalse((False is x) is y) |
| 1704 | self.assertFalse(False is x is y) |
Benjamin Peterson | f6489f9 | 2009-11-25 17:46:26 +0000 | [diff] [blame] | 1705 | |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 1706 | def test_matrix_mul(self): |
| 1707 | # This is not intended to be a comprehensive test, rather just to be few |
| 1708 | # samples of the @ operator in test_grammar.py. |
| 1709 | class M: |
| 1710 | def __matmul__(self, o): |
| 1711 | return 4 |
| 1712 | def __imatmul__(self, o): |
| 1713 | self.other = o |
| 1714 | return self |
| 1715 | m = M() |
| 1716 | self.assertEqual(m @ m, 4) |
| 1717 | m @= 42 |
| 1718 | self.assertEqual(m.other, 42) |
| 1719 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1720 | def test_async_await(self): |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1721 | async def test(): |
| 1722 | def sum(): |
Yury Selivanov | 8fb307c | 2015-07-22 13:33:45 +0300 | [diff] [blame] | 1723 | pass |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1724 | if 1: |
| 1725 | await someobj() |
| 1726 | |
| 1727 | self.assertEqual(test.__name__, 'test') |
| 1728 | self.assertTrue(bool(test.__code__.co_flags & inspect.CO_COROUTINE)) |
| 1729 | |
| 1730 | def decorator(func): |
| 1731 | setattr(func, '_marked', True) |
| 1732 | return func |
| 1733 | |
| 1734 | @decorator |
| 1735 | async def test2(): |
| 1736 | return 22 |
| 1737 | self.assertTrue(test2._marked) |
| 1738 | self.assertEqual(test2.__name__, 'test2') |
| 1739 | self.assertTrue(bool(test2.__code__.co_flags & inspect.CO_COROUTINE)) |
| 1740 | |
| 1741 | def test_async_for(self): |
| 1742 | class Done(Exception): pass |
| 1743 | |
| 1744 | class AIter: |
Yury Selivanov | a6f6edb | 2016-06-09 15:08:31 -0400 | [diff] [blame] | 1745 | def __aiter__(self): |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1746 | return self |
| 1747 | async def __anext__(self): |
| 1748 | raise StopAsyncIteration |
| 1749 | |
| 1750 | async def foo(): |
| 1751 | async for i in AIter(): |
| 1752 | pass |
| 1753 | async for i, j in AIter(): |
| 1754 | pass |
| 1755 | async for i in AIter(): |
| 1756 | pass |
| 1757 | else: |
| 1758 | pass |
| 1759 | raise Done |
| 1760 | |
| 1761 | with self.assertRaises(Done): |
| 1762 | foo().send(None) |
| 1763 | |
| 1764 | def test_async_with(self): |
| 1765 | class Done(Exception): pass |
| 1766 | |
| 1767 | class manager: |
| 1768 | async def __aenter__(self): |
| 1769 | return (1, 2) |
| 1770 | async def __aexit__(self, *exc): |
| 1771 | return False |
| 1772 | |
| 1773 | async def foo(): |
| 1774 | async with manager(): |
| 1775 | pass |
| 1776 | async with manager() as x: |
| 1777 | pass |
| 1778 | async with manager() as (x, y): |
| 1779 | pass |
| 1780 | async with manager(), manager(): |
| 1781 | pass |
| 1782 | async with manager() as x, manager() as y: |
| 1783 | pass |
| 1784 | async with manager() as x, manager(): |
| 1785 | pass |
| 1786 | raise Done |
| 1787 | |
| 1788 | with self.assertRaises(Done): |
| 1789 | foo().send(None) |
| 1790 | |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 1791 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1792 | if __name__ == '__main__': |
Zachary Ware | 38c707e | 2015-04-13 15:00:43 -0500 | [diff] [blame] | 1793 | unittest.main() |