Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Lib/test/test_peepholer.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,13 @@ def forloop():
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):

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix a bug in the peephole optimizer that was not treating correctly constant
conditions with binary operators. Patch by Pablo Galindo.
5 changes: 5 additions & 0 deletions Python/peephole.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down