Get last exception in pdb

前端 未结 5 1675
执笔经年
执笔经年 2020-12-16 11:41

Is there a way to examine the last exception when in pdb/before entering pdb? (Using python 2.7.5).

Immediately (yes, I enter no other commands at all) afte

5条回答
  •  再見小時候
    2020-12-16 11:56

    You can use sys.last_value:

    >>> no_such_var
    Traceback (most recent call last):
      File "", line 1, in 
    NameError: name 'no_such_var' is not defined
    >>> import sys
    >>> sys.last_value
    NameError("name 'no_such_var' is not defined",)
    >>> sys.last_value.args
    ("name 'no_such_var' is not defined",)
    

    >>> no_such_var
    Traceback (most recent call last):
      File "", line 1, in 
    NameError: name 'no_such_var' is not defined
    >>> import pdb, sys
    >>> pdb.set_trace()
    --Return--
    > (1)()->None
    (Pdb) sys.last_value
    NameError("name 'no_such_var' is not defined",)
    

    NOTE: This solution is not perfect. The value is set when an exception is not handled and the interpreter prints an error message and a stack traceback. For example, if the exception is caught using try .. except .., sys.last_value is not set.

提交回复
热议问题