Debian Codegraph @

Lib/test/test_compile.py

raw
1 import contextlib
2 import dis
3 import io
4 import itertools
5 import math
6 import opcode
7 import os
8 import unittest
9 import sys
10 import ast
11 import _ast
12 import tempfile
13 import types
14 import textwrap
15 import warnings
16 try:
17 import _testinternalcapi
18 except ImportError:
19 _testinternalcapi = None
20
21 from test import support
22 from test.support import (script_helper, requires_debug_ranges, run_code,
23 requires_specialization)
24 from test.support.bytecode_helper import instructions_with_positions
25 from test.support.os_helper import FakePath
26
27 class TestSpecifics(unittest.TestCase):
28
29 def compile_single(self, source):
30 compile(source, "<single>", "single")
31
32 def assertInvalidSingle(self, source):
33 self.assertRaises(SyntaxError, self.compile_single, source)
34
35 def test_no_ending_newline(self):
36 compile("hi", "<test>", "exec")
37 compile("hi\r", "<test>", "exec")
38
39 def test_empty(self):
40 compile("", "<test>", "exec")
41
42 def test_other_newlines(self):
43 compile("\r\n", "<test>", "exec")
44 compile("\r", "<test>", "exec")
45 compile("hi\r\nstuff\r\ndef f():\n pass\r", "<test>", "exec")
46 compile("this_is\rreally_old_mac\rdef f():\n pass", "<test>", "exec")
47
48 def test_debug_assignment(self):
49 # catch assignments to __debug__
50 self.assertRaises(SyntaxError, compile, '__debug__ = 1', '?', 'single')
51 import builtins
52 prev = builtins.__debug__
53 setattr(builtins, '__debug__', 'sure')
54 self.assertEqual(__debug__, prev)
55 setattr(builtins, '__debug__', prev)
56
57 def test_argument_handling(self):
58 # detect duplicate positional and keyword arguments
59 self.assertRaises(SyntaxError, eval, 'lambda a,a:0')
60 self.assertRaises(SyntaxError, eval, 'lambda a,a=1:0')
61 self.assertRaises(SyntaxError, eval, 'lambda a=1,a=1:0')
62 self.assertRaises(SyntaxError, exec, 'def f(a, a): pass')
63 self.assertRaises(SyntaxError, exec, 'def f(a = 0, a = 1): pass')
64 self.assertRaises(SyntaxError, exec, 'def f(a): global a; a = 1')
65
66 def test_syntax_error(self):
67 self.assertRaises(SyntaxError, compile, "1+*3", "filename", "exec")
68
69 def test_none_keyword_arg(self):
70 self.assertRaises(SyntaxError, compile, "f(None=1)", "<string>", "exec")
71
72 def test_duplicate_global_local(self):
73 self.assertRaises(SyntaxError, exec, 'def f(a): global a; a = 1')
74
75 def test_exec_with_general_mapping_for_locals(self):
76
77 class M:
78 "Test mapping interface versus possible calls from eval()."
79 def __getitem__(self, key):
80 if key == 'a':
81 return 12
82 raise KeyError
83 def __setitem__(self, key, value):
84 self.results = (key, value)
85 def keys(self):
86 return list('xyz')
87
88 m = M()
89 g = globals()
90 exec('z = a', g, m)
91 self.assertEqual(m.results, ('z', 12))
92 try:
93 exec('z = b', g, m)
94 except NameError:
95 pass
96 else:
97 self.fail('Did not detect a KeyError')
98 exec('z = dir()', g, m)
99 self.assertEqual(m.results, ('z', list('xyz')))
100 exec('z = globals()', g, m)
101 self.assertEqual(m.results, ('z', g))
102 exec('z = locals()', g, m)
103 self.assertEqual(m.results, ('z', m))
104 self.assertRaises(TypeError, exec, 'z = b', m)
105
106 class A:
107 "Non-mapping"
108 pass
109 m = A()
110 self.assertRaises(TypeError, exec, 'z = a', g, m)
111
112 # Verify that dict subclasses work as well
113 class D(dict):
114 def __getitem__(self, key):
115 if key == 'a':
116 return 12
117 return dict.__getitem__(self, key)
118 d = D()
119 exec('z = a', g, d)
120 self.assertEqual(d['z'], 12)
121
122 @unittest.skipIf(support.is_wasi, "exhausts limited stack on WASI")
123 @support.skip_emscripten_stack_overflow()
124 def test_extended_arg(self):
125 repeat = 100
126 longexpr = 'x = x or ' + '-x' * repeat
127 g = {}
128 code = textwrap.dedent('''
129 def f(x):
130 %s
131 %s
132 %s
133 %s
134 %s
135 %s
136 %s
137 %s
138 %s
139 %s
140 # the expressions above have no effect, x == argument
141 while x:
142 x -= 1
143 # EXTENDED_ARG/JUMP_ABSOLUTE here
144 return x
145 ''' % ((longexpr,)*10))
146 exec(code, g)
147 self.assertEqual(g['f'](5), 0)
148
149 def test_argument_order(self):
150 self.assertRaises(SyntaxError, exec, 'def f(a=1, b): pass')
151
152 def test_float_literals(self):
153 # testing bad float literals
154 self.assertRaises(SyntaxError, eval, "2e")
155 self.assertRaises(SyntaxError, eval, "2.0e+")
156 self.assertRaises(SyntaxError, eval, "1e-")
157 self.assertRaises(SyntaxError, eval, "3-4e/21")
158
159 def test_indentation(self):
160 # testing compile() of indented block w/o trailing newline"
161 s = textwrap.dedent("""
162 if 1:
163 if 2:
164 pass
165 """)
166 compile(s, "<string>", "exec")
167
168 # This test is probably specific to CPython and may not generalize
169 # to other implementations. We are trying to ensure that when
170 # the first line of code starts after 256, correct line numbers
171 # in tracebacks are still produced.
172 def test_leading_newlines(self):
173 s256 = "".join(["\n"] * 256 + ["spam"])
174 co = compile(s256, 'fn', 'exec')
175 self.assertEqual(co.co_firstlineno, 1)
176 lines = [line for _, _, line in co.co_lines()]
177 self.assertEqual(lines, [0, 257])
178
179 def test_literals_with_leading_zeroes(self):
180 for arg in ["077787", "0xj", "0x.", "0e", "090000000000000",
181 "080000000000000", "000000000000009", "000000000000008",
182 "0b42", "0BADCAFE", "0o123456789", "0b1.1", "0o4.2",
183 "0b101j", "0o153j", "0b100e1", "0o777e1", "0777",
184 "000777", "000000000000007"]:
185 self.assertRaises(SyntaxError, eval, arg)
186
187 self.assertEqual(eval("0xff"), 255)
188 self.assertEqual(eval("0777."), 777)
189 self.assertEqual(eval("0777.0"), 777)
190 self.assertEqual(eval("000000000000000000000000000000000000000000000000000777e0"), 777)
191 self.assertEqual(eval("0777e1"), 7770)
192 self.assertEqual(eval("0e0"), 0)
193 self.assertEqual(eval("0000e-012"), 0)
194 self.assertEqual(eval("09.5"), 9.5)
195 self.assertEqual(eval("0777j"), 777j)
196 self.assertEqual(eval("000"), 0)
197 self.assertEqual(eval("00j"), 0j)
198 self.assertEqual(eval("00.0"), 0)
199 self.assertEqual(eval("0e3"), 0)
200 self.assertEqual(eval("090000000000000."), 90000000000000.)
201 self.assertEqual(eval("090000000000000.0000000000000000000000"), 90000000000000.)
202 self.assertEqual(eval("090000000000000e0"), 90000000000000.)
203 self.assertEqual(eval("090000000000000e-0"), 90000000000000.)
204 self.assertEqual(eval("090000000000000j"), 90000000000000j)
205 self.assertEqual(eval("000000000000008."), 8.)
206 self.assertEqual(eval("000000000000009."), 9.)
207 self.assertEqual(eval("0b101010"), 42)
208 self.assertEqual(eval("-0b000000000010"), -2)
209 self.assertEqual(eval("0o777"), 511)
210 self.assertEqual(eval("-0o0000010"), -8)
211
212 def test_int_literals_too_long(self):
213 n = 3000
214 source = f"a = 1\nb = 2\nc = {'3'*n}\nd = 4"
215 with support.adjust_int_max_str_digits(n):
216 compile(source, "<long_int_pass>", "exec") # no errors.
217 with support.adjust_int_max_str_digits(n-1):
218 with self.assertRaises(SyntaxError) as err_ctx:
219 compile(source, "<long_int_fail>", "exec")
220 exc = err_ctx.exception
221 self.assertEqual(exc.lineno, 3)
222 self.assertIn('Exceeds the limit ', str(exc))
223 self.assertIn(' Consider hexadecimal ', str(exc))
224
225 def test_unary_minus(self):
226 # Verify treatment of unary minus on negative numbers SF bug #660455
227 if sys.maxsize == 2147483647:
228 # 32-bit machine
229 all_one_bits = '0xffffffff'
230 self.assertEqual(eval(all_one_bits), 4294967295)
231 self.assertEqual(eval("-" + all_one_bits), -4294967295)
232 elif sys.maxsize == 9223372036854775807:
233 # 64-bit machine
234 all_one_bits = '0xffffffffffffffff'
235 self.assertEqual(eval(all_one_bits), 18446744073709551615)
236 self.assertEqual(eval("-" + all_one_bits), -18446744073709551615)
237 else:
238 self.fail("How many bits *does* this machine have???")
239 # Verify treatment of constant folding on -(sys.maxsize+1)
240 # i.e. -2147483648 on 32 bit platforms. Should return int.
241 self.assertIsInstance(eval("%s" % (-sys.maxsize - 1)), int)
242 self.assertIsInstance(eval("%s" % (-sys.maxsize - 2)), int)
243
244 if sys.maxsize == 9223372036854775807:
245 def test_32_63_bit_values(self):
246 a = +4294967296 # 1 << 32
247 b = -4294967296 # 1 << 32
248 c = +281474976710656 # 1 << 48
249 d = -281474976710656 # 1 << 48
250 e = +4611686018427387904 # 1 << 62
251 f = -4611686018427387904 # 1 << 62
252 g = +9223372036854775807 # (1 << 63) - 1
253 h = -9223372036854775807 # (1 << 63) - 1
254
255 for variable in self.test_32_63_bit_values.__code__.co_consts:
256 if variable is not None:
257 self.assertIsInstance(variable, int)
258
259 def test_sequence_unpacking_error(self):
260 # Verify sequence packing/unpacking with "or". SF bug #757818
261 i,j = (1, -1) or (-1, 1)
262 self.assertEqual(i, 1)
263 self.assertEqual(j, -1)
264
265 def test_none_assignment(self):
266 stmts = [
267 'None = 0',
268 'None += 0',
269 '__builtins__.None = 0',
270 'def None(): pass',
271 'class None: pass',
272 '(a, None) = 0, 0',
273 'for None in range(10): pass',
274 'def f(None): pass',
275 'import None',
276 'import x as None',
277 'from x import None',
278 'from x import y as None'
279 ]
280 for stmt in stmts:
281 stmt += "\n"
282 self.assertRaises(SyntaxError, compile, stmt, 'tmp', 'single')
283 self.assertRaises(SyntaxError, compile, stmt, 'tmp', 'exec')
284
285 def test_import(self):
286 succeed = [
287 'import sys',
288 'import os, sys',
289 'import os as bar',
290 'import os.path as bar',
291 'from __future__ import nested_scopes, generators',
292 'from __future__ import (nested_scopes,\ngenerators)',
293 'from __future__ import (nested_scopes,\ngenerators,)',
294 'from sys import stdin, stderr, stdout',
295 'from sys import (stdin, stderr,\nstdout)',
296 'from sys import (stdin, stderr,\nstdout,)',
297 'from sys import (stdin\n, stderr, stdout)',
298 'from sys import (stdin\n, stderr, stdout,)',
299 'from sys import stdin as si, stdout as so, stderr as se',
300 'from sys import (stdin as si, stdout as so, stderr as se)',
301 'from sys import (stdin as si, stdout as so, stderr as se,)',
302 ]
303 fail = [
304 'import (os, sys)',
305 'import (os), (sys)',
306 'import ((os), (sys))',
307 'import (sys',
308 'import sys)',
309 'import (os,)',
310 'import os As bar',
311 'import os.path a bar',
312 'from sys import stdin As stdout',
313 'from sys import stdin a stdout',
314 'from (sys) import stdin',
315 'from __future__ import (nested_scopes',
316 'from __future__ import nested_scopes)',
317 'from __future__ import nested_scopes,\ngenerators',
318 'from sys import (stdin',
319 'from sys import stdin)',
320 'from sys import stdin, stdout,\nstderr',
321 'from sys import stdin si',
322 'from sys import stdin,',
323 'from sys import (*)',
324 'from sys import (stdin,, stdout, stderr)',
325 'from sys import (stdin, stdout),',
326 ]
327 for stmt in succeed:
328 compile(stmt, 'tmp', 'exec')
329 for stmt in fail:
330 self.assertRaises(SyntaxError, compile, stmt, 'tmp', 'exec')
331
332 def test_for_distinct_code_objects(self):
333 # SF bug 1048870
334 def f():
335 f1 = lambda x=1: x
336 f2 = lambda x=2: x
337 return f1, f2
338 f1, f2 = f()
339 self.assertNotEqual(id(f1.__code__), id(f2.__code__))
340
341 def test_lambda_doc(self):
342 l = lambda: "foo"
343 self.assertIsNone(l.__doc__)
344
345 def test_lambda_consts(self):
346 l = lambda: "this is the only const"
347 self.assertEqual(l.__code__.co_consts, ("this is the only const",))
348
349 def test_encoding(self):
350 code = b'# -*- coding: badencoding -*-\npass\n'
351 self.assertRaises(SyntaxError, compile, code, 'tmp', 'exec')
352 code = '# -*- coding: badencoding -*-\n"\xc2\xa4"\n'
353 compile(code, 'tmp', 'exec')
354 self.assertEqual(eval(code), '\xc2\xa4')
355 code = '"\xc2\xa4"\n'
356 self.assertEqual(eval(code), '\xc2\xa4')
357 code = b'"\xc2\xa4"\n'
358 self.assertEqual(eval(code), '\xa4')
359 code = b'# -*- coding: latin1 -*-\n"\xc2\xa4"\n'
360 self.assertEqual(eval(code), '\xc2\xa4')
361 code = b'# -*- coding: utf-8 -*-\n"\xc2\xa4"\n'
362 self.assertEqual(eval(code), '\xa4')
363 code = b'# -*- coding: iso8859-15 -*-\n"\xc2\xa4"\n'
364 self.assertEqual(eval(code), '\xc2\u20ac')
365 code = '"""\\\n# -*- coding: iso8859-15 -*-\n\xc2\xa4"""\n'
366 self.assertEqual(eval(code), '# -*- coding: iso8859-15 -*-\n\xc2\xa4')
367 code = b'"""\\\n# -*- coding: iso8859-15 -*-\n\xc2\xa4"""\n'
368 self.assertEqual(eval(code), '# -*- coding: iso8859-15 -*-\n\xa4')
369
370 def test_subscripts(self):
371 # SF bug 1448804
372 # Class to make testing subscript results easy
373 class str_map(object):
374 def __init__(self):
375 self.data = {}
376 def __getitem__(self, key):
377 return self.data[str(key)]
378 def __setitem__(self, key, value):
379 self.data[str(key)] = value
380 def __delitem__(self, key):
381 del self.data[str(key)]
382 def __contains__(self, key):
383 return str(key) in self.data
384 d = str_map()
385 # Index
386 d[1] = 1
387 self.assertEqual(d[1], 1)
388 d[1] += 1
389 self.assertEqual(d[1], 2)
390 del d[1]
391 self.assertNotIn(1, d)
392 # Tuple of indices
393 d[1, 1] = 1
394 self.assertEqual(d[1, 1], 1)
395 d[1, 1] += 1
396 self.assertEqual(d[1, 1], 2)
397 del d[1, 1]
398 self.assertNotIn((1, 1), d)
399 # Simple slice
400 d[1:2] = 1
401 self.assertEqual(d[1:2], 1)
402 d[1:2] += 1
403 self.assertEqual(d[1:2], 2)
404 del d[1:2]
405 self.assertNotIn(slice(1, 2), d)
406 # Tuple of simple slices
407 d[1:2, 1:2] = 1
408 self.assertEqual(d[1:2, 1:2], 1)
409 d[1:2, 1:2] += 1
410 self.assertEqual(d[1:2, 1:2], 2)
411 del d[1:2, 1:2]
412 self.assertNotIn((slice(1, 2), slice(1, 2)), d)
413 # Extended slice
414 d[1:2:3] = 1
415 self.assertEqual(d[1:2:3], 1)
416 d[1:2:3] += 1
417 self.assertEqual(d[1:2:3], 2)
418 del d[1:2:3]
419 self.assertNotIn(slice(1, 2, 3), d)
420 # Tuple of extended slices
421 d[1:2:3, 1:2:3] = 1
422 self.assertEqual(d[1:2:3, 1:2:3], 1)
423 d[1:2:3, 1:2:3] += 1
424 self.assertEqual(d[1:2:3, 1:2:3], 2)
425 del d[1:2:3, 1:2:3]
426 self.assertNotIn((slice(1, 2, 3), slice(1, 2, 3)), d)
427 # Ellipsis
428 d[...] = 1
429 self.assertEqual(d[...], 1)
430 d[...] += 1
431 self.assertEqual(d[...], 2)
432 del d[...]
433 self.assertNotIn(Ellipsis, d)
434 # Tuple of Ellipses
435 d[..., ...] = 1
436 self.assertEqual(d[..., ...], 1)
437 d[..., ...] += 1
438 self.assertEqual(d[..., ...], 2)
439 del d[..., ...]
440 self.assertNotIn((Ellipsis, Ellipsis), d)
441
442 def test_annotation_limit(self):
443 # more than 255 annotations, should compile ok
444 s = "def f(%s): pass"
445 s %= ', '.join('a%d:%d' % (i,i) for i in range(300))
446 compile(s, '?', 'exec')
447
448 def test_mangling(self):
449 class A:
450 def f():
451 __mangled = 1
452 __not_mangled__ = 2
453 import __mangled_mod # noqa: F401
454 import __package__.module # noqa: F401
455
456 self.assertIn("_A__mangled", A.f.__code__.co_varnames)
457 self.assertIn("__not_mangled__", A.f.__code__.co_varnames)
458 self.assertIn("_A__mangled_mod", A.f.__code__.co_varnames)
459 self.assertIn("__package__", A.f.__code__.co_varnames)
460
461 def test_condition_expression_with_dead_blocks_compiles(self):
462 # See gh-113054
463 compile('if (5 if 5 else T): 0', '<eval>', 'exec')
464
465 def test_condition_expression_with_redundant_comparisons_compiles(self):
466 # See gh-113054, gh-114083
467 exprs = [
468 'if 9<9<9and 9or 9:9',
469 'if 9<9<9and 9or 9or 9:9',
470 'if 9<9<9and 9or 9or 9or 9:9',
471 'if 9<9<9and 9or 9or 9or 9or 9:9',
472 ]
473 for expr in exprs:
474 with self.subTest(expr=expr):
475 with self.assertWarns(SyntaxWarning):
476 compile(expr, '<eval>', 'exec')
477
478 def test_dead_code_with_except_handler_compiles(self):
479 compile(textwrap.dedent("""
480 if None:
481 with CM:
482 x = 1
483 else:
484 x = 2
485 """), '<eval>', 'exec')
486
487 def test_try_except_in_while_with_chained_condition_compiles(self):
488 # see gh-124871
489 compile(textwrap.dedent("""
490 name_1, name_2, name_3 = 1, 2, 3
491 while name_3 <= name_2 > name_1:
492 try:
493 raise
494 except:
495 pass
496 finally:
497 pass
498 """), '<eval>', 'exec')
499
500 def test_compile_invalid_namedexpr(self):
501 # gh-109351
502 m = ast.Module(
503 body=[
504 ast.Expr(
505 value=ast.ListComp(
506 elt=ast.NamedExpr(
507 target=ast.Constant(value=1),
508 value=ast.Constant(value=3),
509 ),
510 generators=[
511 ast.comprehension(
512 target=ast.Name(id="x", ctx=ast.Store()),
513 iter=ast.Name(id="y", ctx=ast.Load()),
514 ifs=[],
515 is_async=0,
516 )
517 ],
518 )
519 )
520 ],
521 type_ignores=[],
522 )
523
524 with self.assertRaisesRegex(TypeError, "NamedExpr target must be a Name"):
525 compile(ast.fix_missing_locations(m), "<file>", "exec")
526
527 def test_compile_redundant_jumps_and_nops_after_moving_cold_blocks(self):
528 # See gh-120367
529 code=textwrap.dedent("""
530 try:
531 pass
532 except:
533 pass
534 else:
535 match name_2:
536 case b'':
537 pass
538 finally:
539 something
540 """)
541
542 tree = ast.parse(code)
543
544 # make all instruction locations the same to create redundancies
545 for node in ast.walk(tree):
546 if hasattr(node,"lineno"):
547 del node.lineno
548 del node.end_lineno
549 del node.col_offset
550 del node.end_col_offset
551
552 compile(ast.fix_missing_locations(tree), "<file>", "exec")
553
554 def test_compile_redundant_jump_after_convert_pseudo_ops(self):
555 # See gh-120367
556 code=textwrap.dedent("""
557 if name_2:
558 pass
559 else:
560 try:
561 pass
562 except:
563 pass
564 ~name_5
565 """)
566
567 tree = ast.parse(code)
568
569 # make all instruction locations the same to create redundancies
570 for node in ast.walk(tree):
571 if hasattr(node,"lineno"):
572 del node.lineno
573 del node.end_lineno
574 del node.col_offset
575 del node.end_col_offset
576
577 compile(ast.fix_missing_locations(tree), "<file>", "exec")
578
579 def test_compile_ast(self):
580 fname = __file__
581 if fname.lower().endswith('pyc'):
582 fname = fname[:-1]
583 with open(fname, encoding='utf-8') as f:
584 fcontents = f.read()
585 sample_code = [
586 ['<assign>', 'x = 5'],
587 ['<ifblock>', """if True:\n pass\n"""],
588 ['<forblock>', """for n in [1, 2, 3]:\n print(n)\n"""],
589 ['<deffunc>', """def foo():\n pass\nfoo()\n"""],
590 [fname, fcontents],
591 ]
592
593 for fname, code in sample_code:
594 co1 = compile(code, '%s1' % fname, 'exec')
595 ast = compile(code, '%s2' % fname, 'exec', _ast.PyCF_ONLY_AST)
596 self.assertTrue(type(ast) == _ast.Module)
597 co2 = compile(ast, '%s3' % fname, 'exec')
598 self.assertEqual(co1, co2)
599 # the code object's filename comes from the second compilation step
600 self.assertEqual(co2.co_filename, '%s3' % fname)
601
602 # raise exception when node type doesn't match with compile mode
603 co1 = compile('print(1)', '<string>', 'exec', _ast.PyCF_ONLY_AST)
604 self.assertRaises(TypeError, compile, co1, '<ast>', 'eval')
605
606 # raise exception when node type is no start node
607 self.assertRaises(TypeError, compile, _ast.If(test=_ast.Name(id='x', ctx=_ast.Load())), '<ast>', 'exec')
608
609 # raise exception when node has invalid children
610 ast = _ast.Module()
611 ast.body = [_ast.BoolOp(op=_ast.Or())]
612 self.assertRaises(TypeError, compile, ast, '<ast>', 'exec')
613
614 def test_compile_invalid_typealias(self):
615 # gh-109341
616 m = ast.Module(
617 body=[
618 ast.TypeAlias(
619 name=ast.Subscript(
620 value=ast.Name(id="foo", ctx=ast.Load()),
621 slice=ast.Constant(value="x"),
622 ctx=ast.Store(),
623 ),
624 type_params=[],
625 value=ast.Name(id="Callable", ctx=ast.Load()),
626 )
627 ],
628 type_ignores=[],
629 )
630
631 with self.assertRaisesRegex(TypeError, "TypeAlias with non-Name name"):
632 compile(ast.fix_missing_locations(m), "<file>", "exec")
633
634 def test_dict_evaluation_order(self):
635 i = 0
636
637 def f():
638 nonlocal i
639 i += 1
640 return i
641
642 d = {f(): f(), f(): f()}
643 self.assertEqual(d, {1: 2, 3: 4})
644
645 def test_compile_filename(self):
646 for filename in 'file.py', b'file.py':
647 code = compile('pass', filename, 'exec')
648 self.assertEqual(code.co_filename, 'file.py')
649 for filename in bytearray(b'file.py'), memoryview(b'file.py'):
650 with self.assertRaises(TypeError):
651 compile('pass', filename, 'exec')
652 self.assertRaises(TypeError, compile, 'pass', list(b'file.py'), 'exec')
653
654 def test_compile_filename_refleak(self):
655 # Regression tests for reference leak in PyUnicode_FSDecoder.
656 # See https://github.com/python/cpython/issues/139748.
657 mortal_str = 'this is a mortal string'
658 # check error path when 'mode' AC conversion failed
659 self.assertRaises(TypeError, compile, b'', mortal_str, mode=1234)
660 # check error path when 'optimize' AC conversion failed
661 self.assertRaises(OverflowError, compile, b'', mortal_str,
662 'exec', optimize=1 << 1000)
663 # check error path when 'dont_inherit' AC conversion failed
664 class EvilBool:
665 def __bool__(self): raise ValueError
666 self.assertRaises(ValueError, compile, b'', mortal_str,
667 'exec', dont_inherit=EvilBool())
668
669 @support.cpython_only
670 def test_same_filename_used(self):
671 s = """def f(): pass\ndef g(): pass"""
672 c = compile(s, "myfile", "exec")
673 for obj in c.co_consts:
674 if isinstance(obj, types.CodeType):
675 self.assertIs(obj.co_filename, c.co_filename)
676
677 def test_single_statement(self):
678 self.compile_single("1 + 2")
679 self.compile_single("\n1 + 2")
680 self.compile_single("1 + 2\n")
681 self.compile_single("1 + 2\n\n")
682 self.compile_single("1 + 2\t\t\n")
683 self.compile_single("1 + 2\t\t\n ")
684 self.compile_single("1 + 2 # one plus two")
685 self.compile_single("1; 2")
686 self.compile_single("import sys; sys")
687 self.compile_single("def f():\n pass")
688 self.compile_single("while False:\n pass")
689 self.compile_single("if x:\n f(x)")
690 self.compile_single("if x:\n f(x)\nelse:\n g(x)")
691 self.compile_single("class T:\n pass")
692 self.compile_single("c = '''\na=1\nb=2\nc=3\n'''")
693
694 def test_bad_single_statement(self):
695 self.assertInvalidSingle('1\n2')
696 self.assertInvalidSingle('def f(): pass')
697 self.assertInvalidSingle('a = 13\nb = 187')
698 self.assertInvalidSingle('del x\ndel y')
699 self.assertInvalidSingle('f()\ng()')
700 self.assertInvalidSingle('f()\n# blah\nblah()')
701 self.assertInvalidSingle('f()\nxy # blah\nblah()')
702 self.assertInvalidSingle('x = 5 # comment\nx = 6\n')
703 self.assertInvalidSingle("c = '''\nd=1\n'''\na = 1\n\nb = 2\n")
704
705 def test_particularly_evil_undecodable(self):
706 # Issue 24022
707 src = b'0000\x00\n00000000000\n\x00\n\x9e\n'
708 with tempfile.TemporaryDirectory() as tmpd:
709 fn = os.path.join(tmpd, "bad.py")
710 with open(fn, "wb") as fp:
711 fp.write(src)
712 res = script_helper.run_python_until_end(fn)[0]
713 self.assertIn(b"source code cannot contain null bytes", res.err)
714
715 def test_yet_more_evil_still_undecodable(self):
716 # Issue #25388
717 src = b"#\x00\n#\xfd\n"
718 with tempfile.TemporaryDirectory() as tmpd:
719 fn = os.path.join(tmpd, "bad.py")
720 with open(fn, "wb") as fp:
721 fp.write(src)
722 res = script_helper.run_python_until_end(fn)[0]
723 self.assertIn(b"source code cannot contain null bytes", res.err)
724
725 @support.cpython_only
726 @unittest.skipIf(support.is_wasi, "exhausts limited stack on WASI")
727 @support.skip_emscripten_stack_overflow()
728 def test_compiler_recursion_limit(self):
729 # Compiler frames are small
730 limit = 100
731 # Android test devices have less memory.
732 crash_depth = limit * (1000 if sys.platform == "android" else 5000)
733 success_depth = limit
734
735 def check_limit(prefix, repeated, mode="single"):
736 expect_ok = prefix + repeated * success_depth
737 compile(expect_ok, '<test>', mode)
738 broken = prefix + repeated * crash_depth
739 details = f"Compiling ({prefix!r} + {repeated!r} * {crash_depth})"
740 with self.assertRaises(RecursionError, msg=details):
741 compile(broken, '<test>', mode)
742
743 check_limit("a", "()")
744 check_limit("a", ".b")
745 check_limit("a", "[0]")
746 check_limit("a", "*a")
747 # XXX Crashes in the parser.
748 # check_limit("a", " if a else a")
749 # check_limit("if a: pass", "\nelif a: pass", mode="exec")
750
751 def test_null_terminated(self):
752 # The source code is null-terminated internally, but bytes-like
753 # objects are accepted, which could be not terminated.
754 with self.assertRaisesRegex(SyntaxError, "cannot contain null"):
755 compile("123\x00", "<dummy>", "eval")
756 with self.assertRaisesRegex(SyntaxError, "cannot contain null"):
757 compile(memoryview(b"123\x00"), "<dummy>", "eval")
758 code = compile(memoryview(b"123\x00")[1:-1], "<dummy>", "eval")
759 self.assertEqual(eval(code), 23)
760 code = compile(memoryview(b"1234")[1:-1], "<dummy>", "eval")
761 self.assertEqual(eval(code), 23)
762 code = compile(memoryview(b"$23$")[1:-1], "<dummy>", "eval")
763 self.assertEqual(eval(code), 23)
764
765 # Also test when eval() and exec() do the compilation step
766 self.assertEqual(eval(memoryview(b"1234")[1:-1]), 23)
767 namespace = dict()
768 exec(memoryview(b"ax = 123")[1:-1], namespace)
769 self.assertEqual(namespace['x'], 12)
770
771 def check_constant(self, func, expected):
772 for const in func.__code__.co_consts:
773 if repr(const) == repr(expected):
774 break
775 else:
776 self.fail("unable to find constant %r in %r"
777 % (expected, func.__code__.co_consts))
778
779 # Merging equal constants is not a strict requirement for the Python
780 # semantics, it's a more an implementation detail.
781 @support.cpython_only
782 def test_merge_constants(self):
783 # Issue #25843: compile() must merge constants which are equal
784 # and have the same type.
785
786 def check_same_constant(const):
787 ns = {}
788 code = "f1, f2 = lambda: %r, lambda: %r" % (const, const)
789 exec(code, ns)
790 f1 = ns['f1']
791 f2 = ns['f2']
792 self.assertIs(f1.__code__.co_consts, f2.__code__.co_consts)
793 self.check_constant(f1, const)
794 self.assertEqual(repr(f1()), repr(const))
795
796 check_same_constant(None)
797 check_same_constant(0.0)
798 check_same_constant(b'abc')
799 check_same_constant('abc')
800
801 # Note: "lambda: ..." emits "LOAD_CONST Ellipsis",
802 # whereas "lambda: Ellipsis" emits "LOAD_GLOBAL Ellipsis"
803 f1, f2 = lambda: ..., lambda: ...
804 self.assertIs(f1.__code__.co_consts, f2.__code__.co_consts)
805 self.check_constant(f1, Ellipsis)
806 self.assertEqual(repr(f1()), repr(Ellipsis))
807
808 # Merge constants in tuple or frozenset
809 f1, f2 = lambda: "not a name", lambda: ("not a name",)
810 f3 = lambda x: x in {("not a name",)}
811 self.assertIs(f1.__code__.co_consts[0],
812 f2.__code__.co_consts[1][0])
813 self.assertIs(next(iter(f3.__code__.co_consts[1])),
814 f2.__code__.co_consts[1])
815
816 # {0} is converted to a constant frozenset({0}) by the peephole
817 # optimizer
818 f1, f2 = lambda x: x in {0}, lambda x: x in {0}
819 self.assertIs(f1.__code__.co_consts, f2.__code__.co_consts)
820 self.check_constant(f1, frozenset({0}))
821 self.assertTrue(f1(0))
822
823 # Merging equal co_linetable is not a strict requirement
824 # for the Python semantics, it's a more an implementation detail.
825 @support.cpython_only
826 def test_merge_code_attrs(self):
827 # See https://bugs.python.org/issue42217
828 f1 = lambda x: x.y.z
829 f2 = lambda a: a.b.c
830
831 self.assertIs(f1.__code__.co_linetable, f2.__code__.co_linetable)
832
833 @support.cpython_only
834 def test_remove_unused_consts(self):
835 def f():
836 "docstring"
837 if True:
838 return "used"
839 else:
840 return "unused"
841
842 self.assertEqual(f.__code__.co_consts,
843 (f.__doc__, "used"))
844
845 @support.cpython_only
846 def test_remove_unused_consts_no_docstring(self):
847 # the first item (None for no docstring in this case) is
848 # always retained.
849 def f():
850 if True:
851 return "used"
852 else:
853 return "unused"
854
855 self.assertEqual(f.__code__.co_consts,
856 (True, "used"))
857
858 @support.cpython_only
859 def test_remove_unused_consts_extended_args(self):
860 N = 1000
861 code = ["def f():\n"]
862 code.append("\ts = ''\n")
863 code.append("\tfor i in range(1):\n")
864 for i in range(N):
865 code.append(f"\t\tif True: s += 't{i}'\n")
866 code.append(f"\t\tif False: s += 'f{i}'\n")
867 code.append("\treturn s\n")
868
869 code = "".join(code)
870 g = {}
871 eval(compile(code, "file.py", "exec"), g)
872 exec(code, g)
873 f = g['f']
874 expected = tuple([''] + [f't{i}' for i in range(N)])
875 self.assertEqual(f.__code__.co_consts, expected)
876 expected = "".join(expected[1:])
877 self.assertEqual(expected, f())
878
879 # Stripping unused constants is not a strict requirement for the
880 # Python semantics, it's a more an implementation detail.
881 @support.cpython_only
882 def test_strip_unused_None(self):
883 # Python 3.10rc1 appended None to co_consts when None is not used
884 # at all. See bpo-45056.
885 def f1():
886 "docstring"
887 return 42
888 self.assertEqual(f1.__code__.co_consts, (f1.__doc__,))
889
890 # This is a regression test for a CPython specific peephole optimizer
891 # implementation bug present in a few releases. It's assertion verifies
892 # that peephole optimization was actually done though that isn't an
893 # indication of the bugs presence or not (crashing is).
894 @support.cpython_only
895 def test_peephole_opt_unreachable_code_array_access_in_bounds(self):
896 """Regression test for issue35193 when run under clang msan."""
897 def unused_code_at_end():
898 return 3
899 raise RuntimeError("unreachable")
900 # The above function definition will trigger the out of bounds
901 # bug in the peephole optimizer as it scans opcodes past the
902 # RETURN_VALUE opcode. This does not always crash an interpreter.
903 # When you build with the clang memory sanitizer it reliably aborts.
904 self.assertEqual(
905 'RETURN_VALUE',
906 list(dis.get_instructions(unused_code_at_end))[-1].opname)
907
908 @support.cpython_only
909 def test_docstring(self):
910 src = textwrap.dedent("""
911 def with_docstring():
912 "docstring"
913
914 def two_strings():
915 "docstring"
916 "not docstring"
917
918 def with_fstring():
919 f"not docstring"
920
921 def with_const_expression():
922 "also" + " not docstring"
923
924 def multiple_const_strings():
925 "not docstring " * 3
926 """)
927
928 for opt in [0, 1, 2]:
929 with self.subTest(opt=opt):
930 code = compile(src, "<test>", "exec", optimize=opt)
931 ns = {}
932 exec(code, ns)
933
934 if opt < 2:
935 self.assertEqual(ns['with_docstring'].__doc__, "docstring")
936 self.assertEqual(ns['two_strings'].__doc__, "docstring")
937 else:
938 self.assertIsNone(ns['with_docstring'].__doc__)
939 self.assertIsNone(ns['two_strings'].__doc__)
940 self.assertIsNone(ns['with_fstring'].__doc__)
941 self.assertIsNone(ns['with_const_expression'].__doc__)
942 self.assertIsNone(ns['multiple_const_strings'].__doc__)
943
944 @support.cpython_only
945 def test_docstring_interactive_mode(self):
946 srcs = [
947 """def with_docstring():
948 "docstring"
949 """,
950 """class with_docstring:
951 "docstring"
952 """,
953 ]
954
955 for opt in [0, 1, 2]:
956 for src in srcs:
957 with self.subTest(opt=opt, src=src):
958 code = compile(textwrap.dedent(src), "<test>", "single", optimize=opt)
959 ns = {}
960 exec(code, ns)
961 if opt < 2:
962 self.assertEqual(ns['with_docstring'].__doc__, "docstring")
963 else:
964 self.assertIsNone(ns['with_docstring'].__doc__)
965
966 @support.cpython_only
967 def test_docstring_omitted(self):
968 # See gh-115347
969 src = textwrap.dedent("""
970 def f():
971 "docstring1"
972 def h():
973 "docstring2"
974 return 42
975
976 class C:
977 "docstring3"
978 pass
979
980 return h
981 """)
982 for opt in [-1, 0, 1, 2]:
983 for mode in ["exec", "single"]:
984 with self.subTest(opt=opt, mode=mode):
985 code = compile(src, "<test>", mode, optimize=opt)
986 output = io.StringIO()
987 with contextlib.redirect_stdout(output):
988 dis.dis(code)
989 self.assertNotIn('NOP', output.getvalue())
990
991 def test_dont_merge_constants(self):
992 # Issue #25843: compile() must not merge constants which are equal
993 # but have a different type.
994
995 def check_different_constants(const1, const2):
996 ns = {}
997 exec("f1, f2 = lambda: %r, lambda: %r" % (const1, const2), ns)
998 f1 = ns['f1']
999 f2 = ns['f2']
1000 self.assertIsNot(f1.__code__, f2.__code__)
1001 self.assertNotEqual(f1.__code__, f2.__code__)
1002 self.check_constant(f1, const1)
1003 self.check_constant(f2, const2)
1004 self.assertEqual(repr(f1()), repr(const1))
1005 self.assertEqual(repr(f2()), repr(const2))
1006
1007 check_different_constants(+0.0, -0.0)
1008 check_different_constants((0,), (0.0,))
1009 check_different_constants('a', b'a')
1010 check_different_constants(('a',), (b'a',))
1011
1012 # check_different_constants() cannot be used because repr(-0j) is
1013 # '(-0-0j)', but when '(-0-0j)' is evaluated to 0j: we loose the sign.
1014 f1, f2 = lambda: +0.0j, lambda: -0.0j
1015 self.assertIsNot(f1.__code__, f2.__code__)
1016 self.check_constant(f1, +0.0j)
1017 self.check_constant(f2, -0.0j)
1018 self.assertEqual(repr(f1()), repr(+0.0j))
1019 self.assertEqual(repr(f2()), repr(-0.0j))
1020
1021 # {0} is converted to a constant frozenset({0}) by the peephole
1022 # optimizer
1023 f1, f2 = lambda x: x in {0}, lambda x: x in {0.0}
1024 self.assertIsNot(f1.__code__, f2.__code__)
1025 self.check_constant(f1, frozenset({0}))
1026 self.check_constant(f2, frozenset({0.0}))
1027 self.assertTrue(f1(0))
1028 self.assertTrue(f2(0.0))
1029
1030 def test_path_like_objects(self):
1031 # An implicit test for PyUnicode_FSDecoder().
1032 compile("42", FakePath("test_compile_pathlike"), "single")
1033
1034 # bpo-31113: Stack overflow when compile a long sequence of
1035 # complex statements.
1036 @support.requires_resource('cpu')
1037 def test_stack_overflow(self):
1038 # Android test devices have less memory.
1039 size = 100_000 if sys.platform == "android" else 200_000
1040 compile("if a: b\n" * size, "<dummy>", "exec")
1041
1042 # Multiple users rely on the fact that CPython does not generate
1043 # bytecode for dead code blocks. See bpo-37500 for more context.
1044 @support.cpython_only
1045 def test_dead_blocks_do_not_generate_bytecode(self):
1046 def unused_block_if():
1047 if 0:
1048 return 42
1049
1050 def unused_block_while():
1051 while 0:
1052 return 42
1053
1054 def unused_block_if_else():
1055 if 1:
1056 return None
1057 else:
1058 return 42
1059
1060 def unused_block_while_else():
1061 while 1:
1062 return None
1063 else:
1064 return 42
1065
1066 funcs = [unused_block_if, unused_block_while,
1067 unused_block_if_else, unused_block_while_else]
1068
1069 for func in funcs:
1070 opcodes = list(dis.get_instructions(func))
1071 self.assertLessEqual(len(opcodes), 4)
1072 self.assertEqual('RETURN_VALUE', opcodes[-1].opname)
1073 self.assertEqual(None, opcodes[-1].argval)
1074
1075 def test_false_while_loop(self):
1076 def break_in_while():
1077 while False:
1078 break
1079
1080 def continue_in_while():
1081 while False:
1082 continue
1083
1084 funcs = [break_in_while, continue_in_while]
1085
1086 # Check that we did not raise but we also don't generate bytecode
1087 for func in funcs:
1088 opcodes = list(dis.get_instructions(func))
1089 self.assertEqual(3, len(opcodes))
1090 self.assertEqual('RETURN_VALUE', opcodes[-1].opname)
1091 self.assertEqual(None, opcodes[1].argval)
1092
1093 def test_consts_in_conditionals(self):
1094 def and_true(x):
1095 return True and x
1096
1097 def and_false(x):
1098 return False and x
1099
1100 def or_true(x):
1101 return True or x
1102
1103 def or_false(x):
1104 return False or x
1105
1106 funcs = [and_true, and_false, or_true, or_false]
1107
1108 # Check that condition is removed.
1109 for func in funcs:
1110 with self.subTest(func=func):
1111 opcodes = list(dis.get_instructions(func))
1112 self.assertLessEqual(len(opcodes), 3)
1113 self.assertIn('LOAD_', opcodes[-2].opname)
1114 self.assertEqual('RETURN_VALUE', opcodes[-1].opname)
1115
1116 def test_imported_load_method(self):
1117 sources = [
1118 """\
1119 import os
1120 def foo():
1121 return os.uname()
1122 """,
1123 """\
1124 import os as operating_system
1125 def foo():
1126 return operating_system.uname()
1127 """,
1128 """\
1129 from os import path
1130 def foo(x):
1131 return path.join(x)
1132 """,
1133 """\
1134 from os import path as os_path
1135 def foo(x):
1136 return os_path.join(x)
1137 """
1138 ]
1139 for source in sources:
1140 namespace = {}
1141 exec(textwrap.dedent(source), namespace)
1142 func = namespace['foo']
1143 with self.subTest(func=func.__name__):
1144 opcodes = list(dis.get_instructions(func))
1145 instructions = [opcode.opname for opcode in opcodes]
1146 self.assertNotIn('LOAD_METHOD', instructions)
1147 self.assertIn('LOAD_ATTR', instructions)
1148 self.assertIn('CALL', instructions)
1149
1150 def test_folding_type_param(self):
1151 get_code_fn_cls = lambda x: x.co_consts[0].co_consts[2]
1152 get_code_type_alias = lambda x: x.co_consts[0].co_consts[3]
1153 snippets = [
1154 ("def foo[T = 40 + 5](): pass", get_code_fn_cls),
1155 ("def foo[**P = 40 + 5](): pass", get_code_fn_cls),
1156 ("def foo[*Ts = 40 + 5](): pass", get_code_fn_cls),
1157 ("class foo[T = 40 + 5]: pass", get_code_fn_cls),
1158 ("class foo[**P = 40 + 5]: pass", get_code_fn_cls),
1159 ("class foo[*Ts = 40 + 5]: pass", get_code_fn_cls),
1160 ("type foo[T = 40 + 5] = 1", get_code_type_alias),
1161 ("type foo[**P = 40 + 5] = 1", get_code_type_alias),
1162 ("type foo[*Ts = 40 + 5] = 1", get_code_type_alias),
1163 ]
1164 for snippet, get_code in snippets:
1165 c = compile(snippet, "<dummy>", "exec")
1166 code = get_code(c)
1167 opcodes = list(dis.get_instructions(code))
1168 instructions = [opcode.opname for opcode in opcodes]
1169 args = [opcode.oparg for opcode in opcodes]
1170 self.assertNotIn(40, args)
1171 self.assertNotIn(5, args)
1172 self.assertIn('LOAD_SMALL_INT', instructions)
1173 self.assertIn(45, args)
1174
1175 def test_lineno_procedure_call(self):
1176 def call():
1177 (
1178 print()
1179 )
1180 line1 = call.__code__.co_firstlineno + 1
1181 assert line1 not in [line for (_, _, line) in call.__code__.co_lines()]
1182
1183 def test_lineno_after_implicit_return(self):
1184 TRUE = True
1185 # Don't use constant True or False, as compiler will remove test
1186 def if1(x):
1187 x()
1188 if TRUE:
1189 pass
1190 def if2(x):
1191 x()
1192 if TRUE:
1193 pass
1194 else:
1195 pass
1196 def if3(x):
1197 x()
1198 if TRUE:
1199 pass
1200 else:
1201 return None
1202 def if4(x):
1203 x()
1204 if not TRUE:
1205 pass
1206 funcs = [ if1, if2, if3, if4]
1207 lastlines = [ 3, 3, 3, 2]
1208 frame = None
1209 def save_caller_frame():
1210 nonlocal frame
1211 frame = sys._getframe(1)
1212 for func, lastline in zip(funcs, lastlines, strict=True):
1213 with self.subTest(func=func):
1214 func(save_caller_frame)
1215 self.assertEqual(frame.f_lineno-frame.f_code.co_firstlineno, lastline)
1216
1217 def test_lineno_after_no_code(self):
1218 def no_code1():
1219 "doc string"
1220
1221 def no_code2():
1222 a: int
1223
1224 for func in (no_code1, no_code2):
1225 with self.subTest(func=func):
1226 if func is no_code1 and no_code1.__doc__ is None:
1227 continue
1228 code = func.__code__
1229 [(start, end, line)] = code.co_lines()
1230 self.assertEqual(start, 0)
1231 self.assertEqual(end, len(code.co_code))
1232 self.assertEqual(line, code.co_firstlineno)
1233
1234 def get_code_lines(self, code):
1235 last_line = -2
1236 res = []
1237 for _, _, line in code.co_lines():
1238 if line is not None and line != last_line:
1239 res.append(line - code.co_firstlineno)
1240 last_line = line
1241 return res
1242
1243 def test_lineno_attribute(self):
1244 def load_attr():
1245 return (
1246 o.
1247 a
1248 )
1249 load_attr_lines = [ 0, 2, 3, 1 ]
1250
1251 def load_method():
1252 return (
1253 o.
1254 m(
1255 0
1256 )
1257 )
1258 load_method_lines = [ 0, 2, 3, 4, 3, 1 ]
1259
1260 def store_attr():
1261 (
1262 o.
1263 a
1264 ) = (
1265 v
1266 )
1267 store_attr_lines = [ 0, 5, 2, 3 ]
1268
1269 def aug_store_attr():
1270 (
1271 o.
1272 a
1273 ) += (
1274 v
1275 )
1276 aug_store_attr_lines = [ 0, 2, 3, 5, 1, 3 ]
1277
1278 funcs = [ load_attr, load_method, store_attr, aug_store_attr]
1279 func_lines = [ load_attr_lines, load_method_lines,
1280 store_attr_lines, aug_store_attr_lines]
1281
1282 for func, lines in zip(funcs, func_lines, strict=True):
1283 with self.subTest(func=func):
1284 code_lines = self.get_code_lines(func.__code__)
1285 self.assertEqual(lines, code_lines)
1286
1287 def test_line_number_genexp(self):
1288
1289 def return_genexp():
1290 return (1
1291 for
1292 x
1293 in
1294 y)
1295 genexp_lines = [0, 4, 2, 0, 4]
1296
1297 genexp_code = return_genexp.__code__.co_consts[0]
1298 code_lines = self.get_code_lines(genexp_code)
1299 self.assertEqual(genexp_lines, code_lines)
1300
1301 def test_line_number_implicit_return_after_async_for(self):
1302
1303 async def test(aseq):
1304 async for i in aseq:
1305 body
1306
1307 expected_lines = [0, 1, 2, 1]
1308 code_lines = self.get_code_lines(test.__code__)
1309 self.assertEqual(expected_lines, code_lines)
1310
1311 def check_line_numbers(self, code, opnames=None):
1312 # Check that all instructions whose op matches opnames
1313 # have a line number. opnames can be a single name, or
1314 # a sequence of names. If it is None, match all ops.
1315
1316 if isinstance(opnames, str):
1317 opnames = (opnames, )
1318 for inst in dis.Bytecode(code):
1319 if opnames and inst.opname in opnames:
1320 self.assertIsNotNone(inst.positions.lineno)
1321
1322 def test_line_number_synthetic_jump_multiple_predecessors(self):
1323 def f():
1324 for x in it:
1325 try:
1326 if C1:
1327 yield 2
1328 except OSError:
1329 pass
1330
1331 self.check_line_numbers(f.__code__, 'JUMP_BACKWARD')
1332
1333 def test_line_number_synthetic_jump_multiple_predecessors_nested(self):
1334 def f():
1335 for x in it:
1336 try:
1337 X = 3
1338 except OSError:
1339 try:
1340 if C3:
1341 X = 4
1342 except OSError:
1343 pass
1344 return 42
1345
1346 self.check_line_numbers(f.__code__, 'JUMP_BACKWARD')
1347
1348 def test_line_number_synthetic_jump_multiple_predecessors_more_nested(self):
1349 def f():
1350 for x in it:
1351 try:
1352 X = 3
1353 except OSError:
1354 try:
1355 if C3:
1356 if C4:
1357 X = 4
1358 except OSError:
1359 try:
1360 if C3:
1361 if C4:
1362 X = 5
1363 except OSError:
1364 pass
1365 return 42
1366
1367 self.check_line_numbers(f.__code__, 'JUMP_BACKWARD')
1368
1369 def test_lineno_of_backward_jump_conditional_in_loop(self):
1370 # Issue gh-107901
1371 def f():
1372 for i in x:
1373 if y:
1374 pass
1375
1376 self.check_line_numbers(f.__code__, 'JUMP_BACKWARD')
1377
1378 def test_big_dict_literal(self):
1379 # The compiler has a flushing point in "compiler_dict" that calls compiles
1380 # a portion of the dictionary literal when the loop that iterates over the items
1381 # reaches 0xFFFF elements but the code was not including the boundary element,
1382 # dropping the key at position 0xFFFF. See bpo-41531 for more information
1383
1384 dict_size = 0xFFFF + 1
1385 the_dict = "{" + ",".join(f"{x}:{x}" for x in range(dict_size)) + "}"
1386 self.assertEqual(len(eval(the_dict)), dict_size)
1387
1388 def test_redundant_jump_in_if_else_break(self):
1389 # Check if bytecode containing jumps that simply point to the next line
1390 # is generated around if-else-break style structures. See bpo-42615.
1391
1392 def if_else_break():
1393 val = 1
1394 while True:
1395 if val > 0:
1396 val -= 1
1397 else:
1398 break
1399 val = -1
1400
1401 INSTR_SIZE = 2
1402 HANDLED_JUMPS = (
1403 'POP_JUMP_IF_FALSE',
1404 'POP_JUMP_IF_TRUE',
1405 'JUMP_ABSOLUTE',
1406 'JUMP_FORWARD',
1407 )
1408
1409 for line, instr in enumerate(
1410 dis.Bytecode(if_else_break, show_caches=True)
1411 ):
1412 if instr.opname == 'JUMP_FORWARD':
1413 self.assertNotEqual(instr.arg, 0)
1414 elif instr.opname in HANDLED_JUMPS:
1415 self.assertNotEqual(instr.arg, (line + 1)*INSTR_SIZE)
1416
1417 def test_no_wraparound_jump(self):
1418 # See https://bugs.python.org/issue46724
1419
1420 def while_not_chained(a, b, c):
1421 while not (a < b < c):
1422 pass
1423
1424 for instr in dis.Bytecode(while_not_chained):
1425 self.assertNotEqual(instr.opname, "EXTENDED_ARG")
1426
1427 @support.cpython_only
1428 def test_uses_slice_instructions(self):
1429
1430 def check_op_count(func, op, expected):
1431 actual = 0
1432 for instr in dis.Bytecode(func):
1433 if instr.opname == op:
1434 actual += 1
1435 self.assertEqual(actual, expected)
1436
1437 def check_consts(func, typ, expected):
1438 expected = set([repr(x) for x in expected])
1439 all_consts = set()
1440 consts = func.__code__.co_consts
1441 for instr in dis.Bytecode(func):
1442 if instr.opname == "LOAD_CONST" and isinstance(consts[instr.oparg], typ):
1443 all_consts.add(repr(consts[instr.oparg]))
1444 self.assertEqual(all_consts, expected)
1445
1446 def load():
1447 return x[a:b] + x [a:] + x[:b] + x[:]
1448
1449 check_op_count(load, "BINARY_SLICE", 3)
1450 check_op_count(load, "BUILD_SLICE", 0)
1451 check_consts(load, slice, [slice(None, None, None)])
1452 check_op_count(load, "BINARY_OP", 4)
1453
1454 def store():
1455 x[a:b] = y
1456 x [a:] = y
1457 x[:b] = y
1458 x[:] = y
1459
1460 check_op_count(store, "STORE_SLICE", 3)
1461 check_op_count(store, "BUILD_SLICE", 0)
1462 check_op_count(store, "STORE_SUBSCR", 1)
1463 check_consts(store, slice, [slice(None, None, None)])
1464
1465 def long_slice():
1466 return x[a:b:c]
1467
1468 check_op_count(long_slice, "BUILD_SLICE", 1)
1469 check_op_count(long_slice, "BINARY_SLICE", 0)
1470 check_consts(long_slice, slice, [])
1471 check_op_count(long_slice, "BINARY_OP", 1)
1472
1473 def aug():
1474 x[a:b] += y
1475
1476 check_op_count(aug, "BINARY_SLICE", 1)
1477 check_op_count(aug, "STORE_SLICE", 1)
1478 check_op_count(aug, "BUILD_SLICE", 0)
1479 check_op_count(aug, "BINARY_OP", 1)
1480 check_op_count(aug, "STORE_SUBSCR", 0)
1481 check_consts(aug, slice, [])
1482
1483 def aug_const():
1484 x[1:2] += y
1485
1486 check_op_count(aug_const, "BINARY_SLICE", 0)
1487 check_op_count(aug_const, "STORE_SLICE", 0)
1488 check_op_count(aug_const, "BINARY_OP", 2)
1489 check_op_count(aug_const, "STORE_SUBSCR", 1)
1490 check_consts(aug_const, slice, [slice(1, 2)])
1491
1492 def compound_const_slice():
1493 x[1:2:3, 4:5:6] = y
1494
1495 check_op_count(compound_const_slice, "BINARY_SLICE", 0)
1496 check_op_count(compound_const_slice, "BUILD_SLICE", 0)
1497 check_op_count(compound_const_slice, "STORE_SLICE", 0)
1498 check_op_count(compound_const_slice, "STORE_SUBSCR", 1)
1499 check_consts(compound_const_slice, slice, [])
1500 check_consts(compound_const_slice, tuple, [(slice(1, 2, 3), slice(4, 5, 6))])
1501
1502 def mutable_slice():
1503 x[[]:] = y
1504
1505 check_consts(mutable_slice, slice, {})
1506
1507 def different_but_equal():
1508 x[:0] = y
1509 x[:0.0] = y
1510 x[:False] = y
1511 x[:None] = y
1512
1513 check_consts(
1514 different_but_equal,
1515 slice,
1516 [
1517 slice(None, 0, None),
1518 slice(None, 0.0, None),
1519 slice(None, False, None),
1520 slice(None, None, None)
1521 ]
1522 )
1523
1524 def test_compare_positions(self):
1525 for opname_prefix, op in [
1526 ("COMPARE_", "<"),
1527 ("COMPARE_", "<="),
1528 ("COMPARE_", ">"),
1529 ("COMPARE_", ">="),
1530 ("CONTAINS_OP", "in"),
1531 ("CONTAINS_OP", "not in"),
1532 ("IS_OP", "is"),
1533 ("IS_OP", "is not"),
1534 ]:
1535 expr = f'a {op} b {op} c'
1536 expected_positions = 2 * [(2, 2, 0, len(expr))]
1537 for source in [
1538 f"\\\n{expr}", f'if \\\n{expr}: x', f"x if \\\n{expr} else y"
1539 ]:
1540 code = compile(source, "<test>", "exec")
1541 actual_positions = [
1542 instruction.positions
1543 for instruction in dis.get_instructions(code)
1544 if instruction.opname.startswith(opname_prefix)
1545 ]
1546 with self.subTest(source):
1547 self.assertEqual(actual_positions, expected_positions)
1548
1549 def test_if_expression_expression_empty_block(self):
1550 # See regression in gh-99708
1551 exprs = [
1552 "assert (False if 1 else True)",
1553 "def f():\n\tif not (False if 1 else True): raise AssertionError",
1554 "def f():\n\tif not (False if 1 else True): return 12",
1555 ]
1556 for expr in exprs:
1557 with self.subTest(expr=expr):
1558 compile(expr, "<single>", "exec")
1559
1560 def test_multi_line_lambda_as_argument(self):
1561 # See gh-101928
1562 code = textwrap.dedent("""
1563 def foo(param, lambda_exp):
1564 pass
1565
1566 foo(param=0,
1567 lambda_exp=lambda:
1568 1)
1569 """)
1570 compile(code, "<test>", "exec")
1571
1572 def test_apply_static_swaps(self):
1573 def f(x, y):
1574 a, a = x, y
1575 return a
1576 self.assertEqual(f("x", "y"), "y")
1577
1578 def test_apply_static_swaps_2(self):
1579 def f(x, y, z):
1580 a, b, a = x, y, z
1581 return a
1582 self.assertEqual(f("x", "y", "z"), "z")
1583
1584 def test_apply_static_swaps_3(self):
1585 def f(x, y, z):
1586 a, a, b = x, y, z
1587 return a
1588 self.assertEqual(f("x", "y", "z"), "y")
1589
1590 def test_variable_dependent(self):
1591 # gh-104635: Since the value of b is dependent on the value of a
1592 # the first STORE_FAST for a should not be skipped. (e.g POP_TOP).
1593 # This test case is added to prevent potential regression from aggressive optimization.
1594 def f():
1595 a = 42; b = a + 54; a = 54
1596 return a, b
1597 self.assertEqual(f(), (54, 96))
1598
1599 def test_duplicated_small_exit_block(self):
1600 # See gh-109627
1601 def f():
1602 while element and something:
1603 try:
1604 return something
1605 except:
1606 pass
1607
1608 def test_cold_block_moved_to_end(self):
1609 # See gh-109719
1610 def f():
1611 while name:
1612 try:
1613 break
1614 except:
1615 pass
1616 else:
1617 1 if 1 else 1
1618
1619 def test_remove_empty_basic_block_with_jump_target_label(self):
1620 # See gh-109823
1621 def f(x):
1622 while x:
1623 0 if 1 else 0
1624
1625 def test_remove_redundant_nop_edge_case(self):
1626 # See gh-109889
1627 def f():
1628 a if (1 if b else c) else d
1629
1630 def test_global_declaration_in_except_used_in_else(self):
1631 # See gh-111123
1632 code = textwrap.dedent("""\
1633 def f():
1634 try:
1635 pass
1636 %s Exception:
1637 global a
1638 else:
1639 print(a)
1640 """)
1641
1642 g, l = {'a': 5}, {}
1643 for kw in ("except", "except*"):
1644 exec(code % kw, g, l);
1645
1646 def test_regression_gh_120225(self):
1647 async def name_4():
1648 match b'':
1649 case True:
1650 pass
1651 case name_5 if f'e':
1652 {name_3: name_4 async for name_2 in name_5}
1653 case []:
1654 pass
1655 [[]]
1656
1657 def test_globals_dict_subclass(self):
1658 # gh-132386
1659 class WeirdDict(dict):
1660 pass
1661
1662 ns = {}
1663 exec('def foo(): return a', WeirdDict(), ns)
1664
1665 self.assertRaises(NameError, ns['foo'])
1666
1667 def test_compile_warnings(self):
1668 # Each invocation of compile() emits compiler warnings, even if they
1669 # have the same message and line number.
1670 source = textwrap.dedent(r"""
1671 # tokenizer
1672 1or 0 # line 3
1673 # code generator
1674 1 is 1 # line 5
1675 """)
1676 with warnings.catch_warnings(record=True) as caught:
1677 warnings.simplefilter("default")
1678 for i in range(2):
1679 # Even if compile() is at the same line.
1680 compile(source, '<stdin>', 'exec')
1681
1682 self.assertEqual([wm.lineno for wm in caught], [3, 5] * 2)
1683
1684 def test_compile_warning_in_finally(self):
1685 # Ensure that warnings inside finally blocks are
1686 # only emitted once despite the block being
1687 # compiled twice (for normal execution and for
1688 # exception handling).
1689 source = textwrap.dedent("""
1690 try:
1691 pass
1692 finally:
1693 1 is 1 # line 5
1694 try:
1695 pass
1696 finally: # nested
1697 1 is 1 # line 9
1698 """)
1699
1700 with warnings.catch_warnings(record=True) as caught:
1701 warnings.simplefilter("always")
1702 compile(source, '<stdin>', 'exec')
1703
1704 self.assertEqual(sorted(wm.lineno for wm in caught), [5, 9])
1705 for wm in caught:
1706 self.assertEqual(wm.category, SyntaxWarning)
1707 self.assertIn("\"is\" with 'int' literal", str(wm.message))
1708
1709 # Other code path is used for "try" with "except*".
1710 source = textwrap.dedent("""
1711 try:
1712 pass
1713 except *Exception:
1714 pass
1715 finally:
1716 1 is 1 # line 7
1717 try:
1718 pass
1719 except *Exception:
1720 pass
1721 finally: # nested
1722 1 is 1 # line 13
1723 """)
1724
1725 with warnings.catch_warnings(record=True) as caught:
1726 warnings.simplefilter("always")
1727 compile(source, '<stdin>', 'exec')
1728
1729 self.assertEqual(sorted(wm.lineno for wm in caught), [7, 13])
1730 for wm in caught:
1731 self.assertEqual(wm.category, SyntaxWarning)
1732 self.assertIn("\"is\" with 'int' literal", str(wm.message))
1733
1734 @support.subTests('src', [
1735 textwrap.dedent("""
1736 def f():
1737 try:
1738 pass
1739 finally:
1740 return 42
1741 """),
1742 textwrap.dedent("""
1743 for x in y:
1744 try:
1745 pass
1746 finally:
1747 break
1748 """),
1749 textwrap.dedent("""
1750 for x in y:
1751 try:
1752 pass
1753 finally:
1754 continue
1755 """),
1756 ])
1757 def test_pep_765_warnings(self, src):
1758 with self.assertWarnsRegex(SyntaxWarning, 'finally'):
1759 compile(src, '<string>', 'exec')
1760 with warnings.catch_warnings():
1761 warnings.simplefilter("error")
1762 tree = ast.parse(src)
1763 with self.assertWarnsRegex(SyntaxWarning, 'finally'):
1764 compile(tree, '<string>', 'exec')
1765
1766 @support.subTests('src', [
1767 textwrap.dedent("""
1768 try:
1769 pass
1770 finally:
1771 def f():
1772 return 42
1773 """),
1774 textwrap.dedent("""
1775 try:
1776 pass
1777 finally:
1778 for x in y:
1779 break
1780 """),
1781 textwrap.dedent("""
1782 try:
1783 pass
1784 finally:
1785 for x in y:
1786 continue
1787 """),
1788 ])
1789 def test_pep_765_no_warnings(self, src):
1790 with warnings.catch_warnings():
1791 warnings.simplefilter("error")
1792 compile(src, '<string>', 'exec')
1793
1794
1795 class TestBooleanExpression(unittest.TestCase):
1796 class Value:
1797 def __init__(self):
1798 self.called = 0
1799
1800 def __bool__(self):
1801 self.called += 1
1802 return self.value
1803
1804 class Yes(Value):
1805 value = True
1806
1807 class No(Value):
1808 value = False
1809
1810 def test_short_circuit_and(self):
1811 v = [self.Yes(), self.No(), self.Yes()]
1812 res = v[0] and v[1] and v[0]
1813 self.assertIs(res, v[1])
1814 self.assertEqual([e.called for e in v], [1, 1, 0])
1815
1816 def test_short_circuit_or(self):
1817 v = [self.No(), self.Yes(), self.No()]
1818 res = v[0] or v[1] or v[0]
1819 self.assertIs(res, v[1])
1820 self.assertEqual([e.called for e in v], [1, 1, 0])
1821
1822 def test_compound(self):
1823 # See gh-124285
1824 v = [self.No(), self.Yes(), self.Yes(), self.Yes()]
1825 res = v[0] and v[1] or v[2] or v[3]
1826 self.assertIs(res, v[2])
1827 self.assertEqual([e.called for e in v], [1, 0, 1, 0])
1828
1829 v = [self.No(), self.No(), self.Yes(), self.Yes(), self.No()]
1830 res = v[0] or v[1] and v[2] or v[3] or v[4]
1831 self.assertIs(res, v[3])
1832 self.assertEqual([e.called for e in v], [1, 1, 0, 1, 0])
1833
1834 def test_exception(self):
1835 # See gh-137288
1836 class Foo:
1837 def __bool__(self):
1838 raise NotImplementedError()
1839
1840 a = Foo()
1841 b = Foo()
1842
1843 with self.assertRaises(NotImplementedError):
1844 bool(a)
1845
1846 with self.assertRaises(NotImplementedError):
1847 c = a or b
1848
1849 @requires_debug_ranges()
1850 class TestSourcePositions(unittest.TestCase):
1851 # Ensure that compiled code snippets have correct line and column numbers
1852 # in `co_positions()`.
1853
1854 def check_positions_against_ast(self, snippet):
1855 # Basic check that makes sure each line and column is at least present
1856 # in one of the AST nodes of the source code.
1857 code = compile(snippet, 'test_compile.py', 'exec')
1858 ast_tree = compile(snippet, 'test_compile.py', 'exec', _ast.PyCF_ONLY_AST)
1859 self.assertTrue(type(ast_tree) == _ast.Module)
1860
1861 # Use an AST visitor that notes all the offsets.
1862 lines, end_lines, columns, end_columns = set(), set(), set(), set()
1863 class SourceOffsetVisitor(ast.NodeVisitor):
1864 def generic_visit(self, node):
1865 super().generic_visit(node)
1866 if not isinstance(node, (ast.expr, ast.stmt, ast.pattern)):
1867 return
1868 lines.add(node.lineno)
1869 end_lines.add(node.end_lineno)
1870 columns.add(node.col_offset)
1871 end_columns.add(node.end_col_offset)
1872
1873 SourceOffsetVisitor().visit(ast_tree)
1874
1875 # Check against the positions in the code object.
1876 for (line, end_line, col, end_col) in code.co_positions():
1877 if line == 0:
1878 continue # This is an artificial module-start line
1879 # If the offset is not None (indicating missing data), ensure that
1880 # it was part of one of the AST nodes.
1881 if line is not None:
1882 self.assertIn(line, lines)
1883 if end_line is not None:
1884 self.assertIn(end_line, end_lines)
1885 if col is not None:
1886 self.assertIn(col, columns)
1887 if end_col is not None:
1888 self.assertIn(end_col, end_columns)
1889
1890 return code, ast_tree
1891
1892 def assertOpcodeSourcePositionIs(self, code, opcode,
1893 line, end_line, column, end_column, occurrence=1):
1894
1895 for instr, position in instructions_with_positions(
1896 dis.Bytecode(code), code.co_positions()
1897 ):
1898 if instr.opname == opcode:
1899 occurrence -= 1
1900 if not occurrence:
1901 self.assertEqual(position[0], line)
1902 self.assertEqual(position[1], end_line)
1903 self.assertEqual(position[2], column)
1904 self.assertEqual(position[3], end_column)
1905 return
1906
1907 self.fail(f"Opcode {opcode} not found in code")
1908
1909 def test_simple_assignment(self):
1910 snippet = "x = 1"
1911 self.check_positions_against_ast(snippet)
1912
1913 def test_compiles_to_extended_op_arg(self):
1914 # Make sure we still have valid positions when the code compiles to an
1915 # EXTENDED_ARG by performing a loop which needs a JUMP_ABSOLUTE after
1916 # a bunch of opcodes.
1917 snippet = "x = x\n" * 10_000
1918 snippet += ("while x != 0:\n"
1919 " x -= 1\n"
1920 "while x != 0:\n"
1921 " x += 1\n"
1922 )
1923
1924 compiled_code, _ = self.check_positions_against_ast(snippet)
1925
1926 self.assertOpcodeSourcePositionIs(compiled_code, 'BINARY_OP',
1927 line=10_000 + 2, end_line=10_000 + 2,
1928 column=2, end_column=8, occurrence=1)
1929 self.assertOpcodeSourcePositionIs(compiled_code, 'BINARY_OP',
1930 line=10_000 + 4, end_line=10_000 + 4,
1931 column=2, end_column=9, occurrence=2)
1932
1933 def test_multiline_expression(self):
1934 snippet = textwrap.dedent("""\
1935 f(
1936 1, 2, 3, 4
1937 )
1938 """)
1939 compiled_code, _ = self.check_positions_against_ast(snippet)
1940 self.assertOpcodeSourcePositionIs(compiled_code, 'CALL',
1941 line=1, end_line=3, column=0, end_column=1)
1942
1943 @requires_specialization
1944 def test_multiline_boolean_expression(self):
1945 snippet = textwrap.dedent("""\
1946 if (a or
1947 (b and not c) or
1948 not (
1949 d > 0)):
1950 x = 42
1951 """)
1952 compiled_code, _ = self.check_positions_against_ast(snippet)
1953 # jump if a is true:
1954 self.assertOpcodeSourcePositionIs(compiled_code, 'POP_JUMP_IF_TRUE',
1955 line=1, end_line=1, column=4, end_column=5, occurrence=1)
1956 # jump if b is false:
1957 self.assertOpcodeSourcePositionIs(compiled_code, 'POP_JUMP_IF_FALSE',
1958 line=2, end_line=2, column=5, end_column=6, occurrence=1)
1959 # jump if c is false:
1960 self.assertOpcodeSourcePositionIs(compiled_code, 'POP_JUMP_IF_FALSE',
1961 line=2, end_line=2, column=15, end_column=16, occurrence=2)
1962 # compare d and 0
1963 self.assertOpcodeSourcePositionIs(compiled_code, 'COMPARE_OP',
1964 line=4, end_line=4, column=8, end_column=13, occurrence=1)
1965 # jump if comparison it True
1966 self.assertOpcodeSourcePositionIs(compiled_code, 'POP_JUMP_IF_TRUE',
1967 line=4, end_line=4, column=8, end_column=13, occurrence=2)
1968
1969 @unittest.skipIf(sys.flags.optimize, "Assertions are disabled in optimized mode")
1970 def test_multiline_assert(self):
1971 snippet = textwrap.dedent("""\
1972 assert (a > 0 and
1973 bb > 0 and
1974 ccc == 1000000), "error msg"
1975 """)
1976 compiled_code, _ = self.check_positions_against_ast(snippet)
1977 self.assertOpcodeSourcePositionIs(compiled_code, 'LOAD_COMMON_CONSTANT',
1978 line=1, end_line=3, column=0, end_column=36, occurrence=1)
1979 # The "error msg":
1980 self.assertOpcodeSourcePositionIs(compiled_code, 'LOAD_CONST',
1981 line=3, end_line=3, column=25, end_column=36, occurrence=2)
1982 self.assertOpcodeSourcePositionIs(compiled_code, 'CALL',
1983 line=1, end_line=3, column=0, end_column=36, occurrence=1)
1984 self.assertOpcodeSourcePositionIs(compiled_code, 'RAISE_VARARGS',
1985 line=1, end_line=3, column=8, end_column=22, occurrence=1)
1986
1987 def test_multiline_generator_expression(self):
1988 snippet = textwrap.dedent("""\
1989 ((x,
1990 2*x)
1991 for x
1992 in [1,2,3] if (x > 0
1993 and x < 100
1994 and x != 50))
1995 """)
1996 compiled_code, _ = self.check_positions_against_ast(snippet)
1997 compiled_code = compiled_code.co_consts[0]
1998 self.assertIsInstance(compiled_code, types.CodeType)
1999 self.assertOpcodeSourcePositionIs(compiled_code, 'YIELD_VALUE',
2000 line=1, end_line=2, column=1, end_column=8, occurrence=1)
2001 self.assertOpcodeSourcePositionIs(compiled_code, 'JUMP_BACKWARD',
2002 line=1, end_line=2, column=1, end_column=8, occurrence=1)
2003 self.assertOpcodeSourcePositionIs(compiled_code, 'RETURN_VALUE',
2004 line=4, end_line=4, column=7, end_column=14, occurrence=1)
2005
2006 def test_multiline_async_generator_expression(self):
2007 snippet = textwrap.dedent("""\
2008 ((x,
2009 2*x)
2010 async for x
2011 in [1,2,3] if (x > 0
2012 and x < 100
2013 and x != 50))
2014 """)
2015 compiled_code, _ = self.check_positions_against_ast(snippet)
2016 compiled_code = compiled_code.co_consts[0]
2017 self.assertIsInstance(compiled_code, types.CodeType)
2018 self.assertOpcodeSourcePositionIs(compiled_code, 'YIELD_VALUE',
2019 line=1, end_line=2, column=1, end_column=8, occurrence=2)
2020 self.assertOpcodeSourcePositionIs(compiled_code, 'RETURN_VALUE',
2021 line=1, end_line=6, column=0, end_column=32, occurrence=1)
2022
2023 def test_multiline_list_comprehension(self):
2024 snippet = textwrap.dedent("""\
2025 [(x,
2026 2*x)
2027 for x
2028 in [1,2,3] if (x > 0
2029 and x < 100
2030 and x != 50)]
2031 """)
2032 compiled_code, _ = self.check_positions_against_ast(snippet)
2033 self.assertIsInstance(compiled_code, types.CodeType)
2034 self.assertOpcodeSourcePositionIs(compiled_code, 'LIST_APPEND',
2035 line=1, end_line=2, column=1, end_column=8, occurrence=1)
2036 self.assertOpcodeSourcePositionIs(compiled_code, 'JUMP_BACKWARD',
2037 line=1, end_line=2, column=1, end_column=8, occurrence=1)
2038
2039 def test_multiline_async_list_comprehension(self):
2040 snippet = textwrap.dedent("""\
2041 async def f():
2042 [(x,
2043 2*x)
2044 async for x
2045 in [1,2,3] if (x > 0
2046 and x < 100
2047 and x != 50)]
2048 """)
2049 compiled_code, _ = self.check_positions_against_ast(snippet)
2050 g = {}
2051 eval(compiled_code, g)
2052 compiled_code = g['f'].__code__
2053 self.assertIsInstance(compiled_code, types.CodeType)
2054 self.assertOpcodeSourcePositionIs(compiled_code, 'LIST_APPEND',
2055 line=2, end_line=3, column=5, end_column=12, occurrence=1)
2056 self.assertOpcodeSourcePositionIs(compiled_code, 'JUMP_BACKWARD',
2057 line=2, end_line=3, column=5, end_column=12, occurrence=1)
2058 self.assertOpcodeSourcePositionIs(compiled_code, 'RETURN_VALUE',
2059 line=2, end_line=7, column=4, end_column=36, occurrence=1)
2060
2061 def test_multiline_set_comprehension(self):
2062 snippet = textwrap.dedent("""\
2063 {(x,
2064 2*x)
2065 for x
2066 in [1,2,3] if (x > 0
2067 and x < 100
2068 and x != 50)}
2069 """)
2070 compiled_code, _ = self.check_positions_against_ast(snippet)
2071 self.assertIsInstance(compiled_code, types.CodeType)
2072 self.assertOpcodeSourcePositionIs(compiled_code, 'SET_ADD',
2073 line=1, end_line=2, column=1, end_column=8, occurrence=1)
2074 self.assertOpcodeSourcePositionIs(compiled_code, 'JUMP_BACKWARD',
2075 line=1, end_line=2, column=1, end_column=8, occurrence=1)
2076
2077 def test_multiline_async_set_comprehension(self):
2078 snippet = textwrap.dedent("""\
2079 async def f():
2080 {(x,
2081 2*x)
2082 async for x
2083 in [1,2,3] if (x > 0
2084 and x < 100
2085 and x != 50)}
2086 """)
2087 compiled_code, _ = self.check_positions_against_ast(snippet)
2088 g = {}
2089 eval(compiled_code, g)
2090 compiled_code = g['f'].__code__
2091 self.assertIsInstance(compiled_code, types.CodeType)
2092 self.assertOpcodeSourcePositionIs(compiled_code, 'SET_ADD',
2093 line=2, end_line=3, column=5, end_column=12, occurrence=1)
2094 self.assertOpcodeSourcePositionIs(compiled_code, 'JUMP_BACKWARD',
2095 line=2, end_line=3, column=5, end_column=12, occurrence=1)
2096 self.assertOpcodeSourcePositionIs(compiled_code, 'RETURN_VALUE',
2097 line=2, end_line=7, column=4, end_column=36, occurrence=1)
2098
2099 def test_multiline_dict_comprehension(self):
2100 snippet = textwrap.dedent("""\
2101 {x:
2102 2*x
2103 for x
2104 in [1,2,3] if (x > 0
2105 and x < 100
2106 and x != 50)}
2107 """)
2108 compiled_code, _ = self.check_positions_against_ast(snippet)
2109 self.assertIsInstance(compiled_code, types.CodeType)
2110 self.assertOpcodeSourcePositionIs(compiled_code, 'MAP_ADD',
2111 line=1, end_line=2, column=1, end_column=7, occurrence=1)
2112 self.assertOpcodeSourcePositionIs(compiled_code, 'JUMP_BACKWARD',
2113 line=1, end_line=2, column=1, end_column=7, occurrence=1)
2114
2115 def test_multiline_async_dict_comprehension(self):
2116 snippet = textwrap.dedent("""\
2117 async def f():
2118 {x:
2119 2*x
2120 async for x
2121 in [1,2,3] if (x > 0
2122 and x < 100
2123 and x != 50)}
2124 """)
2125 compiled_code, _ = self.check_positions_against_ast(snippet)
2126 g = {}
2127 eval(compiled_code, g)
2128 compiled_code = g['f'].__code__
2129 self.assertIsInstance(compiled_code, types.CodeType)
2130 self.assertOpcodeSourcePositionIs(compiled_code, 'MAP_ADD',
2131 line=2, end_line=3, column=5, end_column=11, occurrence=1)
2132 self.assertOpcodeSourcePositionIs(compiled_code, 'JUMP_BACKWARD',
2133 line=2, end_line=3, column=5, end_column=11, occurrence=1)
2134 self.assertOpcodeSourcePositionIs(compiled_code, 'RETURN_VALUE',
2135 line=2, end_line=7, column=4, end_column=36, occurrence=1)
2136
2137 def test_matchcase_sequence(self):
2138 snippet = textwrap.dedent("""\
2139 match x:
2140 case a, b:
2141 pass
2142 """)
2143 compiled_code, _ = self.check_positions_against_ast(snippet)
2144 self.assertOpcodeSourcePositionIs(compiled_code, 'MATCH_SEQUENCE',
2145 line=2, end_line=2, column=9, end_column=13, occurrence=1)
2146 self.assertOpcodeSourcePositionIs(compiled_code, 'UNPACK_SEQUENCE',
2147 line=2, end_line=2, column=9, end_column=13, occurrence=1)
2148 self.assertOpcodeSourcePositionIs(compiled_code, 'STORE_NAME',
2149 line=2, end_line=2, column=9, end_column=13, occurrence=1)
2150 self.assertOpcodeSourcePositionIs(compiled_code, 'STORE_NAME',
2151 line=2, end_line=2, column=9, end_column=13, occurrence=2)
2152
2153 def test_matchcase_sequence_wildcard(self):
2154 snippet = textwrap.dedent("""\
2155 match x:
2156 case a, *b, c:
2157 pass
2158 """)
2159 compiled_code, _ = self.check_positions_against_ast(snippet)
2160 self.assertOpcodeSourcePositionIs(compiled_code, 'MATCH_SEQUENCE',
2161 line=2, end_line=2, column=9, end_column=17, occurrence=1)
2162 self.assertOpcodeSourcePositionIs(compiled_code, 'UNPACK_EX',
2163 line=2, end_line=2, column=9, end_column=17, occurrence=1)
2164 self.assertOpcodeSourcePositionIs(compiled_code, 'STORE_NAME',
2165 line=2, end_line=2, column=9, end_column=17, occurrence=1)
2166 self.assertOpcodeSourcePositionIs(compiled_code, 'STORE_NAME',
2167 line=2, end_line=2, column=9, end_column=17, occurrence=2)
2168 self.assertOpcodeSourcePositionIs(compiled_code, 'STORE_NAME',
2169 line=2, end_line=2, column=9, end_column=17, occurrence=3)
2170
2171 def test_matchcase_mapping(self):
2172 snippet = textwrap.dedent("""\
2173 match x:
2174 case {"a" : a, "b": b}:
2175 pass
2176 """)
2177 compiled_code, _ = self.check_positions_against_ast(snippet)
2178 self.assertOpcodeSourcePositionIs(compiled_code, 'MATCH_MAPPING',
2179 line=2, end_line=2, column=9, end_column=26, occurrence=1)
2180 self.assertOpcodeSourcePositionIs(compiled_code, 'MATCH_KEYS',
2181 line=2, end_line=2, column=9, end_column=26, occurrence=1)
2182 self.assertOpcodeSourcePositionIs(compiled_code, 'STORE_NAME',
2183 line=2, end_line=2, column=9, end_column=26, occurrence=1)
2184 self.assertOpcodeSourcePositionIs(compiled_code, 'STORE_NAME',
2185 line=2, end_line=2, column=9, end_column=26, occurrence=2)
2186
2187 def test_matchcase_mapping_wildcard(self):
2188 snippet = textwrap.dedent("""\
2189 match x:
2190 case {"a" : a, "b": b, **c}:
2191 pass
2192 """)
2193 compiled_code, _ = self.check_positions_against_ast(snippet)
2194 self.assertOpcodeSourcePositionIs(compiled_code, 'MATCH_MAPPING',
2195 line=2, end_line=2, column=9, end_column=31, occurrence=1)
2196 self.assertOpcodeSourcePositionIs(compiled_code, 'MATCH_KEYS',
2197 line=2, end_line=2, column=9, end_column=31, occurrence=1)
2198 self.assertOpcodeSourcePositionIs(compiled_code, 'STORE_NAME',
2199 line=2, end_line=2, column=9, end_column=31, occurrence=1)
2200 self.assertOpcodeSourcePositionIs(compiled_code, 'STORE_NAME',
2201 line=2, end_line=2, column=9, end_column=31, occurrence=2)
2202
2203 def test_matchcase_class(self):
2204 snippet = textwrap.dedent("""\
2205 match x:
2206 case C(a, b):
2207 pass
2208 """)
2209 compiled_code, _ = self.check_positions_against_ast(snippet)
2210 self.assertOpcodeSourcePositionIs(compiled_code, 'MATCH_CLASS',
2211 line=2, end_line=2, column=9, end_column=16, occurrence=1)
2212 self.assertOpcodeSourcePositionIs(compiled_code, 'UNPACK_SEQUENCE',
2213 line=2, end_line=2, column=9, end_column=16, occurrence=1)
2214 self.assertOpcodeSourcePositionIs(compiled_code, 'STORE_NAME',
2215 line=2, end_line=2, column=9, end_column=16, occurrence=1)
2216 self.assertOpcodeSourcePositionIs(compiled_code, 'STORE_NAME',
2217 line=2, end_line=2, column=9, end_column=16, occurrence=2)
2218
2219 def test_matchcase_or(self):
2220 snippet = textwrap.dedent("""\
2221 match x:
2222 case C(1) | C(2):
2223 pass
2224 """)
2225 compiled_code, _ = self.check_positions_against_ast(snippet)
2226 self.assertOpcodeSourcePositionIs(compiled_code, 'MATCH_CLASS',
2227 line=2, end_line=2, column=9, end_column=13, occurrence=1)
2228 self.assertOpcodeSourcePositionIs(compiled_code, 'MATCH_CLASS',
2229 line=2, end_line=2, column=16, end_column=20, occurrence=2)
2230
2231 def test_very_long_line_end_offset(self):
2232 # Make sure we get the correct column offset for offsets
2233 # too large to store in a byte.
2234 long_string = "a" * 1000
2235 snippet = f"g('{long_string}')"
2236
2237 compiled_code, _ = self.check_positions_against_ast(snippet)
2238 self.assertOpcodeSourcePositionIs(compiled_code, 'CALL',
2239 line=1, end_line=1, column=0, end_column=1005)
2240
2241 def test_complex_single_line_expression(self):
2242 snippet = "a - b @ (c * x['key'] + 23)"
2243
2244 compiled_code, _ = self.check_positions_against_ast(snippet)
2245 self.assertOpcodeSourcePositionIs(compiled_code, 'BINARY_OP',
2246 line=1, end_line=1, column=13, end_column=21)
2247 self.assertOpcodeSourcePositionIs(compiled_code, 'BINARY_OP',
2248 line=1, end_line=1, column=9, end_column=21, occurrence=2)
2249 self.assertOpcodeSourcePositionIs(compiled_code, 'BINARY_OP',
2250 line=1, end_line=1, column=9, end_column=26, occurrence=3)
2251 self.assertOpcodeSourcePositionIs(compiled_code, 'BINARY_OP',
2252 line=1, end_line=1, column=4, end_column=27, occurrence=4)
2253 self.assertOpcodeSourcePositionIs(compiled_code, 'BINARY_OP',
2254 line=1, end_line=1, column=0, end_column=27, occurrence=5)
2255
2256 def test_multiline_assert_rewritten_as_method_call(self):
2257 # GH-94694: Don't crash if pytest rewrites a multiline assert as a
2258 # method call with the same location information:
2259 tree = ast.parse("assert (\n42\n)")
2260 old_node = tree.body[0]
2261 new_node = ast.Expr(
2262 ast.Call(
2263 ast.Attribute(
2264 ast.Name("spam", ast.Load()),
2265 "eggs",
2266 ast.Load(),
2267 ),
2268 [],
2269 [],
2270 )
2271 )
2272 ast.copy_location(new_node, old_node)
2273 ast.fix_missing_locations(new_node)
2274 tree.body[0] = new_node
2275 compile(tree, "<test>", "exec")
2276
2277 def test_push_null_load_global_positions(self):
2278 source_template = """
2279 import abc, dis
2280 import ast as art
2281
2282 abc = None
2283 dix = dis
2284 ast = art
2285
2286 def f():
2287 {}
2288 """
2289 for body in [
2290 " abc.a()",
2291 " art.a()",
2292 " ast.a()",
2293 " dis.a()",
2294 " dix.a()",
2295 " abc[...]()",
2296 " art()()",
2297 " (ast or ...)()",
2298 " [dis]()",
2299 " (dix + ...)()",
2300 ]:
2301 with self.subTest(body):
2302 namespace = {}
2303 source = textwrap.dedent(source_template.format(body))
2304 with warnings.catch_warnings():
2305 warnings.simplefilter('ignore', SyntaxWarning)
2306 exec(source, namespace)
2307 code = namespace["f"].__code__
2308 self.assertOpcodeSourcePositionIs(
2309 code,
2310 "LOAD_GLOBAL",
2311 line=10,
2312 end_line=10,
2313 column=4,
2314 end_column=7,
2315 )
2316
2317 def test_attribute_augassign(self):
2318 source = "(\n lhs \n . \n rhs \n ) += 42"
2319 code = compile(source, "<test>", "exec")
2320 self.assertOpcodeSourcePositionIs(
2321 code, "LOAD_ATTR", line=4, end_line=4, column=5, end_column=8
2322 )
2323 self.assertOpcodeSourcePositionIs(
2324 code, "STORE_ATTR", line=4, end_line=4, column=5, end_column=8
2325 )
2326
2327 def test_attribute_del(self):
2328 source = "del (\n lhs \n . \n rhs \n )"
2329 code = compile(source, "<test>", "exec")
2330 self.assertOpcodeSourcePositionIs(
2331 code, "DELETE_ATTR", line=4, end_line=4, column=5, end_column=8
2332 )
2333
2334 def test_attribute_load(self):
2335 source = "(\n lhs \n . \n rhs \n )"
2336 code = compile(source, "<test>", "exec")
2337 self.assertOpcodeSourcePositionIs(
2338 code, "LOAD_ATTR", line=4, end_line=4, column=5, end_column=8
2339 )
2340
2341 def test_attribute_store(self):
2342 source = "(\n lhs \n . \n rhs \n ) = 42"
2343 code = compile(source, "<test>", "exec")
2344 self.assertOpcodeSourcePositionIs(
2345 code, "STORE_ATTR", line=4, end_line=4, column=5, end_column=8
2346 )
2347
2348 def test_method_call(self):
2349 source = "(\n lhs \n . \n rhs \n )()"
2350 code = compile(source, "<test>", "exec")
2351 self.assertOpcodeSourcePositionIs(
2352 code, "LOAD_ATTR", line=4, end_line=4, column=5, end_column=8
2353 )
2354 self.assertOpcodeSourcePositionIs(
2355 code, "CALL", line=4, end_line=5, column=5, end_column=10
2356 )
2357
2358 def test_weird_attribute_position_regressions(self):
2359 def f():
2360 (bar.
2361 baz)
2362 (bar.
2363 baz(
2364 ))
2365 files().setdefault(
2366 0
2367 ).setdefault(
2368 0
2369 )
2370 for line, end_line, column, end_column in f.__code__.co_positions():
2371 self.assertIsNotNone(line)
2372 self.assertIsNotNone(end_line)
2373 self.assertIsNotNone(column)
2374 self.assertIsNotNone(end_column)
2375 self.assertLessEqual((line, column), (end_line, end_column))
2376
2377 @support.cpython_only
2378 def test_column_offset_deduplication(self):
2379 # GH-95150: Code with different column offsets shouldn't be merged!
2380 for source in [
2381 "lambda: a",
2382 "(a for b in c)",
2383 ]:
2384 with self.subTest(source):
2385 code = compile(f"{source}, {source}", "<test>", "eval")
2386 self.assertEqual(len(code.co_consts), 2)
2387 self.assertIsInstance(code.co_consts[0], types.CodeType)
2388 self.assertIsInstance(code.co_consts[1], types.CodeType)
2389 self.assertNotEqual(code.co_consts[0], code.co_consts[1])
2390 self.assertNotEqual(
2391 list(code.co_consts[0].co_positions()),
2392 list(code.co_consts[1].co_positions()),
2393 )
2394
2395 def test_load_super_attr(self):
2396 source = "class C:\n def __init__(self):\n super().__init__()"
2397 for const in compile(source, "<test>", "exec").co_consts[0].co_consts:
2398 if isinstance(const, types.CodeType):
2399 code = const
2400 break
2401 self.assertOpcodeSourcePositionIs(
2402 code, "LOAD_GLOBAL", line=3, end_line=3, column=4, end_column=9
2403 )
2404
2405 def test_lambda_return_position(self):
2406 snippets = [
2407 "f = lambda: x",
2408 "f = lambda: 42",
2409 "f = lambda: 1 + 2",
2410 "f = lambda: a + b",
2411 ]
2412 for snippet in snippets:
2413 with self.subTest(snippet=snippet):
2414 lamb = run_code(snippet)["f"]
2415 positions = lamb.__code__.co_positions()
2416 # assert that all positions are within the lambda
2417 for i, pos in enumerate(positions):
2418 with self.subTest(i=i, pos=pos):
2419 start_line, end_line, start_col, end_col = pos
2420 if i == 0 and start_col == end_col == 0:
2421 # ignore the RESUME in the beginning
2422 continue
2423 self.assertEqual(start_line, 1)
2424 self.assertEqual(end_line, 1)
2425 code_start = snippet.find(":") + 2
2426 code_end = len(snippet)
2427 self.assertGreaterEqual(start_col, code_start)
2428 self.assertLessEqual(end_col, code_end)
2429 self.assertGreaterEqual(end_col, start_col)
2430 self.assertLessEqual(end_col, code_end)
2431
2432 def test_return_in_with_positions(self):
2433 # See gh-98442
2434 def f():
2435 with xyz:
2436 1
2437 2
2438 3
2439 4
2440 return R
2441
2442 # All instructions should have locations on a single line
2443 for instr in dis.get_instructions(f):
2444 start_line, end_line, _, _ = instr.positions
2445 self.assertEqual(start_line, end_line)
2446
2447 # Expect four `LOAD_CONST None` instructions:
2448 # three for the no-exception __exit__ call, and one for the return.
2449 # They should all have the locations of the context manager ('xyz').
2450
2451 load_none = [instr for instr in dis.get_instructions(f) if
2452 instr.opname == 'LOAD_CONST' and instr.argval is None]
2453 return_value = [instr for instr in dis.get_instructions(f) if
2454 instr.opname == 'RETURN_VALUE']
2455
2456 self.assertEqual(len(load_none), 4)
2457 self.assertEqual(len(return_value), 2)
2458 for instr in load_none + return_value:
2459 start_line, end_line, start_col, end_col = instr.positions
2460 self.assertEqual(start_line, f.__code__.co_firstlineno + 1)
2461 self.assertEqual(end_line, f.__code__.co_firstlineno + 1)
2462 self.assertEqual(start_col, 17)
2463 self.assertEqual(end_col, 20)
2464
2465
2466 class TestStaticAttributes(unittest.TestCase):
2467
2468 def test_basic(self):
2469 class C:
2470 def f(self):
2471 self.a = self.b = 42
2472 # read fields are not included
2473 self.f()
2474 self.arr[3]
2475
2476 self.assertIsInstance(C.__static_attributes__, tuple)
2477 self.assertEqual(sorted(C.__static_attributes__), ['a', 'b'])
2478
2479 def test_nested_function(self):
2480 class C:
2481 def f(self):
2482 self.x = 1
2483 self.y = 2
2484 self.x = 3 # check deduplication
2485
2486 def g(self, obj):
2487 self.y = 4
2488 self.z = 5
2489
2490 def h(self, a):
2491 self.u = 6
2492 self.v = 7
2493
2494 obj.self = 8
2495
2496 self.assertEqual(sorted(C.__static_attributes__), ['u', 'v', 'x', 'y', 'z'])
2497
2498 def test_nested_class(self):
2499 class C:
2500 def f(self):
2501 self.x = 42
2502 self.y = 42
2503
2504 class D:
2505 def g(self):
2506 self.y = 42
2507 self.z = 42
2508
2509 self.assertEqual(sorted(C.__static_attributes__), ['x', 'y'])
2510 self.assertEqual(sorted(C.D.__static_attributes__), ['y', 'z'])
2511
2512 def test_subclass(self):
2513 class C:
2514 def f(self):
2515 self.x = 42
2516 self.y = 42
2517
2518 class D(C):
2519 def g(self):
2520 self.y = 42
2521 self.z = 42
2522
2523 self.assertEqual(sorted(C.__static_attributes__), ['x', 'y'])
2524 self.assertEqual(sorted(D.__static_attributes__), ['y', 'z'])
2525
2526
2527 class TestExpressionStackSize(unittest.TestCase):
2528 # These tests check that the computed stack size for a code object
2529 # stays within reasonable bounds (see issue #21523 for an example
2530 # dysfunction).
2531 N = 100
2532
2533 def check_stack_size(self, code):
2534 # To assert that the alleged stack size is not O(N), we
2535 # check that it is smaller than log(N).
2536 if isinstance(code, str):
2537 code = compile(code, "<foo>", "single")
2538 max_size = math.ceil(math.log(len(code.co_code)))
2539 self.assertLessEqual(code.co_stacksize, max_size)
2540
2541 def test_and(self):
2542 self.check_stack_size("x and " * self.N + "x")
2543
2544 def test_or(self):
2545 self.check_stack_size("x or " * self.N + "x")
2546
2547 def test_and_or(self):
2548 self.check_stack_size("x and x or " * self.N + "x")
2549
2550 def test_chained_comparison(self):
2551 self.check_stack_size("x < " * self.N + "x")
2552
2553 def test_if_else(self):
2554 self.check_stack_size("x if x else " * self.N + "x")
2555
2556 def test_binop(self):
2557 self.check_stack_size("x + " * self.N + "x")
2558
2559 def test_list(self):
2560 self.check_stack_size("[" + "x, " * self.N + "x]")
2561
2562 def test_tuple(self):
2563 self.check_stack_size("(" + "x, " * self.N + "x)")
2564
2565 def test_set(self):
2566 self.check_stack_size("{" + "x, " * self.N + "x}")
2567
2568 def test_dict(self):
2569 self.check_stack_size("{" + "x:x, " * self.N + "x:x}")
2570
2571 def test_func_args(self):
2572 self.check_stack_size("f(" + "x, " * self.N + ")")
2573
2574 def test_func_kwargs(self):
2575 kwargs = (f'a{i}=x' for i in range(self.N))
2576 self.check_stack_size("f(" + ", ".join(kwargs) + ")")
2577
2578 def test_meth_args(self):
2579 self.check_stack_size("o.m(" + "x, " * self.N + ")")
2580
2581 def test_meth_kwargs(self):
2582 kwargs = (f'a{i}=x' for i in range(self.N))
2583 self.check_stack_size("o.m(" + ", ".join(kwargs) + ")")
2584
2585 def test_func_and(self):
2586 code = "def f(x):\n"
2587 code += " x and x\n" * self.N
2588 self.check_stack_size(code)
2589
2590 def test_stack_3050(self):
2591 M = 3050
2592 code = "x," * M + "=t"
2593 # This raised on 3.10.0 to 3.10.5
2594 compile(code, "<foo>", "single")
2595
2596 def test_stack_3050_2(self):
2597 M = 3050
2598 args = ", ".join(f"arg{i}:type{i}" for i in range(M))
2599 code = f"def f({args}):\n pass"
2600 # This raised on 3.10.0 to 3.10.5
2601 compile(code, "<foo>", "single")
2602
2603
2604 class TestStackSizeStability(unittest.TestCase):
2605 # Check that repeating certain snippets doesn't increase the stack size
2606 # beyond what a single snippet requires.
2607
2608 def check_stack_size(self, snippet, async_=False):
2609 def compile_snippet(i):
2610 ns = {}
2611 script = """def func():\n""" + i * snippet
2612 if async_:
2613 script = "async " + script
2614 with warnings.catch_warnings():
2615 warnings.simplefilter('ignore', SyntaxWarning)
2616 code = compile(script, "<script>", "exec")
2617 exec(code, ns, ns)
2618 return ns['func'].__code__
2619
2620 sizes = [compile_snippet(i).co_stacksize for i in range(2, 5)]
2621 if len(set(sizes)) != 1:
2622 import dis, io
2623 out = io.StringIO()
2624 dis.dis(compile_snippet(1), file=out)
2625 self.fail("stack sizes diverge with # of consecutive snippets: "
2626 "%s\n%s\n%s" % (sizes, snippet, out.getvalue()))
2627
2628 def test_if(self):
2629 snippet = """
2630 if x:
2631 a
2632 """
2633 self.check_stack_size(snippet)
2634
2635 def test_if_else(self):
2636 snippet = """
2637 if x:
2638 a
2639 elif y:
2640 b
2641 else:
2642 c
2643 """
2644 self.check_stack_size(snippet)
2645
2646 def test_try_except_bare(self):
2647 snippet = """
2648 try:
2649 a
2650 except:
2651 b
2652 """
2653 self.check_stack_size(snippet)
2654
2655 def test_try_except_qualified(self):
2656 snippet = """
2657 try:
2658 a
2659 except ImportError:
2660 b
2661 except:
2662 c
2663 else:
2664 d
2665 """
2666 self.check_stack_size(snippet)
2667
2668 def test_try_except_as(self):
2669 snippet = """
2670 try:
2671 a
2672 except ImportError as e:
2673 b
2674 except:
2675 c
2676 else:
2677 d
2678 """
2679 self.check_stack_size(snippet)
2680
2681 def test_try_except_star_qualified(self):
2682 snippet = """
2683 try:
2684 a
2685 except* ImportError:
2686 b
2687 else:
2688 c
2689 """
2690 self.check_stack_size(snippet)
2691
2692 def test_try_except_star_as(self):
2693 snippet = """
2694 try:
2695 a
2696 except* ImportError as e:
2697 b
2698 else:
2699 c
2700 """
2701 self.check_stack_size(snippet)
2702
2703 def test_try_except_star_finally(self):
2704 snippet = """
2705 try:
2706 a
2707 except* A:
2708 b
2709 finally:
2710 c
2711 """
2712 self.check_stack_size(snippet)
2713
2714 def test_try_finally(self):
2715 snippet = """
2716 try:
2717 a
2718 finally:
2719 b
2720 """
2721 self.check_stack_size(snippet)
2722
2723 def test_with(self):
2724 snippet = """
2725 with x as y:
2726 a
2727 """
2728 self.check_stack_size(snippet)
2729
2730 def test_while_else(self):
2731 snippet = """
2732 while x:
2733 a
2734 else:
2735 b
2736 """
2737 self.check_stack_size(snippet)
2738
2739 def test_for(self):
2740 snippet = """
2741 for x in y:
2742 a
2743 """
2744 self.check_stack_size(snippet)
2745
2746 def test_for_else(self):
2747 snippet = """
2748 for x in y:
2749 a
2750 else:
2751 b
2752 """
2753 self.check_stack_size(snippet)
2754
2755 def test_for_break_continue(self):
2756 snippet = """
2757 for x in y:
2758 if z:
2759 break
2760 elif u:
2761 continue
2762 else:
2763 a
2764 else:
2765 b
2766 """
2767 self.check_stack_size(snippet)
2768
2769 def test_for_break_continue_inside_try_finally_block(self):
2770 snippet = """
2771 for x in y:
2772 try:
2773 if z:
2774 break
2775 elif u:
2776 continue
2777 else:
2778 a
2779 finally:
2780 f
2781 else:
2782 b
2783 """
2784 self.check_stack_size(snippet)
2785
2786 def test_for_break_continue_inside_finally_block(self):
2787 snippet = """
2788 for x in y:
2789 try:
2790 t
2791 finally:
2792 if z:
2793 break
2794 elif u:
2795 continue
2796 else:
2797 a
2798 else:
2799 b
2800 """
2801 self.check_stack_size(snippet)
2802
2803 def test_for_break_continue_inside_except_block(self):
2804 snippet = """
2805 for x in y:
2806 try:
2807 t
2808 except:
2809 if z:
2810 break
2811 elif u:
2812 continue
2813 else:
2814 a
2815 else:
2816 b
2817 """
2818 self.check_stack_size(snippet)
2819
2820 def test_for_break_continue_inside_with_block(self):
2821 snippet = """
2822 for x in y:
2823 with c:
2824 if z:
2825 break
2826 elif u:
2827 continue
2828 else:
2829 a
2830 else:
2831 b
2832 """
2833 self.check_stack_size(snippet)
2834
2835 def test_return_inside_try_finally_block(self):
2836 snippet = """
2837 try:
2838 if z:
2839 return
2840 else:
2841 a
2842 finally:
2843 f
2844 """
2845 self.check_stack_size(snippet)
2846
2847 def test_return_inside_finally_block(self):
2848 snippet = """
2849 try:
2850 t
2851 finally:
2852 if z:
2853 return
2854 else:
2855 a
2856 """
2857 self.check_stack_size(snippet)
2858
2859 def test_return_inside_except_block(self):
2860 snippet = """
2861 try:
2862 t
2863 except:
2864 if z:
2865 return
2866 else:
2867 a
2868 """
2869 self.check_stack_size(snippet)
2870
2871 def test_return_inside_with_block(self):
2872 snippet = """
2873 with c:
2874 if z:
2875 return
2876 else:
2877 a
2878 """
2879 self.check_stack_size(snippet)
2880
2881 def test_async_with(self):
2882 snippet = """
2883 async with x as y:
2884 a
2885 """
2886 self.check_stack_size(snippet, async_=True)
2887
2888 def test_async_for(self):
2889 snippet = """
2890 async for x in y:
2891 a
2892 """
2893 self.check_stack_size(snippet, async_=True)
2894
2895 def test_async_for_else(self):
2896 snippet = """
2897 async for x in y:
2898 a
2899 else:
2900 b
2901 """
2902 self.check_stack_size(snippet, async_=True)
2903
2904 def test_for_break_continue_inside_async_with_block(self):
2905 snippet = """
2906 for x in y:
2907 async with c:
2908 if z:
2909 break
2910 elif u:
2911 continue
2912 else:
2913 a
2914 else:
2915 b
2916 """
2917 self.check_stack_size(snippet, async_=True)
2918
2919 def test_return_inside_async_with_block(self):
2920 snippet = """
2921 async with c:
2922 if z:
2923 return
2924 else:
2925 a
2926 """
2927 self.check_stack_size(snippet, async_=True)
2928
2929 @support.cpython_only
2930 @unittest.skipIf(_testinternalcapi is None, 'need _testinternalcapi module')
2931 class TestInstructionSequence(unittest.TestCase):
2932 def compare_instructions(self, seq, expected):
2933 self.assertEqual([(opcode.opname[i[0]],) + i[1:] for i in seq.get_instructions()],
2934 expected)
2935
2936 def test_basics(self):
2937 seq = _testinternalcapi.new_instruction_sequence()
2938
2939 def add_op(seq, opname, oparg, bl, bc=0, el=0, ec=0):
2940 seq.addop(opcode.opmap[opname], oparg, bl, bc, el, el)
2941
2942 add_op(seq, 'LOAD_CONST', 1, 1)
2943 add_op(seq, 'JUMP', lbl1 := seq.new_label(), 2)
2944 add_op(seq, 'LOAD_CONST', 1, 3)
2945 add_op(seq, 'JUMP', lbl2 := seq.new_label(), 4)
2946 seq.use_label(lbl1)
2947 add_op(seq, 'LOAD_CONST', 2, 4)
2948 seq.use_label(lbl2)
2949 add_op(seq, 'RETURN_VALUE', 0, 3)
2950
2951 expected = [('LOAD_CONST', 1, 1),
2952 ('JUMP', 4, 2),
2953 ('LOAD_CONST', 1, 3),
2954 ('JUMP', 5, 4),
2955 ('LOAD_CONST', 2, 4),
2956 ('RETURN_VALUE', None, 3),
2957 ]
2958
2959 self.compare_instructions(seq, [ex + (0,0,0) for ex in expected])
2960
2961 def test_nested(self):
2962 seq = _testinternalcapi.new_instruction_sequence()
2963 seq.addop(opcode.opmap['LOAD_CONST'], 1, 1, 0, 0, 0)
2964 nested = _testinternalcapi.new_instruction_sequence()
2965 nested.addop(opcode.opmap['LOAD_CONST'], 2, 2, 0, 0, 0)
2966
2967 self.compare_instructions(seq, [('LOAD_CONST', 1, 1, 0, 0, 0)])
2968 self.compare_instructions(nested, [('LOAD_CONST', 2, 2, 0, 0, 0)])
2969
2970 seq.add_nested(nested)
2971 self.compare_instructions(seq, [('LOAD_CONST', 1, 1, 0, 0, 0)])
2972 self.compare_instructions(seq.get_nested()[0], [('LOAD_CONST', 2, 2, 0, 0, 0)])
2973
2974 def test_static_attributes_are_sorted(self):
2975 code = (
2976 'class T:\n'
2977 ' def __init__(self):\n'
2978 ' self.{V1} = 10\n'
2979 ' self.{V2} = 10\n'
2980 ' def foo(self):\n'
2981 ' self.{V3} = 10\n'
2982 )
2983 attributes = ("a", "b", "c")
2984 for perm in itertools.permutations(attributes):
2985 var_names = {f'V{i + 1}': name for i, name in enumerate(perm)}
2986 ns = run_code(code.format(**var_names))
2987 t = ns['T']
2988 self.assertEqual(t.__static_attributes__, attributes)
2989
2990
2991 if __name__ == "__main__":
2992 unittest.main()