2

Apparently I'm allowed to violate the recursion limit after all. It's 1000, but I can do 1020 no problem:

>>> import sys
>>> sys.getrecursionlimit()
1000
>>> def f(depth):
        if depth:
            f(depth - 1)

>>> f(1020)
>>>

Seems 1025 is the actual limit:

>>> import sys
>>> sys.getrecursionlimit()
1000
>>> depth = 0
>>> def f():
        global depth
        depth += 1
        f()

>>> f()
Traceback (most recent call last):
  File "<pyshell#8>", line 1, in <module>
    f()
  File "<pyshell#7>", line 4, in f
    f()
  File "<pyshell#7>", line 4, in f
    f()
  File "<pyshell#7>", line 4, in f
    f()
  [Previous line repeated 1022 more times]
RecursionError: maximum recursion depth exceeded
>>> depth
1025

Why am I allowed to exceed the limit?

I'm using CPython 3.8.5 32 bit on Windows 10 64 bit.

Update: I thought maybe I have an off-by-1 misinterpretation and it's actually 1024 = 210. So I guessed maybe setting the limit to 2000 would allow 2048. But no, it's always the same constant number of extra recursions allowed:

>>> for limit in range(1000, 10001, 1000):
        sys.setrecursionlimit(limit)
        depth = 0
        try:
            f()
        except:
            print(limit, '=>', depth)

1000 => 1025
2000 => 2025
3000 => 3025
4000 => 4025
5000 => 5025
6000 => 6025

================================ RESTART: Shell ================================
>>>

Note that trying limit 7000 crashed so badly that the shell got restarted.

superb rain
  • 5,300
  • 2
  • 11
  • 25
  • It's wierd. My recursion limit is 3000, but it runs for 2961 recursive loops. – Rahul Vishwakarma Aug 10 '20 at 12:46
  • I disagree with the duplication. That other question is about not even reaching the limit but failing at 970, and the answers explain it with other things already being on the stack. That doesn't explain why I can **exceed** the limit. – superb rain Aug 10 '20 at 12:52
  • 2
    The accepted answer explains it very clearly in the final paragraph: i.e. Python adds a little overhead so you can handle the exception gracefully. See also [this comment](https://stackoverflow.com/questions/38265839/max-recursion-is-not-exactly-what-sys-getrecursionlimit-claims-how-come#comment63954704_38265931). – ekhumoro Aug 10 '20 at 12:54
  • @ekhumoro Ah, right. Bit hidden. – superb rain Aug 10 '20 at 12:55
  • Agreed. I was just about to add a comment to make things clearer, but you beat me to it :) – ekhumoro Aug 10 '20 at 13:01

0 Answers0