blob: 4de54488d84b62bb5f6c481dbe37d8ef97cb832f [file] [log] [blame]
Gregory P. Smith49fa4a92018-11-08 17:55:07 -08001import dis
Antoine Pitrou99614052014-05-23 11:46:03 +02002import math
Benjamin Petersond73aca72015-04-21 12:05:19 -04003import os
Raymond Hettinger8a99b502003-06-23 13:36:57 +00004import unittest
Raymond Hettinger8a99b502003-06-23 13:36:57 +00005import sys
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006import _ast
Benjamin Petersond73aca72015-04-21 12:05:19 -04007import tempfile
Benjamin Peterson43b06862011-05-27 09:08:01 -05008import types
Berker Peksag076dbd02015-05-06 07:01:52 +03009from test import support
Hai Shi847f94f2020-06-26 01:17:57 +080010from test.support import script_helper
11from test.support.os_helper import FakePath
12
Peter Schneider-Kampfdee0f02000-07-25 22:15:45 +000013
Raymond Hettinger8a99b502003-06-23 13:36:57 +000014class TestSpecifics(unittest.TestCase):
Jeremy Hylton778e2652001-11-09 19:50:08 +000015
Meador Ingefa21bf02012-01-19 01:08:41 -060016 def compile_single(self, source):
17 compile(source, "<single>", "single")
18
19 def assertInvalidSingle(self, source):
20 self.assertRaises(SyntaxError, self.compile_single, source)
21
Benjamin Petersonaeaa5922009-11-13 00:17:59 +000022 def test_no_ending_newline(self):
23 compile("hi", "<test>", "exec")
24 compile("hi\r", "<test>", "exec")
25
26 def test_empty(self):
27 compile("", "<test>", "exec")
28
29 def test_other_newlines(self):
30 compile("\r\n", "<test>", "exec")
31 compile("\r", "<test>", "exec")
32 compile("hi\r\nstuff\r\ndef f():\n pass\r", "<test>", "exec")
33 compile("this_is\rreally_old_mac\rdef f():\n pass", "<test>", "exec")
34
Raymond Hettinger8a99b502003-06-23 13:36:57 +000035 def test_debug_assignment(self):
36 # catch assignments to __debug__
37 self.assertRaises(SyntaxError, compile, '__debug__ = 1', '?', 'single')
Georg Brandl1a3284e2007-12-02 09:40:06 +000038 import builtins
39 prev = builtins.__debug__
40 setattr(builtins, '__debug__', 'sure')
Serhiy Storchaka3325a672017-12-15 12:35:48 +020041 self.assertEqual(__debug__, prev)
Georg Brandl1a3284e2007-12-02 09:40:06 +000042 setattr(builtins, '__debug__', prev)
Jeremy Hylton778e2652001-11-09 19:50:08 +000043
Raymond Hettinger8a99b502003-06-23 13:36:57 +000044 def test_argument_handling(self):
45 # detect duplicate positional and keyword arguments
46 self.assertRaises(SyntaxError, eval, 'lambda a,a:0')
47 self.assertRaises(SyntaxError, eval, 'lambda a,a=1:0')
48 self.assertRaises(SyntaxError, eval, 'lambda a=1,a=1:0')
Benjamin Petersonec19d952008-06-30 15:01:21 +000049 self.assertRaises(SyntaxError, exec, 'def f(a, a): pass')
50 self.assertRaises(SyntaxError, exec, 'def f(a = 0, a = 1): pass')
51 self.assertRaises(SyntaxError, exec, 'def f(a): global a; a = 1')
Jeremy Hylton778e2652001-11-09 19:50:08 +000052
Raymond Hettinger8a99b502003-06-23 13:36:57 +000053 def test_syntax_error(self):
54 self.assertRaises(SyntaxError, compile, "1+*3", "filename", "exec")
Peter Schneider-Kampfdee0f02000-07-25 22:15:45 +000055
Guido van Rossumcd16bf62007-06-13 18:07:49 +000056 def test_none_keyword_arg(self):
57 self.assertRaises(SyntaxError, compile, "f(None=1)", "<string>", "exec")
58
Raymond Hettinger8a99b502003-06-23 13:36:57 +000059 def test_duplicate_global_local(self):
Benjamin Petersonec19d952008-06-30 15:01:21 +000060 self.assertRaises(SyntaxError, exec, 'def f(a): global a; a = 1')
Peter Schneider-Kampfdee0f02000-07-25 22:15:45 +000061
Raymond Hettinger66bd2332004-08-02 08:30:07 +000062 def test_exec_with_general_mapping_for_locals(self):
63
64 class M:
65 "Test mapping interface versus possible calls from eval()."
66 def __getitem__(self, key):
67 if key == 'a':
68 return 12
69 raise KeyError
70 def __setitem__(self, key, value):
71 self.results = (key, value)
Guido van Rossum63eecc72007-02-22 23:55:25 +000072 def keys(self):
73 return list('xyz')
Raymond Hettinger66bd2332004-08-02 08:30:07 +000074
75 m = M()
76 g = globals()
Georg Brandl7cae87c2006-09-06 06:51:57 +000077 exec('z = a', g, m)
Raymond Hettinger66bd2332004-08-02 08:30:07 +000078 self.assertEqual(m.results, ('z', 12))
79 try:
Georg Brandl7cae87c2006-09-06 06:51:57 +000080 exec('z = b', g, m)
Raymond Hettinger66bd2332004-08-02 08:30:07 +000081 except NameError:
82 pass
83 else:
84 self.fail('Did not detect a KeyError')
Georg Brandl7cae87c2006-09-06 06:51:57 +000085 exec('z = dir()', g, m)
Raymond Hettinger66bd2332004-08-02 08:30:07 +000086 self.assertEqual(m.results, ('z', list('xyz')))
Georg Brandl7cae87c2006-09-06 06:51:57 +000087 exec('z = globals()', g, m)
Raymond Hettinger66bd2332004-08-02 08:30:07 +000088 self.assertEqual(m.results, ('z', g))
Georg Brandl7cae87c2006-09-06 06:51:57 +000089 exec('z = locals()', g, m)
Raymond Hettinger66bd2332004-08-02 08:30:07 +000090 self.assertEqual(m.results, ('z', m))
Benjamin Petersonec19d952008-06-30 15:01:21 +000091 self.assertRaises(TypeError, exec, 'z = b', m)
Raymond Hettinger66bd2332004-08-02 08:30:07 +000092
93 class A:
94 "Non-mapping"
95 pass
96 m = A()
Benjamin Petersonec19d952008-06-30 15:01:21 +000097 self.assertRaises(TypeError, exec, 'z = a', g, m)
Raymond Hettinger66bd2332004-08-02 08:30:07 +000098
99 # Verify that dict subclasses work as well
100 class D(dict):
101 def __getitem__(self, key):
102 if key == 'a':
103 return 12
104 return dict.__getitem__(self, key)
105 d = D()
Georg Brandl7cae87c2006-09-06 06:51:57 +0000106 exec('z = a', g, d)
Raymond Hettinger66bd2332004-08-02 08:30:07 +0000107 self.assertEqual(d['z'], 12)
108
Neal Norwitz6ab080c2005-10-24 00:08:10 +0000109 def test_extended_arg(self):
110 longexpr = 'x = x or ' + '-x' * 2500
Georg Brandl7cae87c2006-09-06 06:51:57 +0000111 g = {}
Neal Norwitz6ab080c2005-10-24 00:08:10 +0000112 code = '''
113def f(x):
114 %s
115 %s
116 %s
117 %s
118 %s
119 %s
120 %s
121 %s
122 %s
123 %s
124 # the expressions above have no effect, x == argument
125 while x:
126 x -= 1
127 # EXTENDED_ARG/JUMP_ABSOLUTE here
128 return x
129''' % ((longexpr,)*10)
Georg Brandl7cae87c2006-09-06 06:51:57 +0000130 exec(code, g)
131 self.assertEqual(g['f'](5), 0)
Neal Norwitz6ab080c2005-10-24 00:08:10 +0000132
Raymond Hettinger8a99b502003-06-23 13:36:57 +0000133 def test_argument_order(self):
Benjamin Petersonec19d952008-06-30 15:01:21 +0000134 self.assertRaises(SyntaxError, exec, 'def f(a=1, b): pass')
Jeremy Hylton121b6eb2001-02-19 23:53:42 +0000135
Raymond Hettinger8a99b502003-06-23 13:36:57 +0000136 def test_float_literals(self):
137 # testing bad float literals
138 self.assertRaises(SyntaxError, eval, "2e")
139 self.assertRaises(SyntaxError, eval, "2.0e+")
140 self.assertRaises(SyntaxError, eval, "1e-")
141 self.assertRaises(SyntaxError, eval, "3-4e/21")
Jeremy Hylton121b6eb2001-02-19 23:53:42 +0000142
Raymond Hettinger8a99b502003-06-23 13:36:57 +0000143 def test_indentation(self):
144 # testing compile() of indented block w/o trailing newline"
145 s = """
Guido van Rossum4b499dd32003-02-13 22:07:59 +0000146if 1:
147 if 2:
148 pass"""
Raymond Hettinger8a99b502003-06-23 13:36:57 +0000149 compile(s, "<string>", "exec")
150
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000151 # This test is probably specific to CPython and may not generalize
152 # to other implementations. We are trying to ensure that when
153 # the first line of code starts after 256, correct line numbers
154 # in tracebacks are still produced.
155 def test_leading_newlines(self):
156 s256 = "".join(["\n"] * 256 + ["spam"])
157 co = compile(s256, 'fn', 'exec')
Mark Shannon877df852020-11-12 09:43:29 +0000158 self.assertEqual(co.co_firstlineno, 1)
Mark Shannon5977a792020-12-02 13:31:40 +0000159 self.assertEqual(list(co.co_lines()), [(0, 8, 257)])
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000160
Raymond Hettinger8a99b502003-06-23 13:36:57 +0000161 def test_literals_with_leading_zeroes(self):
162 for arg in ["077787", "0xj", "0x.", "0e", "090000000000000",
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000163 "080000000000000", "000000000000009", "000000000000008",
164 "0b42", "0BADCAFE", "0o123456789", "0b1.1", "0o4.2",
Miss Islington (bot)eeefa7f2021-06-08 16:52:23 -0700165 "0b101j", "0o153j", "0b100e1", "0o777e1", "0777",
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000166 "000777", "000000000000007"]:
Raymond Hettinger8a99b502003-06-23 13:36:57 +0000167 self.assertRaises(SyntaxError, eval, arg)
168
Raymond Hettinger8a99b502003-06-23 13:36:57 +0000169 self.assertEqual(eval("0xff"), 255)
Raymond Hettinger8a99b502003-06-23 13:36:57 +0000170 self.assertEqual(eval("0777."), 777)
171 self.assertEqual(eval("0777.0"), 777)
172 self.assertEqual(eval("000000000000000000000000000000000000000000000000000777e0"), 777)
173 self.assertEqual(eval("0777e1"), 7770)
174 self.assertEqual(eval("0e0"), 0)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000175 self.assertEqual(eval("0000e-012"), 0)
Raymond Hettinger8a99b502003-06-23 13:36:57 +0000176 self.assertEqual(eval("09.5"), 9.5)
177 self.assertEqual(eval("0777j"), 777j)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000178 self.assertEqual(eval("000"), 0)
Raymond Hettinger8a99b502003-06-23 13:36:57 +0000179 self.assertEqual(eval("00j"), 0j)
180 self.assertEqual(eval("00.0"), 0)
181 self.assertEqual(eval("0e3"), 0)
182 self.assertEqual(eval("090000000000000."), 90000000000000.)
183 self.assertEqual(eval("090000000000000.0000000000000000000000"), 90000000000000.)
184 self.assertEqual(eval("090000000000000e0"), 90000000000000.)
185 self.assertEqual(eval("090000000000000e-0"), 90000000000000.)
186 self.assertEqual(eval("090000000000000j"), 90000000000000j)
Raymond Hettinger8a99b502003-06-23 13:36:57 +0000187 self.assertEqual(eval("000000000000008."), 8.)
188 self.assertEqual(eval("000000000000009."), 9.)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000189 self.assertEqual(eval("0b101010"), 42)
190 self.assertEqual(eval("-0b000000000010"), -2)
191 self.assertEqual(eval("0o777"), 511)
192 self.assertEqual(eval("-0o0000010"), -8)
Raymond Hettinger8a99b502003-06-23 13:36:57 +0000193
194 def test_unary_minus(self):
195 # Verify treatment of unary minus on negative numbers SF bug #660455
Christian Heimesa37d4c62007-12-04 23:02:19 +0000196 if sys.maxsize == 2147483647:
Guido van Rossum6c9e1302003-11-29 23:52:13 +0000197 # 32-bit machine
198 all_one_bits = '0xffffffff'
Guido van Rossume2a383d2007-01-15 16:59:06 +0000199 self.assertEqual(eval(all_one_bits), 4294967295)
200 self.assertEqual(eval("-" + all_one_bits), -4294967295)
Christian Heimesa37d4c62007-12-04 23:02:19 +0000201 elif sys.maxsize == 9223372036854775807:
Guido van Rossum6c9e1302003-11-29 23:52:13 +0000202 # 64-bit machine
Raymond Hettinger8a99b502003-06-23 13:36:57 +0000203 all_one_bits = '0xffffffffffffffff'
Guido van Rossume2a383d2007-01-15 16:59:06 +0000204 self.assertEqual(eval(all_one_bits), 18446744073709551615)
205 self.assertEqual(eval("-" + all_one_bits), -18446744073709551615)
Guido van Rossum6c9e1302003-11-29 23:52:13 +0000206 else:
207 self.fail("How many bits *does* this machine have???")
Ezio Melotti42da6632011-03-15 05:18:48 +0200208 # Verify treatment of constant folding on -(sys.maxsize+1)
Serhiy Storchaka95949422013-08-27 19:40:23 +0300209 # i.e. -2147483648 on 32 bit platforms. Should return int.
Ezio Melottie9615932010-01-24 19:26:24 +0000210 self.assertIsInstance(eval("%s" % (-sys.maxsize - 1)), int)
211 self.assertIsInstance(eval("%s" % (-sys.maxsize - 2)), int)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000212
Christian Heimesa37d4c62007-12-04 23:02:19 +0000213 if sys.maxsize == 9223372036854775807:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000214 def test_32_63_bit_values(self):
215 a = +4294967296 # 1 << 32
216 b = -4294967296 # 1 << 32
217 c = +281474976710656 # 1 << 48
218 d = -281474976710656 # 1 << 48
219 e = +4611686018427387904 # 1 << 62
220 f = -4611686018427387904 # 1 << 62
221 g = +9223372036854775807 # 1 << 63 - 1
222 h = -9223372036854775807 # 1 << 63 - 1
223
Neal Norwitz221085d2007-02-25 20:55:47 +0000224 for variable in self.test_32_63_bit_values.__code__.co_consts:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000225 if variable is not None:
Ezio Melottie9615932010-01-24 19:26:24 +0000226 self.assertIsInstance(variable, int)
Raymond Hettinger8a99b502003-06-23 13:36:57 +0000227
228 def test_sequence_unpacking_error(self):
229 # Verify sequence packing/unpacking with "or". SF bug #757818
230 i,j = (1, -1) or (-1, 1)
231 self.assertEqual(i, 1)
232 self.assertEqual(j, -1)
Guido van Rossum4b499dd32003-02-13 22:07:59 +0000233
Raymond Hettinger11a70c72004-07-17 21:46:25 +0000234 def test_none_assignment(self):
235 stmts = [
236 'None = 0',
237 'None += 0',
238 '__builtins__.None = 0',
239 'def None(): pass',
240 'class None: pass',
241 '(a, None) = 0, 0',
242 'for None in range(10): pass',
243 'def f(None): pass',
Benjamin Peterson78565b22009-06-28 19:19:51 +0000244 'import None',
245 'import x as None',
246 'from x import None',
247 'from x import y as None'
Raymond Hettinger11a70c72004-07-17 21:46:25 +0000248 ]
249 for stmt in stmts:
250 stmt += "\n"
251 self.assertRaises(SyntaxError, compile, stmt, 'tmp', 'single')
252 self.assertRaises(SyntaxError, compile, stmt, 'tmp', 'exec')
Tim Petersd507dab2001-08-30 20:51:59 +0000253
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000254 def test_import(self):
255 succeed = [
256 'import sys',
257 'import os, sys',
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000258 'import os as bar',
259 'import os.path as bar',
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000260 'from __future__ import nested_scopes, generators',
261 'from __future__ import (nested_scopes,\ngenerators)',
262 'from __future__ import (nested_scopes,\ngenerators,)',
263 'from sys import stdin, stderr, stdout',
264 'from sys import (stdin, stderr,\nstdout)',
265 'from sys import (stdin, stderr,\nstdout,)',
266 'from sys import (stdin\n, stderr, stdout)',
267 'from sys import (stdin\n, stderr, stdout,)',
268 'from sys import stdin as si, stdout as so, stderr as se',
269 'from sys import (stdin as si, stdout as so, stderr as se)',
270 'from sys import (stdin as si, stdout as so, stderr as se,)',
271 ]
272 fail = [
273 'import (os, sys)',
274 'import (os), (sys)',
275 'import ((os), (sys))',
276 'import (sys',
277 'import sys)',
278 'import (os,)',
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000279 'import os As bar',
280 'import os.path a bar',
281 'from sys import stdin As stdout',
282 'from sys import stdin a stdout',
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000283 'from (sys) import stdin',
284 'from __future__ import (nested_scopes',
285 'from __future__ import nested_scopes)',
286 'from __future__ import nested_scopes,\ngenerators',
287 'from sys import (stdin',
288 'from sys import stdin)',
289 'from sys import stdin, stdout,\nstderr',
290 'from sys import stdin si',
Serhiy Storchaka34fd4c22018-11-05 16:20:25 +0200291 'from sys import stdin,',
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000292 'from sys import (*)',
293 'from sys import (stdin,, stdout, stderr)',
294 'from sys import (stdin, stdout),',
295 ]
296 for stmt in succeed:
297 compile(stmt, 'tmp', 'exec')
298 for stmt in fail:
299 self.assertRaises(SyntaxError, compile, stmt, 'tmp', 'exec')
300
Raymond Hettinger9047c8f2004-10-24 00:10:06 +0000301 def test_for_distinct_code_objects(self):
302 # SF bug 1048870
303 def f():
304 f1 = lambda x=1: x
305 f2 = lambda x=2: x
306 return f1, f2
307 f1, f2 = f()
Neal Norwitz221085d2007-02-25 20:55:47 +0000308 self.assertNotEqual(id(f1.__code__), id(f2.__code__))
Raymond Hettinger9047c8f2004-10-24 00:10:06 +0000309
Benjamin Petersone6dd2cb2010-03-17 20:56:58 +0000310 def test_lambda_doc(self):
311 l = lambda: "foo"
312 self.assertIsNone(l.__doc__)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000313
Serhiy Storchaka607cb9c2014-09-05 11:00:56 +0300314 def test_encoding(self):
315 code = b'# -*- coding: badencoding -*-\npass\n'
316 self.assertRaises(SyntaxError, compile, code, 'tmp', 'exec')
317 code = '# -*- coding: badencoding -*-\n"\xc2\xa4"\n'
318 compile(code, 'tmp', 'exec')
319 self.assertEqual(eval(code), '\xc2\xa4')
320 code = '"\xc2\xa4"\n'
321 self.assertEqual(eval(code), '\xc2\xa4')
322 code = b'"\xc2\xa4"\n'
323 self.assertEqual(eval(code), '\xa4')
324 code = b'# -*- coding: latin1 -*-\n"\xc2\xa4"\n'
325 self.assertEqual(eval(code), '\xc2\xa4')
326 code = b'# -*- coding: utf-8 -*-\n"\xc2\xa4"\n'
327 self.assertEqual(eval(code), '\xa4')
328 code = b'# -*- coding: iso8859-15 -*-\n"\xc2\xa4"\n'
329 self.assertEqual(eval(code), '\xc2\u20ac')
330 code = '"""\\\n# -*- coding: iso8859-15 -*-\n\xc2\xa4"""\n'
331 self.assertEqual(eval(code), '# -*- coding: iso8859-15 -*-\n\xc2\xa4')
332 code = b'"""\\\n# -*- coding: iso8859-15 -*-\n\xc2\xa4"""\n'
333 self.assertEqual(eval(code), '# -*- coding: iso8859-15 -*-\n\xa4')
Benjamin Peterson5d2ad252010-03-17 20:57:32 +0000334
Nick Coghlaneadee9a2006-03-13 12:31:58 +0000335 def test_subscripts(self):
336 # SF bug 1448804
337 # Class to make testing subscript results easy
338 class str_map(object):
339 def __init__(self):
340 self.data = {}
341 def __getitem__(self, key):
342 return self.data[str(key)]
343 def __setitem__(self, key, value):
344 self.data[str(key)] = value
345 def __delitem__(self, key):
346 del self.data[str(key)]
347 def __contains__(self, key):
348 return str(key) in self.data
349 d = str_map()
350 # Index
351 d[1] = 1
352 self.assertEqual(d[1], 1)
353 d[1] += 1
354 self.assertEqual(d[1], 2)
355 del d[1]
Ezio Melottib58e0bd2010-01-23 15:40:09 +0000356 self.assertNotIn(1, d)
Nick Coghlaneadee9a2006-03-13 12:31:58 +0000357 # Tuple of indices
358 d[1, 1] = 1
359 self.assertEqual(d[1, 1], 1)
360 d[1, 1] += 1
361 self.assertEqual(d[1, 1], 2)
362 del d[1, 1]
Ezio Melottib58e0bd2010-01-23 15:40:09 +0000363 self.assertNotIn((1, 1), d)
Nick Coghlaneadee9a2006-03-13 12:31:58 +0000364 # Simple slice
365 d[1:2] = 1
366 self.assertEqual(d[1:2], 1)
367 d[1:2] += 1
368 self.assertEqual(d[1:2], 2)
369 del d[1:2]
Ezio Melottib58e0bd2010-01-23 15:40:09 +0000370 self.assertNotIn(slice(1, 2), d)
Nick Coghlaneadee9a2006-03-13 12:31:58 +0000371 # Tuple of simple slices
372 d[1:2, 1:2] = 1
373 self.assertEqual(d[1:2, 1:2], 1)
374 d[1:2, 1:2] += 1
375 self.assertEqual(d[1:2, 1:2], 2)
376 del d[1:2, 1:2]
Ezio Melottib58e0bd2010-01-23 15:40:09 +0000377 self.assertNotIn((slice(1, 2), slice(1, 2)), d)
Nick Coghlaneadee9a2006-03-13 12:31:58 +0000378 # Extended slice
379 d[1:2:3] = 1
380 self.assertEqual(d[1:2:3], 1)
381 d[1:2:3] += 1
382 self.assertEqual(d[1:2:3], 2)
383 del d[1:2:3]
Ezio Melottib58e0bd2010-01-23 15:40:09 +0000384 self.assertNotIn(slice(1, 2, 3), d)
Nick Coghlaneadee9a2006-03-13 12:31:58 +0000385 # Tuple of extended slices
386 d[1:2:3, 1:2:3] = 1
387 self.assertEqual(d[1:2:3, 1:2:3], 1)
388 d[1:2:3, 1:2:3] += 1
389 self.assertEqual(d[1:2:3, 1:2:3], 2)
390 del d[1:2:3, 1:2:3]
Ezio Melottib58e0bd2010-01-23 15:40:09 +0000391 self.assertNotIn((slice(1, 2, 3), slice(1, 2, 3)), d)
Nick Coghlaneadee9a2006-03-13 12:31:58 +0000392 # Ellipsis
393 d[...] = 1
394 self.assertEqual(d[...], 1)
395 d[...] += 1
396 self.assertEqual(d[...], 2)
397 del d[...]
Ezio Melottib58e0bd2010-01-23 15:40:09 +0000398 self.assertNotIn(Ellipsis, d)
Nick Coghlaneadee9a2006-03-13 12:31:58 +0000399 # Tuple of Ellipses
400 d[..., ...] = 1
401 self.assertEqual(d[..., ...], 1)
402 d[..., ...] += 1
403 self.assertEqual(d[..., ...], 2)
404 del d[..., ...]
Ezio Melottib58e0bd2010-01-23 15:40:09 +0000405 self.assertNotIn((Ellipsis, Ellipsis), d)
Nick Coghlaneadee9a2006-03-13 12:31:58 +0000406
Guido van Rossum0240b922007-02-26 21:23:50 +0000407 def test_annotation_limit(self):
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +0200408 # more than 255 annotations, should compile ok
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000409 s = "def f(%s): pass"
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +0200410 s %= ', '.join('a%d:%d' % (i,i) for i in range(300))
Guido van Rossum0240b922007-02-26 21:23:50 +0000411 compile(s, '?', 'exec')
Guido van Rossumd8faa362007-04-27 19:54:29 +0000412
413 def test_mangling(self):
414 class A:
415 def f():
416 __mangled = 1
417 __not_mangled__ = 2
418 import __mangled_mod
419 import __package__.module
420
Benjamin Peterson577473f2010-01-19 00:09:57 +0000421 self.assertIn("_A__mangled", A.f.__code__.co_varnames)
422 self.assertIn("__not_mangled__", A.f.__code__.co_varnames)
423 self.assertIn("_A__mangled_mod", A.f.__code__.co_varnames)
424 self.assertIn("__package__", A.f.__code__.co_varnames)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000425
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000426 def test_compile_ast(self):
427 fname = __file__
Brett Cannonf299abd2015-04-13 14:21:02 -0400428 if fname.lower().endswith('pyc'):
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000429 fname = fname[:-1]
Inada Naoki35715d12021-04-04 09:01:23 +0900430 with open(fname, encoding='utf-8') as f:
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000431 fcontents = f.read()
432 sample_code = [
433 ['<assign>', 'x = 5'],
434 ['<ifblock>', """if True:\n pass\n"""],
435 ['<forblock>', """for n in [1, 2, 3]:\n print(n)\n"""],
436 ['<deffunc>', """def foo():\n pass\nfoo()\n"""],
437 [fname, fcontents],
438 ]
439
440 for fname, code in sample_code:
441 co1 = compile(code, '%s1' % fname, 'exec')
442 ast = compile(code, '%s2' % fname, 'exec', _ast.PyCF_ONLY_AST)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000443 self.assertTrue(type(ast) == _ast.Module)
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000444 co2 = compile(ast, '%s3' % fname, 'exec')
445 self.assertEqual(co1, co2)
Neal Norwitzdb4115f2008-03-31 04:20:05 +0000446 # the code object's filename comes from the second compilation step
447 self.assertEqual(co2.co_filename, '%s3' % fname)
448
449 # raise exception when node type doesn't match with compile mode
450 co1 = compile('print(1)', '<string>', 'exec', _ast.PyCF_ONLY_AST)
451 self.assertRaises(TypeError, compile, co1, '<ast>', 'eval')
452
453 # raise exception when node type is no start node
454 self.assertRaises(TypeError, compile, _ast.If(), '<ast>', 'exec')
455
456 # raise exception when node has invalid children
457 ast = _ast.Module()
458 ast.body = [_ast.BoolOp()]
459 self.assertRaises(TypeError, compile, ast, '<ast>', 'exec')
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000460
Benjamin Petersonee853392015-05-28 14:30:26 -0500461 def test_dict_evaluation_order(self):
462 i = 0
463
464 def f():
465 nonlocal i
466 i += 1
467 return i
468
469 d = {f(): f(), f(): f()}
470 self.assertEqual(d, {1: 2, 3: 4})
471
Serhiy Storchaka9305d832016-06-18 13:53:36 +0300472 def test_compile_filename(self):
Serhiy Storchakafebc3322016-08-06 23:29:29 +0300473 for filename in 'file.py', b'file.py':
Serhiy Storchaka9305d832016-06-18 13:53:36 +0300474 code = compile('pass', filename, 'exec')
475 self.assertEqual(code.co_filename, 'file.py')
Serhiy Storchakafebc3322016-08-06 23:29:29 +0300476 for filename in bytearray(b'file.py'), memoryview(b'file.py'):
477 with self.assertWarns(DeprecationWarning):
478 code = compile('pass', filename, 'exec')
479 self.assertEqual(code.co_filename, 'file.py')
Serhiy Storchaka9305d832016-06-18 13:53:36 +0300480 self.assertRaises(TypeError, compile, 'pass', list(b'file.py'), 'exec')
481
Benjamin Peterson43b06862011-05-27 09:08:01 -0500482 @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 Ingefa21bf02012-01-19 01:08:41 -0600490 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 Petersoncff92372012-01-19 17:46:13 -0500512 self.assertInvalidSingle('f()\n# blah\nblah()')
513 self.assertInvalidSingle('f()\nxy # blah\nblah()')
Benjamin Peterson77fc1f32012-01-20 11:01:06 -0500514 self.assertInvalidSingle('x = 5 # comment\nx = 6\n')
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000515
Benjamin Petersond73aca72015-04-21 12:05:19 -0400516 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 Storchaka0d441112015-11-14 15:10:35 +0200526 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 Coghlanaab9c2b2012-11-04 23:14:34 +1000536 @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
Serhiy Storchakaface87c2021-04-25 13:38:00 +0300546 crash_depth = sys.getrecursionlimit() * 300
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000547 success_depth = int(fail_depth * 0.75)
548
Serhiy Storchakaface87c2021-04-25 13:38:00 +0300549 def check_limit(prefix, repeated, mode="single"):
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000550 expect_ok = prefix + repeated * success_depth
Serhiy Storchakaface87c2021-04-25 13:38:00 +0300551 compile(expect_ok, '<test>', mode)
552 for depth in (fail_depth, crash_depth):
553 broken = prefix + repeated * depth
554 details = "Compiling ({!r} + {!r} * {})".format(
555 prefix, repeated, depth)
556 with self.assertRaises(RecursionError, msg=details):
557 compile(broken, '<test>', mode)
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000558
559 check_limit("a", "()")
560 check_limit("a", ".b")
561 check_limit("a", "[0]")
562 check_limit("a", "*a")
Serhiy Storchakaface87c2021-04-25 13:38:00 +0300563 # XXX Crashes in the parser.
564 # check_limit("a", " if a else a")
565 # check_limit("if a: pass", "\nelif a: pass", mode="exec")
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000566
Martin Pantereeb896c2015-11-07 02:32:21 +0000567 def test_null_terminated(self):
568 # The source code is null-terminated internally, but bytes-like
569 # objects are accepted, which could be not terminated.
Martin Panterd61d8602015-11-08 11:09:13 +0000570 with self.assertRaisesRegex(ValueError, "cannot contain null"):
Martin Pantereeb896c2015-11-07 02:32:21 +0000571 compile("123\x00", "<dummy>", "eval")
Martin Panterd61d8602015-11-08 11:09:13 +0000572 with self.assertRaisesRegex(ValueError, "cannot contain null"):
Martin Pantereeb896c2015-11-07 02:32:21 +0000573 compile(memoryview(b"123\x00"), "<dummy>", "eval")
574 code = compile(memoryview(b"123\x00")[1:-1], "<dummy>", "eval")
575 self.assertEqual(eval(code), 23)
576 code = compile(memoryview(b"1234")[1:-1], "<dummy>", "eval")
577 self.assertEqual(eval(code), 23)
578 code = compile(memoryview(b"$23$")[1:-1], "<dummy>", "eval")
579 self.assertEqual(eval(code), 23)
580
581 # Also test when eval() and exec() do the compilation step
582 self.assertEqual(eval(memoryview(b"1234")[1:-1]), 23)
583 namespace = dict()
584 exec(memoryview(b"ax = 123")[1:-1], namespace)
585 self.assertEqual(namespace['x'], 12)
586
Victor Stinner3cdd5fb2016-01-22 12:33:12 +0100587 def check_constant(self, func, expected):
588 for const in func.__code__.co_consts:
589 if repr(const) == repr(expected):
590 break
591 else:
592 self.fail("unable to find constant %r in %r"
593 % (expected, func.__code__.co_consts))
594
595 # Merging equal constants is not a strict requirement for the Python
596 # semantics, it's a more an implementation detail.
597 @support.cpython_only
598 def test_merge_constants(self):
599 # Issue #25843: compile() must merge constants which are equal
600 # and have the same type.
601
602 def check_same_constant(const):
603 ns = {}
604 code = "f1, f2 = lambda: %r, lambda: %r" % (const, const)
605 exec(code, ns)
606 f1 = ns['f1']
607 f2 = ns['f2']
608 self.assertIs(f1.__code__, f2.__code__)
609 self.check_constant(f1, const)
610 self.assertEqual(repr(f1()), repr(const))
611
612 check_same_constant(None)
613 check_same_constant(0)
614 check_same_constant(0.0)
615 check_same_constant(b'abc')
616 check_same_constant('abc')
617
618 # Note: "lambda: ..." emits "LOAD_CONST Ellipsis",
619 # whereas "lambda: Ellipsis" emits "LOAD_GLOBAL Ellipsis"
620 f1, f2 = lambda: ..., lambda: ...
621 self.assertIs(f1.__code__, f2.__code__)
622 self.check_constant(f1, Ellipsis)
623 self.assertEqual(repr(f1()), repr(Ellipsis))
624
INADA Naokif7e4d362018-11-29 00:58:46 +0900625 # Merge constants in tuple or frozenset
626 f1, f2 = lambda: "not a name", lambda: ("not a name",)
627 f3 = lambda x: x in {("not a name",)}
628 self.assertIs(f1.__code__.co_consts[1],
629 f2.__code__.co_consts[1][0])
630 self.assertIs(next(iter(f3.__code__.co_consts[1])),
631 f2.__code__.co_consts[1])
632
Victor Stinner3cdd5fb2016-01-22 12:33:12 +0100633 # {0} is converted to a constant frozenset({0}) by the peephole
634 # optimizer
635 f1, f2 = lambda x: x in {0}, lambda x: x in {0}
636 self.assertIs(f1.__code__, f2.__code__)
637 self.check_constant(f1, frozenset({0}))
638 self.assertTrue(f1(0))
639
Inada Naokibdb941b2021-02-10 09:20:42 +0900640 # Merging equal co_linetable and co_code is not a strict requirement
641 # for the Python semantics, it's a more an implementation detail.
642 @support.cpython_only
643 def test_merge_code_attrs(self):
644 # See https://bugs.python.org/issue42217
645 f1 = lambda x: x.y.z
646 f2 = lambda a: a.b.c
647
648 self.assertIs(f1.__code__.co_linetable, f2.__code__.co_linetable)
649 self.assertIs(f1.__code__.co_code, f2.__code__.co_code)
650
Łukasz Langad41abe82021-09-08 18:25:09 +0200651 # Stripping unused constants is not a strict requirement for the
652 # Python semantics, it's a more an implementation detail.
653 @support.cpython_only
654 def test_strip_unused_consts(self):
655 # Python 3.10rc1 appended None to co_consts when None is not used
656 # at all. See bpo-45056.
657 def f1():
658 "docstring"
659 return 42
660 self.assertEqual(f1.__code__.co_consts, ("docstring", 42))
661
Gregory P. Smith49fa4a92018-11-08 17:55:07 -0800662 # This is a regression test for a CPython specific peephole optimizer
663 # implementation bug present in a few releases. It's assertion verifies
664 # that peephole optimization was actually done though that isn't an
665 # indication of the bugs presence or not (crashing is).
666 @support.cpython_only
667 def test_peephole_opt_unreachable_code_array_access_in_bounds(self):
668 """Regression test for issue35193 when run under clang msan."""
669 def unused_code_at_end():
670 return 3
671 raise RuntimeError("unreachable")
672 # The above function definition will trigger the out of bounds
673 # bug in the peephole optimizer as it scans opcodes past the
674 # RETURN_VALUE opcode. This does not always crash an interpreter.
675 # When you build with the clang memory sanitizer it reliably aborts.
676 self.assertEqual(
677 'RETURN_VALUE',
678 list(dis.get_instructions(unused_code_at_end))[-1].opname)
679
Victor Stinner3cdd5fb2016-01-22 12:33:12 +0100680 def test_dont_merge_constants(self):
681 # Issue #25843: compile() must not merge constants which are equal
682 # but have a different type.
683
684 def check_different_constants(const1, const2):
685 ns = {}
686 exec("f1, f2 = lambda: %r, lambda: %r" % (const1, const2), ns)
687 f1 = ns['f1']
688 f2 = ns['f2']
689 self.assertIsNot(f1.__code__, f2.__code__)
Serhiy Storchaka713640c2017-01-24 20:49:26 +0200690 self.assertNotEqual(f1.__code__, f2.__code__)
Victor Stinner3cdd5fb2016-01-22 12:33:12 +0100691 self.check_constant(f1, const1)
692 self.check_constant(f2, const2)
693 self.assertEqual(repr(f1()), repr(const1))
694 self.assertEqual(repr(f2()), repr(const2))
695
696 check_different_constants(0, 0.0)
697 check_different_constants(+0.0, -0.0)
698 check_different_constants((0,), (0.0,))
Serhiy Storchaka713640c2017-01-24 20:49:26 +0200699 check_different_constants('a', b'a')
700 check_different_constants(('a',), (b'a',))
Victor Stinner3cdd5fb2016-01-22 12:33:12 +0100701
702 # check_different_constants() cannot be used because repr(-0j) is
703 # '(-0-0j)', but when '(-0-0j)' is evaluated to 0j: we loose the sign.
704 f1, f2 = lambda: +0.0j, lambda: -0.0j
705 self.assertIsNot(f1.__code__, f2.__code__)
706 self.check_constant(f1, +0.0j)
707 self.check_constant(f2, -0.0j)
708 self.assertEqual(repr(f1()), repr(+0.0j))
709 self.assertEqual(repr(f2()), repr(-0.0j))
710
711 # {0} is converted to a constant frozenset({0}) by the peephole
712 # optimizer
713 f1, f2 = lambda x: x in {0}, lambda x: x in {0.0}
714 self.assertIsNot(f1.__code__, f2.__code__)
715 self.check_constant(f1, frozenset({0}))
716 self.check_constant(f2, frozenset({0.0}))
717 self.assertTrue(f1(0))
718 self.assertTrue(f2(0.0))
719
Brett Cannona5711202016-09-06 19:36:01 -0700720 def test_path_like_objects(self):
721 # An implicit test for PyUnicode_FSDecoder().
Serhiy Storchakab21d1552018-03-02 11:53:51 +0200722 compile("42", FakePath("test_compile_pathlike"), "single")
Brett Cannona5711202016-09-06 19:36:01 -0700723
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +0200724 def test_stack_overflow(self):
725 # bpo-31113: Stack overflow when compile a long sequence of
726 # complex statements.
727 compile("if a: b\n" * 200000, "<dummy>", "exec")
728
Pablo Galindo18c5f9d2019-07-15 10:15:01 +0100729 # Multiple users rely on the fact that CPython does not generate
730 # bytecode for dead code blocks. See bpo-37500 for more context.
731 @support.cpython_only
732 def test_dead_blocks_do_not_generate_bytecode(self):
733 def unused_block_if():
734 if 0:
735 return 42
736
737 def unused_block_while():
738 while 0:
739 return 42
740
741 def unused_block_if_else():
742 if 1:
743 return None
744 else:
745 return 42
746
747 def unused_block_while_else():
748 while 1:
749 return None
750 else:
751 return 42
752
753 funcs = [unused_block_if, unused_block_while,
754 unused_block_if_else, unused_block_while_else]
755
756 for func in funcs:
757 opcodes = list(dis.get_instructions(func))
Mark Shannon8473cf82020-12-15 11:07:50 +0000758 self.assertLessEqual(len(opcodes), 3)
759 self.assertEqual('LOAD_CONST', opcodes[-2].opname)
760 self.assertEqual(None, opcodes[-2].argval)
761 self.assertEqual('RETURN_VALUE', opcodes[-1].opname)
Pablo Galindo18c5f9d2019-07-15 10:15:01 +0100762
Pablo Galindo6c3e66a2019-10-30 11:53:26 +0000763 def test_false_while_loop(self):
764 def break_in_while():
765 while False:
766 break
767
768 def continue_in_while():
769 while False:
770 continue
771
772 funcs = [break_in_while, continue_in_while]
773
774 # Check that we did not raise but we also don't generate bytecode
775 for func in funcs:
776 opcodes = list(dis.get_instructions(func))
777 self.assertEqual(2, len(opcodes))
778 self.assertEqual('LOAD_CONST', opcodes[0].opname)
779 self.assertEqual(None, opcodes[0].argval)
780 self.assertEqual('RETURN_VALUE', opcodes[1].opname)
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000781
Mark Shannon266b4622020-11-17 19:30:14 +0000782 def test_consts_in_conditionals(self):
783 def and_true(x):
784 return True and x
785
786 def and_false(x):
787 return False and x
788
789 def or_true(x):
790 return True or x
791
792 def or_false(x):
793 return False or x
794
795 funcs = [and_true, and_false, or_true, or_false]
796
797 # Check that condition is removed.
798 for func in funcs:
799 with self.subTest(func=func):
800 opcodes = list(dis.get_instructions(func))
801 self.assertEqual(2, len(opcodes))
802 self.assertIn('LOAD_', opcodes[0].opname)
803 self.assertEqual('RETURN_VALUE', opcodes[1].opname)
804
Mark Shannonc5440932021-03-15 14:24:25 +0000805 def test_lineno_procedure_call(self):
806 def call():
807 (
808 print()
809 )
810 line1 = call.__code__.co_firstlineno + 1
811 assert line1 not in [line for (_, _, line) in call.__code__.co_lines()]
812
Mark Shannon5977a792020-12-02 13:31:40 +0000813 def test_lineno_after_implicit_return(self):
814 TRUE = True
815 # Don't use constant True or False, as compiler will remove test
816 def if1(x):
817 x()
818 if TRUE:
819 pass
820 def if2(x):
821 x()
822 if TRUE:
823 pass
824 else:
825 pass
826 def if3(x):
827 x()
828 if TRUE:
829 pass
830 else:
831 return None
832 def if4(x):
833 x()
834 if not TRUE:
835 pass
836 funcs = [ if1, if2, if3, if4]
837 lastlines = [ 3, 3, 3, 2]
838 frame = None
839 def save_caller_frame():
840 nonlocal frame
841 frame = sys._getframe(1)
842 for func, lastline in zip(funcs, lastlines, strict=True):
843 with self.subTest(func=func):
844 func(save_caller_frame)
845 self.assertEqual(frame.f_lineno-frame.f_code.co_firstlineno, lastline)
Mark Shannon266b4622020-11-17 19:30:14 +0000846
Mark Shannoneaccc122020-12-04 15:22:12 +0000847 def test_lineno_after_no_code(self):
848 def no_code1():
849 "doc string"
850
851 def no_code2():
852 a: int
853
854 for func in (no_code1, no_code2):
855 with self.subTest(func=func):
856 code = func.__code__
857 lines = list(code.co_lines())
858 self.assertEqual(len(lines), 1)
859 start, end, line = lines[0]
860 self.assertEqual(start, 0)
861 self.assertEqual(end, len(code.co_code))
862 self.assertEqual(line, code.co_firstlineno)
863
Mark Shannond48848c2021-03-14 18:01:30 +0000864 def test_lineno_attribute(self):
865 def load_attr():
866 return (
867 o.
868 a
869 )
870 load_attr_lines = [ 2, 3, 1 ]
871
872 def load_method():
873 return (
874 o.
875 m(
876 0
877 )
878 )
879 load_method_lines = [ 2, 3, 4, 3, 1 ]
880
881 def store_attr():
882 (
883 o.
884 a
885 ) = (
886 v
887 )
888 store_attr_lines = [ 5, 2, 3 ]
889
890 def aug_store_attr():
891 (
892 o.
893 a
894 ) += (
895 v
896 )
897 aug_store_attr_lines = [ 2, 3, 5, 1, 3 ]
898
899 funcs = [ load_attr, load_method, store_attr, aug_store_attr]
900 func_lines = [ load_attr_lines, load_method_lines,
901 store_attr_lines, aug_store_attr_lines]
902
903 for func, lines in zip(funcs, func_lines, strict=True):
904 with self.subTest(func=func):
905 code_lines = [ line-func.__code__.co_firstlineno
906 for (_, _, line) in func.__code__.co_lines() ]
907 self.assertEqual(lines, code_lines)
908
Mark Shannon7674c832021-06-21 11:47:16 +0100909 def test_line_number_genexp(self):
910
911 def return_genexp():
912 return (1
913 for
914 x
915 in
916 y)
917 genexp_lines = [None, 1, 3, 1]
918
919 genexp_code = return_genexp.__code__.co_consts[1]
Mark Shannon47695e32021-07-15 15:54:38 +0100920 code_lines = [ None if line is None else line-return_genexp.__code__.co_firstlineno
Mark Shannon7674c832021-06-21 11:47:16 +0100921 for (_, _, line) in genexp_code.co_lines() ]
922 self.assertEqual(genexp_lines, code_lines)
923
Mark Shannon47695e32021-07-15 15:54:38 +0100924 def test_line_number_implicit_return_after_async_for(self):
925
926 async def test(aseq):
927 async for i in aseq:
928 body
929
930 expected_lines = [None, 1, 2, 1]
931 code_lines = [ None if line is None else line-test.__code__.co_firstlineno
932 for (_, _, line) in test.__code__.co_lines() ]
933 self.assertEqual(expected_lines, code_lines)
Mark Shannoneaccc122020-12-04 15:22:12 +0000934
Pablo Galindoc51db0e2020-08-13 09:48:41 +0100935 def test_big_dict_literal(self):
936 # The compiler has a flushing point in "compiler_dict" that calls compiles
937 # a portion of the dictionary literal when the loop that iterates over the items
938 # reaches 0xFFFF elements but the code was not including the boundary element,
939 # dropping the key at position 0xFFFF. See bpo-41531 for more information
940
941 dict_size = 0xFFFF + 1
942 the_dict = "{" + ",".join(f"{x}:{x}" for x in range(dict_size)) + "}"
943 self.assertEqual(len(eval(the_dict)), dict_size)
944
Om Gc71581c2020-12-16 17:48:05 +0530945 def test_redundant_jump_in_if_else_break(self):
946 # Check if bytecode containing jumps that simply point to the next line
947 # is generated around if-else-break style structures. See bpo-42615.
948
949 def if_else_break():
950 val = 1
951 while True:
952 if val > 0:
953 val -= 1
954 else:
955 break
956 val = -1
957
958 INSTR_SIZE = 2
959 HANDLED_JUMPS = (
960 'POP_JUMP_IF_FALSE',
961 'POP_JUMP_IF_TRUE',
962 'JUMP_ABSOLUTE',
963 'JUMP_FORWARD',
964 )
965
966 for line, instr in enumerate(dis.Bytecode(if_else_break)):
967 if instr.opname == 'JUMP_FORWARD':
968 self.assertNotEqual(instr.arg, 0)
969 elif instr.opname in HANDLED_JUMPS:
970 self.assertNotEqual(instr.arg, (line + 1)*INSTR_SIZE)
971
972
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200973class TestExpressionStackSize(unittest.TestCase):
Antoine Pitrou99614052014-05-23 11:46:03 +0200974 # These tests check that the computed stack size for a code object
975 # stays within reasonable bounds (see issue #21523 for an example
976 # dysfunction).
977 N = 100
978
979 def check_stack_size(self, code):
980 # To assert that the alleged stack size is not O(N), we
981 # check that it is smaller than log(N).
982 if isinstance(code, str):
983 code = compile(code, "<foo>", "single")
984 max_size = math.ceil(math.log(len(code.co_code)))
985 self.assertLessEqual(code.co_stacksize, max_size)
986
987 def test_and(self):
988 self.check_stack_size("x and " * self.N + "x")
989
990 def test_or(self):
991 self.check_stack_size("x or " * self.N + "x")
992
993 def test_and_or(self):
994 self.check_stack_size("x and x or " * self.N + "x")
995
996 def test_chained_comparison(self):
997 self.check_stack_size("x < " * self.N + "x")
998
999 def test_if_else(self):
1000 self.check_stack_size("x if x else " * self.N + "x")
1001
1002 def test_binop(self):
1003 self.check_stack_size("x + " * self.N + "x")
1004
Mark Shannon11e0b292021-04-15 14:28:56 +01001005 def test_list(self):
1006 self.check_stack_size("[" + "x, " * self.N + "x]")
1007
1008 def test_tuple(self):
1009 self.check_stack_size("(" + "x, " * self.N + "x)")
1010
1011 def test_set(self):
1012 self.check_stack_size("{" + "x, " * self.N + "x}")
1013
1014 def test_dict(self):
1015 self.check_stack_size("{" + "x:x, " * self.N + "x:x}")
1016
1017 def test_func_args(self):
1018 self.check_stack_size("f(" + "x, " * self.N + ")")
1019
1020 def test_func_kwargs(self):
1021 kwargs = (f'a{i}=x' for i in range(self.N))
1022 self.check_stack_size("f(" + ", ".join(kwargs) + ")")
1023
1024 def test_func_args(self):
1025 self.check_stack_size("o.m(" + "x, " * self.N + ")")
1026
1027 def test_meth_kwargs(self):
1028 kwargs = (f'a{i}=x' for i in range(self.N))
1029 self.check_stack_size("o.m(" + ", ".join(kwargs) + ")")
1030
Antoine Pitrou99614052014-05-23 11:46:03 +02001031 def test_func_and(self):
1032 code = "def f(x):\n"
1033 code += " x and x\n" * self.N
1034 self.check_stack_size(code)
1035
Tim Petersd507dab2001-08-30 20:51:59 +00001036
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001037class TestStackSizeStability(unittest.TestCase):
1038 # Check that repeating certain snippets doesn't increase the stack size
1039 # beyond what a single snippet requires.
1040
1041 def check_stack_size(self, snippet, async_=False):
1042 def compile_snippet(i):
1043 ns = {}
1044 script = """def func():\n""" + i * snippet
1045 if async_:
1046 script = "async " + script
1047 code = compile(script, "<script>", "exec")
1048 exec(code, ns, ns)
1049 return ns['func'].__code__
1050
1051 sizes = [compile_snippet(i).co_stacksize for i in range(2, 5)]
1052 if len(set(sizes)) != 1:
1053 import dis, io
1054 out = io.StringIO()
1055 dis.dis(compile_snippet(1), file=out)
1056 self.fail("stack sizes diverge with # of consecutive snippets: "
1057 "%s\n%s\n%s" % (sizes, snippet, out.getvalue()))
1058
1059 def test_if(self):
1060 snippet = """
1061 if x:
1062 a
1063 """
1064 self.check_stack_size(snippet)
1065
1066 def test_if_else(self):
1067 snippet = """
1068 if x:
1069 a
1070 elif y:
1071 b
1072 else:
1073 c
1074 """
1075 self.check_stack_size(snippet)
1076
1077 def test_try_except_bare(self):
1078 snippet = """
1079 try:
1080 a
1081 except:
1082 b
1083 """
1084 self.check_stack_size(snippet)
1085
1086 def test_try_except_qualified(self):
1087 snippet = """
1088 try:
1089 a
1090 except ImportError:
1091 b
1092 except:
1093 c
1094 else:
1095 d
1096 """
1097 self.check_stack_size(snippet)
1098
1099 def test_try_except_as(self):
1100 snippet = """
1101 try:
1102 a
1103 except ImportError as e:
1104 b
1105 except:
1106 c
1107 else:
1108 d
1109 """
1110 self.check_stack_size(snippet)
1111
1112 def test_try_finally(self):
1113 snippet = """
1114 try:
1115 a
1116 finally:
1117 b
1118 """
1119 self.check_stack_size(snippet)
1120
1121 def test_with(self):
1122 snippet = """
1123 with x as y:
1124 a
1125 """
1126 self.check_stack_size(snippet)
1127
1128 def test_while_else(self):
1129 snippet = """
1130 while x:
1131 a
1132 else:
1133 b
1134 """
1135 self.check_stack_size(snippet)
1136
1137 def test_for(self):
1138 snippet = """
1139 for x in y:
1140 a
1141 """
1142 self.check_stack_size(snippet)
1143
1144 def test_for_else(self):
1145 snippet = """
1146 for x in y:
1147 a
1148 else:
1149 b
1150 """
1151 self.check_stack_size(snippet)
1152
1153 def test_for_break_continue(self):
1154 snippet = """
1155 for x in y:
1156 if z:
1157 break
1158 elif u:
1159 continue
1160 else:
1161 a
1162 else:
1163 b
1164 """
1165 self.check_stack_size(snippet)
1166
1167 def test_for_break_continue_inside_try_finally_block(self):
1168 snippet = """
1169 for x in y:
1170 try:
1171 if z:
1172 break
1173 elif u:
1174 continue
1175 else:
1176 a
1177 finally:
1178 f
1179 else:
1180 b
1181 """
1182 self.check_stack_size(snippet)
1183
Serhiy Storchakafe2bbb12018-03-18 09:56:52 +02001184 def test_for_break_continue_inside_finally_block(self):
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001185 snippet = """
1186 for x in y:
1187 try:
1188 t
1189 finally:
1190 if z:
1191 break
Serhiy Storchakafe2bbb12018-03-18 09:56:52 +02001192 elif u:
1193 continue
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001194 else:
1195 a
1196 else:
1197 b
1198 """
1199 self.check_stack_size(snippet)
1200
1201 def test_for_break_continue_inside_except_block(self):
1202 snippet = """
1203 for x in y:
1204 try:
1205 t
1206 except:
1207 if z:
1208 break
1209 elif u:
1210 continue
1211 else:
1212 a
1213 else:
1214 b
1215 """
1216 self.check_stack_size(snippet)
1217
1218 def test_for_break_continue_inside_with_block(self):
1219 snippet = """
1220 for x in y:
1221 with c:
1222 if z:
1223 break
1224 elif u:
1225 continue
1226 else:
1227 a
1228 else:
1229 b
1230 """
1231 self.check_stack_size(snippet)
1232
1233 def test_return_inside_try_finally_block(self):
1234 snippet = """
1235 try:
1236 if z:
1237 return
1238 else:
1239 a
1240 finally:
1241 f
1242 """
1243 self.check_stack_size(snippet)
1244
1245 def test_return_inside_finally_block(self):
1246 snippet = """
1247 try:
1248 t
1249 finally:
1250 if z:
1251 return
1252 else:
1253 a
1254 """
1255 self.check_stack_size(snippet)
1256
1257 def test_return_inside_except_block(self):
1258 snippet = """
1259 try:
1260 t
1261 except:
1262 if z:
1263 return
1264 else:
1265 a
1266 """
1267 self.check_stack_size(snippet)
1268
1269 def test_return_inside_with_block(self):
1270 snippet = """
1271 with c:
1272 if z:
1273 return
1274 else:
1275 a
1276 """
1277 self.check_stack_size(snippet)
1278
1279 def test_async_with(self):
1280 snippet = """
1281 async with x as y:
1282 a
1283 """
1284 self.check_stack_size(snippet, async_=True)
1285
1286 def test_async_for(self):
1287 snippet = """
1288 async for x in y:
1289 a
1290 """
1291 self.check_stack_size(snippet, async_=True)
1292
1293 def test_async_for_else(self):
1294 snippet = """
1295 async for x in y:
1296 a
1297 else:
1298 b
1299 """
1300 self.check_stack_size(snippet, async_=True)
1301
1302 def test_for_break_continue_inside_async_with_block(self):
1303 snippet = """
1304 for x in y:
1305 async with c:
1306 if z:
1307 break
1308 elif u:
1309 continue
1310 else:
1311 a
1312 else:
1313 b
1314 """
1315 self.check_stack_size(snippet, async_=True)
1316
1317 def test_return_inside_async_with_block(self):
1318 snippet = """
1319 async with c:
1320 if z:
1321 return
1322 else:
1323 a
1324 """
1325 self.check_stack_size(snippet, async_=True)
1326
1327
Raymond Hettinger8a99b502003-06-23 13:36:57 +00001328if __name__ == "__main__":
Antoine Pitrou99614052014-05-23 11:46:03 +02001329 unittest.main()