3

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

4

1 回答 1

4

您可以调用较低级别的方法来获得对调试器的更多控制:

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/pdbbdb较低级别的调试接口:http: //docs.python.org/library/bdb。您可能还想查看他们的源代码。

于 2010-04-08T11:38:30.923 回答