Debian Codegraph @

Lib/test/test_peepholer.py

raw
1 import ast
2 import dis
3 import gc
4 from itertools import combinations, product
5 import opcode
6 import sys
7 import textwrap
8 import unittest
9 try:
10 import _testinternalcapi
11 except ImportError:
12 _testinternalcapi = None
13
14 from test import support
15 from test.support.bytecode_helper import (
16 BytecodeTestCase, CfgOptimizationTestCase, CompilationStepTestCase)
17
18
19 def compile_pattern_with_fast_locals(pattern):
20 source = textwrap.dedent(
21 f"""
22 def f(x):
23 match x:
24 case {pattern}:
25 pass
26 """
27 )
28 namespace = {}
29 exec(source, namespace)
30 return namespace["f"].__code__
31
32
33 def count_instr_recursively(f, opname):
34 count = 0
35 for instr in dis.get_instructions(f):
36 if instr.opname == opname:
37 count += 1
38 if hasattr(f, '__code__'):
39 f = f.__code__
40 for c in f.co_consts:
41 if hasattr(c, 'co_code'):
42 count += count_instr_recursively(c, opname)
43 return count
44
45
46 def get_binop_argval(arg):
47 for i, nb_op in enumerate(opcode._nb_ops):
48 if arg == nb_op[0]:
49 return i
50 assert False, f"{arg} is not a valid BINARY_OP argument."
51
52
53 class TestTranforms(BytecodeTestCase):
54
55 def check_jump_targets(self, code):
56 instructions = list(dis.get_instructions(code))
57 targets = {instr.offset: instr for instr in instructions}
58 for instr in instructions:
59 if 'JUMP_' not in instr.opname:
60 continue
61 tgt = targets[instr.argval]
62 # jump to unconditional jump
63 if tgt.opname in ('JUMP_BACKWARD', 'JUMP_FORWARD'):
64 self.fail(f'{instr.opname} at {instr.offset} '
65 f'jumps to {tgt.opname} at {tgt.offset}')
66 # unconditional jump to RETURN_VALUE
67 if (instr.opname in ('JUMP_BACKWARD', 'JUMP_FORWARD') and
68 tgt.opname == 'RETURN_VALUE'):
69 self.fail(f'{instr.opname} at {instr.offset} '
70 f'jumps to {tgt.opname} at {tgt.offset}')
71
72 def check_lnotab(self, code):
73 "Check that the lnotab byte offsets are sensible."
74 code = dis._get_code_object(code)
75 lnotab = list(dis.findlinestarts(code))
76 # Don't bother checking if the line info is sensible, because
77 # most of the line info we can get at comes from lnotab.
78 min_bytecode = min(t[0] for t in lnotab)
79 max_bytecode = max(t[0] for t in lnotab)
80 self.assertGreaterEqual(min_bytecode, 0)
81 self.assertLess(max_bytecode, len(code.co_code))
82 # This could conceivably test more (and probably should, as there
83 # aren't very many tests of lnotab), if peepholer wasn't scheduled
84 # to be replaced anyway.
85
86 def test_unot(self):
87 # UNARY_NOT POP_JUMP_IF_FALSE --> POP_JUMP_IF_TRUE'
88 def unot(x):
89 if not x == 2:
90 del x
91 self.assertNotInBytecode(unot, 'UNARY_NOT')
92 self.assertNotInBytecode(unot, 'POP_JUMP_IF_FALSE')
93 self.assertInBytecode(unot, 'POP_JUMP_IF_TRUE')
94 self.check_lnotab(unot)
95
96 def test_elim_inversion_of_is_or_in(self):
97 for line, cmp_op, invert in (
98 ('not a is b', 'IS_OP', 1,),
99 ('not a is not b', 'IS_OP', 0,),
100 ('not a in b', 'CONTAINS_OP', 1,),
101 ('not a not in b', 'CONTAINS_OP', 0,),
102 ):
103 with self.subTest(line=line):
104 code = compile(line, '', 'single')
105 self.assertInBytecode(code, cmp_op, invert)
106 self.check_lnotab(code)
107
108 def test_global_as_constant(self):
109 # LOAD_GLOBAL None/True/False --> LOAD_CONST None/True/False
110 def f():
111 x = None
112 x = None
113 return x
114 def g():
115 x = True
116 return x
117 def h():
118 x = False
119 return x
120
121 for func, elem in ((f, None), (g, True), (h, False)):
122 with self.subTest(func=func):
123 self.assertNotInBytecode(func, 'LOAD_GLOBAL')
124 self.assertInBytecode(func, 'LOAD_CONST', elem)
125 self.check_lnotab(func)
126
127 def f():
128 'Adding a docstring made this test fail in Py2.5.0'
129 return None
130
131 self.assertNotInBytecode(f, 'LOAD_GLOBAL')
132 self.assertInBytecode(f, 'LOAD_CONST', None)
133 self.check_lnotab(f)
134
135 def test_while_one(self):
136 # Skip over: LOAD_CONST trueconst POP_JUMP_IF_FALSE xx
137 def f():
138 while 1:
139 pass
140 return list
141 for elem in ('LOAD_CONST', 'POP_JUMP_IF_FALSE'):
142 self.assertNotInBytecode(f, elem)
143 for elem in ('JUMP_BACKWARD',):
144 self.assertInBytecode(f, elem)
145 self.check_lnotab(f)
146
147 def test_pack_unpack(self):
148 for line, elem in (
149 ('a, = a,', 'LOAD_CONST',),
150 ('a, b = a, b', 'SWAP',),
151 ('a, b, c = a, b, c', 'SWAP',),
152 ):
153 with self.subTest(line=line):
154 code = compile(line,'','single')
155 self.assertInBytecode(code, elem)
156 self.assertNotInBytecode(code, 'BUILD_TUPLE')
157 self.assertNotInBytecode(code, 'UNPACK_SEQUENCE')
158 self.check_lnotab(code)
159
160 def test_constant_folding_tuples_of_constants(self):
161 for line, elem in (
162 ('a = 1,2,3', (1, 2, 3)),
163 ('("a","b","c")', ('a', 'b', 'c')),
164 ('a,b,c,d = 1,2,3,4', (1, 2, 3, 4)),
165 ('(None, 1, None)', (None, 1, None)),
166 ('((1, 2), 3, 4)', ((1, 2), 3, 4)),
167 ):
168 with self.subTest(line=line):
169 code = compile(line,'','single')
170 self.assertInBytecode(code, 'LOAD_CONST', elem)
171 self.assertNotInBytecode(code, 'BUILD_TUPLE')
172 self.check_lnotab(code)
173
174 # Long tuples should be folded too.
175 code = compile(repr(tuple(range(10000))),'','single')
176 self.assertNotInBytecode(code, 'BUILD_TUPLE')
177 # One LOAD_CONST for the tuple, one for the None return value
178 load_consts = [instr for instr in dis.get_instructions(code)
179 if instr.opname == 'LOAD_CONST']
180 self.assertEqual(len(load_consts), 2)
181 self.check_lnotab(code)
182
183 # Bug 1053819: Tuple of constants misidentified when presented with:
184 # . . . opcode_with_arg 100 unary_opcode BUILD_TUPLE 1 . . .
185 # The following would segfault upon compilation
186 def crater():
187 (~[
188 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
189 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
190 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
191 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
192 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
193 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
194 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
195 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
196 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
197 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
198 ],)
199 self.check_lnotab(crater)
200
201 def test_constant_folding_lists_of_constants(self):
202 for line, elem in (
203 # in/not in constants with BUILD_LIST should be folded to a tuple:
204 ('a in [1,2,3]', (1, 2, 3)),
205 ('a not in ["a","b","c"]', ('a', 'b', 'c')),
206 ('a in [None, 1, None]', (None, 1, None)),
207 ('a not in [(1, 2), 3, 4]', ((1, 2), 3, 4)),
208 ):
209 with self.subTest(line=line):
210 code = compile(line, '', 'single')
211 self.assertInBytecode(code, 'LOAD_CONST', elem)
212 self.assertNotInBytecode(code, 'BUILD_LIST')
213 self.check_lnotab(code)
214
215 def test_constant_folding_sets_of_constants(self):
216 for line, elem in (
217 # in/not in constants with BUILD_SET should be folded to a frozenset:
218 ('a in {1,2,3}', frozenset({1, 2, 3})),
219 ('a not in {"a","b","c"}', frozenset({'a', 'c', 'b'})),
220 ('a in {None, 1, None}', frozenset({1, None})),
221 ('a not in {(1, 2), 3, 4}', frozenset({(1, 2), 3, 4})),
222 ('a in {1, 2, 3, 3, 2, 1}', frozenset({1, 2, 3})),
223 ):
224 with self.subTest(line=line):
225 code = compile(line, '', 'single')
226 self.assertNotInBytecode(code, 'BUILD_SET')
227 self.assertInBytecode(code, 'LOAD_CONST', elem)
228 self.check_lnotab(code)
229
230 # Ensure that the resulting code actually works:
231 def f(a):
232 return a in {1, 2, 3}
233
234 def g(a):
235 return a not in {1, 2, 3}
236
237 self.assertTrue(f(3))
238 self.assertTrue(not f(4))
239 self.check_lnotab(f)
240
241 self.assertTrue(not g(3))
242 self.assertTrue(g(4))
243 self.check_lnotab(g)
244
245 def test_constant_folding_small_int(self):
246 tests = [
247 ('(0, )[0]', 0),
248 ('(1 + 2, )[0]', 3),
249 ('(2 + 2 * 2, )[0]', 6),
250 ('(1, (1 + 2 + 3, ))[1][0]', 6),
251 ('1 + 2', 3),
252 ('2 + 2 * 2 // 2 - 2', 2),
253 ('(255, )[0]', 255),
254 ('(256, )[0]', None),
255 ('(1000, )[0]', None),
256 ('(1 - 2, )[0]', None),
257 ('255 + 0', 255),
258 ('255 + 1', None),
259 ('-1', None),
260 ('--1', 1),
261 ('--255', 255),
262 ('--256', None),
263 ('~1', None),
264 ('~~1', 1),
265 ('~~255', 255),
266 ('~~256', None),
267 ('++255', 255),
268 ('++256', None),
269 ]
270 for expr, oparg in tests:
271 with self.subTest(expr=expr, oparg=oparg):
272 code = compile(expr, '', 'single')
273 if oparg is not None:
274 self.assertInBytecode(code, 'LOAD_SMALL_INT', oparg)
275 else:
276 self.assertNotInBytecode(code, 'LOAD_SMALL_INT')
277 self.check_lnotab(code)
278
279 def test_constant_folding_unaryop(self):
280 intrinsic_positive = 5
281 tests = [
282 ('-0', 'UNARY_NEGATIVE', None, True, 'LOAD_SMALL_INT', 0),
283 ('-0.0', 'UNARY_NEGATIVE', None, True, 'LOAD_CONST', -0.0),
284 ('-(1.0-1.0)', 'UNARY_NEGATIVE', None, True, 'LOAD_CONST', -0.0),
285 ('-0.5', 'UNARY_NEGATIVE', None, True, 'LOAD_CONST', -0.5),
286 ('---1', 'UNARY_NEGATIVE', None, True, 'LOAD_CONST', -1),
287 ('---""', 'UNARY_NEGATIVE', None, False, None, None),
288 ('~~~1', 'UNARY_INVERT', None, True, 'LOAD_CONST', -2),
289 ('~~~""', 'UNARY_INVERT', None, False, None, None),
290 ('not not True', 'UNARY_NOT', None, True, 'LOAD_CONST', True),
291 ('not not x', 'UNARY_NOT', None, True, 'LOAD_NAME', 'x'), # this should be optimized regardless of constant or not
292 ('+++1', 'CALL_INTRINSIC_1', intrinsic_positive, True, 'LOAD_SMALL_INT', 1),
293 ('---x', 'UNARY_NEGATIVE', None, False, None, None),
294 ('~~~x', 'UNARY_INVERT', None, False, None, None),
295 ('+++x', 'CALL_INTRINSIC_1', intrinsic_positive, False, None, None),
296 ('~True', 'UNARY_INVERT', None, False, None, None),
297 ]
298
299 for (
300 expr,
301 original_opcode,
302 original_argval,
303 is_optimized,
304 optimized_opcode,
305 optimized_argval,
306 ) in tests:
307 with self.subTest(expr=expr, is_optimized=is_optimized):
308 code = compile(expr, "", "single")
309 if is_optimized:
310 self.assertNotInBytecode(code, original_opcode, argval=original_argval)
311 self.assertInBytecode(code, optimized_opcode, argval=optimized_argval)
312 else:
313 self.assertInBytecode(code, original_opcode, argval=original_argval)
314 self.check_lnotab(code)
315
316 # Check that -0.0 works after marshaling
317 def negzero():
318 return -(1.0-1.0)
319
320 for instr in dis.get_instructions(negzero):
321 self.assertNotStartsWith(instr.opname, 'UNARY_')
322 self.check_lnotab(negzero)
323
324 def test_constant_folding_binop(self):
325 tests = [
326 ('1 + 2', 'NB_ADD', True, 'LOAD_SMALL_INT', 3),
327 ('1 + 2 + 3', 'NB_ADD', True, 'LOAD_SMALL_INT', 6),
328 ('1 + ""', 'NB_ADD', False, None, None),
329 ('1 - 2', 'NB_SUBTRACT', True, 'LOAD_CONST', -1),
330 ('1 - 2 - 3', 'NB_SUBTRACT', True, 'LOAD_CONST', -4),
331 ('1 - ""', 'NB_SUBTRACT', False, None, None),
332 ('2 * 2', 'NB_MULTIPLY', True, 'LOAD_SMALL_INT', 4),
333 ('2 * 2 * 2', 'NB_MULTIPLY', True, 'LOAD_SMALL_INT', 8),
334 ('2 / 2', 'NB_TRUE_DIVIDE', True, 'LOAD_CONST', 1.0),
335 ('2 / 2 / 2', 'NB_TRUE_DIVIDE', True, 'LOAD_CONST', 0.5),
336 ('2 / ""', 'NB_TRUE_DIVIDE', False, None, None),
337 ('2 // 2', 'NB_FLOOR_DIVIDE', True, 'LOAD_SMALL_INT', 1),
338 ('2 // 2 // 2', 'NB_FLOOR_DIVIDE', True, 'LOAD_SMALL_INT', 0),
339 ('2 // ""', 'NB_FLOOR_DIVIDE', False, None, None),
340 ('2 % 2', 'NB_REMAINDER', True, 'LOAD_SMALL_INT', 0),
341 ('2 % 2 % 2', 'NB_REMAINDER', True, 'LOAD_SMALL_INT', 0),
342 ('2 % ()', 'NB_REMAINDER', False, None, None),
343 ('2 ** 2', 'NB_POWER', True, 'LOAD_SMALL_INT', 4),
344 ('2 ** 2 ** 2', 'NB_POWER', True, 'LOAD_SMALL_INT', 16),
345 ('2 ** ""', 'NB_POWER', False, None, None),
346 ('2 << 2', 'NB_LSHIFT', True, 'LOAD_SMALL_INT', 8),
347 ('2 << 2 << 2', 'NB_LSHIFT', True, 'LOAD_SMALL_INT', 32),
348 ('2 << ""', 'NB_LSHIFT', False, None, None),
349 ('2 >> 2', 'NB_RSHIFT', True, 'LOAD_SMALL_INT', 0),
350 ('2 >> 2 >> 2', 'NB_RSHIFT', True, 'LOAD_SMALL_INT', 0),
351 ('2 >> ""', 'NB_RSHIFT', False, None, None),
352 ('2 | 2', 'NB_OR', True, 'LOAD_SMALL_INT', 2),
353 ('2 | 2 | 2', 'NB_OR', True, 'LOAD_SMALL_INT', 2),
354 ('2 | ""', 'NB_OR', False, None, None),
355 ('2 & 2', 'NB_AND', True, 'LOAD_SMALL_INT', 2),
356 ('2 & 2 & 2', 'NB_AND', True, 'LOAD_SMALL_INT', 2),
357 ('2 & ""', 'NB_AND', False, None, None),
358 ('2 ^ 2', 'NB_XOR', True, 'LOAD_SMALL_INT', 0),
359 ('2 ^ 2 ^ 2', 'NB_XOR', True, 'LOAD_SMALL_INT', 2),
360 ('2 ^ ""', 'NB_XOR', False, None, None),
361 ('(1, )[0]', 'NB_SUBSCR', True, 'LOAD_SMALL_INT', 1),
362 ('(1, )[-1]', 'NB_SUBSCR', True, 'LOAD_SMALL_INT', 1),
363 ('(1 + 2, )[0]', 'NB_SUBSCR', True, 'LOAD_SMALL_INT', 3),
364 ('(1, (1, 2))[1][1]', 'NB_SUBSCR', True, 'LOAD_SMALL_INT', 2),
365 ('(1, 2)[2-1]', 'NB_SUBSCR', True, 'LOAD_SMALL_INT', 2),
366 ('(1, (1, 2))[1][2-1]', 'NB_SUBSCR', True, 'LOAD_SMALL_INT', 2),
367 ('(1, (1, 2))[1:6][0][2-1]', 'NB_SUBSCR', True, 'LOAD_SMALL_INT', 2),
368 ('"a"[0]', 'NB_SUBSCR', True, 'LOAD_CONST', 'a'),
369 ('("a" + "b")[1]', 'NB_SUBSCR', True, 'LOAD_CONST', 'b'),
370 ('("a" + "b", )[0][1]', 'NB_SUBSCR', True, 'LOAD_CONST', 'b'),
371 ('("a" * 10)[9]', 'NB_SUBSCR', True, 'LOAD_CONST', 'a'),
372 ('(1, )[1]', 'NB_SUBSCR', False, None, None),
373 ('(1, )[-2]', 'NB_SUBSCR', False, None, None),
374 ('"a"[1]', 'NB_SUBSCR', False, None, None),
375 ('"a"[-2]', 'NB_SUBSCR', False, None, None),
376 ('("a" + "b")[2]', 'NB_SUBSCR', False, None, None),
377 ('("a" + "b", )[0][2]', 'NB_SUBSCR', False, None, None),
378 ('("a" + "b", )[1][0]', 'NB_SUBSCR', False, None, None),
379 ('("a" * 10)[10]', 'NB_SUBSCR', False, None, None),
380 ('(1, (1, 2))[2:6][0][2-1]', 'NB_SUBSCR', False, None, None),
381 ]
382
383 for (
384 expr,
385 nb_op,
386 is_optimized,
387 optimized_opcode,
388 optimized_argval
389 ) in tests:
390 with self.subTest(expr=expr, is_optimized=is_optimized):
391 code = compile(expr, '', 'single')
392 nb_op_val = get_binop_argval(nb_op)
393 if is_optimized:
394 self.assertNotInBytecode(code, 'BINARY_OP', argval=nb_op_val)
395 self.assertInBytecode(code, optimized_opcode, argval=optimized_argval)
396 else:
397 self.assertInBytecode(code, 'BINARY_OP', argval=nb_op_val)
398 self.check_lnotab(code)
399
400 # Verify that large sequences do not result from folding
401 code = compile('"x"*10000', '', 'single')
402 self.assertInBytecode(code, 'LOAD_CONST', 10000)
403 self.assertNotIn("x"*10000, code.co_consts)
404 self.check_lnotab(code)
405 code = compile('1<<1000', '', 'single')
406 self.assertInBytecode(code, 'LOAD_CONST', 1000)
407 self.assertNotIn(1<<1000, code.co_consts)
408 self.check_lnotab(code)
409 code = compile('2**1000', '', 'single')
410 self.assertInBytecode(code, 'LOAD_CONST', 1000)
411 self.assertNotIn(2**1000, code.co_consts)
412 self.check_lnotab(code)
413
414 # Test binary subscript on unicode
415 # valid code get optimized
416 code = compile('"foo"[0]', '', 'single')
417 self.assertInBytecode(code, 'LOAD_CONST', 'f')
418 self.assertNotInBytecode(code, 'BINARY_OP')
419 self.check_lnotab(code)
420 code = compile('"\u0061\uffff"[1]', '', 'single')
421 self.assertInBytecode(code, 'LOAD_CONST', '\uffff')
422 self.assertNotInBytecode(code,'BINARY_OP')
423 self.check_lnotab(code)
424
425 # With PEP 393, non-BMP char get optimized
426 code = compile('"\U00012345"[0]', '', 'single')
427 self.assertInBytecode(code, 'LOAD_CONST', '\U00012345')
428 self.assertNotInBytecode(code, 'BINARY_OP')
429 self.check_lnotab(code)
430
431 # invalid code doesn't get optimized
432 # out of range
433 code = compile('"fuu"[10]', '', 'single')
434 self.assertInBytecode(code, 'BINARY_OP')
435 self.check_lnotab(code)
436
437
438 def test_constant_folding_remove_nop_location(self):
439 sources = [
440 """
441 (-
442 -
443 -
444 1)
445 """,
446
447 """
448 (1
449 +
450 2
451 +
452 3)
453 """,
454
455 """
456 (1,
457 2,
458 3)[0]
459 """,
460
461 """
462 [1,
463 2,
464 3]
465 """,
466
467 """
468 {1,
469 2,
470 3}
471 """,
472
473 """
474 1 in [
475 1,
476 2,
477 3
478 ]
479 """,
480
481 """
482 1 in {
483 1,
484 2,
485 3
486 }
487 """,
488
489 """
490 for _ in [1,
491 2,
492 3]:
493 pass
494 """,
495
496 """
497 for _ in [1,
498 2,
499 x]:
500 pass
501 """,
502
503 """
504 for _ in {1,
505 2,
506 3}:
507 pass
508 """
509 ]
510
511 for source in sources:
512 code = compile(textwrap.dedent(source), '', 'single')
513 self.assertNotInBytecode(code, 'NOP')
514
515 def test_elim_extra_return(self):
516 # RETURN LOAD_CONST None RETURN --> RETURN
517 def f(x):
518 return x
519 self.assertNotInBytecode(f, 'LOAD_CONST', None)
520 returns = [instr for instr in dis.get_instructions(f)
521 if instr.opname == 'RETURN_VALUE']
522 self.assertEqual(len(returns), 1)
523 self.check_lnotab(f)
524
525 def test_elim_jump_to_return(self):
526 # JUMP_FORWARD to RETURN --> RETURN
527 def f(cond, true_value, false_value):
528 # Intentionally use two-line expression to test issue37213.
529 return (true_value if cond
530 else false_value)
531 self.check_jump_targets(f)
532 self.assertNotInBytecode(f, 'JUMP_FORWARD')
533 self.assertNotInBytecode(f, 'JUMP_BACKWARD')
534 returns = [instr for instr in dis.get_instructions(f)
535 if instr.opname == 'RETURN_VALUE']
536 self.assertEqual(len(returns), 2)
537 self.check_lnotab(f)
538
539 def test_elim_jump_to_uncond_jump(self):
540 # POP_JUMP_IF_FALSE to JUMP_FORWARD --> POP_JUMP_IF_FALSE to non-jump
541 def f():
542 if a:
543 # Intentionally use two-line expression to test issue37213.
544 if (c
545 or d):
546 foo()
547 else:
548 baz()
549 self.check_jump_targets(f)
550 self.check_lnotab(f)
551
552 def test_elim_jump_to_uncond_jump2(self):
553 # POP_JUMP_IF_FALSE to JUMP_BACKWARD --> POP_JUMP_IF_FALSE to non-jump
554 def f():
555 while a:
556 # Intentionally use two-line expression to test issue37213.
557 if (c
558 or d):
559 a = foo()
560 self.check_jump_targets(f)
561 self.check_lnotab(f)
562
563 def test_elim_jump_to_uncond_jump3(self):
564 # Intentionally use two-line expressions to test issue37213.
565 # POP_JUMP_IF_FALSE to POP_JUMP_IF_FALSE --> POP_JUMP_IF_FALSE to non-jump
566 def f(a, b, c):
567 return ((a and b)
568 and c)
569 self.check_jump_targets(f)
570 self.check_lnotab(f)
571 self.assertEqual(count_instr_recursively(f, 'POP_JUMP_IF_FALSE'), 2)
572 # POP_JUMP_IF_TRUE to POP_JUMP_IF_TRUE --> POP_JUMP_IF_TRUE to non-jump
573 def f(a, b, c):
574 return ((a or b)
575 or c)
576 self.check_jump_targets(f)
577 self.check_lnotab(f)
578 self.assertEqual(count_instr_recursively(f, 'POP_JUMP_IF_TRUE'), 2)
579 # JUMP_IF_FALSE_OR_POP to JUMP_IF_TRUE_OR_POP --> POP_JUMP_IF_FALSE to non-jump
580 def f(a, b, c):
581 return ((a and b)
582 or c)
583 self.check_jump_targets(f)
584 self.check_lnotab(f)
585 self.assertEqual(count_instr_recursively(f, 'POP_JUMP_IF_FALSE'), 1)
586 self.assertEqual(count_instr_recursively(f, 'POP_JUMP_IF_TRUE'), 1)
587 # POP_JUMP_IF_TRUE to POP_JUMP_IF_FALSE --> POP_JUMP_IF_TRUE to non-jump
588 def f(a, b, c):
589 return ((a or b)
590 and c)
591 self.check_jump_targets(f)
592 self.check_lnotab(f)
593 self.assertEqual(count_instr_recursively(f, 'POP_JUMP_IF_FALSE'), 1)
594 self.assertEqual(count_instr_recursively(f, 'POP_JUMP_IF_TRUE'), 1)
595
596 def test_elim_jump_to_uncond_jump4(self):
597 def f():
598 for i in range(5):
599 if i > 3:
600 print(i)
601 self.check_jump_targets(f)
602
603 def test_elim_jump_after_return1(self):
604 # Eliminate dead code: jumps immediately after returns can't be reached
605 def f(cond1, cond2):
606 if cond1: return 1
607 if cond2: return 2
608 while 1:
609 return 3
610 while 1:
611 if cond1: return 4
612 return 5
613 return 6
614 self.assertNotInBytecode(f, 'JUMP_FORWARD')
615 self.assertNotInBytecode(f, 'JUMP_BACKWARD')
616 returns = [instr for instr in dis.get_instructions(f)
617 if instr.opname == 'RETURN_VALUE']
618 self.assertLessEqual(len(returns), 6)
619 self.check_lnotab(f)
620
621 def test_make_function_doesnt_bail(self):
622 def f():
623 def g()->1+1:
624 pass
625 return g
626 self.assertNotInBytecode(f, 'BINARY_OP')
627 self.check_lnotab(f)
628
629 def test_in_literal_list(self):
630 def containtest():
631 return x in [a, b]
632 self.assertEqual(count_instr_recursively(containtest, 'BUILD_LIST'), 0)
633 self.check_lnotab(containtest)
634
635 def test_iterate_literal_list(self):
636 def forloop():
637 for x in [a, b]:
638 pass
639 self.assertEqual(count_instr_recursively(forloop, 'BUILD_LIST'), 0)
640 self.check_lnotab(forloop)
641
642 def test_condition_with_binop_with_bools(self):
643 def f():
644 if True or False:
645 return 1
646 return 0
647 self.assertEqual(f(), 1)
648 self.check_lnotab(f)
649
650 def test_if_with_if_expression(self):
651 # Check bpo-37289
652 def f(x):
653 if (True if x else False):
654 return True
655 return False
656 self.assertTrue(f(True))
657 self.check_lnotab(f)
658
659 def test_trailing_nops(self):
660 # Check the lnotab of a function that even after trivial
661 # optimization has trailing nops, which the lnotab adjustment has to
662 # handle properly (bpo-38115).
663 def f(x):
664 while 1:
665 return 3
666 while 1:
667 return 5
668 return 6
669 self.check_lnotab(f)
670
671 def test_assignment_idiom_in_comprehensions(self):
672 def listcomp():
673 return [y for x in a for y in [f(x)]]
674 self.assertEqual(count_instr_recursively(listcomp, 'FOR_ITER'), 1)
675 def setcomp():
676 return {y for x in a for y in [f(x)]}
677 self.assertEqual(count_instr_recursively(setcomp, 'FOR_ITER'), 1)
678 def dictcomp():
679 return {y: y for x in a for y in [f(x)]}
680 self.assertEqual(count_instr_recursively(dictcomp, 'FOR_ITER'), 1)
681 def genexpr():
682 return (y for x in a for y in [f(x)])
683 self.assertEqual(count_instr_recursively(genexpr, 'FOR_ITER'), 1)
684
685 @support.requires_resource('cpu')
686 def test_format_combinations(self):
687 flags = '-+ #0'
688 testcases = [
689 *product(('', '1234', 'абвг'), 'sra'),
690 *product((1234, -1234), 'duioxX'),
691 *product((1234.5678901, -1234.5678901), 'duifegFEG'),
692 *product((float('inf'), -float('inf')), 'fegFEG'),
693 ]
694 width_precs = [
695 *product(('', '1', '30'), ('', '.', '.0', '.2')),
696 ('', '.40'),
697 ('30', '.40'),
698 ]
699 for value, suffix in testcases:
700 for width, prec in width_precs:
701 for r in range(len(flags) + 1):
702 for spec in combinations(flags, r):
703 fmt = '%' + ''.join(spec) + width + prec + suffix
704 with self.subTest(fmt=fmt, value=value):
705 s1 = fmt % value
706 s2 = eval(f'{fmt!r} % (x,)', {'x': value})
707 self.assertEqual(s2, s1, f'{fmt = }')
708
709 def test_format_misc(self):
710 def format(fmt, *values):
711 vars = [f'x{i+1}' for i in range(len(values))]
712 if len(vars) == 1:
713 args = '(' + vars[0] + ',)'
714 else:
715 args = '(' + ', '.join(vars) + ')'
716 return eval(f'{fmt!r} % {args}', dict(zip(vars, values)))
717
718 self.assertEqual(format('string'), 'string')
719 self.assertEqual(format('x = %s!', 1234), 'x = 1234!')
720 self.assertEqual(format('x = %d!', 1234), 'x = 1234!')
721 self.assertEqual(format('x = %x!', 1234), 'x = 4d2!')
722 self.assertEqual(format('x = %f!', 1234), 'x = 1234.000000!')
723 self.assertEqual(format('x = %s!', 1234.0000625), 'x = 1234.0000625!')
724 self.assertEqual(format('x = %f!', 1234.0000625), 'x = 1234.000063!')
725 self.assertEqual(format('x = %d!', 1234.0000625), 'x = 1234!')
726 self.assertEqual(format('x = %s%% %%%%', 1234), 'x = 1234% %%')
727 self.assertEqual(format('x = %s!', '%% %s'), 'x = %% %s!')
728 self.assertEqual(format('x = %s, y = %d', 12, 34), 'x = 12, y = 34')
729
730 def test_format_errors(self):
731 with self.assertRaisesRegex(TypeError,
732 'not enough arguments for format string'):
733 eval("'%s' % ()")
734 with self.assertRaisesRegex(TypeError,
735 'not all arguments converted during string formatting'):
736 eval("'%s' % (x, y)", {'x': 1, 'y': 2})
737 with self.assertRaisesRegex(ValueError, 'incomplete format'):
738 eval("'%s%' % (x,)", {'x': 1234})
739 with self.assertRaisesRegex(ValueError, 'incomplete format'):
740 eval("'%s%%%' % (x,)", {'x': 1234})
741 with self.assertRaisesRegex(TypeError,
742 'not enough arguments for format string'):
743 eval("'%s%z' % (x,)", {'x': 1234})
744 with self.assertRaisesRegex(ValueError, 'unsupported format character'):
745 eval("'%s%z' % (x, 5)", {'x': 1234})
746 with self.assertRaisesRegex(TypeError, 'a real number is required, not str'):
747 eval("'%d' % (x,)", {'x': '1234'})
748 with self.assertRaisesRegex(TypeError, 'an integer is required, not float'):
749 eval("'%x' % (x,)", {'x': 1234.56})
750 with self.assertRaisesRegex(TypeError, 'an integer is required, not str'):
751 eval("'%x' % (x,)", {'x': '1234'})
752 with self.assertRaisesRegex(TypeError, 'must be real number, not str'):
753 eval("'%f' % (x,)", {'x': '1234'})
754 with self.assertRaisesRegex(TypeError,
755 'not enough arguments for format string'):
756 eval("'%s, %s' % (x, *y)", {'x': 1, 'y': []})
757 with self.assertRaisesRegex(TypeError,
758 'not all arguments converted during string formatting'):
759 eval("'%s, %s' % (x, *y)", {'x': 1, 'y': [2, 3]})
760
761 def test_static_swaps_unpack_two(self):
762 def f(a, b):
763 a, b = a, b
764 b, a = a, b
765 self.assertNotInBytecode(f, "SWAP")
766
767 def test_static_swaps_unpack_three(self):
768 def f(a, b, c):
769 a, b, c = a, b, c
770 a, c, b = a, b, c
771 b, a, c = a, b, c
772 b, c, a = a, b, c
773 c, a, b = a, b, c
774 c, b, a = a, b, c
775 self.assertNotInBytecode(f, "SWAP")
776
777 def test_static_swaps_match_mapping(self):
778 for a, b, c in product("_a", "_b", "_c"):
779 pattern = f"{{'a': {a}, 'b': {b}, 'c': {c}}}"
780 with self.subTest(pattern):
781 code = compile_pattern_with_fast_locals(pattern)
782 self.assertNotInBytecode(code, "SWAP")
783
784 def test_static_swaps_match_class(self):
785 forms = [
786 "C({}, {}, {})",
787 "C({}, {}, c={})",
788 "C({}, b={}, c={})",
789 "C(a={}, b={}, c={})"
790 ]
791 for a, b, c in product("_a", "_b", "_c"):
792 for form in forms:
793 pattern = form.format(a, b, c)
794 with self.subTest(pattern):
795 code = compile_pattern_with_fast_locals(pattern)
796 self.assertNotInBytecode(code, "SWAP")
797
798 def test_static_swaps_match_sequence(self):
799 swaps = {"*_, b, c", "a, *_, c", "a, b, *_"}
800 forms = ["{}, {}, {}", "{}, {}, *{}", "{}, *{}, {}", "*{}, {}, {}"]
801 for a, b, c in product("_a", "_b", "_c"):
802 for form in forms:
803 pattern = form.format(a, b, c)
804 with self.subTest(pattern):
805 code = compile_pattern_with_fast_locals(pattern)
806 if pattern in swaps:
807 # If this fails... great! Remove this pattern from swaps
808 # to prevent regressing on any improvement:
809 self.assertInBytecode(code, "SWAP")
810 else:
811 self.assertNotInBytecode(code, "SWAP")
812
813
814 class TestBuglets(unittest.TestCase):
815
816 def test_bug_11510(self):
817 # folded constant set optimization was commingled with the tuple
818 # unpacking optimization which would fail if the set had duplicate
819 # elements so that the set length was unexpected
820 def f():
821 x, y = {1, 1}
822 return x, y
823 with self.assertRaises(ValueError):
824 f()
825
826 def test_bpo_42057(self):
827 for i in range(10):
828 try:
829 raise Exception
830 except Exception or Exception:
831 pass
832
833 def test_bpo_45773_pop_jump_if_true(self):
834 compile("while True or spam: pass", "<test>", "exec")
835
836 def test_bpo_45773_pop_jump_if_false(self):
837 compile("while True or not spam: pass", "<test>", "exec")
838
839
840 class TestMarkingVariablesAsUnKnown(BytecodeTestCase):
841
842 def setUp(self):
843 self.addCleanup(sys.settrace, sys.gettrace())
844 sys.settrace(None)
845
846 def test_load_fast_known_simple(self):
847 def f():
848 x = 1
849 y = x + x
850 self.assertInBytecode(f, 'LOAD_FAST_BORROW_LOAD_FAST_BORROW')
851
852 def test_load_fast_unknown_simple(self):
853 def f():
854 if condition():
855 x = 1
856 print(x)
857 self.assertInBytecode(f, 'LOAD_FAST_CHECK')
858 self.assertNotInBytecode(f, 'LOAD_FAST')
859
860 def test_load_fast_unknown_because_del(self):
861 def f():
862 x = 1
863 del x
864 print(x)
865 self.assertInBytecode(f, 'LOAD_FAST_CHECK')
866 self.assertNotInBytecode(f, 'LOAD_FAST')
867
868 def test_load_fast_known_because_parameter(self):
869 def f1(x):
870 print(x)
871 self.assertInBytecode(f1, 'LOAD_FAST_BORROW')
872 self.assertNotInBytecode(f1, 'LOAD_FAST_CHECK')
873
874 def f2(*, x):
875 print(x)
876 self.assertInBytecode(f2, 'LOAD_FAST_BORROW')
877 self.assertNotInBytecode(f2, 'LOAD_FAST_CHECK')
878
879 def f3(*args):
880 print(args)
881 self.assertInBytecode(f3, 'LOAD_FAST_BORROW')
882 self.assertNotInBytecode(f3, 'LOAD_FAST_CHECK')
883
884 def f4(**kwargs):
885 print(kwargs)
886 self.assertInBytecode(f4, 'LOAD_FAST_BORROW')
887 self.assertNotInBytecode(f4, 'LOAD_FAST_CHECK')
888
889 def f5(x=0):
890 print(x)
891 self.assertInBytecode(f5, 'LOAD_FAST_BORROW')
892 self.assertNotInBytecode(f5, 'LOAD_FAST_CHECK')
893
894 def test_load_fast_known_because_already_loaded(self):
895 def f():
896 if condition():
897 x = 1
898 print(x)
899 print(x)
900 self.assertInBytecode(f, 'LOAD_FAST_CHECK')
901 self.assertInBytecode(f, 'LOAD_FAST_BORROW')
902
903 def test_load_fast_known_multiple_branches(self):
904 def f():
905 if condition():
906 x = 1
907 else:
908 x = 2
909 print(x)
910 self.assertInBytecode(f, 'LOAD_FAST_BORROW')
911 self.assertNotInBytecode(f, 'LOAD_FAST_CHECK')
912
913 def test_load_fast_unknown_after_error(self):
914 def f():
915 try:
916 res = 1 / 0
917 except ZeroDivisionError:
918 pass
919 return res
920 # LOAD_FAST (known) still occurs in the no-exception branch.
921 # Assert that it doesn't occur in the LOAD_FAST_CHECK branch.
922 self.assertInBytecode(f, 'LOAD_FAST_CHECK')
923
924 def test_load_fast_unknown_after_error_2(self):
925 def f():
926 try:
927 1 / 0
928 except ZeroDivisionError:
929 print(a, b, c, d, e, f, g)
930 a = b = c = d = e = f = g = 1
931 self.assertInBytecode(f, 'LOAD_FAST_CHECK')
932 self.assertNotInBytecode(f, 'LOAD_FAST')
933
934 def test_load_fast_too_many_locals(self):
935 # When there get to be too many locals to analyze completely,
936 # later locals are all converted to LOAD_FAST_CHECK, except
937 # when a store or prior load occurred in the same basicblock.
938 def f():
939 a00 = a01 = a02 = a03 = a04 = a05 = a06 = a07 = a08 = a09 = 1
940 a10 = a11 = a12 = a13 = a14 = a15 = a16 = a17 = a18 = a19 = 1
941 a20 = a21 = a22 = a23 = a24 = a25 = a26 = a27 = a28 = a29 = 1
942 a30 = a31 = a32 = a33 = a34 = a35 = a36 = a37 = a38 = a39 = 1
943 a40 = a41 = a42 = a43 = a44 = a45 = a46 = a47 = a48 = a49 = 1
944 a50 = a51 = a52 = a53 = a54 = a55 = a56 = a57 = a58 = a59 = 1
945 a60 = a61 = a62 = a63 = a64 = a65 = a66 = a67 = a68 = a69 = 1
946 a70 = a71 = a72 = a73 = a74 = a75 = a76 = a77 = a78 = a79 = 1
947 del a72, a73
948 print(a73)
949 print(a70, a71, a72, a73)
950 while True:
951 print(a00, a01, a62, a63)
952 print(a64, a65, a78, a79)
953
954 self.assertInBytecode(f, 'LOAD_FAST_BORROW_LOAD_FAST_BORROW', ("a00", "a01"))
955 self.assertNotInBytecode(f, 'LOAD_FAST_CHECK', "a00")
956 self.assertNotInBytecode(f, 'LOAD_FAST_CHECK', "a01")
957 for i in 62, 63:
958 # First 64 locals: analyze completely
959 self.assertInBytecode(f, 'LOAD_FAST_BORROW', f"a{i:02}")
960 self.assertNotInBytecode(f, 'LOAD_FAST_CHECK', f"a{i:02}")
961 for i in 64, 65, 78, 79:
962 # Locals >=64 not in the same basicblock
963 self.assertInBytecode(f, 'LOAD_FAST_CHECK', f"a{i:02}")
964 self.assertNotInBytecode(f, 'LOAD_FAST', f"a{i:02}")
965 for i in 70, 71:
966 # Locals >=64 in the same basicblock
967 self.assertInBytecode(f, 'LOAD_FAST_BORROW', f"a{i:02}")
968 self.assertNotInBytecode(f, 'LOAD_FAST_CHECK', f"a{i:02}")
969 # del statements should invalidate within basicblocks.
970 self.assertInBytecode(f, 'LOAD_FAST_CHECK', "a72")
971 self.assertNotInBytecode(f, 'LOAD_FAST', "a72")
972 # previous checked loads within a basicblock enable unchecked loads
973 self.assertInBytecode(f, 'LOAD_FAST_CHECK', "a73")
974 self.assertInBytecode(f, 'LOAD_FAST_BORROW', "a73")
975
976 def test_setting_lineno_no_undefined(self):
977 code = textwrap.dedent("""\
978 def f():
979 x = y = 2
980 if not x:
981 return 4
982 for i in range(55):
983 x + 6
984 L = 7
985 L = 8
986 L = 9
987 L = 10
988 """)
989 ns = {}
990 exec(code, ns)
991 f = ns['f']
992 self.assertInBytecode(f, "LOAD_FAST_BORROW")
993 self.assertNotInBytecode(f, "LOAD_FAST_CHECK")
994 co_code = f.__code__.co_code
995 def trace(frame, event, arg):
996 if event == 'line' and frame.f_lineno == 9:
997 frame.f_lineno = 3
998 sys.settrace(None)
999 return None
1000 return trace
1001 sys.settrace(trace)
1002 result = f()
1003 self.assertIsNone(result)
1004 self.assertInBytecode(f, "LOAD_FAST_BORROW")
1005 self.assertNotInBytecode(f, "LOAD_FAST_CHECK")
1006 self.assertEqual(f.__code__.co_code, co_code)
1007
1008 def test_setting_lineno_one_undefined(self):
1009 code = textwrap.dedent("""\
1010 def f():
1011 x = y = 2
1012 if not x:
1013 return 4
1014 for i in range(55):
1015 x + 6
1016 del x
1017 L = 8
1018 L = 9
1019 L = 10
1020 """)
1021 ns = {}
1022 exec(code, ns)
1023 f = ns['f']
1024 self.assertInBytecode(f, "LOAD_FAST_BORROW")
1025 self.assertNotInBytecode(f, "LOAD_FAST_CHECK")
1026 co_code = f.__code__.co_code
1027 def trace(frame, event, arg):
1028 if event == 'line' and frame.f_lineno == 9:
1029 frame.f_lineno = 3
1030 sys.settrace(None)
1031 return None
1032 return trace
1033 e = r"assigning None to 1 unbound local"
1034 with self.assertWarnsRegex(RuntimeWarning, e):
1035 sys.settrace(trace)
1036 result = f()
1037 self.assertEqual(result, 4)
1038 self.assertInBytecode(f, "LOAD_FAST_BORROW")
1039 self.assertNotInBytecode(f, "LOAD_FAST_CHECK")
1040 self.assertEqual(f.__code__.co_code, co_code)
1041
1042 def test_setting_lineno_two_undefined(self):
1043 code = textwrap.dedent("""\
1044 def f():
1045 x = y = 2
1046 if not x:
1047 return 4
1048 for i in range(55):
1049 x + 6
1050 del x, y
1051 L = 8
1052 L = 9
1053 L = 10
1054 """)
1055 ns = {}
1056 exec(code, ns)
1057 f = ns['f']
1058 self.assertInBytecode(f, "LOAD_FAST_BORROW")
1059 self.assertNotInBytecode(f, "LOAD_FAST_CHECK")
1060 co_code = f.__code__.co_code
1061 def trace(frame, event, arg):
1062 if event == 'line' and frame.f_lineno == 9:
1063 frame.f_lineno = 3
1064 sys.settrace(None)
1065 return None
1066 return trace
1067 e = r"assigning None to 2 unbound locals"
1068 with self.assertWarnsRegex(RuntimeWarning, e):
1069 sys.settrace(trace)
1070 result = f()
1071 self.assertEqual(result, 4)
1072 self.assertInBytecode(f, "LOAD_FAST_BORROW")
1073 self.assertNotInBytecode(f, "LOAD_FAST_CHECK")
1074 self.assertEqual(f.__code__.co_code, co_code)
1075
1076 def make_function_with_no_checks(self):
1077 code = textwrap.dedent("""\
1078 def f():
1079 x = 2
1080 L = 3
1081 L = 4
1082 L = 5
1083 if not L:
1084 x + 7
1085 y = 2
1086 """)
1087 ns = {}
1088 exec(code, ns)
1089 f = ns['f']
1090 self.assertInBytecode(f, "LOAD_FAST_BORROW")
1091 self.assertNotInBytecode(f, "LOAD_FAST_CHECK")
1092 return f
1093
1094 def test_modifying_local_does_not_add_check(self):
1095 f = self.make_function_with_no_checks()
1096 def trace(frame, event, arg):
1097 if event == 'line' and frame.f_lineno == 4:
1098 frame.f_locals["x"] = 42
1099 sys.settrace(None)
1100 return None
1101 return trace
1102 sys.settrace(trace)
1103 f()
1104 self.assertInBytecode(f, "LOAD_FAST_BORROW")
1105 self.assertNotInBytecode(f, "LOAD_FAST_CHECK")
1106
1107 def test_initializing_local_does_not_add_check(self):
1108 f = self.make_function_with_no_checks()
1109 def trace(frame, event, arg):
1110 if event == 'line' and frame.f_lineno == 4:
1111 frame.f_locals["y"] = 42
1112 sys.settrace(None)
1113 return None
1114 return trace
1115 sys.settrace(trace)
1116 f()
1117 self.assertInBytecode(f, "LOAD_FAST_BORROW")
1118 self.assertNotInBytecode(f, "LOAD_FAST_CHECK")
1119
1120
1121 class DirectCfgOptimizerTests(CfgOptimizationTestCase):
1122
1123 def test_optimize_cfg_const_index_out_of_range(self):
1124 insts = [
1125 ('LOAD_CONST', 2, 0),
1126 ('RETURN_VALUE', None, 0),
1127 ]
1128 seq = self.seq_from_insts(insts)
1129 with self.assertRaisesRegex(ValueError, "out of range"):
1130 _testinternalcapi.optimize_cfg(seq, [0, 1], 0)
1131
1132 def test_optimize_cfg_consts_must_be_list(self):
1133 insts = [
1134 ('LOAD_CONST', 0, 0),
1135 ('RETURN_VALUE', None, 0),
1136 ]
1137 seq = self.seq_from_insts(insts)
1138 with self.assertRaisesRegex(TypeError, "consts must be a list"):
1139 _testinternalcapi.optimize_cfg(seq, (0,), 0)
1140
1141 def test_compiler_codegen_metadata_consts_roundtrips_optimize_cfg(self):
1142 tree = ast.parse("x = (1, 2)", mode="exec", optimize=1)
1143 insts, meta = _testinternalcapi.compiler_codegen(tree, "<s>", 0)
1144 consts = meta["consts"]
1145 self.assertIsInstance(consts, list)
1146 _testinternalcapi.optimize_cfg(insts, consts, 0)
1147
1148 def test_compiler_codegen_consts_include_none_required_for_implicit_return(self):
1149 # Module "pass" only needs the const table entry for None once
1150 # _PyCodegen_AddReturnAtEnd runs. If metadata["consts"] were taken
1151 # before that, the list would not match LOAD_CONST opargs (here: 0
1152 # for None), and optimize_cfg would read out of range.
1153 tree = ast.parse("pass", mode="exec", optimize=1)
1154 insts, meta = _testinternalcapi.compiler_codegen(tree, "<s>", 0)
1155 consts = meta["consts"]
1156 self.assertEqual(consts, [None])
1157
1158 load_const = opcode.opmap["LOAD_CONST"]
1159 self.assertEqual(
1160 [t[1] for t in insts.get_instructions() if t[0] == load_const],
1161 [0],
1162 )
1163
1164 # As if consts were snapshotted before AddReturnAtEnd: still LOAD_CONST 0, no row.
1165 with self.assertRaisesRegex(ValueError, "out of range"):
1166 _testinternalcapi.optimize_cfg(insts, [], 0)
1167
1168 _testinternalcapi.optimize_cfg(insts, list(consts), 0)
1169
1170 def cfg_optimization_test(self, insts, expected_insts,
1171 consts=None, expected_consts=None,
1172 nlocals=0):
1173
1174 self.check_instructions(insts)
1175 self.check_instructions(expected_insts)
1176
1177 if expected_consts is None:
1178 expected_consts = consts
1179 seq = self.seq_from_insts(insts)
1180 opt_insts, opt_consts = self.get_optimized(seq, consts, nlocals)
1181 expected_insts = self.seq_from_insts(expected_insts).get_instructions()
1182 self.assertInstructionsMatch(opt_insts, expected_insts)
1183 self.assertEqual(opt_consts, expected_consts)
1184
1185 def test_conditional_jump_forward_non_const_condition(self):
1186 insts = [
1187 ('LOAD_NAME', 1, 11),
1188 ('POP_JUMP_IF_TRUE', lbl := self.Label(), 12),
1189 ('LOAD_CONST', 2, 13),
1190 ('RETURN_VALUE', None, 13),
1191 lbl,
1192 ('LOAD_CONST', 3, 14),
1193 ('RETURN_VALUE', None, 14),
1194 ]
1195 expected_insts = [
1196 ('LOAD_NAME', 1, 11),
1197 ('POP_JUMP_IF_TRUE', lbl := self.Label(), 12),
1198 ('LOAD_SMALL_INT', 2, 13),
1199 ('RETURN_VALUE', None, 13),
1200 lbl,
1201 ('LOAD_SMALL_INT', 3, 14),
1202 ('RETURN_VALUE', None, 14),
1203 ]
1204 self.cfg_optimization_test(insts,
1205 expected_insts,
1206 consts=[0, 1, 2, 3, 4],
1207 expected_consts=[0])
1208
1209 def test_list_exceeding_stack_use_guideline(self):
1210 def f():
1211 return [
1212 0, 1, 2, 3, 4,
1213 5, 6, 7, 8, 9,
1214 10, 11, 12, 13, 14,
1215 15, 16, 17, 18, 19,
1216 20, 21, 22, 23, 24,
1217 25, 26, 27, 28, 29,
1218 30, 31, 32, 33, 34,
1219 35, 36, 37, 38, 39
1220 ]
1221 self.assertEqual(f(), list(range(40)))
1222
1223 def test_set_exceeding_stack_use_guideline(self):
1224 def f():
1225 return {
1226 0, 1, 2, 3, 4,
1227 5, 6, 7, 8, 9,
1228 10, 11, 12, 13, 14,
1229 15, 16, 17, 18, 19,
1230 20, 21, 22, 23, 24,
1231 25, 26, 27, 28, 29,
1232 30, 31, 32, 33, 34,
1233 35, 36, 37, 38, 39
1234 }
1235 self.assertEqual(f(), frozenset(range(40)))
1236
1237 def test_nested_const_foldings(self):
1238 # (1, (--2 + ++2 * 2 // 2 - 2, )[0], ~~3, not not True) ==> (1, 2, 3, True)
1239 intrinsic_positive = 5
1240 before = [
1241 ('LOAD_SMALL_INT', 1, 0),
1242 ('NOP', None, 0),
1243 ('LOAD_SMALL_INT', 2, 0),
1244 ('UNARY_NEGATIVE', None, 0),
1245 ('NOP', None, 0),
1246 ('UNARY_NEGATIVE', None, 0),
1247 ('NOP', None, 0),
1248 ('NOP', None, 0),
1249 ('LOAD_SMALL_INT', 2, 0),
1250 ('CALL_INTRINSIC_1', intrinsic_positive, 0),
1251 ('NOP', None, 0),
1252 ('CALL_INTRINSIC_1', intrinsic_positive, 0),
1253 ('BINARY_OP', get_binop_argval('NB_MULTIPLY')),
1254 ('LOAD_SMALL_INT', 2, 0),
1255 ('NOP', None, 0),
1256 ('BINARY_OP', get_binop_argval('NB_FLOOR_DIVIDE')),
1257 ('NOP', None, 0),
1258 ('LOAD_SMALL_INT', 2, 0),
1259 ('BINARY_OP', get_binop_argval('NB_ADD')),
1260 ('NOP', None, 0),
1261 ('LOAD_SMALL_INT', 2, 0),
1262 ('NOP', None, 0),
1263 ('BINARY_OP', get_binop_argval('NB_SUBTRACT')),
1264 ('NOP', None, 0),
1265 ('BUILD_TUPLE', 1, 0),
1266 ('LOAD_SMALL_INT', 0, 0),
1267 ('BINARY_OP', get_binop_argval('NB_SUBSCR'), 0),
1268 ('NOP', None, 0),
1269 ('LOAD_SMALL_INT', 3, 0),
1270 ('NOP', None, 0),
1271 ('UNARY_INVERT', None, 0),
1272 ('NOP', None, 0),
1273 ('UNARY_INVERT', None, 0),
1274 ('NOP', None, 0),
1275 ('LOAD_SMALL_INT', 3, 0),
1276 ('NOP', None, 0),
1277 ('UNARY_NOT', None, 0),
1278 ('NOP', None, 0),
1279 ('UNARY_NOT', None, 0),
1280 ('NOP', None, 0),
1281 ('BUILD_TUPLE', 4, 0),
1282 ('NOP', None, 0),
1283 ('RETURN_VALUE', None, 0)
1284 ]
1285 after = [
1286 ('LOAD_CONST', 1, 0),
1287 ('RETURN_VALUE', None, 0)
1288 ]
1289 self.cfg_optimization_test(before, after, consts=[], expected_consts=[-2, (1, 2, 3, True)])
1290
1291 def test_build_empty_tuple(self):
1292 before = [
1293 ('BUILD_TUPLE', 0, 0),
1294 ('RETURN_VALUE', None, 0),
1295 ]
1296 after = [
1297 ('LOAD_CONST', 0, 0),
1298 ('RETURN_VALUE', None, 0),
1299 ]
1300 self.cfg_optimization_test(before, after, consts=[], expected_consts=[()])
1301
1302 def test_fold_tuple_of_constants(self):
1303 before = [
1304 ('NOP', None, 0),
1305 ('LOAD_SMALL_INT', 1, 0),
1306 ('NOP', None, 0),
1307 ('LOAD_SMALL_INT', 2, 0),
1308 ('NOP', None, 0),
1309 ('NOP', None, 0),
1310 ('LOAD_SMALL_INT', 3, 0),
1311 ('NOP', None, 0),
1312 ('BUILD_TUPLE', 3, 0),
1313 ('RETURN_VALUE', None, 0),
1314 ]
1315 after = [
1316 ('LOAD_CONST', 0, 0),
1317 ('RETURN_VALUE', None, 0),
1318 ]
1319 self.cfg_optimization_test(before, after, consts=[], expected_consts=[(1, 2, 3)])
1320
1321 # not all consts
1322 same = [
1323 ('LOAD_SMALL_INT', 1, 0),
1324 ('LOAD_NAME', 0, 0),
1325 ('LOAD_SMALL_INT', 2, 0),
1326 ('BUILD_TUPLE', 3, 0),
1327 ('RETURN_VALUE', None, 0)
1328 ]
1329 self.cfg_optimization_test(same, same, consts=[])
1330
1331 def test_fold_constant_intrinsic_list_to_tuple(self):
1332 INTRINSIC_LIST_TO_TUPLE = 6
1333
1334 # long tuple
1335 consts = 1000
1336 before = (
1337 [('BUILD_LIST', 0, 0)] +
1338 [('LOAD_CONST', 0, 0), ('LIST_APPEND', 1, 0)] * consts +
1339 [('CALL_INTRINSIC_1', INTRINSIC_LIST_TO_TUPLE, 0), ('RETURN_VALUE', None, 0)]
1340 )
1341 after = [
1342 ('LOAD_CONST', 1, 0),
1343 ('RETURN_VALUE', None, 0)
1344 ]
1345 result_const = tuple(["test"] * consts)
1346 self.cfg_optimization_test(before, after, consts=["test"], expected_consts=["test", result_const])
1347
1348 # empty list
1349 before = [
1350 ('BUILD_LIST', 0, 0),
1351 ('CALL_INTRINSIC_1', INTRINSIC_LIST_TO_TUPLE, 0),
1352 ('RETURN_VALUE', None, 0)
1353 ]
1354 after = [
1355 ('LOAD_CONST', 0, 0),
1356 ('RETURN_VALUE', None, 0)
1357 ]
1358 self.cfg_optimization_test(before, after, consts=[], expected_consts=[()])
1359
1360 # multiple BUILD_LIST 0: ([], 1, [], 2)
1361 same = [
1362 ('BUILD_LIST', 0, 0),
1363 ('BUILD_LIST', 0, 0),
1364 ('LIST_APPEND', 1, 0),
1365 ('LOAD_SMALL_INT', 1, 0),
1366 ('LIST_APPEND', 1, 0),
1367 ('BUILD_LIST', 0, 0),
1368 ('LIST_APPEND', 1, 0),
1369 ('LOAD_SMALL_INT', 2, 0),
1370 ('LIST_APPEND', 1, 0),
1371 ('CALL_INTRINSIC_1', INTRINSIC_LIST_TO_TUPLE, 0),
1372 ('RETURN_VALUE', None, 0)
1373 ]
1374 self.cfg_optimization_test(same, same, consts=[])
1375
1376 # nested folding: (1, 1+1, 3)
1377 before = [
1378 ('BUILD_LIST', 0, 0),
1379 ('LOAD_SMALL_INT', 1, 0),
1380 ('LIST_APPEND', 1, 0),
1381 ('LOAD_SMALL_INT', 1, 0),
1382 ('LOAD_SMALL_INT', 1, 0),
1383 ('BINARY_OP', get_binop_argval('NB_ADD'), 0),
1384 ('LIST_APPEND', 1, 0),
1385 ('LOAD_SMALL_INT', 3, 0),
1386 ('LIST_APPEND', 1, 0),
1387 ('CALL_INTRINSIC_1', INTRINSIC_LIST_TO_TUPLE, 0),
1388 ('RETURN_VALUE', None, 0)
1389 ]
1390 after = [
1391 ('LOAD_CONST', 0, 0),
1392 ('RETURN_VALUE', None, 0)
1393 ]
1394 self.cfg_optimization_test(before, after, consts=[], expected_consts=[(1, 2, 3)])
1395
1396 # NOP's in between: (1, 2, 3)
1397 before = [
1398 ('BUILD_LIST', 0, 0),
1399 ('NOP', None, 0),
1400 ('LOAD_SMALL_INT', 1, 0),
1401 ('NOP', None, 0),
1402 ('NOP', None, 0),
1403 ('LIST_APPEND', 1, 0),
1404 ('NOP', None, 0),
1405 ('LOAD_SMALL_INT', 2, 0),
1406 ('NOP', None, 0),
1407 ('NOP', None, 0),
1408 ('LIST_APPEND', 1, 0),
1409 ('NOP', None, 0),
1410 ('LOAD_SMALL_INT', 3, 0),
1411 ('NOP', None, 0),
1412 ('LIST_APPEND', 1, 0),
1413 ('NOP', None, 0),
1414 ('CALL_INTRINSIC_1', INTRINSIC_LIST_TO_TUPLE, 0),
1415 ('RETURN_VALUE', None, 0)
1416 ]
1417 after = [
1418 ('LOAD_CONST', 0, 0),
1419 ('RETURN_VALUE', None, 0)
1420 ]
1421 self.cfg_optimization_test(before, after, consts=[], expected_consts=[(1, 2, 3)])
1422
1423 def test_optimize_if_const_list(self):
1424 before = [
1425 ('NOP', None, 0),
1426 ('LOAD_SMALL_INT', 1, 0),
1427 ('NOP', None, 0),
1428 ('LOAD_SMALL_INT', 2, 0),
1429 ('NOP', None, 0),
1430 ('NOP', None, 0),
1431 ('LOAD_SMALL_INT', 3, 0),
1432 ('NOP', None, 0),
1433 ('BUILD_LIST', 3, 0),
1434 ('RETURN_VALUE', None, 0),
1435 ]
1436 after = [
1437 ('BUILD_LIST', 0, 0),
1438 ('LOAD_CONST', 0, 0),
1439 ('LIST_EXTEND', 1, 0),
1440 ('RETURN_VALUE', None, 0),
1441 ]
1442 self.cfg_optimization_test(before, after, consts=[], expected_consts=[(1, 2, 3)])
1443
1444 # need minimum 3 consts to optimize
1445 same = [
1446 ('LOAD_SMALL_INT', 1, 0),
1447 ('LOAD_SMALL_INT', 2, 0),
1448 ('BUILD_LIST', 2, 0),
1449 ('RETURN_VALUE', None, 0),
1450 ]
1451 self.cfg_optimization_test(same, same, consts=[])
1452
1453 # not all consts
1454 same = [
1455 ('LOAD_SMALL_INT', 1, 0),
1456 ('LOAD_NAME', 0, 0),
1457 ('LOAD_SMALL_INT', 3, 0),
1458 ('BUILD_LIST', 3, 0),
1459 ('RETURN_VALUE', None, 0),
1460 ]
1461 self.cfg_optimization_test(same, same, consts=[])
1462
1463 def test_optimize_if_const_set(self):
1464 before = [
1465 ('NOP', None, 0),
1466 ('LOAD_SMALL_INT', 1, 0),
1467 ('NOP', None, 0),
1468 ('LOAD_SMALL_INT', 2, 0),
1469 ('NOP', None, 0),
1470 ('NOP', None, 0),
1471 ('LOAD_SMALL_INT', 3, 0),
1472 ('NOP', None, 0),
1473 ('BUILD_SET', 3, 0),
1474 ('RETURN_VALUE', None, 0),
1475 ]
1476 after = [
1477 ('BUILD_SET', 0, 0),
1478 ('LOAD_CONST', 0, 0),
1479 ('SET_UPDATE', 1, 0),
1480 ('RETURN_VALUE', None, 0),
1481 ]
1482 self.cfg_optimization_test(before, after, consts=[], expected_consts=[frozenset({1, 2, 3})])
1483
1484 # need minimum 3 consts to optimize
1485 same = [
1486 ('LOAD_SMALL_INT', 1, 0),
1487 ('LOAD_SMALL_INT', 2, 0),
1488 ('BUILD_SET', 2, 0),
1489 ('RETURN_VALUE', None, 0),
1490 ]
1491 self.cfg_optimization_test(same, same, consts=[])
1492
1493 # not all consts
1494 same = [
1495 ('LOAD_SMALL_INT', 1, 0),
1496 ('LOAD_NAME', 0, 0),
1497 ('LOAD_SMALL_INT', 3, 0),
1498 ('BUILD_SET', 3, 0),
1499 ('RETURN_VALUE', None, 0),
1500 ]
1501 self.cfg_optimization_test(same, same, consts=[])
1502
1503 def test_optimize_literal_list_for_iter(self):
1504 # for _ in [1, 2]: pass ==> for _ in (1, 2): pass
1505 before = [
1506 ('LOAD_SMALL_INT', 1, 0),
1507 ('LOAD_SMALL_INT', 2, 0),
1508 ('BUILD_LIST', 2, 0),
1509 ('GET_ITER', None, 0),
1510 start := self.Label(),
1511 ('FOR_ITER', end := self.Label(), 0),
1512 ('STORE_FAST', 0, 0),
1513 ('JUMP', start, 0),
1514 end,
1515 ('END_FOR', None, 0),
1516 ('POP_ITER', None, 0),
1517 ('LOAD_CONST', 0, 0),
1518 ('RETURN_VALUE', None, 0),
1519 ]
1520 after = [
1521 ('LOAD_CONST', 1, 0),
1522 ('GET_ITER', None, 0),
1523 start := self.Label(),
1524 ('FOR_ITER', end := self.Label(), 0),
1525 ('STORE_FAST', 0, 0),
1526 ('JUMP', start, 0),
1527 end,
1528 ('END_FOR', None, 0),
1529 ('POP_ITER', None, 0),
1530 ('LOAD_CONST', 0, 0),
1531 ('RETURN_VALUE', None, 0),
1532 ]
1533 self.cfg_optimization_test(before, after, consts=[None], expected_consts=[None, (1, 2)])
1534
1535 # for _ in [1, x]: pass ==> for _ in (1, x): pass
1536 before = [
1537 ('LOAD_SMALL_INT', 1, 0),
1538 ('LOAD_NAME', 0, 0),
1539 ('BUILD_LIST', 2, 0),
1540 ('GET_ITER', None, 0),
1541 start := self.Label(),
1542 ('FOR_ITER', end := self.Label(), 0),
1543 ('STORE_FAST', 0, 0),
1544 ('JUMP', start, 0),
1545 end,
1546 ('END_FOR', None, 0),
1547 ('POP_ITER', None, 0),
1548 ('LOAD_CONST', 0, 0),
1549 ('RETURN_VALUE', None, 0),
1550 ]
1551 after = [
1552 ('LOAD_SMALL_INT', 1, 0),
1553 ('LOAD_NAME', 0, 0),
1554 ('BUILD_TUPLE', 2, 0),
1555 ('GET_ITER', None, 0),
1556 start := self.Label(),
1557 ('FOR_ITER', end := self.Label(), 0),
1558 ('STORE_FAST', 0, 0),
1559 ('JUMP', start, 0),
1560 end,
1561 ('END_FOR', None, 0),
1562 ('POP_ITER', None, 0),
1563 ('LOAD_CONST', 0, 0),
1564 ('RETURN_VALUE', None, 0),
1565 ]
1566 self.cfg_optimization_test(before, after, consts=[None], expected_consts=[None])
1567
1568 def test_optimize_literal_set_for_iter(self):
1569 # for _ in {1, 2}: pass ==> for _ in (1, 2): pass
1570 before = [
1571 ('LOAD_SMALL_INT', 1, 0),
1572 ('LOAD_SMALL_INT', 2, 0),
1573 ('BUILD_SET', 2, 0),
1574 ('GET_ITER', None, 0),
1575 start := self.Label(),
1576 ('FOR_ITER', end := self.Label(), 0),
1577 ('STORE_FAST', 0, 0),
1578 ('JUMP', start, 0),
1579 end,
1580 ('END_FOR', None, 0),
1581 ('POP_ITER', None, 0),
1582 ('LOAD_CONST', 0, 0),
1583 ('RETURN_VALUE', None, 0),
1584 ]
1585 after = [
1586 ('LOAD_CONST', 1, 0),
1587 ('GET_ITER', None, 0),
1588 start := self.Label(),
1589 ('FOR_ITER', end := self.Label(), 0),
1590 ('STORE_FAST', 0, 0),
1591 ('JUMP', start, 0),
1592 end,
1593 ('END_FOR', None, 0),
1594 ('POP_ITER', None, 0),
1595 ('LOAD_CONST', 0, 0),
1596 ('RETURN_VALUE', None, 0),
1597 ]
1598 self.cfg_optimization_test(before, after, consts=[None], expected_consts=[None, frozenset({1, 2})])
1599
1600 # non constant literal set is not changed
1601 # for _ in {1, x}: pass ==> for _ in {1, x}: pass
1602 same = [
1603 ('LOAD_SMALL_INT', 1, 0),
1604 ('LOAD_NAME', 0, 0),
1605 ('BUILD_SET', 2, 0),
1606 ('GET_ITER', None, 0),
1607 start := self.Label(),
1608 ('FOR_ITER', end := self.Label(), 0),
1609 ('STORE_FAST', 0, 0),
1610 ('JUMP', start, 0),
1611 end,
1612 ('END_FOR', None, 0),
1613 ('POP_ITER', None, 0),
1614 ('LOAD_CONST', 0, 0),
1615 ('RETURN_VALUE', None, 0),
1616 ]
1617 self.cfg_optimization_test(same, same, consts=[None], expected_consts=[None])
1618
1619 def test_optimize_literal_list_contains(self):
1620 # x in [1, 2] ==> x in (1, 2)
1621 before = [
1622 ('LOAD_NAME', 0, 0),
1623 ('LOAD_SMALL_INT', 1, 0),
1624 ('LOAD_SMALL_INT', 2, 0),
1625 ('BUILD_LIST', 2, 0),
1626 ('CONTAINS_OP', 0, 0),
1627 ('POP_TOP', None, 0),
1628 ('LOAD_CONST', 0, 0),
1629 ('RETURN_VALUE', None, 0),
1630 ]
1631 after = [
1632 ('LOAD_NAME', 0, 0),
1633 ('LOAD_CONST', 1, 0),
1634 ('CONTAINS_OP', 0, 0),
1635 ('POP_TOP', None, 0),
1636 ('LOAD_CONST', 0, 0),
1637 ('RETURN_VALUE', None, 0),
1638 ]
1639 self.cfg_optimization_test(before, after, consts=[None], expected_consts=[None, (1, 2)])
1640
1641 # x in [1, y] ==> x in (1, y)
1642 before = [
1643 ('LOAD_NAME', 0, 0),
1644 ('LOAD_SMALL_INT', 1, 0),
1645 ('LOAD_NAME', 1, 0),
1646 ('BUILD_LIST', 2, 0),
1647 ('CONTAINS_OP', 0, 0),
1648 ('POP_TOP', None, 0),
1649 ('LOAD_CONST', 0, 0),
1650 ('RETURN_VALUE', None, 0),
1651 ]
1652 after = [
1653 ('LOAD_NAME', 0, 0),
1654 ('LOAD_SMALL_INT', 1, 0),
1655 ('LOAD_NAME', 1, 0),
1656 ('BUILD_TUPLE', 2, 0),
1657 ('CONTAINS_OP', 0, 0),
1658 ('POP_TOP', None, 0),
1659 ('LOAD_CONST', 0, 0),
1660 ('RETURN_VALUE', None, 0),
1661 ]
1662 self.cfg_optimization_test(before, after, consts=[None], expected_consts=[None])
1663
1664 def test_optimize_literal_set_contains(self):
1665 # x in {1, 2} ==> x in (1, 2)
1666 before = [
1667 ('LOAD_NAME', 0, 0),
1668 ('LOAD_SMALL_INT', 1, 0),
1669 ('LOAD_SMALL_INT', 2, 0),
1670 ('BUILD_SET', 2, 0),
1671 ('CONTAINS_OP', 0, 0),
1672 ('POP_TOP', None, 0),
1673 ('LOAD_CONST', 0, 0),
1674 ('RETURN_VALUE', None, 0),
1675 ]
1676 after = [
1677 ('LOAD_NAME', 0, 0),
1678 ('LOAD_CONST', 1, 0),
1679 ('CONTAINS_OP', 0, 0),
1680 ('POP_TOP', None, 0),
1681 ('LOAD_CONST', 0, 0),
1682 ('RETURN_VALUE', None, 0),
1683 ]
1684 self.cfg_optimization_test(before, after, consts=[None], expected_consts=[None, frozenset({1, 2})])
1685
1686 # non constant literal set is not changed
1687 # x in {1, y} ==> x in {1, y}
1688 same = [
1689 ('LOAD_NAME', 0, 0),
1690 ('LOAD_SMALL_INT', 1, 0),
1691 ('LOAD_NAME', 1, 0),
1692 ('BUILD_SET', 2, 0),
1693 ('CONTAINS_OP', 0, 0),
1694 ('POP_TOP', None, 0),
1695 ('LOAD_CONST', 0, 0),
1696 ('RETURN_VALUE', None, 0),
1697 ]
1698 self.cfg_optimization_test(same, same, consts=[None], expected_consts=[None])
1699
1700 def test_optimize_unary_not(self):
1701 # test folding
1702 before = [
1703 ('LOAD_SMALL_INT', 1, 0),
1704 ('TO_BOOL', None, 0),
1705 ('UNARY_NOT', None, 0),
1706 ('RETURN_VALUE', None, 0),
1707 ]
1708 after = [
1709 ('LOAD_CONST', 1, 0),
1710 ('RETURN_VALUE', None, 0),
1711 ]
1712 self.cfg_optimization_test(before, after, consts=[], expected_consts=[True, False])
1713
1714 # test cancel out
1715 before = [
1716 ('LOAD_NAME', 0, 0),
1717 ('TO_BOOL', None, 0),
1718 ('UNARY_NOT', None, 0),
1719 ('UNARY_NOT', None, 0),
1720 ('UNARY_NOT', None, 0),
1721 ('UNARY_NOT', None, 0),
1722 ('RETURN_VALUE', None, 0),
1723 ]
1724 after = [
1725 ('LOAD_NAME', 0, 0),
1726 ('TO_BOOL', None, 0),
1727 ('RETURN_VALUE', None, 0),
1728 ]
1729 self.cfg_optimization_test(before, after, consts=[], expected_consts=[])
1730
1731 # test eliminate to bool
1732 before = [
1733 ('LOAD_NAME', 0, 0),
1734 ('TO_BOOL', None, 0),
1735 ('UNARY_NOT', None, 0),
1736 ('TO_BOOL', None, 0),
1737 ('TO_BOOL', None, 0),
1738 ('TO_BOOL', None, 0),
1739 ('RETURN_VALUE', None, 0),
1740 ]
1741 after = [
1742 ('LOAD_NAME', 0, 0),
1743 ('TO_BOOL', None, 0),
1744 ('UNARY_NOT', None, 0),
1745 ('RETURN_VALUE', None, 0),
1746 ]
1747 self.cfg_optimization_test(before, after, consts=[], expected_consts=[])
1748
1749 # test folding & cancel out
1750 before = [
1751 ('LOAD_SMALL_INT', 1, 0),
1752 ('TO_BOOL', None, 0),
1753 ('UNARY_NOT', None, 0),
1754 ('UNARY_NOT', None, 0),
1755 ('UNARY_NOT', None, 0),
1756 ('UNARY_NOT', None, 0),
1757 ('RETURN_VALUE', None, 0),
1758 ]
1759 after = [
1760 ('LOAD_CONST', 0, 0),
1761 ('RETURN_VALUE', None, 0),
1762 ]
1763 self.cfg_optimization_test(before, after, consts=[], expected_consts=[True])
1764
1765 # test folding & eliminate to bool
1766 before = [
1767 ('LOAD_SMALL_INT', 1, 0),
1768 ('TO_BOOL', None, 0),
1769 ('UNARY_NOT', None, 0),
1770 ('TO_BOOL', None, 0),
1771 ('TO_BOOL', None, 0),
1772 ('TO_BOOL', None, 0),
1773 ('RETURN_VALUE', None, 0),
1774 ]
1775 after = [
1776 ('LOAD_CONST', 1, 0),
1777 ('RETURN_VALUE', None, 0),
1778 ]
1779 self.cfg_optimization_test(before, after, consts=[], expected_consts=[True, False])
1780
1781 # test cancel out & eliminate to bool (to bool stays as we are not iterating to a fixed point)
1782 before = [
1783 ('LOAD_NAME', 0, 0),
1784 ('TO_BOOL', None, 0),
1785 ('UNARY_NOT', None, 0),
1786 ('UNARY_NOT', None, 0),
1787 ('UNARY_NOT', None, 0),
1788 ('UNARY_NOT', None, 0),
1789 ('TO_BOOL', None, 0),
1790 ('RETURN_VALUE', None, 0),
1791 ]
1792 after = [
1793 ('LOAD_NAME', 0, 0),
1794 ('TO_BOOL', None, 0),
1795 ('TO_BOOL', None, 0),
1796 ('RETURN_VALUE', None, 0),
1797 ]
1798 self.cfg_optimization_test(before, after, consts=[], expected_consts=[])
1799
1800 is_ = in_ = 0
1801 isnot = notin = 1
1802
1803 # test is/isnot
1804 before = [
1805 ('LOAD_NAME', 0, 0),
1806 ('LOAD_NAME', 1, 0),
1807 ('IS_OP', is_, 0),
1808 ('UNARY_NOT', None, 0),
1809 ('RETURN_VALUE', None, 0),
1810 ]
1811 after = [
1812 ('LOAD_NAME', 0, 0),
1813 ('LOAD_NAME', 1, 0),
1814 ('IS_OP', isnot, 0),
1815 ('RETURN_VALUE', None, 0),
1816 ]
1817 self.cfg_optimization_test(before, after, consts=[], expected_consts=[])
1818
1819 # test is/isnot cancel out
1820 before = [
1821 ('LOAD_NAME', 0, 0),
1822 ('LOAD_NAME', 1, 0),
1823 ('IS_OP', is_, 0),
1824 ('UNARY_NOT', None, 0),
1825 ('UNARY_NOT', None, 0),
1826 ('RETURN_VALUE', None, 0),
1827 ]
1828 after = [
1829 ('LOAD_NAME', 0, 0),
1830 ('LOAD_NAME', 1, 0),
1831 ('IS_OP', is_, 0),
1832 ('RETURN_VALUE', None, 0),
1833 ]
1834 self.cfg_optimization_test(before, after, consts=[], expected_consts=[])
1835
1836 # test is/isnot eliminate to bool
1837 before = [
1838 ('LOAD_NAME', 0, 0),
1839 ('LOAD_NAME', 1, 0),
1840 ('IS_OP', is_, 0),
1841 ('UNARY_NOT', None, 0),
1842 ('TO_BOOL', None, 0),
1843 ('TO_BOOL', None, 0),
1844 ('TO_BOOL', None, 0),
1845 ('RETURN_VALUE', None, 0),
1846 ]
1847 after = [
1848 ('LOAD_NAME', 0, 0),
1849 ('LOAD_NAME', 1, 0),
1850 ('IS_OP', isnot, 0),
1851 ('RETURN_VALUE', None, 0),
1852 ]
1853 self.cfg_optimization_test(before, after, consts=[], expected_consts=[])
1854
1855 # test is/isnot cancel out & eliminate to bool
1856 before = [
1857 ('LOAD_NAME', 0, 0),
1858 ('LOAD_NAME', 1, 0),
1859 ('IS_OP', is_, 0),
1860 ('UNARY_NOT', None, 0),
1861 ('UNARY_NOT', None, 0),
1862 ('TO_BOOL', None, 0),
1863 ('TO_BOOL', None, 0),
1864 ('TO_BOOL', None, 0),
1865 ('RETURN_VALUE', None, 0),
1866 ]
1867 after = [
1868 ('LOAD_NAME', 0, 0),
1869 ('LOAD_NAME', 1, 0),
1870 ('IS_OP', is_, 0),
1871 ('RETURN_VALUE', None, 0),
1872 ]
1873 self.cfg_optimization_test(before, after, consts=[], expected_consts=[])
1874
1875 # test in/notin
1876 before = [
1877 ('LOAD_NAME', 0, 0),
1878 ('LOAD_NAME', 1, 0),
1879 ('CONTAINS_OP', in_, 0),
1880 ('UNARY_NOT', None, 0),
1881 ('RETURN_VALUE', None, 0),
1882 ]
1883 after = [
1884 ('LOAD_NAME', 0, 0),
1885 ('LOAD_NAME', 1, 0),
1886 ('CONTAINS_OP', notin, 0),
1887 ('RETURN_VALUE', None, 0),
1888 ]
1889 self.cfg_optimization_test(before, after, consts=[], expected_consts=[])
1890
1891 # test in/notin cancel out
1892 before = [
1893 ('LOAD_NAME', 0, 0),
1894 ('LOAD_NAME', 1, 0),
1895 ('CONTAINS_OP', in_, 0),
1896 ('UNARY_NOT', None, 0),
1897 ('UNARY_NOT', None, 0),
1898 ('RETURN_VALUE', None, 0),
1899 ]
1900 after = [
1901 ('LOAD_NAME', 0, 0),
1902 ('LOAD_NAME', 1, 0),
1903 ('CONTAINS_OP', in_, 0),
1904 ('RETURN_VALUE', None, 0),
1905 ]
1906 self.cfg_optimization_test(before, after, consts=[], expected_consts=[])
1907
1908 # test is/isnot & eliminate to bool
1909 before = [
1910 ('LOAD_NAME', 0, 0),
1911 ('LOAD_NAME', 1, 0),
1912 ('CONTAINS_OP', in_, 0),
1913 ('UNARY_NOT', None, 0),
1914 ('TO_BOOL', None, 0),
1915 ('TO_BOOL', None, 0),
1916 ('TO_BOOL', None, 0),
1917 ('RETURN_VALUE', None, 0),
1918 ]
1919 after = [
1920 ('LOAD_NAME', 0, 0),
1921 ('LOAD_NAME', 1, 0),
1922 ('CONTAINS_OP', notin, 0),
1923 ('RETURN_VALUE', None, 0),
1924 ]
1925 self.cfg_optimization_test(before, after, consts=[], expected_consts=[])
1926
1927 # test in/notin cancel out & eliminate to bool
1928 before = [
1929 ('LOAD_NAME', 0, 0),
1930 ('LOAD_NAME', 1, 0),
1931 ('CONTAINS_OP', in_, 0),
1932 ('UNARY_NOT', None, 0),
1933 ('UNARY_NOT', None, 0),
1934 ('TO_BOOL', None, 0),
1935 ('RETURN_VALUE', None, 0),
1936 ]
1937 after = [
1938 ('LOAD_NAME', 0, 0),
1939 ('LOAD_NAME', 1, 0),
1940 ('CONTAINS_OP', in_, 0),
1941 ('RETURN_VALUE', None, 0),
1942 ]
1943 self.cfg_optimization_test(before, after, consts=[], expected_consts=[])
1944
1945 def test_optimize_if_const_unaryop(self):
1946 # test unary negative
1947 before = [
1948 ('LOAD_SMALL_INT', 2, 0),
1949 ('UNARY_NEGATIVE', None, 0),
1950 ('UNARY_NEGATIVE', None, 0),
1951 ('RETURN_VALUE', None, 0)
1952 ]
1953 after = [
1954 ('LOAD_SMALL_INT', 2, 0),
1955 ('RETURN_VALUE', None, 0),
1956 ]
1957 self.cfg_optimization_test(before, after, consts=[], expected_consts=[-2])
1958
1959 # test unary invert
1960 before = [
1961 ('LOAD_SMALL_INT', 2, 0),
1962 ('UNARY_INVERT', None, 0),
1963 ('UNARY_INVERT', None, 0),
1964 ('RETURN_VALUE', None, 0)
1965 ]
1966 after = [
1967 ('LOAD_SMALL_INT', 2, 0),
1968 ('RETURN_VALUE', None, 0),
1969 ]
1970 self.cfg_optimization_test(before, after, consts=[], expected_consts=[-3])
1971
1972 # test unary positive
1973 before = [
1974 ('LOAD_SMALL_INT', 2, 0),
1975 ('CALL_INTRINSIC_1', 5, 0),
1976 ('CALL_INTRINSIC_1', 5, 0),
1977 ('RETURN_VALUE', None, 0)
1978 ]
1979 after = [
1980 ('LOAD_SMALL_INT', 2, 0),
1981 ('RETURN_VALUE', None, 0),
1982 ]
1983 self.cfg_optimization_test(before, after, consts=[], expected_consts=[])
1984
1985 def test_optimize_if_const_binop(self):
1986 add = get_binop_argval('NB_ADD')
1987 sub = get_binop_argval('NB_SUBTRACT')
1988 mul = get_binop_argval('NB_MULTIPLY')
1989 div = get_binop_argval('NB_TRUE_DIVIDE')
1990 floor = get_binop_argval('NB_FLOOR_DIVIDE')
1991 rem = get_binop_argval('NB_REMAINDER')
1992 pow = get_binop_argval('NB_POWER')
1993 lshift = get_binop_argval('NB_LSHIFT')
1994 rshift = get_binop_argval('NB_RSHIFT')
1995 or_ = get_binop_argval('NB_OR')
1996 and_ = get_binop_argval('NB_AND')
1997 xor = get_binop_argval('NB_XOR')
1998 subscr = get_binop_argval('NB_SUBSCR')
1999
2000 # test add
2001 before = [
2002 ('LOAD_SMALL_INT', 2, 0),
2003 ('LOAD_SMALL_INT', 2, 0),
2004 ('BINARY_OP', add, 0),
2005 ('LOAD_SMALL_INT', 2, 0),
2006 ('BINARY_OP', add, 0),
2007 ('RETURN_VALUE', None, 0)
2008 ]
2009 after = [
2010 ('LOAD_SMALL_INT', 6, 0),
2011 ('RETURN_VALUE', None, 0)
2012 ]
2013 self.cfg_optimization_test(before, after, consts=[], expected_consts=[])
2014
2015 # test sub
2016 before = [
2017 ('LOAD_SMALL_INT', 2, 0),
2018 ('LOAD_SMALL_INT', 2, 0),
2019 ('BINARY_OP', sub, 0),
2020 ('LOAD_SMALL_INT', 2, 0),
2021 ('BINARY_OP', sub, 0),
2022 ('RETURN_VALUE', None, 0)
2023 ]
2024 after = [
2025 ('LOAD_CONST', 0, 0),
2026 ('RETURN_VALUE', None, 0)
2027 ]
2028 self.cfg_optimization_test(before, after, consts=[], expected_consts=[-2])
2029
2030 # test mul
2031 before = [
2032 ('LOAD_SMALL_INT', 2, 0),
2033 ('LOAD_SMALL_INT', 2, 0),
2034 ('BINARY_OP', mul, 0),
2035 ('LOAD_SMALL_INT', 2, 0),
2036 ('BINARY_OP', mul, 0),
2037 ('RETURN_VALUE', None, 0)
2038 ]
2039 after = [
2040 ('LOAD_SMALL_INT', 8, 0),
2041 ('RETURN_VALUE', None, 0)
2042 ]
2043 self.cfg_optimization_test(before, after, consts=[], expected_consts=[])
2044
2045 # test div
2046 before = [
2047 ('LOAD_SMALL_INT', 2, 0),
2048 ('LOAD_SMALL_INT', 2, 0),
2049 ('BINARY_OP', div, 0),
2050 ('LOAD_SMALL_INT', 2, 0),
2051 ('BINARY_OP', div, 0),
2052 ('RETURN_VALUE', None, 0)
2053 ]
2054 after = [
2055 ('LOAD_CONST', 1, 0),
2056 ('RETURN_VALUE', None, 0)
2057 ]
2058 self.cfg_optimization_test(before, after, consts=[], expected_consts=[1.0, 0.5])
2059
2060 # test floor
2061 before = [
2062 ('LOAD_SMALL_INT', 2, 0),
2063 ('LOAD_SMALL_INT', 2, 0),
2064 ('BINARY_OP', floor, 0),
2065 ('LOAD_SMALL_INT', 2, 0),
2066 ('BINARY_OP', floor, 0),
2067 ('RETURN_VALUE', None, 0)
2068 ]
2069 after = [
2070 ('LOAD_SMALL_INT', 0, 0),
2071 ('RETURN_VALUE', None, 0)
2072 ]
2073 self.cfg_optimization_test(before, after, consts=[], expected_consts=[])
2074
2075 # test rem
2076 before = [
2077 ('LOAD_SMALL_INT', 2, 0),
2078 ('LOAD_SMALL_INT', 2, 0),
2079 ('BINARY_OP', rem, 0),
2080 ('LOAD_SMALL_INT', 2, 0),
2081 ('BINARY_OP', rem, 0),
2082 ('RETURN_VALUE', None, 0)
2083 ]
2084 after = [
2085 ('LOAD_SMALL_INT', 0, 0),
2086 ('RETURN_VALUE', None, 0)
2087 ]
2088 self.cfg_optimization_test(before, after, consts=[], expected_consts=[])
2089
2090 # test pow
2091 before = [
2092 ('LOAD_SMALL_INT', 2, 0),
2093 ('LOAD_SMALL_INT', 2, 0),
2094 ('BINARY_OP', pow, 0),
2095 ('LOAD_SMALL_INT', 2, 0),
2096 ('BINARY_OP', pow, 0),
2097 ('RETURN_VALUE', None, 0)
2098 ]
2099 after = [
2100 ('LOAD_SMALL_INT', 16, 0),
2101 ('RETURN_VALUE', None, 0)
2102 ]
2103 self.cfg_optimization_test(before, after, consts=[], expected_consts=[])
2104
2105 # test lshift
2106 before = [
2107 ('LOAD_SMALL_INT', 1, 0),
2108 ('LOAD_SMALL_INT', 1, 0),
2109 ('BINARY_OP', lshift, 0),
2110 ('LOAD_SMALL_INT', 1, 0),
2111 ('BINARY_OP', lshift, 0),
2112 ('RETURN_VALUE', None, 0)
2113 ]
2114 after = [
2115 ('LOAD_SMALL_INT', 4, 0),
2116 ('RETURN_VALUE', None, 0)
2117 ]
2118 self.cfg_optimization_test(before, after, consts=[], expected_consts=[])
2119
2120 # test rshift
2121 before = [
2122 ('LOAD_SMALL_INT', 4, 0),
2123 ('LOAD_SMALL_INT', 1, 0),
2124 ('BINARY_OP', rshift, 0),
2125 ('LOAD_SMALL_INT', 1, 0),
2126 ('BINARY_OP', rshift, 0),
2127 ('RETURN_VALUE', None, 0)
2128 ]
2129 after = [
2130 ('LOAD_SMALL_INT', 1, 0),
2131 ('RETURN_VALUE', None, 0)
2132 ]
2133 self.cfg_optimization_test(before, after, consts=[], expected_consts=[])
2134
2135 # test or
2136 before = [
2137 ('LOAD_SMALL_INT', 1, 0),
2138 ('LOAD_SMALL_INT', 2, 0),
2139 ('BINARY_OP', or_, 0),
2140 ('LOAD_SMALL_INT', 4, 0),
2141 ('BINARY_OP', or_, 0),
2142 ('RETURN_VALUE', None, 0)
2143 ]
2144 after = [
2145 ('LOAD_SMALL_INT', 7, 0),
2146 ('RETURN_VALUE', None, 0)
2147 ]
2148 self.cfg_optimization_test(before, after, consts=[], expected_consts=[])
2149
2150 # test and
2151 before = [
2152 ('LOAD_SMALL_INT', 1, 0),
2153 ('LOAD_SMALL_INT', 1, 0),
2154 ('BINARY_OP', and_, 0),
2155 ('LOAD_SMALL_INT', 1, 0),
2156 ('BINARY_OP', and_, 0),
2157 ('RETURN_VALUE', None, 0)
2158 ]
2159 after = [
2160 ('LOAD_SMALL_INT', 1, 0),
2161 ('RETURN_VALUE', None, 0)
2162 ]
2163 self.cfg_optimization_test(before, after, consts=[], expected_consts=[])
2164
2165 # test xor
2166 before = [
2167 ('LOAD_SMALL_INT', 2, 0),
2168 ('LOAD_SMALL_INT', 2, 0),
2169 ('BINARY_OP', xor, 0),
2170 ('LOAD_SMALL_INT', 2, 0),
2171 ('BINARY_OP', xor, 0),
2172 ('RETURN_VALUE', None, 0)
2173 ]
2174 after = [
2175 ('LOAD_SMALL_INT', 2, 0),
2176 ('RETURN_VALUE', None, 0)
2177 ]
2178 self.cfg_optimization_test(before, after, consts=[], expected_consts=[])
2179
2180 # test subscr
2181 before = [
2182 ('LOAD_CONST', 0, 0),
2183 ('LOAD_SMALL_INT', 1, 0),
2184 ('BINARY_OP', subscr, 0),
2185 ('LOAD_SMALL_INT', 2, 0),
2186 ('BINARY_OP', subscr, 0),
2187 ('RETURN_VALUE', None, 0)
2188 ]
2189 after = [
2190 ('LOAD_SMALL_INT', 3, 0),
2191 ('RETURN_VALUE', None, 0)
2192 ]
2193 self.cfg_optimization_test(before, after, consts=[(1, (1, 2, 3))], expected_consts=[(1, (1, 2, 3))])
2194
2195
2196 def test_conditional_jump_forward_const_condition(self):
2197 # The unreachable branch of the jump is removed, the jump
2198 # becomes redundant and is replaced by a NOP (for the lineno)
2199
2200 insts = [
2201 ('LOAD_CONST', 3, 11),
2202 ('POP_JUMP_IF_TRUE', lbl := self.Label(), 12),
2203 ('LOAD_CONST', 2, 13),
2204 lbl,
2205 ('LOAD_CONST', 3, 14),
2206 ('RETURN_VALUE', None, 14),
2207 ]
2208 expected_insts = [
2209 ('NOP', None, 11),
2210 ('NOP', None, 12),
2211 ('LOAD_SMALL_INT', 3, 14),
2212 ('RETURN_VALUE', None, 14),
2213 ]
2214 self.cfg_optimization_test(insts,
2215 expected_insts,
2216 consts=[0, 1, 2, 3, 4],
2217 expected_consts=[0])
2218
2219 def test_conditional_jump_backward_non_const_condition(self):
2220 insts = [
2221 lbl1 := self.Label(),
2222 ('LOAD_NAME', 1, 11),
2223 ('POP_JUMP_IF_TRUE', lbl1, 12),
2224 ('LOAD_NAME', 2, 13),
2225 ('RETURN_VALUE', None, 13),
2226 ]
2227 expected = [
2228 lbl := self.Label(),
2229 ('LOAD_NAME', 1, 11),
2230 ('POP_JUMP_IF_TRUE', lbl, 12),
2231 ('LOAD_NAME', 2, 13),
2232 ('RETURN_VALUE', None, 13),
2233 ]
2234 self.cfg_optimization_test(insts, expected, consts=list(range(5)))
2235
2236 def test_conditional_jump_backward_const_condition(self):
2237 # The unreachable branch of the jump is removed
2238 insts = [
2239 lbl1 := self.Label(),
2240 ('LOAD_CONST', 3, 11),
2241 ('POP_JUMP_IF_TRUE', lbl1, 12),
2242 ('LOAD_CONST', 2, 13),
2243 ('RETURN_VALUE', None, 13),
2244 ]
2245 expected_insts = [
2246 lbl := self.Label(),
2247 ('NOP', None, 11),
2248 ('JUMP', lbl, 12),
2249 ]
2250 self.cfg_optimization_test(insts, expected_insts, consts=list(range(5)))
2251
2252 def test_except_handler_label(self):
2253 insts = [
2254 ('SETUP_FINALLY', handler := self.Label(), 10),
2255 ('POP_BLOCK', None, -1),
2256 ('LOAD_CONST', 1, 11),
2257 ('RETURN_VALUE', None, 11),
2258 handler,
2259 ('LOAD_CONST', 2, 12),
2260 ('RETURN_VALUE', None, 12),
2261 ]
2262 expected_insts = [
2263 ('SETUP_FINALLY', handler := self.Label(), 10),
2264 ('LOAD_SMALL_INT', 1, 11),
2265 ('RETURN_VALUE', None, 11),
2266 handler,
2267 ('LOAD_SMALL_INT', 2, 12),
2268 ('RETURN_VALUE', None, 12),
2269 ]
2270 self.cfg_optimization_test(insts, expected_insts, consts=list(range(5)))
2271
2272 def test_no_unsafe_static_swap(self):
2273 # We can't change order of two stores to the same location
2274 insts = [
2275 ('LOAD_CONST', 0, 1),
2276 ('LOAD_CONST', 1, 2),
2277 ('LOAD_CONST', 2, 3),
2278 ('SWAP', 3, 4),
2279 ('STORE_FAST', 1, 4),
2280 ('STORE_FAST', 1, 4),
2281 ('POP_TOP', None, 4),
2282 ('LOAD_CONST', 0, 5),
2283 ('RETURN_VALUE', None, 5)
2284 ]
2285 expected_insts = [
2286 ('LOAD_SMALL_INT', 0, 1),
2287 ('LOAD_SMALL_INT', 1, 2),
2288 ('NOP', None, 3),
2289 ('STORE_FAST', 1, 4),
2290 ('POP_TOP', None, 4),
2291 ('LOAD_SMALL_INT', 0, 5),
2292 ('RETURN_VALUE', None, 5)
2293 ]
2294 self.cfg_optimization_test(insts, expected_insts, consts=list(range(3)), nlocals=1)
2295
2296 def test_dead_store_elimination_in_same_lineno(self):
2297 insts = [
2298 ('LOAD_CONST', 0, 1),
2299 ('LOAD_CONST', 1, 2),
2300 ('LOAD_CONST', 2, 3),
2301 ('STORE_FAST', 1, 4),
2302 ('STORE_FAST', 1, 4),
2303 ('STORE_FAST', 1, 4),
2304 ('LOAD_CONST', 0, 5),
2305 ('RETURN_VALUE', None, 5)
2306 ]
2307 expected_insts = [
2308 ('LOAD_SMALL_INT', 0, 1),
2309 ('LOAD_SMALL_INT', 1, 2),
2310 ('NOP', None, 3),
2311 ('POP_TOP', None, 4),
2312 ('STORE_FAST', 1, 4),
2313 ('LOAD_SMALL_INT', 0, 5),
2314 ('RETURN_VALUE', None, 5)
2315 ]
2316 self.cfg_optimization_test(insts, expected_insts, consts=list(range(3)), nlocals=1)
2317
2318 def test_no_dead_store_elimination_in_different_lineno(self):
2319 insts = [
2320 ('LOAD_CONST', 0, 1),
2321 ('LOAD_CONST', 1, 2),
2322 ('LOAD_CONST', 2, 3),
2323 ('STORE_FAST', 1, 4),
2324 ('STORE_FAST', 1, 5),
2325 ('STORE_FAST', 1, 6),
2326 ('LOAD_CONST', 0, 5),
2327 ('RETURN_VALUE', None, 5)
2328 ]
2329 expected_insts = [
2330 ('LOAD_SMALL_INT', 0, 1),
2331 ('LOAD_SMALL_INT', 1, 2),
2332 ('LOAD_SMALL_INT', 2, 3),
2333 ('STORE_FAST', 1, 4),
2334 ('STORE_FAST', 1, 5),
2335 ('STORE_FAST', 1, 6),
2336 ('LOAD_SMALL_INT', 0, 5),
2337 ('RETURN_VALUE', None, 5)
2338 ]
2339 self.cfg_optimization_test(insts, expected_insts, consts=list(range(3)), nlocals=1)
2340
2341 def test_unconditional_jump_threading(self):
2342
2343 def get_insts(lno1, lno2, op1, op2):
2344 return [
2345 lbl2 := self.Label(),
2346 ('LOAD_NAME', 0, 10),
2347 ('POP_TOP', None, 10),
2348 (op1, lbl1 := self.Label(), lno1),
2349 ('LOAD_NAME', 1, 20),
2350 lbl1,
2351 (op2, lbl2, lno2),
2352 ]
2353
2354 for op1 in ('JUMP', 'JUMP_NO_INTERRUPT'):
2355 for op2 in ('JUMP', 'JUMP_NO_INTERRUPT'):
2356 # different lines
2357 lno1, lno2 = (4, 5)
2358 with self.subTest(lno = (lno1, lno2), ops = (op1, op2)):
2359 insts = get_insts(lno1, lno2, op1, op2)
2360 op = 'JUMP' if 'JUMP' in (op1, op2) else 'JUMP_NO_INTERRUPT'
2361 expected_insts = [
2362 ('LOAD_NAME', 0, 10),
2363 ('POP_TOP', None, 10),
2364 ('NOP', None, 4),
2365 (op, 0, 5),
2366 ]
2367 self.cfg_optimization_test(insts, expected_insts, consts=list(range(5)))
2368
2369 # Threading
2370 for lno1, lno2 in [(-1, -1), (-1, 5), (6, -1), (7, 7)]:
2371 with self.subTest(lno = (lno1, lno2), ops = (op1, op2)):
2372 insts = get_insts(lno1, lno2, op1, op2)
2373 lno = lno1 if lno1 != -1 else lno2
2374 if lno == -1:
2375 lno = 10 # Propagated from the line before
2376
2377 op = 'JUMP' if 'JUMP' in (op1, op2) else 'JUMP_NO_INTERRUPT'
2378 expected_insts = [
2379 ('LOAD_NAME', 0, 10),
2380 ('POP_TOP', None, 10),
2381 (op, 0, lno),
2382 ]
2383 self.cfg_optimization_test(insts, expected_insts, consts=list(range(5)))
2384
2385 def test_list_to_tuple_get_iter(self):
2386 # for _ in (*foo, *bar) -> for _ in [*foo, *bar]
2387 INTRINSIC_LIST_TO_TUPLE = 6
2388 insts = [
2389 ("BUILD_LIST", 0, 1),
2390 ("LOAD_FAST", 0, 2),
2391 ("LIST_EXTEND", 1, 3),
2392 ("LOAD_FAST", 1, 4),
2393 ("LIST_EXTEND", 1, 5),
2394 ("CALL_INTRINSIC_1", INTRINSIC_LIST_TO_TUPLE, 6),
2395 ("GET_ITER", None, 7),
2396 top := self.Label(),
2397 ("FOR_ITER", end := self.Label(), 8),
2398 ("STORE_FAST", 2, 9),
2399 ("JUMP", top, 10),
2400 end,
2401 ("END_FOR", None, 11),
2402 ("POP_TOP", None, 12),
2403 ("LOAD_CONST", 0, 13),
2404 ("RETURN_VALUE", None, 14),
2405 ]
2406 expected_insts = [
2407 ("BUILD_LIST", 0, 1),
2408 ("LOAD_FAST_BORROW", 0, 2),
2409 ("LIST_EXTEND", 1, 3),
2410 ("LOAD_FAST_BORROW", 1, 4),
2411 ("LIST_EXTEND", 1, 5),
2412 ("NOP", None, 6), # ("CALL_INTRINSIC_1", INTRINSIC_LIST_TO_TUPLE, 6),
2413 ("GET_ITER", None, 7),
2414 top := self.Label(),
2415 ("FOR_ITER", end := self.Label(), 8),
2416 ("STORE_FAST", 2, 9),
2417 ("JUMP", top, 10),
2418 end,
2419 ("END_FOR", None, 11),
2420 ("POP_TOP", None, 12),
2421 ("LOAD_CONST", 0, 13),
2422 ("RETURN_VALUE", None, 14),
2423 ]
2424 self.cfg_optimization_test(insts, expected_insts, consts=[None])
2425
2426 def test_list_to_tuple_get_iter_is_safe(self):
2427 a, b = [], []
2428 for item in (*(items := [0, 1, 2, 3]),):
2429 a.append(item)
2430 b.append(items.pop())
2431 self.assertEqual(a, [0, 1, 2, 3])
2432 self.assertEqual(b, [3, 2, 1, 0])
2433 self.assertEqual(items, [])
2434
2435
2436 class OptimizeLoadFastTestCase(DirectCfgOptimizerTests):
2437 def make_bb(self, insts):
2438 last_loc = insts[-1][2]
2439 maxconst = 0
2440 for op, arg, _ in insts:
2441 if op == "LOAD_CONST":
2442 maxconst = max(maxconst, arg)
2443 consts = [None for _ in range(maxconst + 1)]
2444 return insts + [
2445 ("LOAD_CONST", 0, last_loc + 1),
2446 ("RETURN_VALUE", None, last_loc + 2),
2447 ], consts
2448
2449 def check(self, insts, expected_insts, consts=None):
2450 insts_bb, insts_consts = self.make_bb(insts)
2451 expected_insts_bb, exp_consts = self.make_bb(expected_insts)
2452 self.cfg_optimization_test(insts_bb, expected_insts_bb,
2453 consts=insts_consts, expected_consts=exp_consts)
2454
2455 def test_optimized(self):
2456 insts = [
2457 ("LOAD_FAST", 0, 1),
2458 ("LOAD_FAST", 1, 2),
2459 ("BINARY_OP", 2, 3),
2460 ]
2461 expected = [
2462 ("LOAD_FAST_BORROW", 0, 1),
2463 ("LOAD_FAST_BORROW", 1, 2),
2464 ("BINARY_OP", 2, 3),
2465 ]
2466 self.check(insts, expected)
2467
2468 insts = [
2469 ("LOAD_FAST", 0, 1),
2470 ("LOAD_CONST", 1, 2),
2471 ("SWAP", 2, 3),
2472 ("POP_TOP", None, 4),
2473 ]
2474 expected = [
2475 ("LOAD_FAST_BORROW", 0, 1),
2476 ("LOAD_CONST", 1, 2),
2477 ("SWAP", 2, 3),
2478 ("POP_TOP", None, 4),
2479 ]
2480 self.check(insts, expected)
2481
2482 def test_unoptimized_if_unconsumed(self):
2483 insts = [
2484 ("LOAD_FAST", 0, 1),
2485 ("LOAD_FAST", 1, 2),
2486 ("POP_TOP", None, 3),
2487 ]
2488 expected = [
2489 ("LOAD_FAST", 0, 1),
2490 ("LOAD_FAST_BORROW", 1, 2),
2491 ("POP_TOP", None, 3),
2492 ]
2493 self.check(insts, expected)
2494
2495 insts = [
2496 ("LOAD_FAST", 0, 1),
2497 ("COPY", 1, 2),
2498 ("POP_TOP", None, 3),
2499 ]
2500 expected = [
2501 ("LOAD_FAST", 0, 1),
2502 ("NOP", None, 2),
2503 ("NOP", None, 3),
2504 ]
2505 self.check(insts, expected)
2506
2507 def test_unoptimized_if_support_killed(self):
2508 insts = [
2509 ("LOAD_FAST", 0, 1),
2510 ("LOAD_CONST", 0, 2),
2511 ("STORE_FAST", 0, 3),
2512 ("POP_TOP", None, 4),
2513 ]
2514 self.check(insts, insts)
2515
2516 insts = [
2517 ("LOAD_FAST", 0, 1),
2518 ("LOAD_CONST", 0, 2),
2519 ("LOAD_CONST", 0, 3),
2520 ("STORE_FAST_STORE_FAST", ((0 << 4) | 1), 4),
2521 ("POP_TOP", None, 5),
2522 ]
2523 self.check(insts, insts)
2524
2525 insts = [
2526 ("LOAD_FAST", 0, 1),
2527 ("DELETE_FAST", 0, 2),
2528 ("POP_TOP", None, 3),
2529 ]
2530 self.check(insts, insts)
2531
2532 def test_unoptimized_if_aliased(self):
2533 insts = [
2534 ("LOAD_FAST", 0, 1),
2535 ("STORE_FAST", 1, 2),
2536 ]
2537 self.check(insts, insts)
2538
2539 insts = [
2540 ("LOAD_FAST", 0, 1),
2541 ("LOAD_CONST", 0, 3),
2542 ("STORE_FAST_STORE_FAST", ((0 << 4) | 1), 4),
2543 ]
2544 self.check(insts, insts)
2545
2546 def test_consume_no_inputs(self):
2547 insts = [
2548 ("LOAD_FAST", 0, 1),
2549 ("GET_LEN", None, 2),
2550 ("STORE_FAST", 1 , 3),
2551 ("STORE_FAST", 2, 4),
2552 ]
2553 self.check(insts, insts)
2554
2555 def test_consume_some_inputs_no_outputs(self):
2556 insts = [
2557 ("LOAD_FAST", 0, 1),
2558 ("GET_LEN", None, 2),
2559 ("LIST_APPEND", 0, 3),
2560 ]
2561 self.check(insts, insts)
2562
2563 def test_check_exc_match(self):
2564 insts = [
2565 ("LOAD_FAST", 0, 1),
2566 ("LOAD_FAST", 1, 2),
2567 ("CHECK_EXC_MATCH", None, 3)
2568 ]
2569 expected = [
2570 ("LOAD_FAST", 0, 1),
2571 ("LOAD_FAST_BORROW", 1, 2),
2572 ("CHECK_EXC_MATCH", None, 3)
2573 ]
2574 self.check(insts, expected)
2575
2576 def test_for_iter(self):
2577 insts = [
2578 ("LOAD_FAST", 0, 1),
2579 top := self.Label(),
2580 ("FOR_ITER", end := self.Label(), 2),
2581 ("STORE_FAST", 2, 3),
2582 ("JUMP", top, 4),
2583 end,
2584 ("END_FOR", None, 5),
2585 ("POP_TOP", None, 6),
2586 ("LOAD_CONST", 0, 7),
2587 ("RETURN_VALUE", None, 8),
2588 ]
2589 self.cfg_optimization_test(insts, insts, consts=[None])
2590
2591 def test_load_attr(self):
2592 insts = [
2593 ("LOAD_FAST", 0, 1),
2594 ("LOAD_ATTR", 0, 2),
2595 ]
2596 expected = [
2597 ("LOAD_FAST_BORROW", 0, 1),
2598 ("LOAD_ATTR", 0, 2),
2599 ]
2600 self.check(insts, expected)
2601
2602 # Method call, leaves self on stack unconsumed
2603 insts = [
2604 ("LOAD_FAST", 0, 1),
2605 ("LOAD_ATTR", 1, 2),
2606 ]
2607 expected = [
2608 ("LOAD_FAST", 0, 1),
2609 ("LOAD_ATTR", 1, 2),
2610 ]
2611 self.check(insts, expected)
2612
2613 def test_super_attr(self):
2614 insts = [
2615 ("LOAD_FAST", 0, 1),
2616 ("LOAD_FAST", 1, 2),
2617 ("LOAD_FAST", 2, 3),
2618 ("LOAD_SUPER_ATTR", 0, 4),
2619 ]
2620 expected = [
2621 ("LOAD_FAST_BORROW", 0, 1),
2622 ("LOAD_FAST_BORROW", 1, 2),
2623 ("LOAD_FAST_BORROW", 2, 3),
2624 ("LOAD_SUPER_ATTR", 0, 4),
2625 ]
2626 self.check(insts, expected)
2627
2628 # Method call, leaves self on stack unconsumed
2629 insts = [
2630 ("LOAD_FAST", 0, 1),
2631 ("LOAD_FAST", 1, 2),
2632 ("LOAD_FAST", 2, 3),
2633 ("LOAD_SUPER_ATTR", 1, 4),
2634 ]
2635 expected = [
2636 ("LOAD_FAST_BORROW", 0, 1),
2637 ("LOAD_FAST_BORROW", 1, 2),
2638 ("LOAD_FAST", 2, 3),
2639 ("LOAD_SUPER_ATTR", 1, 4),
2640 ]
2641 self.check(insts, expected)
2642
2643 def test_send(self):
2644 insts = [
2645 ("LOAD_FAST", 0, 1),
2646 ("LOAD_FAST", 1, 2),
2647 ("SEND", end := self.Label(), 3),
2648 ("LOAD_CONST", 0, 4),
2649 ("RETURN_VALUE", None, 5),
2650 end,
2651 ("LOAD_CONST", 0, 6),
2652 ("RETURN_VALUE", None, 7)
2653 ]
2654 expected = [
2655 ("LOAD_FAST", 0, 1),
2656 ("LOAD_FAST_BORROW", 1, 2),
2657 ("SEND", end := self.Label(), 3),
2658 ("LOAD_CONST", 0, 4),
2659 ("RETURN_VALUE", None, 5),
2660 end,
2661 ("LOAD_CONST", 0, 6),
2662 ("RETURN_VALUE", None, 7)
2663 ]
2664 self.cfg_optimization_test(insts, expected, consts=[None])
2665
2666 def test_format_simple(self):
2667 # FORMAT_SIMPLE will leave its operand on the stack if it's a unicode
2668 # object. We treat it conservatively and assume that it always leaves
2669 # its operand on the stack.
2670 insts = [
2671 ("LOAD_FAST", 0, 1),
2672 ("FORMAT_SIMPLE", None, 2),
2673 ("STORE_FAST", 1, 3),
2674 ]
2675 self.check(insts, insts)
2676
2677 insts = [
2678 ("LOAD_FAST", 0, 1),
2679 ("FORMAT_SIMPLE", None, 2),
2680 ("POP_TOP", None, 3),
2681 ]
2682 expected = [
2683 ("LOAD_FAST_BORROW", 0, 1),
2684 ("FORMAT_SIMPLE", None, 2),
2685 ("POP_TOP", None, 3),
2686 ]
2687 self.check(insts, expected)
2688
2689 def test_set_function_attribute(self):
2690 # SET_FUNCTION_ATTRIBUTE leaves the function on the stack
2691 insts = [
2692 ("LOAD_CONST", 0, 1),
2693 ("LOAD_FAST", 0, 2),
2694 ("SET_FUNCTION_ATTRIBUTE", 2, 3),
2695 ("STORE_FAST", 1, 4),
2696 ("LOAD_CONST", 0, 5),
2697 ("RETURN_VALUE", None, 6)
2698 ]
2699 self.cfg_optimization_test(insts, insts, consts=[None])
2700
2701 insts = [
2702 ("LOAD_CONST", 0, 1),
2703 ("LOAD_FAST", 0, 2),
2704 ("SET_FUNCTION_ATTRIBUTE", 2, 3),
2705 ("RETURN_VALUE", None, 4)
2706 ]
2707 expected = [
2708 ("LOAD_CONST", 0, 1),
2709 ("LOAD_FAST_BORROW", 0, 2),
2710 ("SET_FUNCTION_ATTRIBUTE", 2, 3),
2711 ("RETURN_VALUE", None, 4)
2712 ]
2713 self.cfg_optimization_test(insts, expected, consts=[None])
2714
2715 def test_get_yield_from_iter(self):
2716 # GET_YIELD_FROM_ITER may leave its operand on the stack
2717 insts = [
2718 ("LOAD_FAST", 0, 1),
2719 ("GET_YIELD_FROM_ITER", None, 2),
2720 ("LOAD_CONST", 0, 3),
2721 send := self.Label(),
2722 ("SEND", end := self.Label(), 5),
2723 ("YIELD_VALUE", 1, 6),
2724 ("RESUME", 2, 7),
2725 ("JUMP", send, 8),
2726 end,
2727 ("END_SEND", None, 9),
2728 ("LOAD_CONST", 0, 10),
2729 ("RETURN_VALUE", None, 11),
2730 ]
2731 self.cfg_optimization_test(insts, insts, consts=[None])
2732
2733 def test_push_exc_info(self):
2734 insts = [
2735 ("LOAD_FAST", 0, 1),
2736 ("PUSH_EXC_INFO", None, 2),
2737 ]
2738 self.check(insts, insts)
2739
2740 def test_load_special(self):
2741 # LOAD_SPECIAL may leave self on the stack
2742 insts = [
2743 ("LOAD_FAST", 0, 1),
2744 ("LOAD_SPECIAL", 0, 2),
2745 ("STORE_FAST", 1, 3),
2746 ]
2747 self.check(insts, insts)
2748
2749
2750 def test_del_in_finally(self):
2751 # This loads `obj` onto the stack, executes `del obj`, then returns the
2752 # `obj` from the stack. See gh-133371 for more details.
2753 def create_obj():
2754 obj = [42]
2755 try:
2756 return obj
2757 finally:
2758 del obj
2759
2760 obj = create_obj()
2761 # The crash in the linked issue happens while running GC during
2762 # interpreter finalization, so run it here manually.
2763 gc.collect()
2764 self.assertEqual(obj, [42])
2765
2766 def test_format_simple_unicode(self):
2767 # Repro from gh-134889
2768 def f():
2769 var = f"{1}"
2770 var = f"{var}"
2771 return var
2772 self.assertEqual(f(), "1")
2773
2774
2775
2776 if __name__ == "__main__":
2777 unittest.main()