Hitting RecursionError but not at getrecursionlevel


I was trying to confirm the recursion depth limit, but I have not been able to on v3. It did work on v2. I looked in the issue tracker and have not found anything. Can anyone see something wrong with my example? Or know of some issue? Else will submit it (or submit my lack of understanding). Thank you.

% python3 -V
Python 3.10.6
# on mac
import sys


def adder(i):
  if(i <= sys.getrecursionlimit()):
    return adder(i+1)

% python3 code.py 
Traceback (most recent call last):
  File "code.py", line 10, in <module>
  File "code.py", line 8, in adder
    return adder(i+1)
  File "code.py", line 8, in adder
    return adder(i+1)
  File "code.py", line 8, in adder
    return adder(i+1)
  [Previous line repeated 2 more times]
  File "code.py", line 7, in adder
RecursionError: maximum recursion depth exceeded while calling a Python object

I don’t think that it is a documented language feature that if you set the recursion limit to N you can make exactly N nested function calls.

For what its worth, when I try your code in Python 3.3 and 3.5, it prints 1 through 7 before the RecursionError; in 3.7 and 3.10 it prints 1 through 6.

I am curious why the actual limit seems to be a few less than N, but the precise limit seems like an implementation detail.

@steven.daprano I agree. I suspect CPython vs X vs Y will all potentially do something a little unique. I just couldn’t stop digging when 2.z worked (exact count) but 3.y did not! However, I feel placated now, thank you!