条件参数的断点和条件断点之间有什么区别?

时间:2015-10-29 18:31:04

标签: python python-2.7 debugging pdb

来自Python库参考:

  

b(reak)[[fi lename:] lineno |功能[,条件]]

     

使用lineno   参数,在当前文件中设置一个休息点。有功能   参数,在其中的第一个可执行语句中设置一个中断   功能。行号可以预先设置文件名和冒号,   在另一个文件中指定一个断点(可能是一个没有的断点)   加载了)。文件在sys.path上搜索。请注意每个   断点被分配一个数字,所有其他断点   命令参考。

     

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

     

无   参数,列出所有中断,包括每个断点,数字   断点被击中的次数,当前忽略计数,和   相关的条件,如果有的话。

     

条件bpnumber [条件]

     

条件是一个表达式,必须在之前计算为true   断点很荣幸。如果条件不存在,任何现有条件   已移除;即,断点是无条件的。

条件的第二个参数的断点(引用的第一部分)和条件断点(引用的第二部分)之间有什么区别?从报价中我们看起来一样。

1 个答案:

答案 0 :(得分:0)

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

条件命令需要一个已定义的断点(即之前需要 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()