Is it possible to automatically break into the debugger when a exception is thrown?

前端 未结 6 543
栀梦
栀梦 2021-02-07 06:00

If ones catches an exception outside of the function it is originally thrown, ones loses access to the local stack. As a result one cannot inspect the values of the variables th

6条回答
  •  梦谈多话
    2021-02-07 06:33

    If you want to wrap only some inner part of a function, or would need to decorate multiple functions otherwise, a context manager could be used as an alternative to the accepted answer. I am now using this simple version that catches all exceptions. I would also recommend using pudb

    from contextlib import contextmanager
    
    @contextmanager
    def postmortem_pudb():
        try:
            yield
        except Exception as exc:
            pudb.post_mortem()
    

    Use like this

    with postmortem_pudb():
        function_that_might_throw_some()
        ...
        another_function_that_might_throw_some()
        ...
        yet_another_function_that_might_throw_some()
    
    

提交回复
热议问题