Antoine Pitrou | ab4a691 | 2014-05-23 11:46:03 +0200 | [diff] [blame] | 1 | import math |
Raymond Hettinger | 8a99b50 | 2003-06-23 13:36:57 +0000 | [diff] [blame] | 2 | import unittest |
Raymond Hettinger | 8a99b50 | 2003-06-23 13:36:57 +0000 | [diff] [blame] | 3 | import sys |
Georg Brandl | fc8eef3 | 2008-03-28 12:11:56 +0000 | [diff] [blame] | 4 | import _ast |
Raymond Hettinger | 8a99b50 | 2003-06-23 13:36:57 +0000 | [diff] [blame] | 5 | from test import test_support |
Florent Xicluna | af61719 | 2010-03-21 11:03:21 +0000 | [diff] [blame] | 6 | import textwrap |
Peter Schneider-Kamp | fdee0f0 | 2000-07-25 22:15:45 +0000 | [diff] [blame] | 7 | |
Raymond Hettinger | 8a99b50 | 2003-06-23 13:36:57 +0000 | [diff] [blame] | 8 | class TestSpecifics(unittest.TestCase): |
Jeremy Hylton | 778e265 | 2001-11-09 19:50:08 +0000 | [diff] [blame] | 9 | |
Benjamin Peterson | e36199b | 2009-11-12 23:39:44 +0000 | [diff] [blame] | 10 | def test_no_ending_newline(self): |
| 11 | compile("hi", "<test>", "exec") |
| 12 | compile("hi\r", "<test>", "exec") |
| 13 | |
| 14 | def test_empty(self): |
| 15 | compile("", "<test>", "exec") |
| 16 | |
| 17 | def test_other_newlines(self): |
| 18 | compile("\r\n", "<test>", "exec") |
| 19 | compile("\r", "<test>", "exec") |
| 20 | compile("hi\r\nstuff\r\ndef f():\n pass\r", "<test>", "exec") |
| 21 | compile("this_is\rreally_old_mac\rdef f():\n pass", "<test>", "exec") |
| 22 | |
Raymond Hettinger | 8a99b50 | 2003-06-23 13:36:57 +0000 | [diff] [blame] | 23 | def test_debug_assignment(self): |
| 24 | # catch assignments to __debug__ |
| 25 | self.assertRaises(SyntaxError, compile, '__debug__ = 1', '?', 'single') |
| 26 | import __builtin__ |
| 27 | prev = __builtin__.__debug__ |
| 28 | setattr(__builtin__, '__debug__', 'sure') |
| 29 | setattr(__builtin__, '__debug__', prev) |
Jeremy Hylton | 778e265 | 2001-11-09 19:50:08 +0000 | [diff] [blame] | 30 | |
Raymond Hettinger | 8a99b50 | 2003-06-23 13:36:57 +0000 | [diff] [blame] | 31 | def test_argument_handling(self): |
| 32 | # detect duplicate positional and keyword arguments |
| 33 | self.assertRaises(SyntaxError, eval, 'lambda a,a:0') |
| 34 | self.assertRaises(SyntaxError, eval, 'lambda a,a=1:0') |
| 35 | self.assertRaises(SyntaxError, eval, 'lambda a=1,a=1:0') |
| 36 | try: |
| 37 | exec 'def f(a, a): pass' |
| 38 | self.fail("duplicate arguments") |
| 39 | except SyntaxError: |
| 40 | pass |
| 41 | try: |
| 42 | exec 'def f(a = 0, a = 1): pass' |
| 43 | self.fail("duplicate keyword arguments") |
| 44 | except SyntaxError: |
| 45 | pass |
| 46 | try: |
| 47 | exec 'def f(a): global a; a = 1' |
| 48 | self.fail("variable is global and local") |
| 49 | except SyntaxError: |
| 50 | pass |
Jeremy Hylton | 778e265 | 2001-11-09 19:50:08 +0000 | [diff] [blame] | 51 | |
Raymond Hettinger | 8a99b50 | 2003-06-23 13:36:57 +0000 | [diff] [blame] | 52 | def test_syntax_error(self): |
| 53 | self.assertRaises(SyntaxError, compile, "1+*3", "filename", "exec") |
Peter Schneider-Kamp | fdee0f0 | 2000-07-25 22:15:45 +0000 | [diff] [blame] | 54 | |
Georg Brandl | e06cf45 | 2007-06-07 13:23:24 +0000 | [diff] [blame] | 55 | def test_none_keyword_arg(self): |
| 56 | self.assertRaises(SyntaxError, compile, "f(None=1)", "<string>", "exec") |
| 57 | |
Raymond Hettinger | 8a99b50 | 2003-06-23 13:36:57 +0000 | [diff] [blame] | 58 | def test_duplicate_global_local(self): |
| 59 | try: |
| 60 | exec 'def f(a): global a; a = 1' |
| 61 | self.fail("variable is global and local") |
| 62 | except SyntaxError: |
| 63 | pass |
Peter Schneider-Kamp | fdee0f0 | 2000-07-25 22:15:45 +0000 | [diff] [blame] | 64 | |
Mark Dickinson | 1658797 | 2012-11-25 13:25:57 +0000 | [diff] [blame] | 65 | def test_exec_functional_style(self): |
| 66 | # Exec'ing a tuple of length 2 works. |
| 67 | g = {'b': 2} |
| 68 | exec("a = b + 1", g) |
| 69 | self.assertEqual(g['a'], 3) |
| 70 | |
| 71 | # As does exec'ing a tuple of length 3. |
| 72 | l = {'b': 3} |
| 73 | g = {'b': 5, 'c': 7} |
| 74 | exec("a = b + c", g, l) |
| 75 | self.assertNotIn('a', g) |
| 76 | self.assertEqual(l['a'], 10) |
| 77 | |
| 78 | # Tuples not of length 2 or 3 are invalid. |
| 79 | with self.assertRaises(TypeError): |
| 80 | exec("a = b + 1",) |
| 81 | |
| 82 | with self.assertRaises(TypeError): |
| 83 | exec("a = b + 1", {}, {}, {}) |
| 84 | |
| 85 | # Can't mix and match the two calling forms. |
| 86 | g = {'a': 3, 'b': 4} |
| 87 | l = {} |
| 88 | with self.assertRaises(TypeError): |
| 89 | exec("a = b + 1", g) in g |
| 90 | with self.assertRaises(TypeError): |
| 91 | exec("a = b + 1", g, l) in g, l |
| 92 | |
Robert Jordens | af09c77 | 2014-07-29 17:24:24 +0200 | [diff] [blame] | 93 | def test_nested_qualified_exec(self): |
| 94 | # Can use qualified exec in nested functions. |
| 95 | code = [""" |
| 96 | def g(): |
| 97 | def f(): |
| 98 | if True: |
| 99 | exec "" in {}, {} |
| 100 | """, """ |
| 101 | def g(): |
| 102 | def f(): |
| 103 | if True: |
| 104 | exec("", {}, {}) |
Benjamin Peterson | 4f09e61 | 2014-08-09 19:39:50 -0700 | [diff] [blame] | 105 | """, """ |
| 106 | def g(): |
| 107 | def f(): |
| 108 | if True: |
| 109 | exec("", {}) |
Robert Jordens | af09c77 | 2014-07-29 17:24:24 +0200 | [diff] [blame] | 110 | """] |
| 111 | for c in code: |
| 112 | compile(c, "<code>", "exec") |
| 113 | |
Raymond Hettinger | 66bd233 | 2004-08-02 08:30:07 +0000 | [diff] [blame] | 114 | def test_exec_with_general_mapping_for_locals(self): |
| 115 | |
| 116 | class M: |
| 117 | "Test mapping interface versus possible calls from eval()." |
| 118 | def __getitem__(self, key): |
| 119 | if key == 'a': |
| 120 | return 12 |
| 121 | raise KeyError |
| 122 | def __setitem__(self, key, value): |
| 123 | self.results = (key, value) |
| 124 | def keys(self): |
| 125 | return list('xyz') |
| 126 | |
| 127 | m = M() |
| 128 | g = globals() |
| 129 | exec 'z = a' in g, m |
| 130 | self.assertEqual(m.results, ('z', 12)) |
| 131 | try: |
| 132 | exec 'z = b' in g, m |
| 133 | except NameError: |
| 134 | pass |
| 135 | else: |
| 136 | self.fail('Did not detect a KeyError') |
| 137 | exec 'z = dir()' in g, m |
| 138 | self.assertEqual(m.results, ('z', list('xyz'))) |
| 139 | exec 'z = globals()' in g, m |
| 140 | self.assertEqual(m.results, ('z', g)) |
| 141 | exec 'z = locals()' in g, m |
| 142 | self.assertEqual(m.results, ('z', m)) |
| 143 | try: |
| 144 | exec 'z = b' in m |
| 145 | except TypeError: |
| 146 | pass |
| 147 | else: |
| 148 | self.fail('Did not validate globals as a real dict') |
| 149 | |
| 150 | class A: |
| 151 | "Non-mapping" |
| 152 | pass |
| 153 | m = A() |
| 154 | try: |
| 155 | exec 'z = a' in g, m |
| 156 | except TypeError: |
| 157 | pass |
| 158 | else: |
| 159 | self.fail('Did not validate locals as a mapping') |
| 160 | |
| 161 | # Verify that dict subclasses work as well |
| 162 | class D(dict): |
| 163 | def __getitem__(self, key): |
| 164 | if key == 'a': |
| 165 | return 12 |
| 166 | return dict.__getitem__(self, key) |
| 167 | d = D() |
| 168 | exec 'z = a' in g, d |
| 169 | self.assertEqual(d['z'], 12) |
| 170 | |
Neal Norwitz | 6ab080c | 2005-10-24 00:08:10 +0000 | [diff] [blame] | 171 | def test_extended_arg(self): |
| 172 | longexpr = 'x = x or ' + '-x' * 2500 |
| 173 | code = ''' |
| 174 | def f(x): |
| 175 | %s |
| 176 | %s |
| 177 | %s |
| 178 | %s |
| 179 | %s |
| 180 | %s |
| 181 | %s |
| 182 | %s |
| 183 | %s |
| 184 | %s |
| 185 | # the expressions above have no effect, x == argument |
| 186 | while x: |
| 187 | x -= 1 |
| 188 | # EXTENDED_ARG/JUMP_ABSOLUTE here |
| 189 | return x |
| 190 | ''' % ((longexpr,)*10) |
| 191 | exec code |
| 192 | self.assertEqual(f(5), 0) |
| 193 | |
Raymond Hettinger | 8a99b50 | 2003-06-23 13:36:57 +0000 | [diff] [blame] | 194 | def test_complex_args(self): |
Senthil Kumaran | ce8e33a | 2010-01-08 19:04:16 +0000 | [diff] [blame] | 195 | |
Florent Xicluna | af61719 | 2010-03-21 11:03:21 +0000 | [diff] [blame] | 196 | with test_support.check_py3k_warnings( |
| 197 | ("tuple parameter unpacking has been removed", SyntaxWarning)): |
| 198 | exec textwrap.dedent(''' |
Raymond Hettinger | 8a99b50 | 2003-06-23 13:36:57 +0000 | [diff] [blame] | 199 | def comp_args((a, b)): |
| 200 | return a,b |
| 201 | self.assertEqual(comp_args((1, 2)), (1, 2)) |
Thomas Heller | 6b17abf | 2002-07-09 09:23:27 +0000 | [diff] [blame] | 202 | |
Raymond Hettinger | 8a99b50 | 2003-06-23 13:36:57 +0000 | [diff] [blame] | 203 | def comp_args((a, b)=(3, 4)): |
| 204 | return a, b |
| 205 | self.assertEqual(comp_args((1, 2)), (1, 2)) |
| 206 | self.assertEqual(comp_args(), (3, 4)) |
Jeremy Hylton | 047e2c9 | 2001-01-19 03:25:56 +0000 | [diff] [blame] | 207 | |
Raymond Hettinger | 8a99b50 | 2003-06-23 13:36:57 +0000 | [diff] [blame] | 208 | def comp_args(a, (b, c)): |
| 209 | return a, b, c |
| 210 | self.assertEqual(comp_args(1, (2, 3)), (1, 2, 3)) |
Jeremy Hylton | 121b6eb | 2001-02-19 23:53:42 +0000 | [diff] [blame] | 211 | |
Raymond Hettinger | 8a99b50 | 2003-06-23 13:36:57 +0000 | [diff] [blame] | 212 | def comp_args(a=2, (b, c)=(3, 4)): |
| 213 | return a, b, c |
| 214 | self.assertEqual(comp_args(1, (2, 3)), (1, 2, 3)) |
| 215 | self.assertEqual(comp_args(), (2, 3, 4)) |
Florent Xicluna | af61719 | 2010-03-21 11:03:21 +0000 | [diff] [blame] | 216 | ''') |
Jeremy Hylton | 121b6eb | 2001-02-19 23:53:42 +0000 | [diff] [blame] | 217 | |
Raymond Hettinger | 8a99b50 | 2003-06-23 13:36:57 +0000 | [diff] [blame] | 218 | def test_argument_order(self): |
| 219 | try: |
| 220 | exec 'def f(a=1, (b, c)): pass' |
| 221 | self.fail("non-default args after default") |
| 222 | except SyntaxError: |
| 223 | pass |
Jeremy Hylton | 121b6eb | 2001-02-19 23:53:42 +0000 | [diff] [blame] | 224 | |
Raymond Hettinger | 8a99b50 | 2003-06-23 13:36:57 +0000 | [diff] [blame] | 225 | def test_float_literals(self): |
| 226 | # testing bad float literals |
| 227 | self.assertRaises(SyntaxError, eval, "2e") |
| 228 | self.assertRaises(SyntaxError, eval, "2.0e+") |
| 229 | self.assertRaises(SyntaxError, eval, "1e-") |
| 230 | self.assertRaises(SyntaxError, eval, "3-4e/21") |
Jeremy Hylton | 121b6eb | 2001-02-19 23:53:42 +0000 | [diff] [blame] | 231 | |
Raymond Hettinger | 8a99b50 | 2003-06-23 13:36:57 +0000 | [diff] [blame] | 232 | def test_indentation(self): |
| 233 | # testing compile() of indented block w/o trailing newline" |
| 234 | s = """ |
Guido van Rossum | 4b499dd3 | 2003-02-13 22:07:59 +0000 | [diff] [blame] | 235 | if 1: |
| 236 | if 2: |
| 237 | pass""" |
Raymond Hettinger | 8a99b50 | 2003-06-23 13:36:57 +0000 | [diff] [blame] | 238 | compile(s, "<string>", "exec") |
| 239 | |
Neal Norwitz | ed65755 | 2006-07-10 00:04:44 +0000 | [diff] [blame] | 240 | # This test is probably specific to CPython and may not generalize |
| 241 | # to other implementations. We are trying to ensure that when |
| 242 | # the first line of code starts after 256, correct line numbers |
| 243 | # in tracebacks are still produced. |
| 244 | def test_leading_newlines(self): |
| 245 | s256 = "".join(["\n"] * 256 + ["spam"]) |
| 246 | co = compile(s256, 'fn', 'exec') |
| 247 | self.assertEqual(co.co_firstlineno, 257) |
| 248 | self.assertEqual(co.co_lnotab, '') |
| 249 | |
Raymond Hettinger | 8a99b50 | 2003-06-23 13:36:57 +0000 | [diff] [blame] | 250 | def test_literals_with_leading_zeroes(self): |
| 251 | for arg in ["077787", "0xj", "0x.", "0e", "090000000000000", |
Eric Smith | 9ff19b5 | 2008-03-17 17:32:20 +0000 | [diff] [blame] | 252 | "080000000000000", "000000000000009", "000000000000008", |
| 253 | "0b42", "0BADCAFE", "0o123456789", "0b1.1", "0o4.2", |
Amaury Forgeot d'Arc | 5216721 | 2008-04-24 18:07:05 +0000 | [diff] [blame] | 254 | "0b101j2", "0o153j2", "0b100e1", "0o777e1", "0o8", "0o78"]: |
Raymond Hettinger | 8a99b50 | 2003-06-23 13:36:57 +0000 | [diff] [blame] | 255 | self.assertRaises(SyntaxError, eval, arg) |
| 256 | |
| 257 | self.assertEqual(eval("0777"), 511) |
| 258 | self.assertEqual(eval("0777L"), 511) |
| 259 | self.assertEqual(eval("000777"), 511) |
| 260 | self.assertEqual(eval("0xff"), 255) |
| 261 | self.assertEqual(eval("0xffL"), 255) |
| 262 | self.assertEqual(eval("0XfF"), 255) |
| 263 | self.assertEqual(eval("0777."), 777) |
| 264 | self.assertEqual(eval("0777.0"), 777) |
| 265 | self.assertEqual(eval("000000000000000000000000000000000000000000000000000777e0"), 777) |
| 266 | self.assertEqual(eval("0777e1"), 7770) |
| 267 | self.assertEqual(eval("0e0"), 0) |
| 268 | self.assertEqual(eval("0000E-012"), 0) |
| 269 | self.assertEqual(eval("09.5"), 9.5) |
| 270 | self.assertEqual(eval("0777j"), 777j) |
| 271 | self.assertEqual(eval("00j"), 0j) |
| 272 | self.assertEqual(eval("00.0"), 0) |
| 273 | self.assertEqual(eval("0e3"), 0) |
| 274 | self.assertEqual(eval("090000000000000."), 90000000000000.) |
| 275 | self.assertEqual(eval("090000000000000.0000000000000000000000"), 90000000000000.) |
| 276 | self.assertEqual(eval("090000000000000e0"), 90000000000000.) |
| 277 | self.assertEqual(eval("090000000000000e-0"), 90000000000000.) |
| 278 | self.assertEqual(eval("090000000000000j"), 90000000000000j) |
| 279 | self.assertEqual(eval("000000000000007"), 7) |
| 280 | self.assertEqual(eval("000000000000008."), 8.) |
| 281 | self.assertEqual(eval("000000000000009."), 9.) |
Eric Smith | 9ff19b5 | 2008-03-17 17:32:20 +0000 | [diff] [blame] | 282 | self.assertEqual(eval("0b101010"), 42) |
| 283 | self.assertEqual(eval("-0b000000000010"), -2) |
| 284 | self.assertEqual(eval("0o777"), 511) |
| 285 | self.assertEqual(eval("-0o0000010"), -8) |
Mark Dickinson | 64b7e50 | 2008-07-16 09:40:03 +0000 | [diff] [blame] | 286 | self.assertEqual(eval("020000000000.0"), 20000000000.0) |
| 287 | self.assertEqual(eval("037777777777e0"), 37777777777.0) |
| 288 | self.assertEqual(eval("01000000000000000000000.0"), |
| 289 | 1000000000000000000000.0) |
Raymond Hettinger | 8a99b50 | 2003-06-23 13:36:57 +0000 | [diff] [blame] | 290 | |
| 291 | def test_unary_minus(self): |
| 292 | # Verify treatment of unary minus on negative numbers SF bug #660455 |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 293 | if sys.maxint == 2147483647: |
| 294 | # 32-bit machine |
| 295 | all_one_bits = '0xffffffff' |
| 296 | self.assertEqual(eval(all_one_bits), 4294967295L) |
| 297 | self.assertEqual(eval("-" + all_one_bits), -4294967295L) |
| 298 | elif sys.maxint == 9223372036854775807: |
| 299 | # 64-bit machine |
Raymond Hettinger | 8a99b50 | 2003-06-23 13:36:57 +0000 | [diff] [blame] | 300 | all_one_bits = '0xffffffffffffffff' |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 301 | self.assertEqual(eval(all_one_bits), 18446744073709551615L) |
| 302 | self.assertEqual(eval("-" + all_one_bits), -18446744073709551615L) |
| 303 | else: |
| 304 | self.fail("How many bits *does* this machine have???") |
Ezio Melotti | 24b07bc | 2011-03-15 18:55:01 +0200 | [diff] [blame] | 305 | # Verify treatment of constant folding on -(sys.maxint+1) |
Neil Schemenauer | 6ec6ab0 | 2006-07-09 21:19:29 +0000 | [diff] [blame] | 306 | # i.e. -2147483648 on 32 bit platforms. Should return int, not long. |
Ezio Melotti | b0f5adc | 2010-01-24 16:58:36 +0000 | [diff] [blame] | 307 | self.assertIsInstance(eval("%s" % (-sys.maxint - 1)), int) |
| 308 | self.assertIsInstance(eval("%s" % (-sys.maxint - 2)), long) |
Raymond Hettinger | 8a99b50 | 2003-06-23 13:36:57 +0000 | [diff] [blame] | 309 | |
Neal Norwitz | 28746ab | 2006-07-09 22:14:42 +0000 | [diff] [blame] | 310 | if sys.maxint == 9223372036854775807: |
| 311 | def test_32_63_bit_values(self): |
| 312 | a = +4294967296 # 1 << 32 |
| 313 | b = -4294967296 # 1 << 32 |
| 314 | c = +281474976710656 # 1 << 48 |
| 315 | d = -281474976710656 # 1 << 48 |
| 316 | e = +4611686018427387904 # 1 << 62 |
| 317 | f = -4611686018427387904 # 1 << 62 |
| 318 | g = +9223372036854775807 # 1 << 63 - 1 |
| 319 | h = -9223372036854775807 # 1 << 63 - 1 |
| 320 | |
| 321 | for variable in self.test_32_63_bit_values.func_code.co_consts: |
| 322 | if variable is not None: |
Ezio Melotti | b0f5adc | 2010-01-24 16:58:36 +0000 | [diff] [blame] | 323 | self.assertIsInstance(variable, int) |
Neal Norwitz | 28746ab | 2006-07-09 22:14:42 +0000 | [diff] [blame] | 324 | |
Raymond Hettinger | 8a99b50 | 2003-06-23 13:36:57 +0000 | [diff] [blame] | 325 | def test_sequence_unpacking_error(self): |
| 326 | # Verify sequence packing/unpacking with "or". SF bug #757818 |
| 327 | i,j = (1, -1) or (-1, 1) |
| 328 | self.assertEqual(i, 1) |
| 329 | self.assertEqual(j, -1) |
Guido van Rossum | 4b499dd3 | 2003-02-13 22:07:59 +0000 | [diff] [blame] | 330 | |
Raymond Hettinger | 11a70c7 | 2004-07-17 21:46:25 +0000 | [diff] [blame] | 331 | def test_none_assignment(self): |
| 332 | stmts = [ |
| 333 | 'None = 0', |
| 334 | 'None += 0', |
| 335 | '__builtins__.None = 0', |
| 336 | 'def None(): pass', |
| 337 | 'class None: pass', |
| 338 | '(a, None) = 0, 0', |
| 339 | 'for None in range(10): pass', |
| 340 | 'def f(None): pass', |
Benjamin Peterson | 565e1b6 | 2009-06-13 03:46:30 +0000 | [diff] [blame] | 341 | 'import None', |
| 342 | 'import x as None', |
| 343 | 'from x import None', |
| 344 | 'from x import y as None' |
Raymond Hettinger | 11a70c7 | 2004-07-17 21:46:25 +0000 | [diff] [blame] | 345 | ] |
| 346 | for stmt in stmts: |
| 347 | stmt += "\n" |
| 348 | self.assertRaises(SyntaxError, compile, stmt, 'tmp', 'single') |
| 349 | self.assertRaises(SyntaxError, compile, stmt, 'tmp', 'exec') |
Benjamin Peterson | 565e1b6 | 2009-06-13 03:46:30 +0000 | [diff] [blame] | 350 | # This is ok. |
| 351 | compile("from None import x", "tmp", "exec") |
Benjamin Peterson | d1f5a59 | 2009-06-13 13:06:21 +0000 | [diff] [blame] | 352 | compile("from x import None as y", "tmp", "exec") |
| 353 | compile("import None as x", "tmp", "exec") |
Tim Peters | d507dab | 2001-08-30 20:51:59 +0000 | [diff] [blame] | 354 | |
Anthony Baxter | 1a4ddae | 2004-08-31 10:07:13 +0000 | [diff] [blame] | 355 | def test_import(self): |
| 356 | succeed = [ |
| 357 | 'import sys', |
| 358 | 'import os, sys', |
Neal Norwitz | fb48afa | 2006-07-08 05:31:37 +0000 | [diff] [blame] | 359 | 'import os as bar', |
| 360 | 'import os.path as bar', |
Anthony Baxter | 1a4ddae | 2004-08-31 10:07:13 +0000 | [diff] [blame] | 361 | 'from __future__ import nested_scopes, generators', |
| 362 | 'from __future__ import (nested_scopes,\ngenerators)', |
| 363 | 'from __future__ import (nested_scopes,\ngenerators,)', |
| 364 | 'from sys import stdin, stderr, stdout', |
| 365 | 'from sys import (stdin, stderr,\nstdout)', |
| 366 | 'from sys import (stdin, stderr,\nstdout,)', |
| 367 | 'from sys import (stdin\n, stderr, stdout)', |
| 368 | 'from sys import (stdin\n, stderr, stdout,)', |
| 369 | 'from sys import stdin as si, stdout as so, stderr as se', |
| 370 | 'from sys import (stdin as si, stdout as so, stderr as se)', |
| 371 | 'from sys import (stdin as si, stdout as so, stderr as se,)', |
| 372 | ] |
| 373 | fail = [ |
| 374 | 'import (os, sys)', |
| 375 | 'import (os), (sys)', |
| 376 | 'import ((os), (sys))', |
| 377 | 'import (sys', |
| 378 | 'import sys)', |
| 379 | 'import (os,)', |
Neal Norwitz | fb48afa | 2006-07-08 05:31:37 +0000 | [diff] [blame] | 380 | 'import os As bar', |
| 381 | 'import os.path a bar', |
Georg Brandl | 9575fb2 | 2006-07-08 12:15:27 +0000 | [diff] [blame] | 382 | 'from sys import stdin As stdout', |
| 383 | 'from sys import stdin a stdout', |
Anthony Baxter | 1a4ddae | 2004-08-31 10:07:13 +0000 | [diff] [blame] | 384 | 'from (sys) import stdin', |
| 385 | 'from __future__ import (nested_scopes', |
| 386 | 'from __future__ import nested_scopes)', |
| 387 | 'from __future__ import nested_scopes,\ngenerators', |
| 388 | 'from sys import (stdin', |
| 389 | 'from sys import stdin)', |
| 390 | 'from sys import stdin, stdout,\nstderr', |
| 391 | 'from sys import stdin si', |
| 392 | 'from sys import stdin,' |
| 393 | 'from sys import (*)', |
| 394 | 'from sys import (stdin,, stdout, stderr)', |
| 395 | 'from sys import (stdin, stdout),', |
| 396 | ] |
| 397 | for stmt in succeed: |
| 398 | compile(stmt, 'tmp', 'exec') |
| 399 | for stmt in fail: |
| 400 | self.assertRaises(SyntaxError, compile, stmt, 'tmp', 'exec') |
| 401 | |
Raymond Hettinger | 9047c8f | 2004-10-24 00:10:06 +0000 | [diff] [blame] | 402 | def test_for_distinct_code_objects(self): |
| 403 | # SF bug 1048870 |
| 404 | def f(): |
| 405 | f1 = lambda x=1: x |
| 406 | f2 = lambda x=2: x |
| 407 | return f1, f2 |
| 408 | f1, f2 = f() |
| 409 | self.assertNotEqual(id(f1.func_code), id(f2.func_code)) |
| 410 | |
Benjamin Peterson | 0dee9c1 | 2010-03-17 20:41:42 +0000 | [diff] [blame] | 411 | def test_lambda_doc(self): |
| 412 | l = lambda: "foo" |
| 413 | self.assertIsNone(l.__doc__) |
| 414 | |
Serhiy Storchaka | 3eb554f | 2014-09-05 10:22:05 +0300 | [diff] [blame] | 415 | @test_support.requires_unicode |
| 416 | def test_encoding(self): |
| 417 | code = b'# -*- coding: badencoding -*-\npass\n' |
| 418 | self.assertRaises(SyntaxError, compile, code, 'tmp', 'exec') |
Neal Norwitz | e98ccf6 | 2006-03-23 05:39:47 +0000 | [diff] [blame] | 419 | code = u"# -*- coding: utf-8 -*-\npass\n" |
| 420 | self.assertRaises(SyntaxError, compile, code, "tmp", "exec") |
Serhiy Storchaka | 3eb554f | 2014-09-05 10:22:05 +0300 | [diff] [blame] | 421 | code = 'u"\xc2\xa4"\n' |
| 422 | self.assertEqual(eval(code), u'\xc2\xa4') |
| 423 | code = u'u"\xc2\xa4"\n' |
| 424 | self.assertEqual(eval(code), u'\xc2\xa4') |
| 425 | code = '# -*- coding: latin1 -*-\nu"\xc2\xa4"\n' |
| 426 | self.assertEqual(eval(code), u'\xc2\xa4') |
| 427 | code = '# -*- coding: utf-8 -*-\nu"\xc2\xa4"\n' |
| 428 | self.assertEqual(eval(code), u'\xa4') |
| 429 | code = '# -*- coding: iso8859-15 -*-\nu"\xc2\xa4"\n' |
| 430 | self.assertEqual(eval(code), test_support.u(r'\xc2\u20ac')) |
| 431 | code = 'u"""\\\n# -*- coding: utf-8 -*-\n\xc2\xa4"""\n' |
| 432 | self.assertEqual(eval(code), u'# -*- coding: utf-8 -*-\n\xc2\xa4') |
Neal Norwitz | e98ccf6 | 2006-03-23 05:39:47 +0000 | [diff] [blame] | 433 | |
Nick Coghlan | eadee9a | 2006-03-13 12:31:58 +0000 | [diff] [blame] | 434 | def test_subscripts(self): |
| 435 | # SF bug 1448804 |
| 436 | # Class to make testing subscript results easy |
| 437 | class str_map(object): |
| 438 | def __init__(self): |
| 439 | self.data = {} |
| 440 | def __getitem__(self, key): |
| 441 | return self.data[str(key)] |
| 442 | def __setitem__(self, key, value): |
| 443 | self.data[str(key)] = value |
| 444 | def __delitem__(self, key): |
| 445 | del self.data[str(key)] |
| 446 | def __contains__(self, key): |
| 447 | return str(key) in self.data |
| 448 | d = str_map() |
| 449 | # Index |
| 450 | d[1] = 1 |
| 451 | self.assertEqual(d[1], 1) |
| 452 | d[1] += 1 |
| 453 | self.assertEqual(d[1], 2) |
| 454 | del d[1] |
Ezio Melotti | aa98058 | 2010-01-23 23:04:36 +0000 | [diff] [blame] | 455 | self.assertNotIn(1, d) |
Nick Coghlan | eadee9a | 2006-03-13 12:31:58 +0000 | [diff] [blame] | 456 | # Tuple of indices |
| 457 | d[1, 1] = 1 |
| 458 | self.assertEqual(d[1, 1], 1) |
| 459 | d[1, 1] += 1 |
| 460 | self.assertEqual(d[1, 1], 2) |
| 461 | del d[1, 1] |
Ezio Melotti | aa98058 | 2010-01-23 23:04:36 +0000 | [diff] [blame] | 462 | self.assertNotIn((1, 1), d) |
Nick Coghlan | eadee9a | 2006-03-13 12:31:58 +0000 | [diff] [blame] | 463 | # Simple slice |
| 464 | d[1:2] = 1 |
| 465 | self.assertEqual(d[1:2], 1) |
| 466 | d[1:2] += 1 |
| 467 | self.assertEqual(d[1:2], 2) |
| 468 | del d[1:2] |
Ezio Melotti | aa98058 | 2010-01-23 23:04:36 +0000 | [diff] [blame] | 469 | self.assertNotIn(slice(1, 2), d) |
Nick Coghlan | eadee9a | 2006-03-13 12:31:58 +0000 | [diff] [blame] | 470 | # Tuple of simple slices |
| 471 | d[1:2, 1:2] = 1 |
| 472 | self.assertEqual(d[1:2, 1:2], 1) |
| 473 | d[1:2, 1:2] += 1 |
| 474 | self.assertEqual(d[1:2, 1:2], 2) |
| 475 | del d[1:2, 1:2] |
Ezio Melotti | aa98058 | 2010-01-23 23:04:36 +0000 | [diff] [blame] | 476 | self.assertNotIn((slice(1, 2), slice(1, 2)), d) |
Nick Coghlan | eadee9a | 2006-03-13 12:31:58 +0000 | [diff] [blame] | 477 | # Extended slice |
| 478 | d[1:2:3] = 1 |
| 479 | self.assertEqual(d[1:2:3], 1) |
| 480 | d[1:2:3] += 1 |
| 481 | self.assertEqual(d[1:2:3], 2) |
| 482 | del d[1:2:3] |
Ezio Melotti | aa98058 | 2010-01-23 23:04:36 +0000 | [diff] [blame] | 483 | self.assertNotIn(slice(1, 2, 3), d) |
Nick Coghlan | eadee9a | 2006-03-13 12:31:58 +0000 | [diff] [blame] | 484 | # Tuple of extended slices |
| 485 | d[1:2:3, 1:2:3] = 1 |
| 486 | self.assertEqual(d[1:2:3, 1:2:3], 1) |
| 487 | d[1:2:3, 1:2:3] += 1 |
| 488 | self.assertEqual(d[1:2:3, 1:2:3], 2) |
| 489 | del d[1:2:3, 1:2:3] |
Ezio Melotti | aa98058 | 2010-01-23 23:04:36 +0000 | [diff] [blame] | 490 | self.assertNotIn((slice(1, 2, 3), slice(1, 2, 3)), d) |
Nick Coghlan | eadee9a | 2006-03-13 12:31:58 +0000 | [diff] [blame] | 491 | # Ellipsis |
| 492 | d[...] = 1 |
| 493 | self.assertEqual(d[...], 1) |
| 494 | d[...] += 1 |
| 495 | self.assertEqual(d[...], 2) |
| 496 | del d[...] |
Ezio Melotti | aa98058 | 2010-01-23 23:04:36 +0000 | [diff] [blame] | 497 | self.assertNotIn(Ellipsis, d) |
Nick Coghlan | eadee9a | 2006-03-13 12:31:58 +0000 | [diff] [blame] | 498 | # Tuple of Ellipses |
| 499 | d[..., ...] = 1 |
| 500 | self.assertEqual(d[..., ...], 1) |
| 501 | d[..., ...] += 1 |
| 502 | self.assertEqual(d[..., ...], 2) |
| 503 | del d[..., ...] |
Ezio Melotti | aa98058 | 2010-01-23 23:04:36 +0000 | [diff] [blame] | 504 | self.assertNotIn((Ellipsis, Ellipsis), d) |
Nick Coghlan | eadee9a | 2006-03-13 12:31:58 +0000 | [diff] [blame] | 505 | |
Jeremy Hylton | 37075c5 | 2007-02-27 01:01:59 +0000 | [diff] [blame] | 506 | def test_mangling(self): |
| 507 | class A: |
| 508 | def f(): |
| 509 | __mangled = 1 |
| 510 | __not_mangled__ = 2 |
| 511 | import __mangled_mod |
| 512 | import __package__.module |
| 513 | |
Ezio Melotti | aa98058 | 2010-01-23 23:04:36 +0000 | [diff] [blame] | 514 | self.assertIn("_A__mangled", A.f.func_code.co_varnames) |
| 515 | self.assertIn("__not_mangled__", A.f.func_code.co_varnames) |
| 516 | self.assertIn("_A__mangled_mod", A.f.func_code.co_varnames) |
| 517 | self.assertIn("__package__", A.f.func_code.co_varnames) |
Jeremy Hylton | 37075c5 | 2007-02-27 01:01:59 +0000 | [diff] [blame] | 518 | |
Georg Brandl | fc8eef3 | 2008-03-28 12:11:56 +0000 | [diff] [blame] | 519 | def test_compile_ast(self): |
| 520 | fname = __file__ |
| 521 | if fname.lower().endswith(('pyc', 'pyo')): |
| 522 | fname = fname[:-1] |
| 523 | with open(fname, 'r') as f: |
| 524 | fcontents = f.read() |
| 525 | sample_code = [ |
| 526 | ['<assign>', 'x = 5'], |
| 527 | ['<print1>', 'print 1'], |
| 528 | ['<printv>', 'print v'], |
| 529 | ['<printTrue>', 'print True'], |
| 530 | ['<printList>', 'print []'], |
| 531 | ['<ifblock>', """if True:\n pass\n"""], |
| 532 | ['<forblock>', """for n in [1, 2, 3]:\n print n\n"""], |
| 533 | ['<deffunc>', """def foo():\n pass\nfoo()\n"""], |
| 534 | [fname, fcontents], |
| 535 | ] |
| 536 | |
| 537 | for fname, code in sample_code: |
| 538 | co1 = compile(code, '%s1' % fname, 'exec') |
| 539 | ast = compile(code, '%s2' % fname, 'exec', _ast.PyCF_ONLY_AST) |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 540 | self.assertTrue(type(ast) == _ast.Module) |
Georg Brandl | fc8eef3 | 2008-03-28 12:11:56 +0000 | [diff] [blame] | 541 | co2 = compile(ast, '%s3' % fname, 'exec') |
| 542 | self.assertEqual(co1, co2) |
Georg Brandl | f2bfd54 | 2008-03-29 13:24:23 +0000 | [diff] [blame] | 543 | # the code object's filename comes from the second compilation step |
| 544 | self.assertEqual(co2.co_filename, '%s3' % fname) |
| 545 | |
| 546 | # raise exception when node type doesn't match with compile mode |
| 547 | co1 = compile('print 1', '<string>', 'exec', _ast.PyCF_ONLY_AST) |
| 548 | self.assertRaises(TypeError, compile, co1, '<ast>', 'eval') |
| 549 | |
| 550 | # raise exception when node type is no start node |
| 551 | self.assertRaises(TypeError, compile, _ast.If(), '<ast>', 'exec') |
| 552 | |
| 553 | # raise exception when node has invalid children |
| 554 | ast = _ast.Module() |
| 555 | ast.body = [_ast.BoolOp()] |
| 556 | self.assertRaises(TypeError, compile, ast, '<ast>', 'exec') |
Georg Brandl | fc8eef3 | 2008-03-28 12:11:56 +0000 | [diff] [blame] | 557 | |
| 558 | |
Antoine Pitrou | ab4a691 | 2014-05-23 11:46:03 +0200 | [diff] [blame] | 559 | class TestStackSize(unittest.TestCase): |
| 560 | # These tests check that the computed stack size for a code object |
| 561 | # stays within reasonable bounds (see issue #21523 for an example |
| 562 | # dysfunction). |
| 563 | N = 100 |
| 564 | |
| 565 | def check_stack_size(self, code): |
| 566 | # To assert that the alleged stack size is not O(N), we |
| 567 | # check that it is smaller than log(N). |
| 568 | if isinstance(code, str): |
| 569 | code = compile(code, "<foo>", "single") |
| 570 | max_size = math.ceil(math.log(len(code.co_code))) |
| 571 | self.assertLessEqual(code.co_stacksize, max_size) |
| 572 | |
| 573 | def test_and(self): |
| 574 | self.check_stack_size("x and " * self.N + "x") |
| 575 | |
| 576 | def test_or(self): |
| 577 | self.check_stack_size("x or " * self.N + "x") |
| 578 | |
| 579 | def test_and_or(self): |
| 580 | self.check_stack_size("x and x or " * self.N + "x") |
| 581 | |
| 582 | def test_chained_comparison(self): |
| 583 | self.check_stack_size("x < " * self.N + "x") |
| 584 | |
| 585 | def test_if_else(self): |
| 586 | self.check_stack_size("x if x else " * self.N + "x") |
| 587 | |
| 588 | def test_binop(self): |
| 589 | self.check_stack_size("x + " * self.N + "x") |
| 590 | |
| 591 | def test_func_and(self): |
| 592 | code = "def f(x):\n" |
| 593 | code += " x and x\n" * self.N |
| 594 | self.check_stack_size(code) |
| 595 | |
| 596 | |
Raymond Hettinger | 8a99b50 | 2003-06-23 13:36:57 +0000 | [diff] [blame] | 597 | def test_main(): |
Antoine Pitrou | ab4a691 | 2014-05-23 11:46:03 +0200 | [diff] [blame] | 598 | test_support.run_unittest(__name__) |
Tim Peters | d507dab | 2001-08-30 20:51:59 +0000 | [diff] [blame] | 599 | |
Raymond Hettinger | 8a99b50 | 2003-06-23 13:36:57 +0000 | [diff] [blame] | 600 | if __name__ == "__main__": |
Antoine Pitrou | ab4a691 | 2014-05-23 11:46:03 +0200 | [diff] [blame] | 601 | unittest.main() |