Antoine Pitrou | 9961405 | 2014-05-23 11:46:03 +0200 | [diff] [blame] | 1 | import math |
Benjamin Peterson | d73aca7 | 2015-04-21 12:05:19 -0400 | [diff] [blame] | 2 | import os |
Raymond Hettinger | 8a99b50 | 2003-06-23 13:36:57 +0000 | [diff] [blame] | 3 | import unittest |
Raymond Hettinger | 8a99b50 | 2003-06-23 13:36:57 +0000 | [diff] [blame] | 4 | import sys |
Martin v. Löwis | 618dc5e | 2008-03-30 20:03:44 +0000 | [diff] [blame] | 5 | import _ast |
Benjamin Peterson | d73aca7 | 2015-04-21 12:05:19 -0400 | [diff] [blame] | 6 | import tempfile |
Benjamin Peterson | 43b0686 | 2011-05-27 09:08:01 -0500 | [diff] [blame] | 7 | import types |
Berker Peksag | 076dbd0 | 2015-05-06 07:01:52 +0300 | [diff] [blame] | 8 | from test import support |
| 9 | from test.support import script_helper |
Peter Schneider-Kamp | fdee0f0 | 2000-07-25 22:15:45 +0000 | [diff] [blame] | 10 | |
Raymond Hettinger | 8a99b50 | 2003-06-23 13:36:57 +0000 | [diff] [blame] | 11 | class TestSpecifics(unittest.TestCase): |
Jeremy Hylton | 778e265 | 2001-11-09 19:50:08 +0000 | [diff] [blame] | 12 | |
Meador Inge | fa21bf0 | 2012-01-19 01:08:41 -0600 | [diff] [blame] | 13 | def compile_single(self, source): |
| 14 | compile(source, "<single>", "single") |
| 15 | |
| 16 | def assertInvalidSingle(self, source): |
| 17 | self.assertRaises(SyntaxError, self.compile_single, source) |
| 18 | |
Benjamin Peterson | aeaa592 | 2009-11-13 00:17:59 +0000 | [diff] [blame] | 19 | def test_no_ending_newline(self): |
| 20 | compile("hi", "<test>", "exec") |
| 21 | compile("hi\r", "<test>", "exec") |
| 22 | |
| 23 | def test_empty(self): |
| 24 | compile("", "<test>", "exec") |
| 25 | |
| 26 | def test_other_newlines(self): |
| 27 | compile("\r\n", "<test>", "exec") |
| 28 | compile("\r", "<test>", "exec") |
| 29 | compile("hi\r\nstuff\r\ndef f():\n pass\r", "<test>", "exec") |
| 30 | compile("this_is\rreally_old_mac\rdef f():\n pass", "<test>", "exec") |
| 31 | |
Raymond Hettinger | 8a99b50 | 2003-06-23 13:36:57 +0000 | [diff] [blame] | 32 | def test_debug_assignment(self): |
| 33 | # catch assignments to __debug__ |
| 34 | self.assertRaises(SyntaxError, compile, '__debug__ = 1', '?', 'single') |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 35 | import builtins |
| 36 | prev = builtins.__debug__ |
| 37 | setattr(builtins, '__debug__', 'sure') |
| 38 | setattr(builtins, '__debug__', prev) |
Jeremy Hylton | 778e265 | 2001-11-09 19:50:08 +0000 | [diff] [blame] | 39 | |
Raymond Hettinger | 8a99b50 | 2003-06-23 13:36:57 +0000 | [diff] [blame] | 40 | def test_argument_handling(self): |
| 41 | # detect duplicate positional and keyword arguments |
| 42 | self.assertRaises(SyntaxError, eval, 'lambda a,a:0') |
| 43 | self.assertRaises(SyntaxError, eval, 'lambda a,a=1:0') |
| 44 | self.assertRaises(SyntaxError, eval, 'lambda a=1,a=1:0') |
Benjamin Peterson | ec19d95 | 2008-06-30 15:01:21 +0000 | [diff] [blame] | 45 | self.assertRaises(SyntaxError, exec, 'def f(a, a): pass') |
| 46 | self.assertRaises(SyntaxError, exec, 'def f(a = 0, a = 1): pass') |
| 47 | self.assertRaises(SyntaxError, exec, 'def f(a): global a; a = 1') |
Jeremy Hylton | 778e265 | 2001-11-09 19:50:08 +0000 | [diff] [blame] | 48 | |
Raymond Hettinger | 8a99b50 | 2003-06-23 13:36:57 +0000 | [diff] [blame] | 49 | def test_syntax_error(self): |
| 50 | self.assertRaises(SyntaxError, compile, "1+*3", "filename", "exec") |
Peter Schneider-Kamp | fdee0f0 | 2000-07-25 22:15:45 +0000 | [diff] [blame] | 51 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 52 | def test_none_keyword_arg(self): |
| 53 | self.assertRaises(SyntaxError, compile, "f(None=1)", "<string>", "exec") |
| 54 | |
Raymond Hettinger | 8a99b50 | 2003-06-23 13:36:57 +0000 | [diff] [blame] | 55 | def test_duplicate_global_local(self): |
Benjamin Peterson | ec19d95 | 2008-06-30 15:01:21 +0000 | [diff] [blame] | 56 | self.assertRaises(SyntaxError, exec, 'def f(a): global a; a = 1') |
Peter Schneider-Kamp | fdee0f0 | 2000-07-25 22:15:45 +0000 | [diff] [blame] | 57 | |
Raymond Hettinger | 66bd233 | 2004-08-02 08:30:07 +0000 | [diff] [blame] | 58 | def test_exec_with_general_mapping_for_locals(self): |
| 59 | |
| 60 | class M: |
| 61 | "Test mapping interface versus possible calls from eval()." |
| 62 | def __getitem__(self, key): |
| 63 | if key == 'a': |
| 64 | return 12 |
| 65 | raise KeyError |
| 66 | def __setitem__(self, key, value): |
| 67 | self.results = (key, value) |
Guido van Rossum | 63eecc7 | 2007-02-22 23:55:25 +0000 | [diff] [blame] | 68 | def keys(self): |
| 69 | return list('xyz') |
Raymond Hettinger | 66bd233 | 2004-08-02 08:30:07 +0000 | [diff] [blame] | 70 | |
| 71 | m = M() |
| 72 | g = globals() |
Georg Brandl | 7cae87c | 2006-09-06 06:51:57 +0000 | [diff] [blame] | 73 | exec('z = a', g, m) |
Raymond Hettinger | 66bd233 | 2004-08-02 08:30:07 +0000 | [diff] [blame] | 74 | self.assertEqual(m.results, ('z', 12)) |
| 75 | try: |
Georg Brandl | 7cae87c | 2006-09-06 06:51:57 +0000 | [diff] [blame] | 76 | exec('z = b', g, m) |
Raymond Hettinger | 66bd233 | 2004-08-02 08:30:07 +0000 | [diff] [blame] | 77 | except NameError: |
| 78 | pass |
| 79 | else: |
| 80 | self.fail('Did not detect a KeyError') |
Georg Brandl | 7cae87c | 2006-09-06 06:51:57 +0000 | [diff] [blame] | 81 | exec('z = dir()', g, m) |
Raymond Hettinger | 66bd233 | 2004-08-02 08:30:07 +0000 | [diff] [blame] | 82 | self.assertEqual(m.results, ('z', list('xyz'))) |
Georg Brandl | 7cae87c | 2006-09-06 06:51:57 +0000 | [diff] [blame] | 83 | exec('z = globals()', g, m) |
Raymond Hettinger | 66bd233 | 2004-08-02 08:30:07 +0000 | [diff] [blame] | 84 | self.assertEqual(m.results, ('z', g)) |
Georg Brandl | 7cae87c | 2006-09-06 06:51:57 +0000 | [diff] [blame] | 85 | exec('z = locals()', g, m) |
Raymond Hettinger | 66bd233 | 2004-08-02 08:30:07 +0000 | [diff] [blame] | 86 | self.assertEqual(m.results, ('z', m)) |
Benjamin Peterson | ec19d95 | 2008-06-30 15:01:21 +0000 | [diff] [blame] | 87 | self.assertRaises(TypeError, exec, 'z = b', m) |
Raymond Hettinger | 66bd233 | 2004-08-02 08:30:07 +0000 | [diff] [blame] | 88 | |
| 89 | class A: |
| 90 | "Non-mapping" |
| 91 | pass |
| 92 | m = A() |
Benjamin Peterson | ec19d95 | 2008-06-30 15:01:21 +0000 | [diff] [blame] | 93 | self.assertRaises(TypeError, exec, 'z = a', g, m) |
Raymond Hettinger | 66bd233 | 2004-08-02 08:30:07 +0000 | [diff] [blame] | 94 | |
| 95 | # Verify that dict subclasses work as well |
| 96 | class D(dict): |
| 97 | def __getitem__(self, key): |
| 98 | if key == 'a': |
| 99 | return 12 |
| 100 | return dict.__getitem__(self, key) |
| 101 | d = D() |
Georg Brandl | 7cae87c | 2006-09-06 06:51:57 +0000 | [diff] [blame] | 102 | exec('z = a', g, d) |
Raymond Hettinger | 66bd233 | 2004-08-02 08:30:07 +0000 | [diff] [blame] | 103 | self.assertEqual(d['z'], 12) |
| 104 | |
Neal Norwitz | 6ab080c | 2005-10-24 00:08:10 +0000 | [diff] [blame] | 105 | def test_extended_arg(self): |
| 106 | longexpr = 'x = x or ' + '-x' * 2500 |
Georg Brandl | 7cae87c | 2006-09-06 06:51:57 +0000 | [diff] [blame] | 107 | g = {} |
Neal Norwitz | 6ab080c | 2005-10-24 00:08:10 +0000 | [diff] [blame] | 108 | code = ''' |
| 109 | def f(x): |
| 110 | %s |
| 111 | %s |
| 112 | %s |
| 113 | %s |
| 114 | %s |
| 115 | %s |
| 116 | %s |
| 117 | %s |
| 118 | %s |
| 119 | %s |
| 120 | # the expressions above have no effect, x == argument |
| 121 | while x: |
| 122 | x -= 1 |
| 123 | # EXTENDED_ARG/JUMP_ABSOLUTE here |
| 124 | return x |
| 125 | ''' % ((longexpr,)*10) |
Georg Brandl | 7cae87c | 2006-09-06 06:51:57 +0000 | [diff] [blame] | 126 | exec(code, g) |
| 127 | self.assertEqual(g['f'](5), 0) |
Neal Norwitz | 6ab080c | 2005-10-24 00:08:10 +0000 | [diff] [blame] | 128 | |
Raymond Hettinger | 8a99b50 | 2003-06-23 13:36:57 +0000 | [diff] [blame] | 129 | def test_argument_order(self): |
Benjamin Peterson | ec19d95 | 2008-06-30 15:01:21 +0000 | [diff] [blame] | 130 | self.assertRaises(SyntaxError, exec, 'def f(a=1, b): pass') |
Jeremy Hylton | 121b6eb | 2001-02-19 23:53:42 +0000 | [diff] [blame] | 131 | |
Raymond Hettinger | 8a99b50 | 2003-06-23 13:36:57 +0000 | [diff] [blame] | 132 | def test_float_literals(self): |
| 133 | # testing bad float literals |
| 134 | self.assertRaises(SyntaxError, eval, "2e") |
| 135 | self.assertRaises(SyntaxError, eval, "2.0e+") |
| 136 | self.assertRaises(SyntaxError, eval, "1e-") |
| 137 | self.assertRaises(SyntaxError, eval, "3-4e/21") |
Jeremy Hylton | 121b6eb | 2001-02-19 23:53:42 +0000 | [diff] [blame] | 138 | |
Raymond Hettinger | 8a99b50 | 2003-06-23 13:36:57 +0000 | [diff] [blame] | 139 | def test_indentation(self): |
| 140 | # testing compile() of indented block w/o trailing newline" |
| 141 | s = """ |
Guido van Rossum | 4b499dd3 | 2003-02-13 22:07:59 +0000 | [diff] [blame] | 142 | if 1: |
| 143 | if 2: |
| 144 | pass""" |
Raymond Hettinger | 8a99b50 | 2003-06-23 13:36:57 +0000 | [diff] [blame] | 145 | compile(s, "<string>", "exec") |
| 146 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 147 | # This test is probably specific to CPython and may not generalize |
| 148 | # to other implementations. We are trying to ensure that when |
| 149 | # the first line of code starts after 256, correct line numbers |
| 150 | # in tracebacks are still produced. |
| 151 | def test_leading_newlines(self): |
| 152 | s256 = "".join(["\n"] * 256 + ["spam"]) |
| 153 | co = compile(s256, 'fn', 'exec') |
| 154 | self.assertEqual(co.co_firstlineno, 257) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 155 | self.assertEqual(co.co_lnotab, bytes()) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 156 | |
Raymond Hettinger | 8a99b50 | 2003-06-23 13:36:57 +0000 | [diff] [blame] | 157 | def test_literals_with_leading_zeroes(self): |
| 158 | for arg in ["077787", "0xj", "0x.", "0e", "090000000000000", |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 159 | "080000000000000", "000000000000009", "000000000000008", |
| 160 | "0b42", "0BADCAFE", "0o123456789", "0b1.1", "0o4.2", |
| 161 | "0b101j2", "0o153j2", "0b100e1", "0o777e1", "0777", |
| 162 | "000777", "000000000000007"]: |
Raymond Hettinger | 8a99b50 | 2003-06-23 13:36:57 +0000 | [diff] [blame] | 163 | self.assertRaises(SyntaxError, eval, arg) |
| 164 | |
Raymond Hettinger | 8a99b50 | 2003-06-23 13:36:57 +0000 | [diff] [blame] | 165 | self.assertEqual(eval("0xff"), 255) |
Raymond Hettinger | 8a99b50 | 2003-06-23 13:36:57 +0000 | [diff] [blame] | 166 | self.assertEqual(eval("0777."), 777) |
| 167 | self.assertEqual(eval("0777.0"), 777) |
| 168 | self.assertEqual(eval("000000000000000000000000000000000000000000000000000777e0"), 777) |
| 169 | self.assertEqual(eval("0777e1"), 7770) |
| 170 | self.assertEqual(eval("0e0"), 0) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 171 | self.assertEqual(eval("0000e-012"), 0) |
Raymond Hettinger | 8a99b50 | 2003-06-23 13:36:57 +0000 | [diff] [blame] | 172 | self.assertEqual(eval("09.5"), 9.5) |
| 173 | self.assertEqual(eval("0777j"), 777j) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 174 | self.assertEqual(eval("000"), 0) |
Raymond Hettinger | 8a99b50 | 2003-06-23 13:36:57 +0000 | [diff] [blame] | 175 | self.assertEqual(eval("00j"), 0j) |
| 176 | self.assertEqual(eval("00.0"), 0) |
| 177 | self.assertEqual(eval("0e3"), 0) |
| 178 | self.assertEqual(eval("090000000000000."), 90000000000000.) |
| 179 | self.assertEqual(eval("090000000000000.0000000000000000000000"), 90000000000000.) |
| 180 | self.assertEqual(eval("090000000000000e0"), 90000000000000.) |
| 181 | self.assertEqual(eval("090000000000000e-0"), 90000000000000.) |
| 182 | self.assertEqual(eval("090000000000000j"), 90000000000000j) |
Raymond Hettinger | 8a99b50 | 2003-06-23 13:36:57 +0000 | [diff] [blame] | 183 | self.assertEqual(eval("000000000000008."), 8.) |
| 184 | self.assertEqual(eval("000000000000009."), 9.) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 185 | self.assertEqual(eval("0b101010"), 42) |
| 186 | self.assertEqual(eval("-0b000000000010"), -2) |
| 187 | self.assertEqual(eval("0o777"), 511) |
| 188 | self.assertEqual(eval("-0o0000010"), -8) |
Raymond Hettinger | 8a99b50 | 2003-06-23 13:36:57 +0000 | [diff] [blame] | 189 | |
| 190 | def test_unary_minus(self): |
| 191 | # Verify treatment of unary minus on negative numbers SF bug #660455 |
Christian Heimes | a37d4c6 | 2007-12-04 23:02:19 +0000 | [diff] [blame] | 192 | if sys.maxsize == 2147483647: |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 193 | # 32-bit machine |
| 194 | all_one_bits = '0xffffffff' |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 195 | self.assertEqual(eval(all_one_bits), 4294967295) |
| 196 | self.assertEqual(eval("-" + all_one_bits), -4294967295) |
Christian Heimes | a37d4c6 | 2007-12-04 23:02:19 +0000 | [diff] [blame] | 197 | elif sys.maxsize == 9223372036854775807: |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 198 | # 64-bit machine |
Raymond Hettinger | 8a99b50 | 2003-06-23 13:36:57 +0000 | [diff] [blame] | 199 | all_one_bits = '0xffffffffffffffff' |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 200 | self.assertEqual(eval(all_one_bits), 18446744073709551615) |
| 201 | self.assertEqual(eval("-" + all_one_bits), -18446744073709551615) |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 202 | else: |
| 203 | self.fail("How many bits *does* this machine have???") |
Ezio Melotti | 42da663 | 2011-03-15 05:18:48 +0200 | [diff] [blame] | 204 | # Verify treatment of constant folding on -(sys.maxsize+1) |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 205 | # i.e. -2147483648 on 32 bit platforms. Should return int. |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 206 | self.assertIsInstance(eval("%s" % (-sys.maxsize - 1)), int) |
| 207 | self.assertIsInstance(eval("%s" % (-sys.maxsize - 2)), int) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 208 | |
Christian Heimes | a37d4c6 | 2007-12-04 23:02:19 +0000 | [diff] [blame] | 209 | if sys.maxsize == 9223372036854775807: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 210 | def test_32_63_bit_values(self): |
| 211 | a = +4294967296 # 1 << 32 |
| 212 | b = -4294967296 # 1 << 32 |
| 213 | c = +281474976710656 # 1 << 48 |
| 214 | d = -281474976710656 # 1 << 48 |
| 215 | e = +4611686018427387904 # 1 << 62 |
| 216 | f = -4611686018427387904 # 1 << 62 |
| 217 | g = +9223372036854775807 # 1 << 63 - 1 |
| 218 | h = -9223372036854775807 # 1 << 63 - 1 |
| 219 | |
Neal Norwitz | 221085d | 2007-02-25 20:55:47 +0000 | [diff] [blame] | 220 | for variable in self.test_32_63_bit_values.__code__.co_consts: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 221 | if variable is not None: |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 222 | self.assertIsInstance(variable, int) |
Raymond Hettinger | 8a99b50 | 2003-06-23 13:36:57 +0000 | [diff] [blame] | 223 | |
| 224 | def test_sequence_unpacking_error(self): |
| 225 | # Verify sequence packing/unpacking with "or". SF bug #757818 |
| 226 | i,j = (1, -1) or (-1, 1) |
| 227 | self.assertEqual(i, 1) |
| 228 | self.assertEqual(j, -1) |
Guido van Rossum | 4b499dd3 | 2003-02-13 22:07:59 +0000 | [diff] [blame] | 229 | |
Raymond Hettinger | 11a70c7 | 2004-07-17 21:46:25 +0000 | [diff] [blame] | 230 | def test_none_assignment(self): |
| 231 | stmts = [ |
| 232 | 'None = 0', |
| 233 | 'None += 0', |
| 234 | '__builtins__.None = 0', |
| 235 | 'def None(): pass', |
| 236 | 'class None: pass', |
| 237 | '(a, None) = 0, 0', |
| 238 | 'for None in range(10): pass', |
| 239 | 'def f(None): pass', |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 240 | 'import None', |
| 241 | 'import x as None', |
| 242 | 'from x import None', |
| 243 | 'from x import y as None' |
Raymond Hettinger | 11a70c7 | 2004-07-17 21:46:25 +0000 | [diff] [blame] | 244 | ] |
| 245 | for stmt in stmts: |
| 246 | stmt += "\n" |
| 247 | self.assertRaises(SyntaxError, compile, stmt, 'tmp', 'single') |
| 248 | self.assertRaises(SyntaxError, compile, stmt, 'tmp', 'exec') |
Tim Peters | d507dab | 2001-08-30 20:51:59 +0000 | [diff] [blame] | 249 | |
Anthony Baxter | 1a4ddae | 2004-08-31 10:07:13 +0000 | [diff] [blame] | 250 | def test_import(self): |
| 251 | succeed = [ |
| 252 | 'import sys', |
| 253 | 'import os, sys', |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 254 | 'import os as bar', |
| 255 | 'import os.path as bar', |
Anthony Baxter | 1a4ddae | 2004-08-31 10:07:13 +0000 | [diff] [blame] | 256 | 'from __future__ import nested_scopes, generators', |
| 257 | 'from __future__ import (nested_scopes,\ngenerators)', |
| 258 | 'from __future__ import (nested_scopes,\ngenerators,)', |
| 259 | 'from sys import stdin, stderr, stdout', |
| 260 | 'from sys import (stdin, stderr,\nstdout)', |
| 261 | 'from sys import (stdin, stderr,\nstdout,)', |
| 262 | 'from sys import (stdin\n, stderr, stdout)', |
| 263 | 'from sys import (stdin\n, stderr, stdout,)', |
| 264 | 'from sys import stdin as si, stdout as so, stderr as se', |
| 265 | 'from sys import (stdin as si, stdout as so, stderr as se)', |
| 266 | 'from sys import (stdin as si, stdout as so, stderr as se,)', |
| 267 | ] |
| 268 | fail = [ |
| 269 | 'import (os, sys)', |
| 270 | 'import (os), (sys)', |
| 271 | 'import ((os), (sys))', |
| 272 | 'import (sys', |
| 273 | 'import sys)', |
| 274 | 'import (os,)', |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 275 | 'import os As bar', |
| 276 | 'import os.path a bar', |
| 277 | 'from sys import stdin As stdout', |
| 278 | 'from sys import stdin a stdout', |
Anthony Baxter | 1a4ddae | 2004-08-31 10:07:13 +0000 | [diff] [blame] | 279 | 'from (sys) import stdin', |
| 280 | 'from __future__ import (nested_scopes', |
| 281 | 'from __future__ import nested_scopes)', |
| 282 | 'from __future__ import nested_scopes,\ngenerators', |
| 283 | 'from sys import (stdin', |
| 284 | 'from sys import stdin)', |
| 285 | 'from sys import stdin, stdout,\nstderr', |
| 286 | 'from sys import stdin si', |
| 287 | 'from sys import stdin,' |
| 288 | 'from sys import (*)', |
| 289 | 'from sys import (stdin,, stdout, stderr)', |
| 290 | 'from sys import (stdin, stdout),', |
| 291 | ] |
| 292 | for stmt in succeed: |
| 293 | compile(stmt, 'tmp', 'exec') |
| 294 | for stmt in fail: |
| 295 | self.assertRaises(SyntaxError, compile, stmt, 'tmp', 'exec') |
| 296 | |
Raymond Hettinger | 9047c8f | 2004-10-24 00:10:06 +0000 | [diff] [blame] | 297 | def test_for_distinct_code_objects(self): |
| 298 | # SF bug 1048870 |
| 299 | def f(): |
| 300 | f1 = lambda x=1: x |
| 301 | f2 = lambda x=2: x |
| 302 | return f1, f2 |
| 303 | f1, f2 = f() |
Neal Norwitz | 221085d | 2007-02-25 20:55:47 +0000 | [diff] [blame] | 304 | self.assertNotEqual(id(f1.__code__), id(f2.__code__)) |
Raymond Hettinger | 9047c8f | 2004-10-24 00:10:06 +0000 | [diff] [blame] | 305 | |
Benjamin Peterson | e6dd2cb | 2010-03-17 20:56:58 +0000 | [diff] [blame] | 306 | def test_lambda_doc(self): |
| 307 | l = lambda: "foo" |
| 308 | self.assertIsNone(l.__doc__) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 309 | |
Serhiy Storchaka | 607cb9c | 2014-09-05 11:00:56 +0300 | [diff] [blame] | 310 | def test_encoding(self): |
| 311 | code = b'# -*- coding: badencoding -*-\npass\n' |
| 312 | self.assertRaises(SyntaxError, compile, code, 'tmp', 'exec') |
| 313 | code = '# -*- coding: badencoding -*-\n"\xc2\xa4"\n' |
| 314 | compile(code, 'tmp', 'exec') |
| 315 | self.assertEqual(eval(code), '\xc2\xa4') |
| 316 | code = '"\xc2\xa4"\n' |
| 317 | self.assertEqual(eval(code), '\xc2\xa4') |
| 318 | code = b'"\xc2\xa4"\n' |
| 319 | self.assertEqual(eval(code), '\xa4') |
| 320 | code = b'# -*- coding: latin1 -*-\n"\xc2\xa4"\n' |
| 321 | self.assertEqual(eval(code), '\xc2\xa4') |
| 322 | code = b'# -*- coding: utf-8 -*-\n"\xc2\xa4"\n' |
| 323 | self.assertEqual(eval(code), '\xa4') |
| 324 | code = b'# -*- coding: iso8859-15 -*-\n"\xc2\xa4"\n' |
| 325 | self.assertEqual(eval(code), '\xc2\u20ac') |
| 326 | code = '"""\\\n# -*- coding: iso8859-15 -*-\n\xc2\xa4"""\n' |
| 327 | self.assertEqual(eval(code), '# -*- coding: iso8859-15 -*-\n\xc2\xa4') |
| 328 | code = b'"""\\\n# -*- coding: iso8859-15 -*-\n\xc2\xa4"""\n' |
| 329 | self.assertEqual(eval(code), '# -*- coding: iso8859-15 -*-\n\xa4') |
Benjamin Peterson | 5d2ad25 | 2010-03-17 20:57:32 +0000 | [diff] [blame] | 330 | |
Nick Coghlan | eadee9a | 2006-03-13 12:31:58 +0000 | [diff] [blame] | 331 | def test_subscripts(self): |
| 332 | # SF bug 1448804 |
| 333 | # Class to make testing subscript results easy |
| 334 | class str_map(object): |
| 335 | def __init__(self): |
| 336 | self.data = {} |
| 337 | def __getitem__(self, key): |
| 338 | return self.data[str(key)] |
| 339 | def __setitem__(self, key, value): |
| 340 | self.data[str(key)] = value |
| 341 | def __delitem__(self, key): |
| 342 | del self.data[str(key)] |
| 343 | def __contains__(self, key): |
| 344 | return str(key) in self.data |
| 345 | d = str_map() |
| 346 | # Index |
| 347 | d[1] = 1 |
| 348 | self.assertEqual(d[1], 1) |
| 349 | d[1] += 1 |
| 350 | self.assertEqual(d[1], 2) |
| 351 | del d[1] |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 352 | self.assertNotIn(1, d) |
Nick Coghlan | eadee9a | 2006-03-13 12:31:58 +0000 | [diff] [blame] | 353 | # Tuple of indices |
| 354 | d[1, 1] = 1 |
| 355 | self.assertEqual(d[1, 1], 1) |
| 356 | d[1, 1] += 1 |
| 357 | self.assertEqual(d[1, 1], 2) |
| 358 | del d[1, 1] |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 359 | self.assertNotIn((1, 1), d) |
Nick Coghlan | eadee9a | 2006-03-13 12:31:58 +0000 | [diff] [blame] | 360 | # Simple slice |
| 361 | d[1:2] = 1 |
| 362 | self.assertEqual(d[1:2], 1) |
| 363 | d[1:2] += 1 |
| 364 | self.assertEqual(d[1:2], 2) |
| 365 | del d[1:2] |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 366 | self.assertNotIn(slice(1, 2), d) |
Nick Coghlan | eadee9a | 2006-03-13 12:31:58 +0000 | [diff] [blame] | 367 | # Tuple of simple slices |
| 368 | d[1:2, 1:2] = 1 |
| 369 | self.assertEqual(d[1:2, 1:2], 1) |
| 370 | d[1:2, 1:2] += 1 |
| 371 | self.assertEqual(d[1:2, 1:2], 2) |
| 372 | del d[1:2, 1:2] |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 373 | self.assertNotIn((slice(1, 2), slice(1, 2)), d) |
Nick Coghlan | eadee9a | 2006-03-13 12:31:58 +0000 | [diff] [blame] | 374 | # Extended slice |
| 375 | d[1:2:3] = 1 |
| 376 | self.assertEqual(d[1:2:3], 1) |
| 377 | d[1:2:3] += 1 |
| 378 | self.assertEqual(d[1:2:3], 2) |
| 379 | del d[1:2:3] |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 380 | self.assertNotIn(slice(1, 2, 3), d) |
Nick Coghlan | eadee9a | 2006-03-13 12:31:58 +0000 | [diff] [blame] | 381 | # Tuple of extended slices |
| 382 | d[1:2:3, 1:2:3] = 1 |
| 383 | self.assertEqual(d[1:2:3, 1:2:3], 1) |
| 384 | d[1:2:3, 1:2:3] += 1 |
| 385 | self.assertEqual(d[1:2:3, 1:2:3], 2) |
| 386 | del d[1:2:3, 1:2:3] |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 387 | self.assertNotIn((slice(1, 2, 3), slice(1, 2, 3)), d) |
Nick Coghlan | eadee9a | 2006-03-13 12:31:58 +0000 | [diff] [blame] | 388 | # Ellipsis |
| 389 | d[...] = 1 |
| 390 | self.assertEqual(d[...], 1) |
| 391 | d[...] += 1 |
| 392 | self.assertEqual(d[...], 2) |
| 393 | del d[...] |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 394 | self.assertNotIn(Ellipsis, d) |
Nick Coghlan | eadee9a | 2006-03-13 12:31:58 +0000 | [diff] [blame] | 395 | # Tuple of Ellipses |
| 396 | d[..., ...] = 1 |
| 397 | self.assertEqual(d[..., ...], 1) |
| 398 | d[..., ...] += 1 |
| 399 | self.assertEqual(d[..., ...], 2) |
| 400 | del d[..., ...] |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 401 | self.assertNotIn((Ellipsis, Ellipsis), d) |
Nick Coghlan | eadee9a | 2006-03-13 12:31:58 +0000 | [diff] [blame] | 402 | |
Guido van Rossum | 0240b92 | 2007-02-26 21:23:50 +0000 | [diff] [blame] | 403 | def test_annotation_limit(self): |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 404 | # 16 bits are available for # of annotations, but only 8 bits are |
| 405 | # available for the parameter count, hence 255 |
Guido van Rossum | 0240b92 | 2007-02-26 21:23:50 +0000 | [diff] [blame] | 406 | # is the max. Ensure the result of too many annotations is a |
| 407 | # SyntaxError. |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 408 | s = "def f(%s): pass" |
| 409 | s %= ', '.join('a%d:%d' % (i,i) for i in range(256)) |
Guido van Rossum | 0240b92 | 2007-02-26 21:23:50 +0000 | [diff] [blame] | 410 | self.assertRaises(SyntaxError, compile, s, '?', 'exec') |
| 411 | # Test that the max # of annotations compiles. |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 412 | s = "def f(%s): pass" |
| 413 | s %= ', '.join('a%d:%d' % (i,i) for i in range(255)) |
Guido van Rossum | 0240b92 | 2007-02-26 21:23:50 +0000 | [diff] [blame] | 414 | compile(s, '?', 'exec') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 415 | |
| 416 | def test_mangling(self): |
| 417 | class A: |
| 418 | def f(): |
| 419 | __mangled = 1 |
| 420 | __not_mangled__ = 2 |
| 421 | import __mangled_mod |
| 422 | import __package__.module |
| 423 | |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 424 | self.assertIn("_A__mangled", A.f.__code__.co_varnames) |
| 425 | self.assertIn("__not_mangled__", A.f.__code__.co_varnames) |
| 426 | self.assertIn("_A__mangled_mod", A.f.__code__.co_varnames) |
| 427 | self.assertIn("__package__", A.f.__code__.co_varnames) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 428 | |
Martin v. Löwis | 618dc5e | 2008-03-30 20:03:44 +0000 | [diff] [blame] | 429 | def test_compile_ast(self): |
| 430 | fname = __file__ |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 431 | if fname.lower().endswith('pyc'): |
Martin v. Löwis | 618dc5e | 2008-03-30 20:03:44 +0000 | [diff] [blame] | 432 | fname = fname[:-1] |
| 433 | with open(fname, 'r') as f: |
| 434 | fcontents = f.read() |
| 435 | sample_code = [ |
| 436 | ['<assign>', 'x = 5'], |
| 437 | ['<ifblock>', """if True:\n pass\n"""], |
| 438 | ['<forblock>', """for n in [1, 2, 3]:\n print(n)\n"""], |
| 439 | ['<deffunc>', """def foo():\n pass\nfoo()\n"""], |
| 440 | [fname, fcontents], |
| 441 | ] |
| 442 | |
| 443 | for fname, code in sample_code: |
| 444 | co1 = compile(code, '%s1' % fname, 'exec') |
| 445 | ast = compile(code, '%s2' % fname, 'exec', _ast.PyCF_ONLY_AST) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 446 | self.assertTrue(type(ast) == _ast.Module) |
Martin v. Löwis | 618dc5e | 2008-03-30 20:03:44 +0000 | [diff] [blame] | 447 | co2 = compile(ast, '%s3' % fname, 'exec') |
| 448 | self.assertEqual(co1, co2) |
Neal Norwitz | db4115f | 2008-03-31 04:20:05 +0000 | [diff] [blame] | 449 | # the code object's filename comes from the second compilation step |
| 450 | self.assertEqual(co2.co_filename, '%s3' % fname) |
| 451 | |
| 452 | # raise exception when node type doesn't match with compile mode |
| 453 | co1 = compile('print(1)', '<string>', 'exec', _ast.PyCF_ONLY_AST) |
| 454 | self.assertRaises(TypeError, compile, co1, '<ast>', 'eval') |
| 455 | |
| 456 | # raise exception when node type is no start node |
| 457 | self.assertRaises(TypeError, compile, _ast.If(), '<ast>', 'exec') |
| 458 | |
| 459 | # raise exception when node has invalid children |
| 460 | ast = _ast.Module() |
| 461 | ast.body = [_ast.BoolOp()] |
| 462 | self.assertRaises(TypeError, compile, ast, '<ast>', 'exec') |
Martin v. Löwis | 618dc5e | 2008-03-30 20:03:44 +0000 | [diff] [blame] | 463 | |
Benjamin Peterson | ee85339 | 2015-05-28 14:30:26 -0500 | [diff] [blame] | 464 | def test_dict_evaluation_order(self): |
| 465 | i = 0 |
| 466 | |
| 467 | def f(): |
| 468 | nonlocal i |
| 469 | i += 1 |
| 470 | return i |
| 471 | |
| 472 | d = {f(): f(), f(): f()} |
| 473 | self.assertEqual(d, {1: 2, 3: 4}) |
| 474 | |
Serhiy Storchaka | 9305d83 | 2016-06-18 13:53:36 +0300 | [diff] [blame] | 475 | def test_compile_filename(self): |
| 476 | for filename in ('file.py', b'file.py', |
| 477 | bytearray(b'file.py'), memoryview(b'file.py')): |
| 478 | code = compile('pass', filename, 'exec') |
| 479 | self.assertEqual(code.co_filename, 'file.py') |
| 480 | self.assertRaises(TypeError, compile, 'pass', list(b'file.py'), 'exec') |
| 481 | |
Benjamin Peterson | 43b0686 | 2011-05-27 09:08:01 -0500 | [diff] [blame] | 482 | @support.cpython_only |
| 483 | def test_same_filename_used(self): |
| 484 | s = """def f(): pass\ndef g(): pass""" |
| 485 | c = compile(s, "myfile", "exec") |
| 486 | for obj in c.co_consts: |
| 487 | if isinstance(obj, types.CodeType): |
| 488 | self.assertIs(obj.co_filename, c.co_filename) |
| 489 | |
Meador Inge | fa21bf0 | 2012-01-19 01:08:41 -0600 | [diff] [blame] | 490 | def test_single_statement(self): |
| 491 | self.compile_single("1 + 2") |
| 492 | self.compile_single("\n1 + 2") |
| 493 | self.compile_single("1 + 2\n") |
| 494 | self.compile_single("1 + 2\n\n") |
| 495 | self.compile_single("1 + 2\t\t\n") |
| 496 | self.compile_single("1 + 2\t\t\n ") |
| 497 | self.compile_single("1 + 2 # one plus two") |
| 498 | self.compile_single("1; 2") |
| 499 | self.compile_single("import sys; sys") |
| 500 | self.compile_single("def f():\n pass") |
| 501 | self.compile_single("while False:\n pass") |
| 502 | self.compile_single("if x:\n f(x)") |
| 503 | self.compile_single("if x:\n f(x)\nelse:\n g(x)") |
| 504 | self.compile_single("class T:\n pass") |
| 505 | |
| 506 | def test_bad_single_statement(self): |
| 507 | self.assertInvalidSingle('1\n2') |
| 508 | self.assertInvalidSingle('def f(): pass') |
| 509 | self.assertInvalidSingle('a = 13\nb = 187') |
| 510 | self.assertInvalidSingle('del x\ndel y') |
| 511 | self.assertInvalidSingle('f()\ng()') |
Benjamin Peterson | cff9237 | 2012-01-19 17:46:13 -0500 | [diff] [blame] | 512 | self.assertInvalidSingle('f()\n# blah\nblah()') |
| 513 | self.assertInvalidSingle('f()\nxy # blah\nblah()') |
Benjamin Peterson | 77fc1f3 | 2012-01-20 11:01:06 -0500 | [diff] [blame] | 514 | self.assertInvalidSingle('x = 5 # comment\nx = 6\n') |
Martin v. Löwis | 618dc5e | 2008-03-30 20:03:44 +0000 | [diff] [blame] | 515 | |
Benjamin Peterson | d73aca7 | 2015-04-21 12:05:19 -0400 | [diff] [blame] | 516 | def test_particularly_evil_undecodable(self): |
| 517 | # Issue 24022 |
| 518 | src = b'0000\x00\n00000000000\n\x00\n\x9e\n' |
| 519 | with tempfile.TemporaryDirectory() as tmpd: |
| 520 | fn = os.path.join(tmpd, "bad.py") |
| 521 | with open(fn, "wb") as fp: |
| 522 | fp.write(src) |
| 523 | res = script_helper.run_python_until_end(fn)[0] |
| 524 | self.assertIn(b"Non-UTF-8", res.err) |
| 525 | |
Serhiy Storchaka | 0d44111 | 2015-11-14 15:10:35 +0200 | [diff] [blame] | 526 | def test_yet_more_evil_still_undecodable(self): |
| 527 | # Issue #25388 |
| 528 | src = b"#\x00\n#\xfd\n" |
| 529 | with tempfile.TemporaryDirectory() as tmpd: |
| 530 | fn = os.path.join(tmpd, "bad.py") |
| 531 | with open(fn, "wb") as fp: |
| 532 | fp.write(src) |
| 533 | res = script_helper.run_python_until_end(fn)[0] |
| 534 | self.assertIn(b"Non-UTF-8", res.err) |
| 535 | |
Nick Coghlan | aab9c2b | 2012-11-04 23:14:34 +1000 | [diff] [blame] | 536 | @support.cpython_only |
| 537 | def test_compiler_recursion_limit(self): |
| 538 | # Expected limit is sys.getrecursionlimit() * the scaling factor |
| 539 | # in symtable.c (currently 3) |
| 540 | # We expect to fail *at* that limit, because we use up some of |
| 541 | # the stack depth limit in the test suite code |
| 542 | # So we check the expected limit and 75% of that |
| 543 | # XXX (ncoghlan): duplicating the scaling factor here is a little |
| 544 | # ugly. Perhaps it should be exposed somewhere... |
| 545 | fail_depth = sys.getrecursionlimit() * 3 |
| 546 | success_depth = int(fail_depth * 0.75) |
| 547 | |
| 548 | def check_limit(prefix, repeated): |
| 549 | expect_ok = prefix + repeated * success_depth |
| 550 | self.compile_single(expect_ok) |
| 551 | broken = prefix + repeated * fail_depth |
| 552 | details = "Compiling ({!r} + {!r} * {})".format( |
| 553 | prefix, repeated, fail_depth) |
Yury Selivanov | f488fb4 | 2015-07-03 01:04:23 -0400 | [diff] [blame] | 554 | with self.assertRaises(RecursionError, msg=details): |
Nick Coghlan | aab9c2b | 2012-11-04 23:14:34 +1000 | [diff] [blame] | 555 | self.compile_single(broken) |
| 556 | |
| 557 | check_limit("a", "()") |
| 558 | check_limit("a", ".b") |
| 559 | check_limit("a", "[0]") |
| 560 | check_limit("a", "*a") |
| 561 | |
Martin Panter | eeb896c | 2015-11-07 02:32:21 +0000 | [diff] [blame] | 562 | def test_null_terminated(self): |
| 563 | # The source code is null-terminated internally, but bytes-like |
| 564 | # objects are accepted, which could be not terminated. |
Martin Panter | d61d860 | 2015-11-08 11:09:13 +0000 | [diff] [blame] | 565 | with self.assertRaisesRegex(ValueError, "cannot contain null"): |
Martin Panter | eeb896c | 2015-11-07 02:32:21 +0000 | [diff] [blame] | 566 | compile("123\x00", "<dummy>", "eval") |
Martin Panter | d61d860 | 2015-11-08 11:09:13 +0000 | [diff] [blame] | 567 | with self.assertRaisesRegex(ValueError, "cannot contain null"): |
Martin Panter | eeb896c | 2015-11-07 02:32:21 +0000 | [diff] [blame] | 568 | compile(memoryview(b"123\x00"), "<dummy>", "eval") |
| 569 | code = compile(memoryview(b"123\x00")[1:-1], "<dummy>", "eval") |
| 570 | self.assertEqual(eval(code), 23) |
| 571 | code = compile(memoryview(b"1234")[1:-1], "<dummy>", "eval") |
| 572 | self.assertEqual(eval(code), 23) |
| 573 | code = compile(memoryview(b"$23$")[1:-1], "<dummy>", "eval") |
| 574 | self.assertEqual(eval(code), 23) |
| 575 | |
| 576 | # Also test when eval() and exec() do the compilation step |
| 577 | self.assertEqual(eval(memoryview(b"1234")[1:-1]), 23) |
| 578 | namespace = dict() |
| 579 | exec(memoryview(b"ax = 123")[1:-1], namespace) |
| 580 | self.assertEqual(namespace['x'], 12) |
| 581 | |
Victor Stinner | 3cdd5fb | 2016-01-22 12:33:12 +0100 | [diff] [blame] | 582 | def check_constant(self, func, expected): |
| 583 | for const in func.__code__.co_consts: |
| 584 | if repr(const) == repr(expected): |
| 585 | break |
| 586 | else: |
| 587 | self.fail("unable to find constant %r in %r" |
| 588 | % (expected, func.__code__.co_consts)) |
| 589 | |
| 590 | # Merging equal constants is not a strict requirement for the Python |
| 591 | # semantics, it's a more an implementation detail. |
| 592 | @support.cpython_only |
| 593 | def test_merge_constants(self): |
| 594 | # Issue #25843: compile() must merge constants which are equal |
| 595 | # and have the same type. |
| 596 | |
| 597 | def check_same_constant(const): |
| 598 | ns = {} |
| 599 | code = "f1, f2 = lambda: %r, lambda: %r" % (const, const) |
| 600 | exec(code, ns) |
| 601 | f1 = ns['f1'] |
| 602 | f2 = ns['f2'] |
| 603 | self.assertIs(f1.__code__, f2.__code__) |
| 604 | self.check_constant(f1, const) |
| 605 | self.assertEqual(repr(f1()), repr(const)) |
| 606 | |
| 607 | check_same_constant(None) |
| 608 | check_same_constant(0) |
| 609 | check_same_constant(0.0) |
| 610 | check_same_constant(b'abc') |
| 611 | check_same_constant('abc') |
| 612 | |
| 613 | # Note: "lambda: ..." emits "LOAD_CONST Ellipsis", |
| 614 | # whereas "lambda: Ellipsis" emits "LOAD_GLOBAL Ellipsis" |
| 615 | f1, f2 = lambda: ..., lambda: ... |
| 616 | self.assertIs(f1.__code__, f2.__code__) |
| 617 | self.check_constant(f1, Ellipsis) |
| 618 | self.assertEqual(repr(f1()), repr(Ellipsis)) |
| 619 | |
| 620 | # {0} is converted to a constant frozenset({0}) by the peephole |
| 621 | # optimizer |
| 622 | f1, f2 = lambda x: x in {0}, lambda x: x in {0} |
| 623 | self.assertIs(f1.__code__, f2.__code__) |
| 624 | self.check_constant(f1, frozenset({0})) |
| 625 | self.assertTrue(f1(0)) |
| 626 | |
| 627 | def test_dont_merge_constants(self): |
| 628 | # Issue #25843: compile() must not merge constants which are equal |
| 629 | # but have a different type. |
| 630 | |
| 631 | def check_different_constants(const1, const2): |
| 632 | ns = {} |
| 633 | exec("f1, f2 = lambda: %r, lambda: %r" % (const1, const2), ns) |
| 634 | f1 = ns['f1'] |
| 635 | f2 = ns['f2'] |
| 636 | self.assertIsNot(f1.__code__, f2.__code__) |
| 637 | self.check_constant(f1, const1) |
| 638 | self.check_constant(f2, const2) |
| 639 | self.assertEqual(repr(f1()), repr(const1)) |
| 640 | self.assertEqual(repr(f2()), repr(const2)) |
| 641 | |
| 642 | check_different_constants(0, 0.0) |
| 643 | check_different_constants(+0.0, -0.0) |
| 644 | check_different_constants((0,), (0.0,)) |
| 645 | |
| 646 | # check_different_constants() cannot be used because repr(-0j) is |
| 647 | # '(-0-0j)', but when '(-0-0j)' is evaluated to 0j: we loose the sign. |
| 648 | f1, f2 = lambda: +0.0j, lambda: -0.0j |
| 649 | self.assertIsNot(f1.__code__, f2.__code__) |
| 650 | self.check_constant(f1, +0.0j) |
| 651 | self.check_constant(f2, -0.0j) |
| 652 | self.assertEqual(repr(f1()), repr(+0.0j)) |
| 653 | self.assertEqual(repr(f2()), repr(-0.0j)) |
| 654 | |
| 655 | # {0} is converted to a constant frozenset({0}) by the peephole |
| 656 | # optimizer |
| 657 | f1, f2 = lambda x: x in {0}, lambda x: x in {0.0} |
| 658 | self.assertIsNot(f1.__code__, f2.__code__) |
| 659 | self.check_constant(f1, frozenset({0})) |
| 660 | self.check_constant(f2, frozenset({0.0})) |
| 661 | self.assertTrue(f1(0)) |
| 662 | self.assertTrue(f2(0.0)) |
| 663 | |
Nick Coghlan | aab9c2b | 2012-11-04 23:14:34 +1000 | [diff] [blame] | 664 | |
Antoine Pitrou | 9961405 | 2014-05-23 11:46:03 +0200 | [diff] [blame] | 665 | class TestStackSize(unittest.TestCase): |
| 666 | # These tests check that the computed stack size for a code object |
| 667 | # stays within reasonable bounds (see issue #21523 for an example |
| 668 | # dysfunction). |
| 669 | N = 100 |
| 670 | |
| 671 | def check_stack_size(self, code): |
| 672 | # To assert that the alleged stack size is not O(N), we |
| 673 | # check that it is smaller than log(N). |
| 674 | if isinstance(code, str): |
| 675 | code = compile(code, "<foo>", "single") |
| 676 | max_size = math.ceil(math.log(len(code.co_code))) |
| 677 | self.assertLessEqual(code.co_stacksize, max_size) |
| 678 | |
| 679 | def test_and(self): |
| 680 | self.check_stack_size("x and " * self.N + "x") |
| 681 | |
| 682 | def test_or(self): |
| 683 | self.check_stack_size("x or " * self.N + "x") |
| 684 | |
| 685 | def test_and_or(self): |
| 686 | self.check_stack_size("x and x or " * self.N + "x") |
| 687 | |
| 688 | def test_chained_comparison(self): |
| 689 | self.check_stack_size("x < " * self.N + "x") |
| 690 | |
| 691 | def test_if_else(self): |
| 692 | self.check_stack_size("x if x else " * self.N + "x") |
| 693 | |
| 694 | def test_binop(self): |
| 695 | self.check_stack_size("x + " * self.N + "x") |
| 696 | |
| 697 | def test_func_and(self): |
| 698 | code = "def f(x):\n" |
| 699 | code += " x and x\n" * self.N |
| 700 | self.check_stack_size(code) |
| 701 | |
Tim Peters | d507dab | 2001-08-30 20:51:59 +0000 | [diff] [blame] | 702 | |
Raymond Hettinger | 8a99b50 | 2003-06-23 13:36:57 +0000 | [diff] [blame] | 703 | if __name__ == "__main__": |
Antoine Pitrou | 9961405 | 2014-05-23 11:46:03 +0200 | [diff] [blame] | 704 | unittest.main() |