码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2017-12-17 11:09:57      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:err   修改   公众   default   tde   exce   ges   gpo   ace   

这里需要修改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
为您的量化投资理想插上翅膀
技术分享图片

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

标签:err   修改   公众   default   tde   exce   ges   gpo   ace   

原文地址:http://www.cnblogs.com/swiftcore/p/8051401.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!