使用PDB调试Python程序

长平狐 发布于 2013/01/06 11:20
阅读 126
收藏 0

 

本文讨论在没有方便的IDE工具可用的情况下,使用pdb调试python程序
 
源码例子
例如,有模拟税收计算的程序:
 
#!/usr/bin/python 

def debug_demo(val): 

                if val <= 1600 : 
                                print "level 1" 
                                print 0 
                elif val <= 3500 : 
                                print "level 2" 
                                print (val - 1600) * 0.05 
                elif val <= 6500 : 
                                print "level 3" 
                                print (val - 3500) * 0.10 + (3500-1600) * 0.05 
                else: 
                                print "level 4" 
                                print (val - 6500) * 0.20 + (6500-3500) * 0.10 + (3500-1600) * 0.05 
                                 

#~def debug_demo                 
                 
if __name__ == "__main__": 
                 
                debug_demo(4500) 
 
debug_demo函数计算4500的入账所需的税收。
 
如何调试?
 
1.加入断点
 
在需要插入断点的地方,加入红色部分代码:如果_DEBUG值为True,则在该处开始调试(加入_DEBUG的原因是为了方便打开/关闭调试)。
 
#!/usr/bin/python 
_DEBUG=True 
def debug_demo(val): 
                if _DEBUG == True: 
                                import pdb 
                                pdb.set_trace() 

                if val <= 1600 : 
                                print "level 1" 
                                print 0 
                elif val <= 3500 : 
                                print "level 2" 
                                print (val - 1600) * 0.05 
                elif val <= 6500 : 
                                print "level 3" 
                                print (val - 3500) * 0.10 + (3500-1600) * 0.05 
                else: 
                                print "level 4" 
                                print (val - 6500) * 0.20 + (6500-3500) * 0.10 + (3500-1600) * 0.05 


#~def debug_demo                 

if __name__ == "__main__": 

                debug_demo(4500) 
 
 
2.开始运行调试
 
  运行程序./debug_demo.py,得到
 
> /usr/local/qspace/user_network/debug_demo.py(7)debug_demo() 
-> if val <= 1600 : 
(Pdb)
-> val <= 1600 : 指示当前执行的语句,(Pdb)等待你的调试指令. pdb的指令很丰富,输入h指令可以查看指令的使用方法。下面简单介绍常用指令:
  
  查看代码上下文,l(小写L)
 
(Pdb) l 
    2         _DEBUG=True 
    3         def debug_demo(val): 
    4                         if _DEBUG == True: 
    5                                         import pdb 
    6                                         pdb.set_trace() 
    7     ->                 if val <= 1600 : 
    8                                         print "level 1" 
    9                                         print 0 
10                         elif val <= 3500 : 
11                                         print "level 2" 
12                                         print (val - 1600) * 0.05 
(Pdb)    
 
左边是行号,右边是代码正文。
 
  监视变量:p 变量名
 
(Pdb) p val 
4500 
(Pdb)    
 
 单步执行: n
 
-> elif val <= 3500 : 
(Pdb) l 
    5                                         import pdb 
    6                                         pdb.set_trace() 
    7                         if val <= 1600 : 
    8                                         print "level 1" 
    9                                         print 0 
10    ->                 elif val <= 3500 : 
11                                         print "level 2" 
12                                         print (val - 1600) * 0.05 
13                         elif val <= 6500 : 
14                                         print "level 3" 
15                                         print (val - 3500) * 0.10 + (3500-1600) * 0.05 
 
 加入断点:b 行号
 
(Pdb) b 14 
 
运行到断点: c
 
(Pdb) c 
> /***** 
-> print "level 3" 
(Pdb) l 
    9                                         print 0 
10                         elif val <= 3500 : 
11                                         print "level 2" 
12                                         print (val - 1600) * 0.05 
13                         elif val <= 6500 : 
14 B->                                 print "level 3" 
15                                         print (val - 3500) * 0.10 + (3500-1600) * 0.05 
16                         else: 
17                                         print "level 4" 
18                                         print (val - 6500) * 0.20 + (6500-3500) * 0.10 + (3500-1600) * 0.05 
19     
 
执行到函数返回前: r
 
(Pdb) r 
level 3 
195.0 
--Return-- 
> /****()
->None 
-> print (val - 3500) * 0.10 + (3500-1600) * 0.05 
(Pdb)    
 
说明:
  pdb还有很多其他很多有用的指令,读者可以自行探索。输入h,h 命令。就可以得到命令的详细帮助。
  不过,我个人认为一般无需启动这种调试方法,一般使用日志输出进行调试即可,除非遇到了非常微妙的错误。这时,单步调试的威力便显示出来了。

 


转载声明: 本文转自  http://blogold.chinaunix.net/u2/63996/showart_1817104.html (ChinaUnix)

 


原文链接:http://blog.csdn.net/sunboy_2050/article/details/6183248
加载中
返回顶部
顶部