关于pdb如何调试python程序,请阅读官网文档.
https://docs.python.org/2/library/pdb.html?highlight=pdb#pdb
h(elp) [command] ---- 查看可用的调试命令
w(here) 输入当前栈的跟踪情况, 换句话说就是看程序执行到了哪里
b(reak) 下断点,配合c((ont)inue)使用,效果很好.
cl(ear) 清除断点
l(ist) 查看代码n(ext) 执行完当前行,进入下一行
c((ont)inue) 继续向下执行代码,直至遇到断点或者代码执行完
p expression 执行当前表达式,并打印当前值
q 退出不调试
> /home/gnu/examples/text/re/re_findall.py(8)<module>()
-> text = "abaabaaabaababccccabc"
(Pdb) dir() --------- python函数 dir()
['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'pdb', 're']
(Pdb) w -------- 查看代码执行到了什么位置
> /home/gnu/examples/text/re/re_findall.py(8)<module>()
-> text = "abaabaaabaababccccabc"
(Pdb) n -------- 执行当前代码
> /home/gnu/examples/text/re/re_findall.py(10)<module>()
-> pattern = 'ab'
(Pdb) -------- 执行按下Enter键,等效于重复执行上一条命令
> /home/gnu/examples/text/re/re_findall.py(12)<module>()
-> matches = re.findall(pattern, text)
(Pdb) w
> /home/gnu/examples/text/re/re_findall.py(12)<module>()
-> matches = re.findall(pattern, text)
(Pdb) b 13 -------- 在第13行设置断点(cl 可清除所有断点)
Breakpoint 1 at /home/gnu/examples/text/re/re_findall.py:13
(Pdb) l 1,20 -------- 查看1到20行代码
1 #!/usr/bin/env python
2
3 import pdb
4 import re
5
6
7 pdb.set_trace()
8 text = "abaabaaabaababccccabc"
9
10 pattern = 'ab'
11
12 -> matches = re.findall(pattern, text)
13 B print matches
[EOF]
(Pdb) c -------- 向下执行程序, 直至遇到断点或者程序代码执行完毕
> /home/gnu/examples/text/re/re_findall.py(13)<module>()
-> print matches
(Pdb) n
['ab', 'ab', 'ab', 'ab', 'ab', 'ab']
--Return--
> /home/gnu/examples/text/re/re_findall.py(13)<module>()->None
-> print matches
(Pdb) n
Exception AttributeError: "'NoneType' object has no attribute 'path'" in <function _remove at 0xaa4668> ignored
https://docs.python.org/2/library/pdb.html?highlight=pdb#pdb
h(elp) [command] ---- 查看可用的调试命令
w(here) 输入当前栈的跟踪情况, 换句话说就是看程序执行到了哪里
b(reak) 下断点,配合c((ont)inue)使用,效果很好.
cl(ear) 清除断点
l(ist) 查看代码n(ext) 执行完当前行,进入下一行
c((ont)inue) 继续向下执行代码,直至遇到断点或者代码执行完
p expression 执行当前表达式,并打印当前值
q 退出不调试
上面部分命令附带有其他参数,请查阅手册。
#!/usr/bin/env python
import pdb
import re
pdb.set_trace() # 设置调试起点
text = "abaabaaabaababccccabc"
pattern = 'ab'
matches = re.findall(pattern, text)
print matches
使用上面介绍的命令调试上述源代码,例如:
nixawk@gnu:~/examples/text/re$ python re_findall.py> /home/gnu/examples/text/re/re_findall.py(8)<module>()
-> text = "abaabaaabaababccccabc"
(Pdb) dir() --------- python函数 dir()
['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'pdb', 're']
(Pdb) w -------- 查看代码执行到了什么位置
> /home/gnu/examples/text/re/re_findall.py(8)<module>()
-> text = "abaabaaabaababccccabc"
(Pdb) n -------- 执行当前代码
> /home/gnu/examples/text/re/re_findall.py(10)<module>()
-> pattern = 'ab'
(Pdb) -------- 执行按下Enter键,等效于重复执行上一条命令
> /home/gnu/examples/text/re/re_findall.py(12)<module>()
-> matches = re.findall(pattern, text)
(Pdb) w
> /home/gnu/examples/text/re/re_findall.py(12)<module>()
-> matches = re.findall(pattern, text)
(Pdb) b 13 -------- 在第13行设置断点(cl 可清除所有断点)
Breakpoint 1 at /home/gnu/examples/text/re/re_findall.py:13
(Pdb) l 1,20 -------- 查看1到20行代码
1 #!/usr/bin/env python
2
3 import pdb
4 import re
5
6
7 pdb.set_trace()
8 text = "abaabaaabaababccccabc"
9
10 pattern = 'ab'
11
12 -> matches = re.findall(pattern, text)
13 B print matches
[EOF]
(Pdb) c -------- 向下执行程序, 直至遇到断点或者程序代码执行完毕
> /home/gnu/examples/text/re/re_findall.py(13)<module>()
-> print matches
(Pdb) n
['ab', 'ab', 'ab', 'ab', 'ab', 'ab']
--Return--
> /home/gnu/examples/text/re/re_findall.py(13)<module>()->None
-> print matches
(Pdb) n
Exception AttributeError: "'NoneType' object has no attribute 'path'" in <function _remove at 0xaa4668> ignored