如何检查Python引发异常的函数的范围? [英] How do I inspect the scope of a function where Python raises an exception?

查看:158
本文介绍了如何检查Python引发异常的函数的范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近发现了Python的非常有用的'-i'标志



pre
-i:运行脚本后,也是PYTHONINSPECT = x)
和强制提示,即使stdin似乎不是终端

这是很好的检查对象在全局范围内,但如果在函数调用中引发异常,会发生什么,我想检查函数的局部变量?自然,我对第一次提出异常的范围感兴趣,有没有办法得到它?

解决方案

在交互式提示符下,立即输入

 >>> import pdb 
>>>> pdb.pm()

pdb.pm()是验尸调试器。这将使您处于引发异常的范围,然后可以使用通常的pdb命令。



我一直使用这个 。它是标准库的一部分(不需要ipython),并且不需要将调试命令编辑到源代码中。



唯一的诀窍是记住立即执行;如果您先输入任何其他命令,您将失去发生异常的范围。


I've recently discovered the very useful '-i' flag to Python

-i     : inspect interactively after running script, (also PYTHONINSPECT=x)
         and force prompts, even if stdin does not appear to be a terminal

this is great for inspecting objects in the global scope, but what happens if the exception was raised in a function call, and I'd like to inspect the local variables of the function? Naturally, I'm interested in the scope of where the exception was first raised, is there any way to get to it?

解决方案

At the interactive prompt, immediately type

>>> import pdb
>>> pdb.pm()

pdb.pm() is the "post-mortem" debugger. It will put you at the scope where the exception was raised, and then you can use the usual pdb commands.

I use this all the time. It's part of the standard library (no ipython necessary) and doesn't require editing debugging commands into your source code.

The only trick is to remember to do it right away; if you type any other commands first, you'll lose the scope where the exception occurred.

这篇关于如何检查Python引发异常的函数的范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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