diff options
-rw-r--r-- | Lib/test/test_peepholer.py | 7 | ||||
-rw-r--r-- | Misc/NEWS.d/next/Core and Builtins/2019-06-14-06-32-33.bpo-37269.SjVVAe.rst | 2 | ||||
-rw-r--r-- | Python/peephole.c | 5 |
3 files changed, 14 insertions, 0 deletions
diff --git a/Lib/test/test_peepholer.py b/Lib/test/test_peepholer.py index 860ceeb..5d00240 100644 --- a/Lib/test/test_peepholer.py +++ b/Lib/test/test_peepholer.py @@ -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): diff --git a/Misc/NEWS.d/next/Core and Builtins/2019-06-14-06-32-33.bpo-37269.SjVVAe.rst b/Misc/NEWS.d/next/Core and Builtins/2019-06-14-06-32-33.bpo-37269.SjVVAe.rst new file mode 100644 index 0000000..b9b7906 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2019-06-14-06-32-33.bpo-37269.SjVVAe.rst @@ -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. diff --git a/Python/peephole.c b/Python/peephole.c index 6f3e2ed..d7b1dfc 100644 --- a/Python/peephole.c +++ b/Python/peephole.c @@ -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); |