通过在 IPython 中切换 %pdb 来自动调用 pdb 来解决错误。是否有等效的操作(或解决方法)来自动调用 pdb 警告?
将 pdb.set_trace() 添加到报告的行号并不理想,因为警告位于从我的代码调用的库文件中。
“python -m pdb myscript.py”会这样做,但我在 ipython repl 中,而不是常规的 python repl。
这里的用例是查找类型提示错误,这些错误被报告为像这样的警告“RuntimeWarning:在相等时遇到无效值”
通过在 IPython 中切换 %pdb 来自动调用 pdb 来解决错误。是否有等效的操作(或解决方法)来自动调用 pdb 警告?
将 pdb.set_trace() 添加到报告的行号并不理想,因为警告位于从我的代码调用的库文件中。
“python -m pdb myscript.py”会这样做,但我在 ipython repl 中,而不是常规的 python repl。
这里的用例是查找类型提示错误,这些错误被报告为像这样的警告“RuntimeWarning:在相等时遇到无效值”
通过设置一个简单的过滤器将警告变成'error'
:
from warnings import warn, simplefilter
simplefilter('error')
这似乎工作得很好,例如一个示例函数:
def foo():
a = '20'
warn("Horrible Things", RuntimeWarning)
现在调用时会触发pdb
:
foo()
> <ipython-input-78-f33d9e580240>(4)foo()
1 from warnings import warn
2 def foo():
3 a = '20'
----> 4 warn("Horrible Things", RuntimeWarning)
ipdb>