summaryrefslogtreecommitdiff
path: root/Lib/test/test_compile.py
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2019-07-29 07:47:30 -0700
committerGitHub <noreply@github.com>2019-07-29 07:47:30 -0700
commit9ea738e580f58c3d2f9b0d56561d57b9e9412973 (patch)
tree68cc99aed67b5c29da48fe0e6242f9367bb36474 /Lib/test/test_compile.py
parentcf52bd0b9b1c1b7ecdd91e1ebebd15ae5c213a2c (diff)
downloadcpython-git-9ea738e580f58c3d2f9b0d56561d57b9e9412973.tar.gz
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 <Pablogsal@gmail.com>
Diffstat (limited to 'Lib/test/test_compile.py')
-rw-r--r--Lib/test/test_compile.py34
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