如何在源代码中设置pdb中断条件?

时间:2014-03-21 11:40:18

标签: python pdb

问题:如果我有一个复杂的断点条件,我不希望pdb.set_trace()每次重新启动程序时手动粘贴断点命令。

有没有办法在我的源代码中设置断点?

我尝试了什么:我阅读了文档并尝试插入此行:

pdb.run('break 445, a == 1')

但是一旦执行到达该行,我就会收到此错误:

    pdb.run('break 445, a == 1')
  File "C:\Python25\lib\pdb.py", line 1123, in run
    Pdb().run(statement, globals, locals)
  File "C:\Python25\lib\bdb.py", line 366, in run
    exec cmd in globals, locals
  File "<string>", line 1
    break 445, a == 1
            ^
SyntaxError: invalid syntax

1 个答案:

答案 0 :(得分:3)

pdb无法做到这一点。但你可以在自己的程序中自己构建它,有两种方式。

第一种方法是使用全局变量来控制断点条目。像这样的东西将是非常微不足道的(简单的例子):

_breakpoints = {}

def reset_breakpoints(disabled=[]):
    global _breakpoints
    _breakpoints = dict((x, False) for x in disabled)

def set_breakpoint(tag, condition=True):
    if tag not in _breakpoints:
        _breakpoints[tag] = True
        if condition:
            pdb.set_trace()
    else:
        if _breakpoints[tag] and condition:
            pdb.set_trace()

def mycode():
    some_command()
    set_breakpoint('mycode0')
    another_command()
    set_breakpoint('mycode1', x == 4)

第一次,你在字典中没有它,所以你要添加它并停止。但是,你可以改变字典中的值,然后不是实际停止,而是先检查你是否应该。您可以使用eval等执行更复杂的条件,但它可能比上述更容易。这是相当轻量级的,让您完全控制停止的时间。唯一的事情,你必须记住在重新启动调试会话时自己初始化dict(通常清除它),如果你设置为False。它没有集成到调试器中,但它也不会花费太多。

要记住的另一件事是......在你进入一个调制器之前,你没有调试器可以控制。这意味着,除非您首先执行pdb.run(),否则您实际上并不在调试器中,并且在没有停止的情况下无法进入调试器(据我所知)。一旦你进入调试器,虽然有一些聪明,但是使用堆栈可以获得pdb对象。这是在IPython中,不幸的是,实际的堆栈深度可能会有所不同,具体取决于您使用的是哪个pdb等,但您可以尝试类似下面的内容来获取对当前运行的Pdb对象的引用。获得后,您可以使用其界面直接与其进行交互。一个例子:

In [331]: %debug pass
NOTE: Enter 'c' at the ipdb>  prompt to continue execution.
None
> <string>(1)<module>()

