]> granicus.if.org Git - python/commitdiff
Issue #21523: Fix over-pessimistic computation of the stack effect of some opcodes...
authorAntoine Pitrou <solipsis@pitrou.net>
Fri, 23 May 2014 09:46:03 +0000 (11:46 +0200)
committerAntoine Pitrou <solipsis@pitrou.net>
Fri, 23 May 2014 09:46:03 +0000 (11:46 +0200)
This also fixes a quadratic compilation time issue noticeable when compiling
code with a large number of "and" and "or" operators.

Lib/test/test_compile.py
Misc/NEWS
Python/compile.c

index 4bcb43840853aa00dd560d642645c16d21f04948..ec2709dbd1c706b9a400b07016da962bdb31e3ad 100644 (file)
@@ -1,3 +1,4 @@
+import math
 import unittest
 import sys
 import _ast
@@ -519,8 +520,46 @@ if 1:
         self.assertRaises(TypeError, compile, ast, '<ast>', 'exec')
 
 
+class TestStackSize(unittest.TestCase):
+    # These tests check that the computed stack size for a code object
+    # stays within reasonable bounds (see issue #21523 for an example
+    # dysfunction).
+    N = 100
+
+    def check_stack_size(self, code):
+        # To assert that the alleged stack size is not O(N), we
+        # check that it is smaller than log(N).
+        if isinstance(code, str):
+            code = compile(code, "<foo>", "single")
+        max_size = math.ceil(math.log(len(code.co_code)))
+        self.assertLessEqual(code.co_stacksize, max_size)
+
+    def test_and(self):
+        self.check_stack_size("x and " * self.N + "x")
+
+    def test_or(self):
+        self.check_stack_size("x or " * self.N + "x")
+
+    def test_and_or(self):
+        self.check_stack_size("x and x or " * self.N + "x")
+
+    def test_chained_comparison(self):
+        self.check_stack_size("x < " * self.N + "x")
+
+    def test_if_else(self):
+        self.check_stack_size("x if x else " * self.N + "x")
+
+    def test_binop(self):
+        self.check_stack_size("x + " * self.N + "x")
+
+    def test_func_and(self):
+        code = "def f(x):\n"
+        code += "   x and x\n" * self.N
+        self.check_stack_size(code)
+
+
 def test_main():
-    test_support.run_unittest(TestSpecifics)
+    test_support.run_unittest(__name__)
 
 if __name__ == "__main__":
-    test_main()
+    unittest.main()
index 51e1726f71bc536554c3627da05462593ab381c4..ac9bc3693056bdd6406af17d05d14aec3b089f4d 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,11 @@ What's New in Python 2.7.8?
 Core and Builtins
 -----------------
 
+- Issue #21523: Fix over-pessimistic computation of the stack effect of
+  some opcodes in the compiler.  This also fixes a quadratic compilation
+  time issue noticeable when compiling code with a large number of "and"
+  and "or" operators.
+
 Library
 -------
 
index 8354c75b393222317e6cb2fa2a30475eb2a0bb17..1cf53f95ac615958835f5af27a14715393cfc157 100644 (file)
@@ -3483,12 +3483,16 @@ stackdepth_walk(struct compiler *c, basicblock *b, int depth, int maxdepth)
             target_depth = depth;
             if (instr->i_opcode == FOR_ITER) {
                 target_depth = depth-2;
-            } else if (instr->i_opcode == SETUP_FINALLY ||
-                       instr->i_opcode == SETUP_EXCEPT) {
+            }
+            else if (instr->i_opcode == SETUP_FINALLY ||
+                     instr->i_opcode == SETUP_EXCEPT) {
                 target_depth = depth+3;
                 if (target_depth > maxdepth)
                     maxdepth = target_depth;
             }
+            else if (instr->i_opcode == JUMP_IF_TRUE_OR_POP ||
+                     instr->i_opcode == JUMP_IF_FALSE_OR_POP)
+                depth = depth - 1;
             maxdepth = stackdepth_walk(c, instr->i_target,
                                        target_depth, maxdepth);
             if (instr->i_opcode == JUMP_ABSOLUTE ||