diff options
Diffstat (limited to 'Lib/test/test_compile.py')
-rw-r--r-- | Lib/test/test_compile.py | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/Lib/test/test_compile.py b/Lib/test/test_compile.py index 56f73f6315..9d77f7af05 100644 --- a/Lib/test/test_compile.py +++ b/Lib/test/test_compile.py @@ -697,6 +697,40 @@ if 1: # complex statements. compile("if a: b\n" * 200000, "<dummy>", "exec") + # Multiple users rely on the fact that CPython does not generate + # bytecode for dead code blocks. See bpo-37500 for more context. + @support.cpython_only + def test_dead_blocks_do_not_generate_bytecode(self): + def unused_block_if(): + if 0: + return 42 + + def unused_block_while(): + while 0: + return 42 + + def unused_block_if_else(): + if 1: + return None + else: + return 42 + + def unused_block_while_else(): + while 1: + return None + else: + return 42 + + funcs = [unused_block_if, unused_block_while, + unused_block_if_else, unused_block_while_else] + + for func in funcs: + opcodes = list(dis.get_instructions(func)) + self.assertEqual(2, len(opcodes)) + self.assertEqual('LOAD_CONST', opcodes[0].opname) + self.assertEqual(None, opcodes[0].argval) + self.assertEqual('RETURN_VALUE', opcodes[1].opname) + class TestExpressionStackSize(unittest.TestCase): # These tests check that the computed stack size for a code object |