2010-04-08 47 views
2

我正在实现一个“断点”系统用于我的Python开发中,这将允许我调用一个函数,实际上它调用pdb.set_trace();Python(pdb) - 排队要执行的命令

我想要实现的一些功能需要我从代码控制pdb,而我处于set_trace上下文中。

例子:

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(); 

在上面的例子中,我该如何落实#****周围限定的评论?

在这个系统的其他部分,我想在不离开pdb会话的情况下发出'up'命令或者两个顺序的'up'命令(所以用户在pdb提示符下结束,但是在调用堆栈)。

回答

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/pdb并为bdb较低级别的调试接口的位置:http://docs.python.org/library/bdb。你也可能想看看他们的源代码。