ipdb> import inspect
ipdb> f = inspect.stack()[6]
ipdb> f0 = f[0]
ipdb> myipdb = f0.f_locals['self']
ipdb> myipdb.
myipdb.aliases               myipdb.dispatch_exception    myipdb.do_next               myipdb.fncache               myipdb.help_help             myipdb.lastcmd               myipdb.runctx
myipdb.botframe              myipdb.dispatch_line         myipdb.do_p                  myipdb.forget                myipdb.help_ignore           myipdb.lineinfo              myipdb.runeval
myipdb.bp_commands           myipdb.dispatch_return       myipdb.do_pdef               myipdb.format_stack_entry    myipdb.help_j                myipdb.lineno                myipdb.set_break
myipdb.break_anywhere        myipdb.displayhook           myipdb.do_pdoc               myipdb.get_all_breaks        myipdb.help_jump             myipdb.list_command_pydb     myipdb.set_colors
myipdb.break_here            myipdb.do_EOF                myipdb.do_pfile              myipdb.get_break             myipdb.help_l                myipdb.lookupmodule          myipdb.set_continue
myipdb.breaks                myipdb.do_a                  myipdb.do_pinfo              myipdb.get_breaks            myipdb.help_list             myipdb.mainpyfile            myipdb.set_next
myipdb.canonic               myipdb.do_alias              myipdb.do_pinfo2             myipdb.get_file_breaks       myipdb.help_n                myipdb.misc_header           myipdb.set_quit
myipdb.checkline             myipdb.do_args               myipdb.do_pp                 myipdb.get_names             myipdb.help_next             myipdb.new_do_down           myipdb.set_return
myipdb.clear_all_breaks      myipdb.do_b                  myipdb.do_psource            myipdb.get_stack             myipdb.help_p                myipdb.new_do_frame          myipdb.set_step
myipdb.clear_all_file_breaks myipdb.do_break              myipdb.do_q                  myipdb.handle_command_def    myipdb.help_pdb              myipdb.new_do_quit           myipdb.set_trace
myipdb.clear_bpbynumber      myipdb.do_bt                 myipdb.do_quit               myipdb.help_EOF              myipdb.help_pp               myipdb.new_do_restart        myipdb.set_until
myipdb.clear_break           myipdb.do_c                  myipdb.do_r                  myipdb.help_a                myipdb.help_q                myipdb.new_do_up             myipdb.setup
myipdb.cmdloop               myipdb.do_cl                 myipdb.do_restart            myipdb.help_alias            myipdb.help_quit             myipdb.nohelp                myipdb.shell
myipdb.cmdqueue              myipdb.do_clear              myipdb.do_return             myipdb.help_args             myipdb.help_r                myipdb.onecmd                myipdb.stack
myipdb.color_scheme_table    myipdb.do_commands           myipdb.do_retval             myipdb.help_b                myipdb.help_restart          myipdb.parseline             myipdb.stdin
myipdb.columnize             myipdb.do_condition          myipdb.do_run                myipdb.help_break            myipdb.help_return           myipdb.parser                myipdb.stdout
myipdb.commands              myipdb.do_cont               myipdb.do_rv                 myipdb.help_bt               myipdb.help_run              myipdb.postcmd               myipdb.stop_here
myipdb.commands_bnum         myipdb.do_continue           myipdb.do_s                  myipdb.help_c                myipdb.help_s                myipdb.postloop              myipdb.stopframe
myipdb.commands_defining     myipdb.do_d                  myipdb.do_step               myipdb.help_cl               myipdb.help_step             myipdb.precmd                myipdb.stoplineno
myipdb.commands_doprompt     myipdb.do_debug              myipdb.do_tbreak             myipdb.help_clear            myipdb.help_tbreak           myipdb.preloop               myipdb.trace_dispatch
myipdb.commands_resuming     myipdb.do_disable            myipdb.do_u                  myipdb.help_commands         myipdb.help_u                myipdb.print_list_lines      myipdb.undoc_header
myipdb.commands_silent       myipdb.do_down               myipdb.do_unalias            myipdb.help_condition        myipdb.help_unalias          myipdb.print_stack_entry     myipdb.use_rawinput
myipdb.complete              myipdb.do_enable             myipdb.do_unt                myipdb.help_cont             myipdb.help_unt              myipdb.print_stack_trace     myipdb.user_call
myipdb.complete_help         myipdb.do_exit               myipdb.do_until              myipdb.help_continue         myipdb.help_until            myipdb.print_topics          myipdb.user_exception
myipdb.completedefault       myipdb.do_h                  myipdb.do_up                 myipdb.help_d                myipdb.help_up               myipdb.prompt                myipdb.user_line
myipdb.completekey           myipdb.do_help               myipdb.do_w                  myipdb.help_debug            myipdb.help_w                myipdb.quitting              myipdb.user_return
myipdb.completenames         myipdb.do_ignore             myipdb.do_whatis             myipdb.help_disable          myipdb.help_whatis           myipdb.rcLines
myipdb.curframe              myipdb.do_j                  myipdb.do_where              myipdb.help_down             myipdb.help_where            myipdb.reset
myipdb.curindex              myipdb.do_jump               myipdb.doc_header            myipdb.help_enable           myipdb.identchars            myipdb.returnframe
myipdb.default               myipdb.do_l                  myipdb.doc_leader            myipdb.help_exec             myipdb.interaction           myipdb.ruler
myipdb.defaultFile           myipdb.do_list               myipdb.emptyline             myipdb.help_exit             myipdb.intro                 myipdb.run
myipdb.dispatch_call         myipdb.do_n                  myipdb.execRcLines           myipdb.help_h                myipdb.is_pydb               myipdb.runcall
ipdb> myipdb.