Python 中的调试控制台是如何实现的?
我见过几个提供调试控制台的 Python IDE(例如 PyDev Extensions、WingIDE)——一个在断点所在方法的上下文中运行的交互式终端。 这使您可以打印成员、调用其他方法并查看结果以及重新定义方法以尝试修复错误。 凉爽的。
谁能告诉我这是如何实现的? 我知道有 Code 模块,它提供了 InteractiveConsole 类,但我不知道如何在当前加载的代码的上下文中运行它。 我对 Python 很陌生,因此将不胜感激!
I've seen a couple of Python IDE's (e.g. PyDev Extensions, WingIDE) that provide a debug console - an interactive terminal that runs in the context of the method where the breakpoint is. This lets you print members, call other methods and see the results, and redefine methods to try to fix bugs. Cool.
Can anyone tell me how this is implemented? I know there's the Code module, which provides an InteractiveConsole class, but I don't know how this can be run in the context of currently loaded code. I'm quite new to Python, so gentle assistance would be appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以尝试查看 python 调试器 pdb。 它的使用方式类似于 gdb,但是是用纯 python 实现的。 在 python 安装目录中查找 pdb.py。
You could try looking at the python debugger pdb. It's like gdb in how you use it, but implemented in pure python. Have a look for pdb.py in your python install directory.
是的,我很惭愧地承认它实际上在 InteractiveConsole 的文档中。 您可以通过将 locals() 函数的结果传递给 InteractiveConsole 构造函数来使其在本地上下文中运行。 我找不到在不终止应用程序的情况下关闭 InteractiveConsole 的方法,因此我将其扩展为在捕获 SystemExit 异常时仅关闭控制台。 我不喜欢这样,但我还没有找到更好的方法。
这是一些(相当简单的)示例代码,演示了调试控制台。
Right, I'm ashamed to admit it's actually in the documentation for InteractiveConsole after all. You can make it run in the local context by passing in the result of the locals() function to the InteractiveConsole constructor. I couldn't find a way to close the InteractiveConsole without killing the application, so I've extended it to just close the console when it catches the SystemExit exception. I don't like it, but I haven't yet found a better way.
Here's some (fairly trivial) sample code that demonstrates the debug console.
http://docs.python.org/3.0/library/functions.html#输入
http://docs.python.org/3.0/library/functions.html#然而
,我确信他们的实现比这更安全。
http://docs.python.org/3.0/library/functions.html#input
http://docs.python.org/3.0/library/functions.html#eval
I'm sure, however, that their implementation is much more foolsafe than this.
Python 在 bdb 模块 中有一个调试器框架。 我不确定您列出的 IDE 是否使用它,但肯定可以用它实现完整的 Python 调试器。
Python has a debugger framework in the bdb module. I'm not sure if the IDE's you list use it but it's certainly possible to implement a full Python debugger with it.
如果您想尝试自己的 Python 控制台,那么这是一个很好的开始:
但对于实际工作,请使用 InteractiveConsole。
If you want to experiment with your own Python console then this is a nice start:
But for real work use the InteractiveConsole instead.