2010-04-08 76 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。你也可能想看看他們的源代碼。