Python (pdb) - Queueing up commands to execute

坚强是说给别人听的谎言 提交于 2019-12-05 02:57:20

问题


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).


回答1:


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.



来源:https://stackoverflow.com/questions/2596738/python-pdb-queueing-up-commands-to-execute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!