抛出异常时是否可以自动进入调试器?

时间:2022-09-25 19:05:52

If ones catches an exception outside of the function it is originally thrown, ones loses access to the local stack. As a result one cannot inspect the values of the variables that might have caused the exception.

如果在最初抛出函数之外捕获异常,则会失去对本地堆栈的访问权限。因此,无法检查可能导致异常的变量值。

Is there a way to automatically start break into the debugger (import pdb; pdb.set_trace()) whenever a exception is thrown to inspect the local stack?

每当抛出异常来检查本地堆栈时,有没有办法自动启动进入调试器(导入pdb; pdb.set_trace())?

update

Thanks for your two answers, I +1ed you, but I found a different solution.

谢谢你的两个答案,我+ 1你,但我发现了一个不同的解决方案。

3 个解决方案

#1


12  

You don't want to break on every exception; idiomatic Python code uses exceptions heavily (EAFP) so you'd be continually breaking in unrelated code.

你不想打破每一个例外;惯用的Python代码大量使用异常(EAFP),因此您将不断破坏不相关的代码。

Instead, use pdb post-mortem: import pdb; pdb.pm(). This uses sys.last_traceback to inspect the stack including the locals at the throw point.

相反,使用pdb post-mortem:import pdb; pdb.pm()。这使用sys.last_traceback来检查堆栈,包括抛出点处的本地。

#2


8  

ipython supports this (http://ipython.org). from inside ipython, do

ipython支持这个(http://ipython.org)。从ipython内部来做

%pdb on

and from then on, it will automatically drop you inside the debugger whenever you get an exception.

从那时起,每当出现异常时,它都会自动将您放入调试器中。

note that you'll (probably) quickly tire of this in general use... every time you mistype something and get a syntax error, you'll have to exit the debugger. but it's sometimes useful.

请注意,您(可能)很快就会厌倦了这种情况......每当您输入错误并出现语法错误时,您将不得不退出调试器。但它有时很有用。

#3


1  

I found what I was looking for in an answer to What is the simplest way of using Python pdb to inspect the cause of an unhandled exception?

我在回答什么是使用Python pdb检查未处理异常的原因的最简单方法时找到了我要找的东西?

Wrap it with that:

包裹它:

<!-- language: lang-py -->
def debug_on(*exceptions):
    if not exceptions:
        exceptions = (AssertionError, )
    def decorator(f):
        @functools.wraps(f)
        def wrapper(*args, **kwargs):
            try:
                return f(*args, **kwargs)
            except exceptions:
                pdb.post_mortem(sys.exc_info()[2])
        return wrapper
    return decorator

Example:

@debug_on(TypeError)
def buggy_function()
    ....
    raise TypeError

#1


12  

You don't want to break on every exception; idiomatic Python code uses exceptions heavily (EAFP) so you'd be continually breaking in unrelated code.

你不想打破每一个例外;惯用的Python代码大量使用异常(EAFP),因此您将不断破坏不相关的代码。

Instead, use pdb post-mortem: import pdb; pdb.pm(). This uses sys.last_traceback to inspect the stack including the locals at the throw point.

相反,使用pdb post-mortem:import pdb; pdb.pm()。这使用sys.last_traceback来检查堆栈,包括抛出点处的本地。

#2


8  

ipython supports this (http://ipython.org). from inside ipython, do

ipython支持这个(http://ipython.org)。从ipython内部来做

%pdb on

and from then on, it will automatically drop you inside the debugger whenever you get an exception.

从那时起,每当出现异常时,它都会自动将您放入调试器中。

note that you'll (probably) quickly tire of this in general use... every time you mistype something and get a syntax error, you'll have to exit the debugger. but it's sometimes useful.

请注意,您(可能)很快就会厌倦了这种情况......每当您输入错误并出现语法错误时,您将不得不退出调试器。但它有时很有用。

#3


1  

I found what I was looking for in an answer to What is the simplest way of using Python pdb to inspect the cause of an unhandled exception?

我在回答什么是使用Python pdb检查未处理异常的原因的最简单方法时找到了我要找的东西?

Wrap it with that:

包裹它:

<!-- language: lang-py -->
def debug_on(*exceptions):
    if not exceptions:
        exceptions = (AssertionError, )
    def decorator(f):
        @functools.wraps(f)
        def wrapper(*args, **kwargs):
            try:
                return f(*args, **kwargs)
            except exceptions:
                pdb.post_mortem(sys.exc_info()[2])
        return wrapper
    return decorator

Example:

@debug_on(TypeError)
def buggy_function()
    ....
    raise TypeError