From the documentation:
sys.getrecursionlimit()
Return the current value of the recursion limit, the maximum depth of the Python interpreter stack. This limit prevents infinite recursion from causing an overflow of the C stack and crashing Python. It can be set by setrecursionlimit().
I am currently hitting the recursion limit when pickling an object. The object I am pickling only has a few levels of nesting, so I am a bit puzzled by what is happening.
I have been able to circumvent the issue with the following hack:
try:
return pickle.dumps(x)
except:
try:
recursionlimit = getrecursionlimit()
setrecursionlimit(2*recursionlimit)
dumped = pickle.dumps(x)
setrecursionlimit(recursionlimit)
return dumped
except:
raise
Testing the above snippet on different contexts sometimes leads to success on the first try
, and sometimes it leads to success on the second try
. So far I have not been able to make it raise
the exception.
To further debug my issue it would be helpful to have a way to obtain the current depth of the stack. That would allow me to verify if the entering stack depth is determining whether the snippet above will succeed on the first try
or on the second.
Does the standard library provide a function to get the depth of the stack, or if not, how can I obtain it?
def get_stack_depth():
# what goes here?
You can see the whole call stack from inspect.stack()
, so currently taken depth would be len(inspect.stack())
.
On the other hand, I guess you got the complete stack printed out when "maximum recursion depth exceeded" exception was raised. That stack trace should show you exactly what went wrong.
If speed is an issue, it's way faster to bypass inspect module.
def get_stack_size():
"""Get stack size for caller's frame.
%timeit len(inspect.stack())
8.86 ms ± 42.5 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
%timeit get_stack_size()
4.17 µs ± 11.5 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
"""
size = 2 # current frame and caller's frame always exist
while True:
try:
sys._getframe(size)
size += 1
except ValueError:
return size - 1 # subtract current frame
来源:https://stackoverflow.com/questions/34115298/how-do-i-get-the-current-depth-of-the-python-interpreter-stack