Python 中的调试控制台是如何实现的?

发布于 2024-07-12 00:19:00 字数 247 浏览 5 评论 0原文

我见过几个提供调试控制台的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

吃颗糖壮壮胆 2024-07-19 00:19:00

您可以尝试查看 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.

挽清梦 2024-07-19 00:19:00

是的,我很惭愧地承认它实际上在 InteractiveConsole 的文档中。 您可以通过将 locals() 函数的结果传递给 InteractiveConsole 构造函数来使其在本地上下文中运行。 我找不到在不终止应用程序的情况下关闭 InteractiveConsole 的方法,因此我将其扩展为在捕获 SystemExit 异常时仅关闭控制台。 我不喜欢这样,但我还没有找到更好的方法。

这是一些(相当简单的)示例代码,演示了调试控制台。

import code

class EmbeddedConsole(code.InteractiveConsole):
    def start(self):
        try:
            self.interact("Debug console starting...")
        except:
            print("Debug console closing...")

def print_names():
    print(adam)
    print(bob)

adam = "I am Adam"
bob = "I am Bob"

print_names()
console = EmbeddedConsole(locals())
console.start()
print_names()

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.

import code

class EmbeddedConsole(code.InteractiveConsole):
    def start(self):
        try:
            self.interact("Debug console starting...")
        except:
            print("Debug console closing...")

def print_names():
    print(adam)
    print(bob)

adam = "I am Adam"
bob = "I am Bob"

print_names()
console = EmbeddedConsole(locals())
console.start()
print_names()
完美的未来在梦里 2024-07-19 00:19:00

http://docs.python.org/3.0/library/functions.html#输入
http://docs.python.org/3.0/library/functions.html#然而

def start_interpreter():
     while(True):
          code = input("Python Console >")
          eval(code)

,我确信他们的实现比这更安全。

http://docs.python.org/3.0/library/functions.html#input
http://docs.python.org/3.0/library/functions.html#eval

def start_interpreter():
     while(True):
          code = input("Python Console >")
          eval(code)

I'm sure, however, that their implementation is much more foolsafe than this.

傻比既视感 2024-07-19 00:19:00

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.

痕至 2024-07-19 00:19:00

如果您想尝试自己的 Python 控制台,那么这是一个很好的开始:

cmd = None
while cmd != 'exit':
    cmd = raw_input('>>> ')
    try:
        exec(cmd)
    except:
        print 'exception'

但对于实际工作,请使用 InteractiveConsole。

If you want to experiment with your own Python console then this is a nice start:

cmd = None
while cmd != 'exit':
    cmd = raw_input('>>> ')
    try:
        exec(cmd)
    except:
        print 'exception'

But for real work use the InteractiveConsole instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文