Python 异常后,启动启动debug调试

这里需要修改python在异常发生后的处理流程
 
sys.excepthook 是python系统的异常处理器,重写一个自定义处理器,并在打印异常信息后,启动pdb。
 
import sys
def info(type, value, tb):
    # 异常类型
    # 异常值
    # 调用栈
    if hasattr(sys, 'ps1') or not sys.stderr.isatty():
        # we are in interactive mode or we don't have a tty-like
        # device, so we call the default hook
        sys.__excepthook__(type, value, tb)
    else:
        import traceback, pdb
        # we are NOT in interactive mode, print the exception...
        traceback.print_exception(type, value, tb)
        print
        # ...then start the debugger in post-mortem mode.
        pdb.post_mortem(tb)
sys.excepthook = info

 

只要在代码里,开始import 这个自定义debug module,就可以修改掉系统默认的异常处理过程  __excepthook__
 

----------------------------
关注微信公众号号:
挖金矿工:goldminer1024
为您的量化投资理想插上翅膀
goldminer1024
posted @ 2017-12-17 09:31  swiftcore  阅读(530)  评论(0编辑  收藏  举报