Why does Python allow more recursion depth than the limit when running using IDLE's shell?

Viewed 105

Python seems to allow more recursion depth than the actual limit defined (default 1000, here it is 20 to make it easier), when the script is run using IDLE shell 3.9.1 (coding the script in IDLE and then pressing F5), on Python 3.9.1 (tags/v3.9.1:1e5d33e, Dec 7 2020, 17:08:21) [MSC v.1927 64 bit (AMD64)] on win32 (Windows 10 1909, build 18363.1316).

With the code:

import sys, inspect
sys.setrecursionlimit(20)
print("Recursion limit:", sys.getrecursionlimit()) #ouputs 20

recursionCounter = 0

def recursive():
    global recursionCounter
    recursionCounter += 1
    print("stack:", len(inspect.stack())) #starts at 5 (IDLE is adding some nested loops) but stops at 36, higher than the recursion limit 
    recursive()

          
try:
    recursive()
except RecursionError:
    print("RecursionError raised. recursionCounter value is:" , recursionCounter) #outputs 33

The full output is:

Recursion limit: 20
stack: 5
stack: 6
stack: 7
(...)
stack: 35
stack: 36
RecursionError raised. recursionCounter value is: 33

Same thing with a lowered or increased limit, whatever the recursion limit is set to, Python goes deeper than it, exceeding it by the same amount (13 here).

Is there something special with recursion limits or RecursionError that I missed and that can explain this effect?

0 Answers
Related