How do I get the current depth of the Python interpreter stack?

巧了我就是萌 提交于 2019-11-28 07:58:52

问题


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?

回答1:


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.




回答2:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!