Python (pdb) - 排队要执行的命令

发布于 2024-08-28 18:41:21 字数 617 浏览 1 评论 0原文

我正在实现一个用于 Python 开发的“断点”系统,该系统允许我调用一个函数,该函数本质上调用 pdb.set_trace();

我想要实现的一些功能要求我在 set_trace 上下文中时通过代码控制 pdb。

示例:

disableList = []
def breakpoint(name=None):
    def d():
        disableList.append(name)
        #****
        #issue 'run' command to pdb so user
        #does not have to type 'c'
        #****

    if name in disableList:
        return

    print "Use d() to disable breakpoint, 'c' to continue"
    pdb.set_trace();

在上面的示例中,如何实现 #**** 标记的注释?

在该系统的其他部分,我想发出一个“up”命令,或两个连续的“up”命令,而不离开 pdb 会话(因此用户最终会出现 pdb 提示符,但在调用堆栈上上升两级)。

I am implementing a "breakpoint" system for use in my Python development that will allow me to call a function that, in essence, calls pdb.set_trace();

Some of the functionality that I would like to implement requires me to control pdb from code while I am within a set_trace context.

Example:

disableList = []
def breakpoint(name=None):
    def d():
        disableList.append(name)
        #****
        #issue 'run' command to pdb so user
        #does not have to type 'c'
        #****

    if name in disableList:
        return

    print "Use d() to disable breakpoint, 'c' to continue"
    pdb.set_trace();

In the above example, how do I implement the comments demarked by the #**** ?

In other parts of this system, I would like to issue an 'up' command, or two sequential 'up' commands without leaving the pdb session (so the user ends up at a pdb prompt but up two levels on the call stack).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

浮生未歇 2024-09-04 18:41:21

您可以调用较低级别的方法来更好地控制调试器:

def debug():
    import pdb
    import sys

    # set up the debugger
    debugger = pdb.Pdb()
    debugger.reset()

    # your custom stuff here
    debugger.do_where(None) # run the "where" command

    # invoke the interactive debugging prompt
    users_frame = sys._getframe().f_back # frame where the user invoked `debug()`
    debugger.interaction(users_frame, None)

if __name__ == '__main__':
    print 1
    debug()
    print 2

您可以在此处找到 pdb 模块的文档:http://docs.python.org/library/pdb 以及 bdb 较低级别的调试接口:http://docs.python.org/library/bdb。您可能还想查看他们的源代码。

You could invoke lower-level methods to get more control over the debugger:

def debug():
    import pdb
    import sys

    # set up the debugger
    debugger = pdb.Pdb()
    debugger.reset()

    # your custom stuff here
    debugger.do_where(None) # run the "where" command

    # invoke the interactive debugging prompt
    users_frame = sys._getframe().f_back # frame where the user invoked `debug()`
    debugger.interaction(users_frame, None)

if __name__ == '__main__':
    print 1
    debug()
    print 2

You can find documentation for the pdb module here: http://docs.python.org/library/pdb and for the bdb lower-level debugging interface here: http://docs.python.org/library/bdb. You may also want to look at their source code.

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