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

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

如何设置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


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5674944.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存