[Python-checkins] [3.7] bpo-37269: Correctly optimise conditionals with constant booleans (GH-14071) (GH-14073)
Pablo Galindo
webhook-mailer at python.org
Fri Jun 14 02:18:55 EDT 2019
https://github.com/python/cpython/commit/5292179afc6fd0dc533add054d4790773c9766d0
commit: 5292179afc6fd0dc533add054d4790773c9766d0
branch: 3.7
author: Pablo Galindo <Pablogsal at gmail.com>
committer: GitHub <noreply at github.com>
date: 2019-06-14T07:18:51+01:00
summary:
[3.7] bpo-37269: Correctly optimise conditionals with constant booleans (GH-14071) (GH-14073)
Fix a regression introduced by af8646c8054d0f4180a2013383039b6a472f9698 that was causing code of the form:
if True and False:
do_something()
to be optimized incorrectly, eliminating the block..
(cherry picked from commit 05f831865545b08c9a21cfb7773af58b76ec64cb)
Co-authored-by: Pablo Galindo <Pablogsal at gmail.com>
files:
A Misc/NEWS.d/next/Core and Builtins/2019-06-14-06-32-33.bpo-37269.SjVVAe.rst
M Lib/test/test_peepholer.py
M Python/peephole.c
diff --git a/Lib/test/test_peepholer.py b/Lib/test/test_peepholer.py
index 0cc1e92907b5..1a031544fc01 100644
--- a/Lib/test/test_peepholer.py
+++ b/Lib/test/test_peepholer.py
@@ -311,6 +311,13 @@ def test_constant_folding(self):
self.assertFalse(instr.opname.startswith('BINARY_'))
self.assertFalse(instr.opname.startswith('BUILD_'))
+ 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 000000000000..b9b79066774f
--- /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 1ae62fa39a27..f1b71ed1a730 100644
--- a/Python/peephole.c
+++ b/Python/peephole.c
@@ -313,6 +313,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);
More information about the Python-checkins
mailing list