Commit 05f83186 authored by Pablo Galindo's avatar Pablo Galindo Committed by GitHub

bpo-37269: Correctly optimise conditionals with constant booleans (GH-14071)

Fix a regression introduced by af8646c8 that was causing code of the form:

if True and False:
   do_something()

to be optimized incorrectly, eliminating the block.
parent d0eeb936
......@@ -414,6 +414,13 @@ class TestTranforms(BytecodeTestCase):
pass
self.assertEqual(count_instr_recursively(forloop, 'BUILD_LIST'), 0)
def test_condition_with_binop_with_bools(self):
def f():
if True or False:
return 1
return 0
self.assertEqual(f(), 1)
class TestBuglets(unittest.TestCase):
......
Fix a bug in the peephole optimizer that was not treating correctly constant
conditions with binary operators. Patch by Pablo Galindo.
......@@ -315,6 +315,11 @@ PyCode_Optimize(PyObject *code, PyObject* consts, PyObject *names,
fill_nops(codestr, op_start, nexti + 1);
cumlc = 0;
} else if (is_true == 0) {
if (i > 1 &&
(_Py_OPCODE(codestr[i - 1]) == POP_JUMP_IF_TRUE ||
_Py_OPCODE(codestr[i - 1]) == POP_JUMP_IF_FALSE)) {
break;
}
h = get_arg(codestr, nexti) / sizeof(_Py_CODEUNIT);
tgt = find_op(codestr, codelen, h);
fill_nops(codestr, op_start, tgt);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment