Skip to content

Commit d5ce5ec

Browse files
committed
group constant folding related tests together in TestTransforms
1 parent 7b94894 commit d5ce5ec

File tree

1 file changed

+115
-114
lines changed

1 file changed

+115
-114
lines changed

Lib/test/test_peepholer.py

Lines changed: 115 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -235,120 +235,6 @@ def g(a):
235235
self.assertTrue(g(4))
236236
self.check_lnotab(g)
237237

238-
def test_elim_extra_return(self):
239-
# RETURN LOAD_CONST None RETURN --> RETURN
240-
def f(x):
241-
return x
242-
self.assertNotInBytecode(f, 'LOAD_CONST', None)
243-
returns = [instr for instr in dis.get_instructions(f)
244-
if instr.opname == 'RETURN_VALUE']
245-
self.assertEqual(len(returns), 1)
246-
self.check_lnotab(f)
247-
248-
def test_elim_jump_to_return(self):
249-
# JUMP_FORWARD to RETURN --> RETURN
250-
def f(cond, true_value, false_value):
251-
# Intentionally use two-line expression to test issue37213.
252-
return (true_value if cond
253-
else false_value)
254-
self.check_jump_targets(f)
255-
self.assertNotInBytecode(f, 'JUMP_FORWARD')
256-
self.assertNotInBytecode(f, 'JUMP_BACKWARD')
257-
returns = [instr for instr in dis.get_instructions(f)
258-
if instr.opname == 'RETURN_VALUE']
259-
self.assertEqual(len(returns), 2)
260-
self.check_lnotab(f)
261-
262-
def test_elim_jump_to_uncond_jump(self):
263-
# POP_JUMP_IF_FALSE to JUMP_FORWARD --> POP_JUMP_IF_FALSE to non-jump
264-
def f():
265-
if a:
266-
# Intentionally use two-line expression to test issue37213.
267-
if (c
268-
or d):
269-
foo()
270-
else:
271-
baz()
272-
self.check_jump_targets(f)
273-
self.check_lnotab(f)
274-
275-
def test_elim_jump_to_uncond_jump2(self):
276-
# POP_JUMP_IF_FALSE to JUMP_BACKWARD --> POP_JUMP_IF_FALSE to non-jump
277-
def f():
278-
while a:
279-
# Intentionally use two-line expression to test issue37213.
280-
if (c
281-
or d):
282-
a = foo()
283-
self.check_jump_targets(f)
284-
self.check_lnotab(f)
285-
286-
def test_elim_jump_to_uncond_jump3(self):
287-
# Intentionally use two-line expressions to test issue37213.
288-
# POP_JUMP_IF_FALSE to POP_JUMP_IF_FALSE --> POP_JUMP_IF_FALSE to non-jump
289-
def f(a, b, c):
290-
return ((a and b)
291-
and c)
292-
self.check_jump_targets(f)
293-
self.check_lnotab(f)
294-
self.assertEqual(count_instr_recursively(f, 'POP_JUMP_IF_FALSE'), 2)
295-
# POP_JUMP_IF_TRUE to POP_JUMP_IF_TRUE --> POP_JUMP_IF_TRUE to non-jump
296-
def f(a, b, c):
297-
return ((a or b)
298-
or c)
299-
self.check_jump_targets(f)
300-
self.check_lnotab(f)
301-
self.assertEqual(count_instr_recursively(f, 'POP_JUMP_IF_TRUE'), 2)
302-
# JUMP_IF_FALSE_OR_POP to JUMP_IF_TRUE_OR_POP --> POP_JUMP_IF_FALSE to non-jump
303-
def f(a, b, c):
304-
return ((a and b)
305-
or c)
306-
self.check_jump_targets(f)
307-
self.check_lnotab(f)
308-
self.assertEqual(count_instr_recursively(f, 'POP_JUMP_IF_FALSE'), 1)
309-
self.assertEqual(count_instr_recursively(f, 'POP_JUMP_IF_TRUE'), 1)
310-
# POP_JUMP_IF_TRUE to POP_JUMP_IF_FALSE --> POP_JUMP_IF_TRUE to non-jump
311-
def f(a, b, c):
312-
return ((a or b)
313-
and c)
314-
self.check_jump_targets(f)
315-
self.check_lnotab(f)
316-
self.assertEqual(count_instr_recursively(f, 'POP_JUMP_IF_FALSE'), 1)
317-
self.assertEqual(count_instr_recursively(f, 'POP_JUMP_IF_TRUE'), 1)
318-
319-
def test_elim_jump_to_uncond_jump4(self):
320-
def f():
321-
for i in range(5):
322-
if i > 3:
323-
print(i)
324-
self.check_jump_targets(f)
325-
326-
def test_elim_jump_after_return1(self):
327-
# Eliminate dead code: jumps immediately after returns can't be reached
328-
def f(cond1, cond2):
329-
if cond1: return 1
330-
if cond2: return 2
331-
while 1:
332-
return 3
333-
while 1:
334-
if cond1: return 4
335-
return 5
336-
return 6
337-
self.assertNotInBytecode(f, 'JUMP_FORWARD')
338-
self.assertNotInBytecode(f, 'JUMP_BACKWARD')
339-
returns = [instr for instr in dis.get_instructions(f)
340-
if instr.opname == 'RETURN_VALUE']
341-
self.assertLessEqual(len(returns), 6)
342-
self.check_lnotab(f)
343-
344-
def test_make_function_doesnt_bail(self):
345-
def f():
346-
def g()->1+1:
347-
pass
348-
return g
349-
self.assertNotInBytecode(f, 'BINARY_OP')
350-
self.check_lnotab(f)
351-
352238
def test_constant_folding_small_int(self):
353239
tests = [
354240
('(0, )[0]', 0),
@@ -485,6 +371,7 @@ def test_constant_folding_binop(self):
485371
('("a" * 10)[10]', 'NB_SUBSCR', False, None, None),
486372
('(1, (1, 2))[2:6][0][2-1]', 'NB_SUBSCR', False, None, None),
487373
]
374+
488375
for (
489376
expr,
490377
nb_op,
@@ -617,6 +504,120 @@ def test_constant_folding_remove_nop_location(self):
617504
code = compile(textwrap.dedent(source), '', 'single')
618505
self.assertNotInBytecode(code, 'NOP')
619506

507+
def test_elim_extra_return(self):
508+
# RETURN LOAD_CONST None RETURN --> RETURN
509+
def f(x):
510+
return x
511+
self.assertNotInBytecode(f, 'LOAD_CONST', None)
512+
returns = [instr for instr in dis.get_instructions(f)
513+
if instr.opname == 'RETURN_VALUE']
514+
self.assertEqual(len(returns), 1)
515+
self.check_lnotab(f)
516+
517+
def test_elim_jump_to_return(self):
518+
# JUMP_FORWARD to RETURN --> RETURN
519+
def f(cond, true_value, false_value):
520+
# Intentionally use two-line expression to test issue37213.
521+
return (true_value if cond
522+
else false_value)
523+
self.check_jump_targets(f)
524+
self.assertNotInBytecode(f, 'JUMP_FORWARD')
525+
self.assertNotInBytecode(f, 'JUMP_BACKWARD')
526+
returns = [instr for instr in dis.get_instructions(f)
527+
if instr.opname == 'RETURN_VALUE']
528+
self.assertEqual(len(returns), 2)
529+
self.check_lnotab(f)
530+
531+
def test_elim_jump_to_uncond_jump(self):
532+
# POP_JUMP_IF_FALSE to JUMP_FORWARD --> POP_JUMP_IF_FALSE to non-jump
533+
def f():
534+
if a:
535+
# Intentionally use two-line expression to test issue37213.
536+
if (c
537+
or d):
538+
foo()
539+
else:
540+
baz()
541+
self.check_jump_targets(f)
542+
self.check_lnotab(f)
543+
544+
def test_elim_jump_to_uncond_jump2(self):
545+
# POP_JUMP_IF_FALSE to JUMP_BACKWARD --> POP_JUMP_IF_FALSE to non-jump
546+
def f():
547+
while a:
548+
# Intentionally use two-line expression to test issue37213.
549+
if (c
550+
or d):
551+
a = foo()
552+
self.check_jump_targets(f)
553+
self.check_lnotab(f)
554+
555+
def test_elim_jump_to_uncond_jump3(self):
556+
# Intentionally use two-line expressions to test issue37213.
557+
# POP_JUMP_IF_FALSE to POP_JUMP_IF_FALSE --> POP_JUMP_IF_FALSE to non-jump
558+
def f(a, b, c):
559+
return ((a and b)
560+
and c)
561+
self.check_jump_targets(f)
562+
self.check_lnotab(f)
563+
self.assertEqual(count_instr_recursively(f, 'POP_JUMP_IF_FALSE'), 2)
564+
# POP_JUMP_IF_TRUE to POP_JUMP_IF_TRUE --> POP_JUMP_IF_TRUE to non-jump
565+
def f(a, b, c):
566+
return ((a or b)
567+
or c)
568+
self.check_jump_targets(f)
569+
self.check_lnotab(f)
570+
self.assertEqual(count_instr_recursively(f, 'POP_JUMP_IF_TRUE'), 2)
571+
# JUMP_IF_FALSE_OR_POP to JUMP_IF_TRUE_OR_POP --> POP_JUMP_IF_FALSE to non-jump
572+
def f(a, b, c):
573+
return ((a and b)
574+
or c)
575+
self.check_jump_targets(f)
576+
self.check_lnotab(f)
577+
self.assertEqual(count_instr_recursively(f, 'POP_JUMP_IF_FALSE'), 1)
578+
self.assertEqual(count_instr_recursively(f, 'POP_JUMP_IF_TRUE'), 1)
579+
# POP_JUMP_IF_TRUE to POP_JUMP_IF_FALSE --> POP_JUMP_IF_TRUE to non-jump
580+
def f(a, b, c):
581+
return ((a or b)
582+
and 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+
588+
def test_elim_jump_to_uncond_jump4(self):
589+
def f():
590+
for i in range(5):
591+
if i > 3:
592+
print(i)
593+
self.check_jump_targets(f)
594+
595+
def test_elim_jump_after_return1(self):
596+
# Eliminate dead code: jumps immediately after returns can't be reached
597+
def f(cond1, cond2):
598+
if cond1: return 1
599+
if cond2: return 2
600+
while 1:
601+
return 3
602+
while 1:
603+
if cond1: return 4
604+
return 5
605+
return 6
606+
self.assertNotInBytecode(f, 'JUMP_FORWARD')
607+
self.assertNotInBytecode(f, 'JUMP_BACKWARD')
608+
returns = [instr for instr in dis.get_instructions(f)
609+
if instr.opname == 'RETURN_VALUE']
610+
self.assertLessEqual(len(returns), 6)
611+
self.check_lnotab(f)
612+
613+
def test_make_function_doesnt_bail(self):
614+
def f():
615+
def g()->1+1:
616+
pass
617+
return g
618+
self.assertNotInBytecode(f, 'BINARY_OP')
619+
self.check_lnotab(f)
620+
620621
def test_in_literal_list(self):
621622
def containtest():
622623
return x in [a, b]

0 commit comments

Comments
 (0)