From 9ea738e580f58c3d2f9b0d56561d57b9e9412973 Mon Sep 17 00:00:00 2001 From: "Miss Islington (bot)" <31488909+miss-islington@users.noreply.github.com> Date: Mon, 29 Jul 2019 07:47:30 -0700 Subject: bpo-37500: Make sure dead code does not generate bytecode but also detect syntax errors (GH-14612) https://bugs.python.org/issue37500 Add a new field to the compiler structure that allows to be configured so no bytecode is emitted. In this way is possible to detect errors by walking the nodes while preserving optimizations. https://bugs.python.org/issue37500 (cherry picked from commit 18c5f9d44dde37c0fae5585a604c6027825252d2) Co-authored-by: Pablo Galindo --- Lib/test/test_compile.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'Lib/test/test_compile.py') 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, "", "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 -- cgit v1.2.1