"""
recursion_test.py (Python 2.7.5)
How low can we go? Apparently between 16000 and 32000 levels down.
Running on OS X 2.66GHz intel core i7 with 8GB RAM
$ python recursion_test.py
-- exception --
recursion_limit is default
max_depth_achieved is 998
exception type '<type 'exceptions.RuntimeError'>'
exception args '('maximum recursion depth exceeded',)'
-- exception --
recursion_limit is 2000
max_depth_achieved is 1997
exception type '<type 'exceptions.RuntimeError'>'
exception args '('maximum recursion depth exceeded',)'
-- exception --
recursion_limit is 4000
max_depth_achieved is 3996
exception type '<type 'exceptions.RuntimeError'>'
exception args '('maximum recursion depth exceeded',)'
-- exception --
recursion_limit is 8000
max_depth_achieved is 7995
exception type '<type 'exceptions.RuntimeError'>'
exception args '('maximum recursion depth exceeded',)'
-- exception --
recursion_limit is 16000
max_depth_achieved is 15994
exception type '<type 'exceptions.RuntimeError'>'
exception args '('maximum recursion depth exceeded',)'
Segmentation fault: 11
Jim Mahoney | cs.marlboro.edu | MIT License | Feb 2014
"""
import sys
max_depth_achieved = 0
recursion_limit = 'default'
def increase_recursion_limit():
global recursion_limit
if recursion_limit == 'default':
recursion_limit = 2000
else:
recursion_limit *= 2
sys.setrecursionlimit(recursion_limit)
def recur(depth):
global max_depth_achieved
max_depth_achieved = depth
recur(depth + 1)
def doit():
try:
recur(1)
except Exception as e:
print "-- exception --"
print "recursion_limit is {}".format(recursion_limit)
print "max_depth_achieved is {}".format(max_depth_achieved)
print "exception type '{}'".format(type(e))
print "exception args '{}'".format(e.args)
print
increase_recursion_limit()
doit()
doit()
syntax highlighted by Code2HTML, v. 0.93pm6