【问题标题】:Is it possible to use bpython as a full debugger?是否可以将 bpython 用作完整的调试器?
【发布时间】:2012-03-27 15:33:26
【问题描述】:

我想使用bpython 解释器进行调试。 我的问题类似于“Is it possible to go into ipython from code?”,它询问 ipython。

如果您使用ipdb.set_trace(),您将获得完整的ipython 会话及其所有便利。然而,bpdb.set_trace() 没有给我一个 bpython 会话,它给了我标准的pdb 调试器。

有什么方法可以设置以便我可以在 bpython 会话中进行调试?

【问题讨论】:

  • 试一试会花很多钱吗?
  • @Marcin 我应该澄清bpdb.set_trace() 给了我标准的pdb 调试器
  • 那么,这是个问题吗?问题是如何产生的?
  • 是的:如果您使用ipdb.set_trace(),您将获得完整的ipython 调试器会话及其所有便利。在我看来 bpython 更好用,但是 bpdb.set_trace() 并没有给我一个 bpython 会话
  • @MRE 是的,这是可行的。看我的回答。

标签: python debugging bpython


【解决方案1】:

是的,使用this wrapper,您也可以通过将语句import bpdb 添加到您的代码来访问它。在您想中断的地方添加bpdb.set_trace(),然后在pdb 解释器中输入“B”以跳转到bpython 会话,其中包含堆栈框架中的所有内容:

# The MIT License
#
# Copyright (c) 2008 Bob Farrell
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.


import pdb
import bpython

class BPdb(pdb.Pdb):
    """ PDB with BPython support. """

    def __init__(self):
        pdb.Pdb.__init__(self)
        self.rcLines = []
        self.prompt = '(BPdb) '
        self.intro = 'Use "B" to enter bpython, Ctrl-d to exit it.'

    def postloop(self):
        # We only want to show the intro message once.
        self.intro = None
        pdb.Pdb.postloop(self)

    ### cmd.Cmd commands


    def do_Bpython(self, arg):
        bpython.embed(self.curframe.f_locals, ['-i'])


    def help_Bpython(self):
        print "B(python)"
        print
        print ("Invoke the bpython interpreter for this stack frame. To exit "
               "bpython and return to a standard pdb press Ctrl-d")


    ### shortcuts
    do_B = do_Bpython
    help_B = help_Bpython

你的问题对我来说似乎完全正确!

【讨论】:

  • 嗨,感谢您向我指出这一点,但这只是 pdb 的包装。它包含在bpdb 模块中。当我使用它时,它只会给我pdb 调试器(即没有突出显示、完成、文档等)。除非它在你的机器上做一些不同的事情?
  • @MrE 这似乎是为bpython 提供的范围,但Amjith 是绝对正确的。为了将pdb 扩展为令人愉快的调试器,pdb++ 是要走的路。 :)
  • 进一步检查,也就是阅读上面的代码,从pdb 调试器会话中,如果您键入“B”,您将跳转到bpython 会话。所以我已将其标记为正确。
  • @MrE 很高兴我们俩都能为您提供帮助!对于您收到的最初的反对票和简洁的回复,我代表我的同行道歉。我很高兴我们能够介入并提供帮助。 :)
  • 无需道歉。希望您不介意我将其余信息添加到您的答案中。如果您愿意,请随时改写。
【解决方案2】:

如果您正在寻找一个看起来更酷的调试器,它可以完成补全和语法突出显示,您可能想看看 pdb++。 http://pypi.python.org/pypi/pdbpp/

它是 pdb 的替代品。所以你可以继续使用

 import pdb; pdb.set_trace()

它会让你进入 pdb++ 提示符。

【讨论】:

  • 嘿,这真是太好了。不像 bpython 那样流畅(在交互模式下),但仍然是一个大而轻松的升级。
  • 我同意,完成度不如 bpython 好。我喜欢粘性功能,上面显示了代码并突出显示正在执行的当前行。
  • 已经安装了 pdbpp 和 python3.7+,你可以调用 breakpoint builtin 和 pdbpp 调试器将发生
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-04
  • 1970-01-01
  • 2011-10-03
  • 1970-01-01
  • 2015-10-05
  • 1970-01-01
相关资源
最近更新 更多