Here is a better test:
n = 0
def test_recursion_limit():
def call():
global n
n += 1
call()
try:
call()
except RuntimeError:
print(n)
test_recursion_limit()
If you put it in spam.py
and execute that, it should return 998 for both python2 and python3. It's one stack frame short because of the initial test_recursion_limit
frame.
If you're running in a REPL such as ipython, you are already inside a few frames, so you will see a lower count - it's not that the recursion limit is undershot, it's that the implementation of the REPL itself uses some stack frames.
>>> # freshly opened ipython session
>>> import inspect
>>> len(inspect.stack())
10
You can check the current recursion limit by calling sys.getrecursionlimit()
function. The default value of 1000 is chosen as a sensible default, it's a safeguard against eclipsing system resources when you accidentally execute an infinitely recursive call. That's very easy to do when mucking around with custom __getattr__
implementations, for example.
If you're blowing the stack legitimately and you need to increase the limit, it can be modified with sys.setrecursionlimit
.