What is the simplest way of using Python pdb to inspect the cause of an unhandled exception?

一笑奈何 提交于 2019-12-05 13:38:20

Wrap it with that:

def debug_on(*exceptions):
    if not exceptions:
        exceptions = (AssertionError, )
    def decorator(f):
        @functools.wraps(f)
        def wrapper(*args, **kwargs):
            try:
                return f(*args, **kwargs)
            except exceptions:
                pdb.post_mortem(sys.exc_info()[2])
        return wrapper
    return decorator

Example:

@debug_on(TypeError)
def buggy_function()
    ....
    raise TypeError
AKX

The unittest superset nose has an option that drops you to pdb when a test fails, if it's okay for you to use nose as your test runner:

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