[Python-checkins] Only eliminate jumps to successor block if jump is unconditional. (GH-24417)

markshannon webhook-mailer at python.org
Tue Feb 2 09:59:32 EST 2021


https://github.com/python/cpython/commit/802b645e81a72399a7ef47ef000d468c775dcd3e
commit: 802b645e81a72399a7ef47ef000d468c775dcd3e
branch: master
author: Mark Shannon <mark at hotpy.org>
committer: markshannon <mark at hotpy.org>
date: 2021-02-02T14:59:15Z
summary:

Only eliminate jumps to successor block if jump is unconditional. (GH-24417)

* Prevents elimination of the sole test of a value in statements like:
   if x or True: ...

files:
M Lib/test/test_bool.py
M Python/compile.c

diff --git a/Lib/test/test_bool.py b/Lib/test/test_bool.py
index 7b3a3859e0893..bec44d0b9c591 100644
--- a/Lib/test/test_bool.py
+++ b/Lib/test/test_bool.py
@@ -354,6 +354,22 @@ def test_real_and_imag(self):
         self.assertIs(type(False.real), int)
         self.assertIs(type(False.imag), int)
 
+    def test_bool_called_at_least_once(self):
+        class X:
+            def __init__(self):
+                self.count = 0
+            def __bool__(self):
+                self.count += 1
+                return True
+
+        def f(x):
+            if x or True:
+                pass
+
+        x = X()
+        f(x)
+        self.assertGreaterEqual(x.count, 1)
+
 def test_main():
     support.run_unittest(BoolTest)
 
diff --git a/Python/compile.c b/Python/compile.c
index 9927f5abfb964..a0a257fa6bafc 100644
--- a/Python/compile.c
+++ b/Python/compile.c
@@ -6586,27 +6586,14 @@ optimize_cfg(struct assembler *a, PyObject *consts)
     for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
         if (b->b_iused > 0) {
             struct instr *b_last_instr = &b->b_instr[b->b_iused - 1];
-            if (b_last_instr->i_opcode == POP_JUMP_IF_FALSE ||
-                b_last_instr->i_opcode == POP_JUMP_IF_TRUE ||
-                b_last_instr->i_opcode == JUMP_ABSOLUTE ||
+            if (b_last_instr->i_opcode == JUMP_ABSOLUTE ||
                 b_last_instr->i_opcode == JUMP_FORWARD) {
                 if (b_last_instr->i_target == b->b_next) {
                     assert(b->b_next->b_iused);
                     b->b_nofallthrough = 0;
-                    switch(b_last_instr->i_opcode) {
-                        case POP_JUMP_IF_FALSE:
-                        case POP_JUMP_IF_TRUE:
-                            b_last_instr->i_opcode = POP_TOP;
-                            b_last_instr->i_target = NULL;
-                            b_last_instr->i_oparg = 0;
-                            break;
-                        case JUMP_ABSOLUTE:
-                        case JUMP_FORWARD:
-                            b_last_instr->i_opcode = NOP;
-                            clean_basic_block(b, -1);
-                            maybe_empty_blocks = 1;
-                            break;
-                    }
+                    b_last_instr->i_opcode = NOP;
+                    clean_basic_block(b, -1);
+                    maybe_empty_blocks = 1;
                 }
             }
         }



More information about the Python-checkins mailing list