The question is: What is the difference between eval() and another functions which don't so massively fill the call stack?
That eval()
and exec()
seem to differ from the other builtin functions and own functions in relation to stack space need is stated already in the question, so an answer that it is because it is that way is confirmation of the fact, but not an explanation why?
I am aware of Why Python raises RecursionError before it exceeds the real recursion limit? question, but the answers there don't apply to following case:
from sys import getrecursionlimit
print(f'sys: maxRecursionDepth = {getrecursionlimit()}')
cnt = 0
def f3(s):
global cnt
cnt += 1
eval(s)
# ---
try:
f3('f3(s)')
except RecursionError:
print(f'f3() maxRecursionDepth = {cnt}')
which outputs:
sys: maxRecursionDepth = 1000
f3() maxRecursionDepth = 333
It seems that calling eval()
consumes two recursion levels before the counter in the recursive function is increased again.
I would like to know why is it that way? What is the actual deep reason for the observed behavior?
The answers in the link above don't apply here because they explain that the stack might be already used by other processes of Python and therefore the counter does not count up to the recursion limit. This does not explain: What is the difference between eval()/exec() and another functions which don't so massively fill the call stack? I would expect a 499 or similar as result, but not 333 out of 1000.