mirror of
https://github.com/python/cpython.git
synced 2025-08-30 21:48:47 +00:00
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
This commit is contained in:
parent
cd6e83b481
commit
18c5f9d44d
4 changed files with 174 additions and 18 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue