Python学习之路(46)——pdb调试

pdb是Python自带的一个包,为Python程序提供一种交互的源代码调试功能,主要包括设置断点、单步调试、进入函数调试、查看当前代码段等。

下表为pdb常用命令:

使用pdb.set_trace()进入调试模式。

 

如下示例:

import pdb
a = "aaa"
pdb.set_trace()
b = "bbb"
c = "ccc"
final = a + b + c
print(final)

  

运行后进入pdb调试状态:

> d:\project\python\pro_py3\test.py(4)<module>()
-> b = "bbb"
(Pdb) 

  

使用常用命令:

> d:\project\python\pro_py3\test.py(4)<module>()
-> b = "bbb"
(Pdb) n
> d:\project\python\pro_py3\test.py(5)<module>()
-> c = "ccc"
(Pdb) 
> d:\project\python\pro_py3\test.py(6)<module>()
-> final = a + b + c
(Pdb) l
  1  	import pdb
  2  	a = "aaa"
  3  	pdb.set_trace()
  4  	b = "bbb"
  5  	c = "ccc"
  6  ->	final = a + b + c
  7  	print(final)
[EOF]
(Pdb) pp b
'bbb'
(Pdb) pp c
'ccc'
(Pdb) pp a
'aaa'
(Pdb) 
'aaa'
(Pdb) p a
'aaa'
(Pdb) p b
'bbb'
(Pdb) p c
'ccc'
(Pdb) q
Traceback (most recent call last):
  File "D:/Project/Python/Pro_py3/test.py", line 6, in <module>
    final = a + b + c
  File "D:/Project/Python/Pro_py3/test.py", line 6, in <module>
    final = a + b + c
  File "C:\Python35\lib\bdb.py", line 48, in trace_dispatch
    return self.dispatch_line(frame)
  File "C:\Python35\lib\bdb.py", line 67, in dispatch_line
    if self.quitting: raise BdbQuit
bdb.BdbQuit

  

使用函数的例子:

import pdb
def combine(s1,s2):      # define subroutine combine, which...
    s3 = s1 + s2 + s1    # sandwiches s2 between copies of s1, ...
    s3 = '"' + s3 +'"'   # encloses it in double quotes,...
    return s3            # and returns it.
a = "aaa"
pdb.set_trace()
b = "bbb"
c = "ccc"
final = combine(a,b)
print(final)

  

运行后,如果直接使用n进行debug,则运行到函数 final = combine(a,b) 会当做普通的赋值语句进行处理。而使用s可以进入函数块。

> d:\project\python\pro_py3\test.py(8)<module>()
-> b = "bbb"
(Pdb) n
> d:\project\python\pro_py3\test.py(9)<module>()
-> c = "ccc"
(Pdb) n
> d:\project\python\pro_py3\test.py(10)<module>()
-> final = combine(a,b)
(Pdb) s
--Call--
> d:\project\python\pro_py3\test.py(2)combine()
-> def combine(s1,s2):      # define subroutine combine, which...
(Pdb) n
> d:\project\python\pro_py3\test.py(3)combine()
-> s3 = s1 + s2 + s1    # sandwiches s2 between copies of s1, ...
(Pdb) l
  1  	import pdb
  2  	def combine(s1,s2):      # define subroutine combine, which...
  3  ->	    s3 = s1 + s2 + s1    # sandwiches s2 between copies of s1, ...
  4  	    s3 = '"' + s3 +'"'   # encloses it in double quotes,...
  5  	    return s3            # and returns it.
  6  	a = "aaa"
  7  	pdb.set_trace()
  8  	b = "bbb"
  9  	c = "ccc"
 10  	final = combine(a,b)
 11  	print(final)
(Pdb) n
> d:\project\python\pro_py3\test.py(4)combine()
-> s3 = '"' + s3 +'"'   # encloses it in double quotes,...
(Pdb) n
> d:\project\python\pro_py3\test.py(5)combine()
-> return s3            # and returns it.
(Pdb) n
--Return--
> d:\project\python\pro_py3\test.py(5)combine()->'"aaabbbaaa"'
-> return s3            # and returns it.
(Pdb) n
> d:\project\python\pro_py3\test.py(11)<module>()
-> print(final)
(Pdb) c
"aaabbbaaa"

  

另外,还可以使用pdb.run(function())的方式进行调试。

import pdb
def test():
    print("hello", end='')
    print("world")
pdb.run("test()")

##########执行结果##########
> <string>(1)<module>()
(Pdb) n
helloworld
--Return--
> <string>(1)<module>()->None
(Pdb) s
> c:\python35\lib\bdb.py(435)run()
-> self.quitting = True
(Pdb) c

  

posted on 2018-03-28 20:20  nicolas_Z  阅读(468)  评论(0)    收藏  举报

导航