Raymond Hettinger | 8a99b50 | 2003-06-23 13:36:57 +0000 | [diff] [blame] | 1 | import unittest |
Raymond Hettinger | 8a99b50 | 2003-06-23 13:36:57 +0000 | [diff] [blame] | 2 | import sys |
Martin v. Löwis | 618dc5e | 2008-03-30 20:03:44 +0000 | [diff] [blame] | 3 | import _ast |
Benjamin Peterson | 43b0686 | 2011-05-27 09:08:01 -0500 | [diff] [blame] | 4 | import types |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 5 | from test import support |
Peter Schneider-Kamp | fdee0f0 | 2000-07-25 22:15:45 +0000 | [diff] [blame] | 6 | |
Raymond Hettinger | 8a99b50 | 2003-06-23 13:36:57 +0000 | [diff] [blame] | 7 | class TestSpecifics(unittest.TestCase): |
Jeremy Hylton | 778e265 | 2001-11-09 19:50:08 +0000 | [diff] [blame] | 8 | |
Meador Inge | fa21bf0 | 2012-01-19 01:08:41 -0600 | [diff] [blame] | 9 | def compile_single(self, source): |
| 10 | compile(source, "<single>", "single") |
| 11 | |
| 12 | def assertInvalidSingle(self, source): |
| 13 | self.assertRaises(SyntaxError, self.compile_single, source) |
| 14 | |
Benjamin Peterson | aeaa592 | 2009-11-13 00:17:59 +0000 | [diff] [blame] | 15 | def test_no_ending_newline(self): |
| 16 | compile("hi", "<test>", "exec") |
| 17 | compile("hi\r", "<test>", "exec") |
| 18 | |
| 19 | def test_empty(self): |
| 20 | compile("", "<test>", "exec") |
| 21 | |
| 22 | def test_other_newlines(self): |
| 23 | compile("\r\n", "<test>", "exec") |
| 24 | compile("\r", "<test>", "exec") |
| 25 | compile("hi\r\nstuff\r\ndef f():\n pass\r", "<test>", "exec") |
| 26 | compile("this_is\rreally_old_mac\rdef f():\n pass", "<test>", "exec") |
| 27 | |
Raymond Hettinger | 8a99b50 | 2003-06-23 13:36:57 +0000 | [diff] [blame] | 28 | def test_debug_assignment(self): |
| 29 | # catch assignments to __debug__ |
| 30 | self.assertRaises(SyntaxError, compile, '__debug__ = 1', '?', 'single') |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 31 | import builtins |
| 32 | prev = builtins.__debug__ |
| 33 | setattr(builtins, '__debug__', 'sure') |
| 34 | setattr(builtins, '__debug__', prev) |
Jeremy Hylton | 778e265 | 2001-11-09 19:50:08 +0000 | [diff] [blame] | 35 | |
Raymond Hettinger | 8a99b50 | 2003-06-23 13:36:57 +0000 | [diff] [blame] | 36 | def test_argument_handling(self): |
| 37 | # detect duplicate positional and keyword arguments |
| 38 | self.assertRaises(SyntaxError, eval, 'lambda a,a:0') |
| 39 | self.assertRaises(SyntaxError, eval, 'lambda a,a=1:0') |
| 40 | self.assertRaises(SyntaxError, eval, 'lambda a=1,a=1:0') |
Benjamin Peterson | ec19d95 | 2008-06-30 15:01:21 +0000 | [diff] [blame] | 41 | self.assertRaises(SyntaxError, exec, 'def f(a, a): pass') |
| 42 | self.assertRaises(SyntaxError, exec, 'def f(a = 0, a = 1): pass') |
| 43 | self.assertRaises(SyntaxError, exec, 'def f(a): global a; a = 1') |
Jeremy Hylton | 778e265 | 2001-11-09 19:50:08 +0000 | [diff] [blame] | 44 | |
Raymond Hettinger | 8a99b50 | 2003-06-23 13:36:57 +0000 | [diff] [blame] | 45 | def test_syntax_error(self): |
| 46 | self.assertRaises(SyntaxError, compile, "1+*3", "filename", "exec") |
Peter Schneider-Kamp | fdee0f0 | 2000-07-25 22:15:45 +0000 | [diff] [blame] | 47 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 48 | def test_none_keyword_arg(self): |
| 49 | self.assertRaises(SyntaxError, compile, "f(None=1)", "<string>", "exec") |
| 50 | |
Raymond Hettinger | 8a99b50 | 2003-06-23 13:36:57 +0000 | [diff] [blame] | 51 | def test_duplicate_global_local(self): |
Benjamin Peterson | ec19d95 | 2008-06-30 15:01:21 +0000 | [diff] [blame] | 52 | self.assertRaises(SyntaxError, exec, 'def f(a): global a; a = 1') |
Peter Schneider-Kamp | fdee0f0 | 2000-07-25 22:15:45 +0000 | [diff] [blame] | 53 | |
Raymond Hettinger | 66bd233 | 2004-08-02 08:30:07 +0000 | [diff] [blame] | 54 | def test_exec_with_general_mapping_for_locals(self): |
| 55 | |
| 56 | class M: |
| 57 | "Test mapping interface versus possible calls from eval()." |
| 58 | def __getitem__(self, key): |
| 59 | if key == 'a': |
| 60 | return 12 |
| 61 | raise KeyError |
| 62 | def __setitem__(self, key, value): |
| 63 | self.results = (key, value) |
Guido van Rossum | 63eecc7 | 2007-02-22 23:55:25 +0000 | [diff] [blame] | 64 | def keys(self): |
| 65 | return list('xyz') |
Raymond Hettinger | 66bd233 | 2004-08-02 08:30:07 +0000 | [diff] [blame] | 66 | |
| 67 | m = M() |
| 68 | g = globals() |
Georg Brandl | 7cae87c | 2006-09-06 06:51:57 +0000 | [diff] [blame] | 69 | exec('z = a', g, m) |
Raymond Hettinger | 66bd233 | 2004-08-02 08:30:07 +0000 | [diff] [blame] | 70 | self.assertEqual(m.results, ('z', 12)) |
| 71 | try: |
Georg Brandl | 7cae87c | 2006-09-06 06:51:57 +0000 | [diff] [blame] | 72 | exec('z = b', g, m) |
Raymond Hettinger | 66bd233 | 2004-08-02 08:30:07 +0000 | [diff] [blame] | 73 | except NameError: |
| 74 | pass |
| 75 | else: |
| 76 | self.fail('Did not detect a KeyError') |
Georg Brandl | 7cae87c | 2006-09-06 06:51:57 +0000 | [diff] [blame] | 77 | exec('z = dir()', g, m) |
Raymond Hettinger | 66bd233 | 2004-08-02 08:30:07 +0000 | [diff] [blame] | 78 | self.assertEqual(m.results, ('z', list('xyz'))) |
Georg Brandl | 7cae87c | 2006-09-06 06:51:57 +0000 | [diff] [blame] | 79 | exec('z = globals()', g, m) |
Raymond Hettinger | 66bd233 | 2004-08-02 08:30:07 +0000 | [diff] [blame] | 80 | self.assertEqual(m.results, ('z', g)) |
Georg Brandl | 7cae87c | 2006-09-06 06:51:57 +0000 | [diff] [blame] | 81 | exec('z = locals()', g, m) |
Raymond Hettinger | 66bd233 | 2004-08-02 08:30:07 +0000 | [diff] [blame] | 82 | self.assertEqual(m.results, ('z', m)) |
Benjamin Peterson | ec19d95 | 2008-06-30 15:01:21 +0000 | [diff] [blame] | 83 | self.assertRaises(TypeError, exec, 'z = b', m) |
Raymond Hettinger | 66bd233 | 2004-08-02 08:30:07 +0000 | [diff] [blame] | 84 | |
| 85 | class A: |
| 86 | "Non-mapping" |
| 87 | pass |
| 88 | m = A() |
Benjamin Peterson | ec19d95 | 2008-06-30 15:01:21 +0000 | [diff] [blame] | 89 | self.assertRaises(TypeError, exec, 'z = a', g, m) |
Raymond Hettinger | 66bd233 | 2004-08-02 08:30:07 +0000 | [diff] [blame] | 90 | |
| 91 | # Verify that dict subclasses work as well |
| 92 | class D(dict): |
| 93 | def __getitem__(self, key): |
| 94 | if key == 'a': |
| 95 | return 12 |
| 96 | return dict.__getitem__(self, key) |
| 97 | d = D() |
Georg Brandl | 7cae87c | 2006-09-06 06:51:57 +0000 | [diff] [blame] | 98 | exec('z = a', g, d) |
Raymond Hettinger | 66bd233 | 2004-08-02 08:30:07 +0000 | [diff] [blame] | 99 | self.assertEqual(d['z'], 12) |
| 100 | |
Neal Norwitz | 6ab080c | 2005-10-24 00:08:10 +0000 | [diff] [blame] | 101 | def test_extended_arg(self): |
| 102 | longexpr = 'x = x or ' + '-x' * 2500 |
Georg Brandl | 7cae87c | 2006-09-06 06:51:57 +0000 | [diff] [blame] | 103 | g = {} |
Neal Norwitz | 6ab080c | 2005-10-24 00:08:10 +0000 | [diff] [blame] | 104 | code = ''' |
| 105 | def f(x): |
| 106 | %s |
| 107 | %s |
| 108 | %s |
| 109 | %s |
| 110 | %s |
| 111 | %s |
| 112 | %s |
| 113 | %s |
| 114 | %s |
| 115 | %s |
| 116 | # the expressions above have no effect, x == argument |
| 117 | while x: |
| 118 | x -= 1 |
| 119 | # EXTENDED_ARG/JUMP_ABSOLUTE here |
| 120 | return x |
| 121 | ''' % ((longexpr,)*10) |
Georg Brandl | 7cae87c | 2006-09-06 06:51:57 +0000 | [diff] [blame] | 122 | exec(code, g) |
| 123 | self.assertEqual(g['f'](5), 0) |
Neal Norwitz | 6ab080c | 2005-10-24 00:08:10 +0000 | [diff] [blame] | 124 | |
Raymond Hettinger | 8a99b50 | 2003-06-23 13:36:57 +0000 | [diff] [blame] | 125 | def test_argument_order(self): |
Benjamin Peterson | ec19d95 | 2008-06-30 15:01:21 +0000 | [diff] [blame] | 126 | self.assertRaises(SyntaxError, exec, 'def f(a=1, b): pass') |
Jeremy Hylton | 121b6eb | 2001-02-19 23:53:42 +0000 | [diff] [blame] | 127 | |
Raymond Hettinger | 8a99b50 | 2003-06-23 13:36:57 +0000 | [diff] [blame] | 128 | def test_float_literals(self): |
| 129 | # testing bad float literals |
| 130 | self.assertRaises(SyntaxError, eval, "2e") |
| 131 | self.assertRaises(SyntaxError, eval, "2.0e+") |
| 132 | self.assertRaises(SyntaxError, eval, "1e-") |
| 133 | self.assertRaises(SyntaxError, eval, "3-4e/21") |
Jeremy Hylton | 121b6eb | 2001-02-19 23:53:42 +0000 | [diff] [blame] | 134 | |
Raymond Hettinger | 8a99b50 | 2003-06-23 13:36:57 +0000 | [diff] [blame] | 135 | def test_indentation(self): |
| 136 | # testing compile() of indented block w/o trailing newline" |
| 137 | s = """ |
Guido van Rossum | 4b499dd3 | 2003-02-13 22:07:59 +0000 | [diff] [blame] | 138 | if 1: |
| 139 | if 2: |
| 140 | pass""" |
Raymond Hettinger | 8a99b50 | 2003-06-23 13:36:57 +0000 | [diff] [blame] | 141 | compile(s, "<string>", "exec") |
| 142 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 143 | # This test is probably specific to CPython and may not generalize |
| 144 | # to other implementations. We are trying to ensure that when |
| 145 | # the first line of code starts after 256, correct line numbers |
| 146 | # in tracebacks are still produced. |
| 147 | def test_leading_newlines(self): |
| 148 | s256 = "".join(["\n"] * 256 + ["spam"]) |
| 149 | co = compile(s256, 'fn', 'exec') |
| 150 | self.assertEqual(co.co_firstlineno, 257) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 151 | self.assertEqual(co.co_lnotab, bytes()) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 152 | |
Raymond Hettinger | 8a99b50 | 2003-06-23 13:36:57 +0000 | [diff] [blame] | 153 | def test_literals_with_leading_zeroes(self): |
| 154 | for arg in ["077787", "0xj", "0x.", "0e", "090000000000000", |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 155 | "080000000000000", "000000000000009", "000000000000008", |
| 156 | "0b42", "0BADCAFE", "0o123456789", "0b1.1", "0o4.2", |
| 157 | "0b101j2", "0o153j2", "0b100e1", "0o777e1", "0777", |
| 158 | "000777", "000000000000007"]: |
Raymond Hettinger | 8a99b50 | 2003-06-23 13:36:57 +0000 | [diff] [blame] | 159 | self.assertRaises(SyntaxError, eval, arg) |
| 160 | |
Raymond Hettinger | 8a99b50 | 2003-06-23 13:36:57 +0000 | [diff] [blame] | 161 | self.assertEqual(eval("0xff"), 255) |
Raymond Hettinger | 8a99b50 | 2003-06-23 13:36:57 +0000 | [diff] [blame] | 162 | self.assertEqual(eval("0777."), 777) |
| 163 | self.assertEqual(eval("0777.0"), 777) |
| 164 | self.assertEqual(eval("000000000000000000000000000000000000000000000000000777e0"), 777) |
| 165 | self.assertEqual(eval("0777e1"), 7770) |
| 166 | self.assertEqual(eval("0e0"), 0) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 167 | self.assertEqual(eval("0000e-012"), 0) |
Raymond Hettinger | 8a99b50 | 2003-06-23 13:36:57 +0000 | [diff] [blame] | 168 | self.assertEqual(eval("09.5"), 9.5) |
| 169 | self.assertEqual(eval("0777j"), 777j) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 170 | self.assertEqual(eval("000"), 0) |
Raymond Hettinger | 8a99b50 | 2003-06-23 13:36:57 +0000 | [diff] [blame] | 171 | self.assertEqual(eval("00j"), 0j) |
| 172 | self.assertEqual(eval("00.0"), 0) |
| 173 | self.assertEqual(eval("0e3"), 0) |
| 174 | self.assertEqual(eval("090000000000000."), 90000000000000.) |
| 175 | self.assertEqual(eval("090000000000000.0000000000000000000000"), 90000000000000.) |
| 176 | self.assertEqual(eval("090000000000000e0"), 90000000000000.) |
| 177 | self.assertEqual(eval("090000000000000e-0"), 90000000000000.) |
| 178 | self.assertEqual(eval("090000000000000j"), 90000000000000j) |
Raymond Hettinger | 8a99b50 | 2003-06-23 13:36:57 +0000 | [diff] [blame] | 179 | self.assertEqual(eval("000000000000008."), 8.) |
| 180 | self.assertEqual(eval("000000000000009."), 9.) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 181 | self.assertEqual(eval("0b101010"), 42) |
| 182 | self.assertEqual(eval("-0b000000000010"), -2) |
| 183 | self.assertEqual(eval("0o777"), 511) |
| 184 | self.assertEqual(eval("-0o0000010"), -8) |
Raymond Hettinger | 8a99b50 | 2003-06-23 13:36:57 +0000 | [diff] [blame] | 185 | |
| 186 | def test_unary_minus(self): |
| 187 | # Verify treatment of unary minus on negative numbers SF bug #660455 |
Christian Heimes | a37d4c6 | 2007-12-04 23:02:19 +0000 | [diff] [blame] | 188 | if sys.maxsize == 2147483647: |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 189 | # 32-bit machine |
| 190 | all_one_bits = '0xffffffff' |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 191 | self.assertEqual(eval(all_one_bits), 4294967295) |
| 192 | self.assertEqual(eval("-" + all_one_bits), -4294967295) |
Christian Heimes | a37d4c6 | 2007-12-04 23:02:19 +0000 | [diff] [blame] | 193 | elif sys.maxsize == 9223372036854775807: |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 194 | # 64-bit machine |
Raymond Hettinger | 8a99b50 | 2003-06-23 13:36:57 +0000 | [diff] [blame] | 195 | all_one_bits = '0xffffffffffffffff' |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 196 | self.assertEqual(eval(all_one_bits), 18446744073709551615) |
| 197 | self.assertEqual(eval("-" + all_one_bits), -18446744073709551615) |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 198 | else: |
| 199 | self.fail("How many bits *does* this machine have???") |
Ezio Melotti | 42da663 | 2011-03-15 05:18:48 +0200 | [diff] [blame] | 200 | # Verify treatment of constant folding on -(sys.maxsize+1) |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 201 | # i.e. -2147483648 on 32 bit platforms. Should return int. |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 202 | self.assertIsInstance(eval("%s" % (-sys.maxsize - 1)), int) |
| 203 | self.assertIsInstance(eval("%s" % (-sys.maxsize - 2)), int) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 204 | |
Christian Heimes | a37d4c6 | 2007-12-04 23:02:19 +0000 | [diff] [blame] | 205 | if sys.maxsize == 9223372036854775807: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 206 | def test_32_63_bit_values(self): |
| 207 | a = +4294967296 # 1 << 32 |
| 208 | b = -4294967296 # 1 << 32 |
| 209 | c = +281474976710656 # 1 << 48 |
| 210 | d = -281474976710656 # 1 << 48 |
| 211 | e = +4611686018427387904 # 1 << 62 |
| 212 | f = -4611686018427387904 # 1 << 62 |
| 213 | g = +9223372036854775807 # 1 << 63 - 1 |
| 214 | h = -9223372036854775807 # 1 << 63 - 1 |
| 215 | |
Neal Norwitz | 221085d | 2007-02-25 20:55:47 +0000 | [diff] [blame] | 216 | for variable in self.test_32_63_bit_values.__code__.co_consts: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 217 | if variable is not None: |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 218 | self.assertIsInstance(variable, int) |
Raymond Hettinger | 8a99b50 | 2003-06-23 13:36:57 +0000 | [diff] [blame] | 219 | |
| 220 | def test_sequence_unpacking_error(self): |
| 221 | # Verify sequence packing/unpacking with "or". SF bug #757818 |
| 222 | i,j = (1, -1) or (-1, 1) |
| 223 | self.assertEqual(i, 1) |
| 224 | self.assertEqual(j, -1) |
Guido van Rossum | 4b499dd3 | 2003-02-13 22:07:59 +0000 | [diff] [blame] | 225 | |
Raymond Hettinger | 11a70c7 | 2004-07-17 21:46:25 +0000 | [diff] [blame] | 226 | def test_none_assignment(self): |
| 227 | stmts = [ |
| 228 | 'None = 0', |
| 229 | 'None += 0', |
| 230 | '__builtins__.None = 0', |
| 231 | 'def None(): pass', |
| 232 | 'class None: pass', |
| 233 | '(a, None) = 0, 0', |
| 234 | 'for None in range(10): pass', |
| 235 | 'def f(None): pass', |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 236 | 'import None', |
| 237 | 'import x as None', |
| 238 | 'from x import None', |
| 239 | 'from x import y as None' |
Raymond Hettinger | 11a70c7 | 2004-07-17 21:46:25 +0000 | [diff] [blame] | 240 | ] |
| 241 | for stmt in stmts: |
| 242 | stmt += "\n" |
| 243 | self.assertRaises(SyntaxError, compile, stmt, 'tmp', 'single') |
| 244 | self.assertRaises(SyntaxError, compile, stmt, 'tmp', 'exec') |
Tim Peters | d507dab | 2001-08-30 20:51:59 +0000 | [diff] [blame] | 245 | |
Anthony Baxter | 1a4ddae | 2004-08-31 10:07:13 +0000 | [diff] [blame] | 246 | def test_import(self): |
| 247 | succeed = [ |
| 248 | 'import sys', |
| 249 | 'import os, sys', |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 250 | 'import os as bar', |
| 251 | 'import os.path as bar', |
Anthony Baxter | 1a4ddae | 2004-08-31 10:07:13 +0000 | [diff] [blame] | 252 | 'from __future__ import nested_scopes, generators', |
| 253 | 'from __future__ import (nested_scopes,\ngenerators)', |
| 254 | 'from __future__ import (nested_scopes,\ngenerators,)', |
| 255 | 'from sys import stdin, stderr, stdout', |
| 256 | 'from sys import (stdin, stderr,\nstdout)', |
| 257 | 'from sys import (stdin, stderr,\nstdout,)', |
| 258 | 'from sys import (stdin\n, stderr, stdout)', |
| 259 | 'from sys import (stdin\n, stderr, stdout,)', |
| 260 | 'from sys import stdin as si, stdout as so, stderr as se', |
| 261 | 'from sys import (stdin as si, stdout as so, stderr as se)', |
| 262 | 'from sys import (stdin as si, stdout as so, stderr as se,)', |
| 263 | ] |
| 264 | fail = [ |
| 265 | 'import (os, sys)', |
| 266 | 'import (os), (sys)', |
| 267 | 'import ((os), (sys))', |
| 268 | 'import (sys', |
| 269 | 'import sys)', |
| 270 | 'import (os,)', |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 271 | 'import os As bar', |
| 272 | 'import os.path a bar', |
| 273 | 'from sys import stdin As stdout', |
| 274 | 'from sys import stdin a stdout', |
Anthony Baxter | 1a4ddae | 2004-08-31 10:07:13 +0000 | [diff] [blame] | 275 | 'from (sys) import stdin', |
| 276 | 'from __future__ import (nested_scopes', |
| 277 | 'from __future__ import nested_scopes)', |
| 278 | 'from __future__ import nested_scopes,\ngenerators', |
| 279 | 'from sys import (stdin', |
| 280 | 'from sys import stdin)', |
| 281 | 'from sys import stdin, stdout,\nstderr', |
| 282 | 'from sys import stdin si', |
| 283 | 'from sys import stdin,' |
| 284 | 'from sys import (*)', |
| 285 | 'from sys import (stdin,, stdout, stderr)', |
| 286 | 'from sys import (stdin, stdout),', |
| 287 | ] |
| 288 | for stmt in succeed: |
| 289 | compile(stmt, 'tmp', 'exec') |
| 290 | for stmt in fail: |
| 291 | self.assertRaises(SyntaxError, compile, stmt, 'tmp', 'exec') |
| 292 | |
Raymond Hettinger | 9047c8f | 2004-10-24 00:10:06 +0000 | [diff] [blame] | 293 | def test_for_distinct_code_objects(self): |
| 294 | # SF bug 1048870 |
| 295 | def f(): |
| 296 | f1 = lambda x=1: x |
| 297 | f2 = lambda x=2: x |
| 298 | return f1, f2 |
| 299 | f1, f2 = f() |
Neal Norwitz | 221085d | 2007-02-25 20:55:47 +0000 | [diff] [blame] | 300 | self.assertNotEqual(id(f1.__code__), id(f2.__code__)) |
Raymond Hettinger | 9047c8f | 2004-10-24 00:10:06 +0000 | [diff] [blame] | 301 | |
Benjamin Peterson | e6dd2cb | 2010-03-17 20:56:58 +0000 | [diff] [blame] | 302 | def test_lambda_doc(self): |
| 303 | l = lambda: "foo" |
| 304 | self.assertIsNone(l.__doc__) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 305 | |
Benjamin Peterson | 5d2ad25 | 2010-03-17 20:57:32 +0000 | [diff] [blame] | 306 | ## def test_unicode_encoding(self): |
| 307 | ## code = "# -*- coding: utf-8 -*-\npass\n" |
| 308 | ## self.assertRaises(SyntaxError, compile, code, "tmp", "exec") |
| 309 | |
Nick Coghlan | eadee9a | 2006-03-13 12:31:58 +0000 | [diff] [blame] | 310 | def test_subscripts(self): |
| 311 | # SF bug 1448804 |
| 312 | # Class to make testing subscript results easy |
| 313 | class str_map(object): |
| 314 | def __init__(self): |
| 315 | self.data = {} |
| 316 | def __getitem__(self, key): |
| 317 | return self.data[str(key)] |
| 318 | def __setitem__(self, key, value): |
| 319 | self.data[str(key)] = value |
| 320 | def __delitem__(self, key): |
| 321 | del self.data[str(key)] |
| 322 | def __contains__(self, key): |
| 323 | return str(key) in self.data |
| 324 | d = str_map() |
| 325 | # Index |
| 326 | d[1] = 1 |
| 327 | self.assertEqual(d[1], 1) |
| 328 | d[1] += 1 |
| 329 | self.assertEqual(d[1], 2) |
| 330 | del d[1] |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 331 | self.assertNotIn(1, d) |
Nick Coghlan | eadee9a | 2006-03-13 12:31:58 +0000 | [diff] [blame] | 332 | # Tuple of indices |
| 333 | d[1, 1] = 1 |
| 334 | self.assertEqual(d[1, 1], 1) |
| 335 | d[1, 1] += 1 |
| 336 | self.assertEqual(d[1, 1], 2) |
| 337 | del d[1, 1] |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 338 | self.assertNotIn((1, 1), d) |
Nick Coghlan | eadee9a | 2006-03-13 12:31:58 +0000 | [diff] [blame] | 339 | # Simple slice |
| 340 | d[1:2] = 1 |
| 341 | self.assertEqual(d[1:2], 1) |
| 342 | d[1:2] += 1 |
| 343 | self.assertEqual(d[1:2], 2) |
| 344 | del d[1:2] |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 345 | self.assertNotIn(slice(1, 2), d) |
Nick Coghlan | eadee9a | 2006-03-13 12:31:58 +0000 | [diff] [blame] | 346 | # Tuple of simple slices |
| 347 | d[1:2, 1:2] = 1 |
| 348 | self.assertEqual(d[1:2, 1:2], 1) |
| 349 | d[1:2, 1:2] += 1 |
| 350 | self.assertEqual(d[1:2, 1:2], 2) |
| 351 | del d[1:2, 1:2] |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 352 | self.assertNotIn((slice(1, 2), slice(1, 2)), d) |
Nick Coghlan | eadee9a | 2006-03-13 12:31:58 +0000 | [diff] [blame] | 353 | # Extended slice |
| 354 | d[1:2:3] = 1 |
| 355 | self.assertEqual(d[1:2:3], 1) |
| 356 | d[1:2:3] += 1 |
| 357 | self.assertEqual(d[1:2:3], 2) |
| 358 | del d[1:2:3] |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 359 | self.assertNotIn(slice(1, 2, 3), d) |
Nick Coghlan | eadee9a | 2006-03-13 12:31:58 +0000 | [diff] [blame] | 360 | # Tuple of extended slices |
| 361 | d[1:2:3, 1:2:3] = 1 |
| 362 | self.assertEqual(d[1:2:3, 1:2:3], 1) |
| 363 | d[1:2:3, 1:2:3] += 1 |
| 364 | self.assertEqual(d[1:2:3, 1:2:3], 2) |
| 365 | del d[1:2:3, 1:2:3] |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 366 | self.assertNotIn((slice(1, 2, 3), slice(1, 2, 3)), d) |
Nick Coghlan | eadee9a | 2006-03-13 12:31:58 +0000 | [diff] [blame] | 367 | # Ellipsis |
| 368 | d[...] = 1 |
| 369 | self.assertEqual(d[...], 1) |
| 370 | d[...] += 1 |
| 371 | self.assertEqual(d[...], 2) |
| 372 | del d[...] |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 373 | self.assertNotIn(Ellipsis, d) |
Nick Coghlan | eadee9a | 2006-03-13 12:31:58 +0000 | [diff] [blame] | 374 | # Tuple of Ellipses |
| 375 | d[..., ...] = 1 |
| 376 | self.assertEqual(d[..., ...], 1) |
| 377 | d[..., ...] += 1 |
| 378 | self.assertEqual(d[..., ...], 2) |
| 379 | del d[..., ...] |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 380 | self.assertNotIn((Ellipsis, Ellipsis), d) |
Nick Coghlan | eadee9a | 2006-03-13 12:31:58 +0000 | [diff] [blame] | 381 | |
Guido van Rossum | 0240b92 | 2007-02-26 21:23:50 +0000 | [diff] [blame] | 382 | def test_annotation_limit(self): |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 383 | # 16 bits are available for # of annotations, but only 8 bits are |
| 384 | # available for the parameter count, hence 255 |
Guido van Rossum | 0240b92 | 2007-02-26 21:23:50 +0000 | [diff] [blame] | 385 | # is the max. Ensure the result of too many annotations is a |
| 386 | # SyntaxError. |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 387 | s = "def f(%s): pass" |
| 388 | 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] | 389 | self.assertRaises(SyntaxError, compile, s, '?', 'exec') |
| 390 | # Test that the max # of annotations compiles. |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 391 | s = "def f(%s): pass" |
| 392 | 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] | 393 | compile(s, '?', 'exec') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 394 | |
| 395 | def test_mangling(self): |
| 396 | class A: |
| 397 | def f(): |
| 398 | __mangled = 1 |
| 399 | __not_mangled__ = 2 |
| 400 | import __mangled_mod |
| 401 | import __package__.module |
| 402 | |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 403 | self.assertIn("_A__mangled", A.f.__code__.co_varnames) |
| 404 | self.assertIn("__not_mangled__", A.f.__code__.co_varnames) |
| 405 | self.assertIn("_A__mangled_mod", A.f.__code__.co_varnames) |
| 406 | self.assertIn("__package__", A.f.__code__.co_varnames) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 407 | |
Martin v. Löwis | 618dc5e | 2008-03-30 20:03:44 +0000 | [diff] [blame] | 408 | def test_compile_ast(self): |
| 409 | fname = __file__ |
| 410 | if fname.lower().endswith(('pyc', 'pyo')): |
| 411 | fname = fname[:-1] |
| 412 | with open(fname, 'r') as f: |
| 413 | fcontents = f.read() |
| 414 | sample_code = [ |
| 415 | ['<assign>', 'x = 5'], |
| 416 | ['<ifblock>', """if True:\n pass\n"""], |
| 417 | ['<forblock>', """for n in [1, 2, 3]:\n print(n)\n"""], |
| 418 | ['<deffunc>', """def foo():\n pass\nfoo()\n"""], |
| 419 | [fname, fcontents], |
| 420 | ] |
| 421 | |
| 422 | for fname, code in sample_code: |
| 423 | co1 = compile(code, '%s1' % fname, 'exec') |
| 424 | ast = compile(code, '%s2' % fname, 'exec', _ast.PyCF_ONLY_AST) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 425 | self.assertTrue(type(ast) == _ast.Module) |
Martin v. Löwis | 618dc5e | 2008-03-30 20:03:44 +0000 | [diff] [blame] | 426 | co2 = compile(ast, '%s3' % fname, 'exec') |
| 427 | self.assertEqual(co1, co2) |
Neal Norwitz | db4115f | 2008-03-31 04:20:05 +0000 | [diff] [blame] | 428 | # the code object's filename comes from the second compilation step |
| 429 | self.assertEqual(co2.co_filename, '%s3' % fname) |
| 430 | |
| 431 | # raise exception when node type doesn't match with compile mode |
| 432 | co1 = compile('print(1)', '<string>', 'exec', _ast.PyCF_ONLY_AST) |
| 433 | self.assertRaises(TypeError, compile, co1, '<ast>', 'eval') |
| 434 | |
| 435 | # raise exception when node type is no start node |
| 436 | self.assertRaises(TypeError, compile, _ast.If(), '<ast>', 'exec') |
| 437 | |
| 438 | # raise exception when node has invalid children |
| 439 | ast = _ast.Module() |
| 440 | ast.body = [_ast.BoolOp()] |
| 441 | self.assertRaises(TypeError, compile, ast, '<ast>', 'exec') |
Martin v. Löwis | 618dc5e | 2008-03-30 20:03:44 +0000 | [diff] [blame] | 442 | |
Benjamin Peterson | 43b0686 | 2011-05-27 09:08:01 -0500 | [diff] [blame] | 443 | @support.cpython_only |
| 444 | def test_same_filename_used(self): |
| 445 | s = """def f(): pass\ndef g(): pass""" |
| 446 | c = compile(s, "myfile", "exec") |
| 447 | for obj in c.co_consts: |
| 448 | if isinstance(obj, types.CodeType): |
| 449 | self.assertIs(obj.co_filename, c.co_filename) |
| 450 | |
Meador Inge | fa21bf0 | 2012-01-19 01:08:41 -0600 | [diff] [blame] | 451 | def test_single_statement(self): |
| 452 | self.compile_single("1 + 2") |
| 453 | self.compile_single("\n1 + 2") |
| 454 | self.compile_single("1 + 2\n") |
| 455 | self.compile_single("1 + 2\n\n") |
| 456 | self.compile_single("1 + 2\t\t\n") |
| 457 | self.compile_single("1 + 2\t\t\n ") |
| 458 | self.compile_single("1 + 2 # one plus two") |
| 459 | self.compile_single("1; 2") |
| 460 | self.compile_single("import sys; sys") |
| 461 | self.compile_single("def f():\n pass") |
| 462 | self.compile_single("while False:\n pass") |
| 463 | self.compile_single("if x:\n f(x)") |
| 464 | self.compile_single("if x:\n f(x)\nelse:\n g(x)") |
| 465 | self.compile_single("class T:\n pass") |
| 466 | |
| 467 | def test_bad_single_statement(self): |
| 468 | self.assertInvalidSingle('1\n2') |
| 469 | self.assertInvalidSingle('def f(): pass') |
| 470 | self.assertInvalidSingle('a = 13\nb = 187') |
| 471 | self.assertInvalidSingle('del x\ndel y') |
| 472 | self.assertInvalidSingle('f()\ng()') |
Benjamin Peterson | cff9237 | 2012-01-19 17:46:13 -0500 | [diff] [blame] | 473 | self.assertInvalidSingle('f()\n# blah\nblah()') |
| 474 | self.assertInvalidSingle('f()\nxy # blah\nblah()') |
Benjamin Peterson | 77fc1f3 | 2012-01-20 11:01:06 -0500 | [diff] [blame] | 475 | self.assertInvalidSingle('x = 5 # comment\nx = 6\n') |
Martin v. Löwis | 618dc5e | 2008-03-30 20:03:44 +0000 | [diff] [blame] | 476 | |
Nick Coghlan | aab9c2b | 2012-11-04 23:14:34 +1000 | [diff] [blame] | 477 | @support.cpython_only |
| 478 | def test_compiler_recursion_limit(self): |
| 479 | # Expected limit is sys.getrecursionlimit() * the scaling factor |
| 480 | # in symtable.c (currently 3) |
| 481 | # We expect to fail *at* that limit, because we use up some of |
| 482 | # the stack depth limit in the test suite code |
| 483 | # So we check the expected limit and 75% of that |
| 484 | # XXX (ncoghlan): duplicating the scaling factor here is a little |
| 485 | # ugly. Perhaps it should be exposed somewhere... |
| 486 | fail_depth = sys.getrecursionlimit() * 3 |
| 487 | success_depth = int(fail_depth * 0.75) |
| 488 | |
| 489 | def check_limit(prefix, repeated): |
| 490 | expect_ok = prefix + repeated * success_depth |
| 491 | self.compile_single(expect_ok) |
| 492 | broken = prefix + repeated * fail_depth |
| 493 | details = "Compiling ({!r} + {!r} * {})".format( |
| 494 | prefix, repeated, fail_depth) |
| 495 | with self.assertRaises(RuntimeError, msg=details): |
| 496 | self.compile_single(broken) |
| 497 | |
| 498 | check_limit("a", "()") |
| 499 | check_limit("a", ".b") |
| 500 | check_limit("a", "[0]") |
| 501 | check_limit("a", "*a") |
| 502 | |
| 503 | |
Raymond Hettinger | 8a99b50 | 2003-06-23 13:36:57 +0000 | [diff] [blame] | 504 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 505 | support.run_unittest(TestSpecifics) |
Tim Peters | d507dab | 2001-08-30 20:51:59 +0000 | [diff] [blame] | 506 | |
Raymond Hettinger | 8a99b50 | 2003-06-23 13:36:57 +0000 | [diff] [blame] | 507 | if __name__ == "__main__": |
| 508 | test_main() |