如何从源代码中设置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

解决方法:

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.
上一篇:我如何步骤使用python调试器来打破每个函数调用?


下一篇:使用Python pdb检查未处理异常原因的最简单方法是什么?