栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何设置sys.excepthook在python中全局调用pdb?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何设置sys.excepthook在python中全局调用pdb?

这就是你需要的

http://ynniv.com/blog/2007/11/debugging-
python.html

三种方式,第一种是简单但粗略的方法(Thomas Heller)-将以下内容添加到site-packages / sitecustomize.py中:

import pdb, sys, tracebackdef info(type, value, tb):    traceback.print_exception(type, value, tb)    pdb.pm()sys.excepthook = info

第二个更为复杂,并从菜谱中检查交互模式(奇怪地跳过了交互模式下的调试):

# pre snippet, to be included in 'sitecustomize.py'import sysdef 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.pm()sys.excepthook = info

第三个(ynniv)(除非重定向stdin或stderr,否则始终启动调试器)

# pre snippet, to be included in 'sitecustomize.py'import sysdef info(type, value, tb):   if (#hasattr(sys, "ps1") or       not sys.stderr.isatty() or        not sys.stdin.isatty()):       # stdin or stderr is redirected, just do the normal thing       original_hook(type, value, tb)   else:       # a terminal is attached and stderr is not redirected, debug        import traceback, pdb       traceback.print_exception(type, value, tb)       print       pdb.pm()       #traceback.print_stack()original_hook = sys.excepthookif sys.excepthook == sys.__excepthook__:    # if someone already patched excepthook, let them win    sys.excepthook = info


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/669695.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号