]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Merged revisions 70344 via svnmerge from
authorAntoine Pitrou <solipsis@pitrou.net>
Fri, 13 Mar 2009 20:38:56 +0000 (20:38 +0000)
committerAntoine Pitrou <solipsis@pitrou.net>
Fri, 13 Mar 2009 20:38:56 +0000 (20:38 +0000)
svn+ssh://pythondev@svn.python.org/python/branches/py3k

........
  r70344 | antoine.pitrou | 2009-03-13 20:25:20 +0100 (ven., 13 mars 2009) | 4 lines

  Issue #5392: when a very low recursion limit was set, the interpreter would
  abort with a fatal error after the recursion limit was hit twice.
........

Include/ceval.h
Lib/test/test_sys.py
Misc/NEWS

index 919c4946016fb26690e4bd52fda769569e1b51b7..7bd8179e7680a4e5ac589d8c17ce53cf8d372f05 100644 (file)
@@ -92,11 +92,10 @@ PyAPI_DATA(int) _Py_CheckRecursionLimit;
 #  define _Py_MakeRecCheck(x)  (++(x) > _Py_CheckRecursionLimit)
 #endif
 
-#ifdef USE_STACKCHECK
-#  define _Py_MakeEndRecCheck(x)  (--(x) < _Py_CheckRecursionLimit - 50)
-#else
-#  define _Py_MakeEndRecCheck(x)  (--(x) < _Py_CheckRecursionLimit - 50)
-#endif
+#define _Py_MakeEndRecCheck(x) \
+       (--(x) < ((_Py_CheckRecursionLimit > 100) \
+               ? (_Py_CheckRecursionLimit - 50) \
+               : (3 * (_Py_CheckRecursionLimit >> 2))))
 
 #define Py_ALLOW_RECURSION \
   do { unsigned char _old = PyThreadState_GET()->recursion_critical;\
index 7655fbf05fed75b7715b689b7da929689709da7b..353ed8810e162cb171c509b5b9f2bd55aa0ab43a 100644 (file)
@@ -2,6 +2,8 @@
 import unittest, test.support
 import sys, io, os
 import struct
+import subprocess
+import textwrap
 
 class SysModuleTest(unittest.TestCase):
 
@@ -155,6 +157,46 @@ class SysModuleTest(unittest.TestCase):
         self.assertEqual(sys.getrecursionlimit(), 10000)
         sys.setrecursionlimit(oldlimit)
 
+    def test_recursionlimit_recovery(self):
+        # NOTE: this test is slightly fragile in that it depends on the current
+        # recursion count when executing the test being low enough so as to
+        # trigger the recursion recovery detection in the _Py_MakeEndRecCheck
+        # macro (see ceval.h).
+        oldlimit = sys.getrecursionlimit()
+        def f():
+            f()
+        try:
+            for i in (50, 1000):
+                # Issue #5392: stack overflow after hitting recursion limit twice
+                sys.setrecursionlimit(i)
+                self.assertRaises(RuntimeError, f)
+                self.assertRaises(RuntimeError, f)
+        finally:
+            sys.setrecursionlimit(oldlimit)
+
+    def test_recursionlimit_fatalerror(self):
+        # A fatal error occurs if a second recursion limit is hit when recovering
+        # from a first one.
+        code = textwrap.dedent("""
+            import sys
+
+            def f():
+                try:
+                    f()
+                except RuntimeError:
+                    f()
+
+            sys.setrecursionlimit(%d)
+            f()""")
+        for i in (50, 1000):
+            sub = subprocess.Popen([sys.executable, '-c', code % i],
+                stderr=subprocess.PIPE)
+            err = sub.communicate()[1]
+            self.assertTrue(sub.returncode, sub.returncode)
+            self.assertTrue(
+                b"Fatal Python error: Cannot recover from stack overflow" in err,
+                err)
+
     def test_getwindowsversion(self):
         if hasattr(sys, "getwindowsversion"):
             v = sys.getwindowsversion()
index 3734db4bc6634b72bd7469101797d7ee281e1130..97b8fd1d9720b849f245956017b111369fcfa9d9 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,9 @@ What's New in Python 3.0.2?
 Core and Builtins
 -----------------
 
+- Issue #5392: when a very low recursion limit was set, the interpreter would
+  abort with a fatal error after the recursion limit was hit twice.
+
 - Issue #5247: Improve error message when unknown format codes are
   used when using str.format() with str, int, and float arguments.