Python - 当异常会被抛出时启动交互式调试器 [英] Python - start interactive debugger when exception would be otherwise thrown

查看:109
本文介绍了Python - 当异常会被抛出时启动交互式调试器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法让一个python程序启动一个交互式调试器,像什么 import pdb; pdb.set_trace()而不是真的抛出一个异常?



我知道做这个工作的难度,但它会更有价值比一个巨大的堆栈跟踪,之后我必须使用来找出插入断点的位置,然后重新启动程序来进行调试。我知道简单地使调试器开始而不是抛出异常是没有意义的,因为任何异常都可以被捕获在一个级别或另一个级别,所以如果我可以选择一个异常的列表,交互式调试会话将会启动而不是被抛出(因为我知道这个列表中的异常真的是错误,之后没有有意义的程序行为)...



我听说Common Lisp有这样的东西,但我不知道它是如何正常工作的,只是这个真正的lispers赞美了很多...

解决方案

最简单的方法是将整个代码封装在 try 块中,如下所示:

 如果__name__ =='__main__':

try:
raise异常()
除了
import pdb
pdb.set_trace()

有一个更复杂的解决方案使用s sys.excepthook 来覆盖未捕获异常的处理,如这个配方

  ## {{{http://code.activestate.com/recipes/65287/(r5)
#code snippet,要包含在'sitecustomize.py'
import sys

def info(type,value,tb):
如果hasattr(sys,'ps1')或不是sys.stderr.isatty():
#我们处于交互模式,或者我们没有一个类似tty的
#设备,所以我们调用默认钩子
sys .__ excepthook __(type,value,tb)
else:
import traceback,pdb
#我们不是在交互模式,打印例外...
traceback.print_exception(type,value,tb)
print
#...然后在验尸模式下启动调试器。
pdb.pm()

sys.excepthook = info
## http://code.activestate.com/recipes/65287/}}}}

上述代码应该包含在sitecustomize.py中,这是由python自动导入的。调试器仅在python以非交互模式运行时启动。


Is there any way to make a python program start an interactive debugger, like what import pdb; pdb.set_trace() instead of actually throwing an exception?

I know the difficulty of making this work, but it would be much more valuable than a huge stack trace after which I have to use to figure out where to insert breakpoints and then restart the program to debug it. I know that simply making the debugger start instead of throwing an exception would not make sense because any exception can be caught at a level or another, so if I could just select a list of exceptions for which an interactive debug session would start instead of them being thrown (because I know the exceptions in this list would really be "errors" and no meaningful program behavior could follow afterwards)...

I've heard that Common Lisp has something like this, but I don't know how it works exactly, just that "true lispers" praise it a lot...

解决方案

The simplest way is to wrap your entire code inside a try block like this:

if __name__ == '__main__':

    try:
        raise Exception()
    except:
        import pdb
        pdb.set_trace()

There is a more complicated solution which uses sys.excepthook to override the handling of uncaught exceptions, as described in this recipe

## {{{ http://code.activestate.com/recipes/65287/ (r5)
# code snippet, to be included in 'sitecustomize.py'
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.pm()

sys.excepthook = info
## end of http://code.activestate.com/recipes/65287/ }}}

The above code should be included in 'sitecustomize.py', which is automatically imported by python. The debugger is only started when python is run in non-interactive mode.

这篇关于Python - 当异常会被抛出时启动交互式调试器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