import sys
class dynamic_regulation_recursion_limit:
def __init__(self, func):
self.func = func
self.Increased_limit = 1000
def __call__(self, *args, **kwargs):
while True:
try:
return self.func(*args, **kwargs)
except RecursionError:
print("x, y= ", *args)
print("stack= ", len(inspect.stack()))
print("self.recursionlimit= ", self.recursionlimit)
print()
self.recursionlimit = sys.getrecursionlimit() + self.Increased_limit
sys.setrecursionlimit(self.recursionlimit)
@dynamic_regulation_recursion_limit
def testadd(x, y):
if x==0:
print(y)
else:
return testadd(x-1, y+1)
testadd(999, 0)
the printed results are unexpected to me
x, y = 669330
stack= 665
self.recursionlimit= 1000
x, y = 335664
stack= 1333
self.recursionlimit= 2000
x, y = 2997
stack= 1999
self.recursionlimit= 3000
* * every time I RecursionError, the, len (inspect.stack ()) display is far away from the recursionlimit.
what"s going on? * *
def dynamic_regulation_recursion_limit(func):
def wrapper(*args, **kwargs):
while True:
try:
return func(*args, **kwargs)
except RecursionError:
print("x, y= ", *args)
print("stack= ", len(inspect.stack()))
print("self.recursionlimit= ", sys.getrecursionlimit())
print()
recursionlimit = 1000 + sys.getrecursionlimit()
sys.setrecursionlimit(recursionlimit)
return wrapper
the functional decorator I wrote at first
turns out to be normal
x, y = 503496
stack= 997
self.recursionlimit= 1000
x, y = 3996
stack= 1997
self.recursionlimit= 2000
rewrite to class decorator
< H1 > the output result does not understand < / H1 > stack= 665
self.recursionlimit= 1000
stack= 1333
self.recursionlimit= 2000
stack= 1999
self.recursionlimit= 3000
A rule is that an exception is thrown at the limit value 2 / p > 3.