【问题标题】:What is the difference between a break point with argument for condition, and a condition break point?带有条件参数的断点和条件断点有什么区别?
【发布时间】:2015-10-30 18:55:08
【问题描述】:

来自 Python 库参考:

b(reak) [[文件名:]lineno |函数[, 条件]]

带亚麻布 参数,在当前文件中设置一个中断。有一个功能 参数,在其中的第一个可执行语句处设置一个中断 功能。行号可以以文件名和冒号为前缀, 在另一个文件中指定一个断点(可能是一个没有被 尚未加载)。在 sys.path 上搜索该文件。请注意,每个 断点被分配一个编号,所有其他断点都分配给该编号 命令参考。

如果存在第二个参数,它就是一个表达式 必须在断点被兑现之前评估为真。

没有 参数,列出所有断点,包括每个断点的数量 断点被命中的次数,当前的忽略计数,以及 相关条件(如果有)。

条件 bpnumber [条件]

Condition 是一个表达式,它必须在 支持断点。 如果条件不存在,则任何现有条件 已移除;即,断点是无条件的。

条件的第二个参数的断点(引号中的第一部分)和条件断点(引号中的第二部分)有什么区别?从报价来看,它们在我看来是一样的。

【问题讨论】:

    标签: python python-2.7 debugging pdb


    【解决方案1】:

    b 命令定义断点。您可以指定条件,但不必这样做。

    condition 命令需要一个已经定义的断点(即之前需要一个 b 命令)并更改它的条件。

    以下两个调试器命令可以认为是相等的:

    b debugme:3,x==100
    

    和

    b debugme:3
    condition 1 x==100
    

    让我们开始实践吧:

    创建以下 Python 脚本并将其命名为 debugme.py:

    def myfunction():
        for x in range(0,200):
            print x
    
    if __name__ == "__main__":
        myfunction()
    

    语法 1 演练(b 带条件):

    C:\tmp\py>C:\Python27\python.exe
    Python 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import pdb
    >>> import debugme
    >>> pdb.run('debugme.myfunction()')
    > <string>(1)<module>()
    (Pdb) b debugme:3,x==100                               <---- note this line
    Breakpoint 1 at c:\tmp\py\debugme.py:3
    (Pdb) c
    0
    1
    2
    3
    ...
    97
    98
    99
    > c:\tmp\py\debugme.py(3)myfunction()
    -> print x
    (Pdb) p x
    100
    (Pdb) q
    >>> exit()
    

    语法2演练(b无条件加条件):

    C:\tmp\py>C:\Python27\python.exe
    Python 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import pdb
    >>> import debugme
    >>> pdb.run('debugme.myfunction()')
    > <string>(1)<module>()
    (Pdb) b debugme:3                                     <---- note this line
    Breakpoint 1 at c:\tmp\py\debugme.py:3
    (Pdb) b
    Num Type         Disp Enb   Where
    1   breakpoint   keep yes   at c:\tmp\py\debugme.py:3
    (Pdb) condition 1 x==100                              <---- and this one
    (Pdb) b
    Num Type         Disp Enb   Where
    1   breakpoint   keep yes   at c:\tmp\py\debugme.py:3
            stop only if x==100
    (Pdb) c
    0
    1
    2
    3
    ...
    97
    98
    99
    > c:\tmp\py\debugme.py(3)myfunction()
    -> print x
    (Pdb) p x
    100
    (Pdb) q
    >>> exit()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-09
      • 1970-01-01
      • 2011-02-14
      相关资源
      最近更新 更多