views:

71

answers:

1

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

Thanks!

+2  A: 

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.

Alex Morega