pdb

作者: shuff1e | 来源:发表于2018-03-08 11:41 被阅读6次
    [root@shuffle-dev py_test]$ vim debug.py   
      1 #!/usr/bin/env python
      2 # -*- coding: utf-8 -*-
      3 
      4 from __future__ import division
      5 import pdb
      6 import subprocess
      7 
      8 def _executeCmd(cmd):
      9     pdb.set_trace()                                                                                                                       
     10     try:
     11         p = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
     12         output = ""
     13         for line in p.stdout.readlines():
     14             output = output + line
     15         retval = p.wait()
     16         pdb.set_trace()
     17         return output
     18     except BaseException,e:
     19         pdb.set_trace()
     20         pass
     21 
     22 if __name__=='__main__':
     23     cmd="free -g | grep - | awk '{print $4}'"
     24     print _executeCmd(cmd)
    
    [root@shuffle-dev py_test]$ python debug.py
    > /root/py_test/debug.py(10)_executeCmd()
    -> try:
    (Pdb) l
      5     import pdb
      6     import subprocess
      7  
      8     def _executeCmd(cmd):
      9         pdb.set_trace()
     10  ->     try:
     11             p = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
     12             output = ""
     13             for line in p.stdout.readlines():
     14                 output = output + line
     15             retval = p.wait()
    (Pdb) p cmd
    "free -g | grep - | awk '{print $4}'"
    (Pdb) c
    > /root/py_test/debug.py(17)_executeCmd()
    -> return output
    (Pdb) l
     12             output = ""
     13             for line in p.stdout.readlines():
     14                 output = output + line
     15             retval = p.wait()
     16             pdb.set_trace()
     17  ->         return output
     18         except BaseException,e:
     19             pdb.set_trace()
     20             pass
     21  
     22     if __name__=='__main__':
    (Pdb) p output
    '14\n'
    (Pdb) l
     23         cmd="free -g | grep - | awk '{print $4}'"
     24         print _executeCmd(cmd)
    [EOF]
    (Pdb) c
    14
    
    

    自己写的还是太垃圾,还是看别人的吧。

    [root@shuffle-dev py_test]$ vim utils.py
      1 #!/usr/bin/env python
      2 # -*- coding: utf-8 -*-
      3 
      4 def add(a,b):
      5     return a+b  
    
    [root@shuffle-dev py_test]$ vim debugBetter.py
      1 #!/usr/bin/env python
      2 # -*- coding: utf-8 -*-
      3 
      4 import utils
      5 
      6 def cal(a,b):
      7     import pdb;pdb.set_trace()                                                                                                            
      8     c=utils.add(a,b)
      9     print c
     10 
     11 if __name__=='__main__':
     12     cal(3,4)
    
    [root@shuffle-dev py_test]$ python debugBetter.py
    > /root/py_test/debugBetter.py(8)cal()
    -> c=utils.add(a,b)
    (Pdb) s
    --Call--
    > /root/py_test/utils.py(4)add()
    -> def add(a,b):
    (Pdb) l
      1     #!/usr/bin/env python
      2     # -*- coding: utf-8 -*-
      3  
      4  -> def add(a,b):
      5         return a+b
    [EOF]
    (Pdb) b 5
    Breakpoint 1 at /root/py_test/utils.py:5
    (Pdb) c
    > /root/py_test/utils.py(5)add()
    -> return a+b
    (Pdb) n
    --Return--
    > /root/py_test/utils.py(5)add()->7
    -> return a+b
    (Pdb) n
    > /root/py_test/debugBetter.py(9)cal()
    -> print c
    (Pdb) n
    7
    --Return--
    > /root/py_test/debugBetter.py(9)cal()->None
    -> print c
    (Pdb) p c
    7
    (Pdb) q
    Traceback (most recent call last):
      File "debugBetter.py", line 12, in <module>
        cal(3,4)
      File "debugBetter.py", line 9, in cal
        print c
      File "/usr/lib64/python2.6/bdb.py", line 50, in trace_dispatch
        return self.dispatch_return(frame, arg)
      File "/usr/lib64/python2.6/bdb.py", line 84, in dispatch_return
        if self.quitting: raise BdbQuit
    bdb.BdbQuit
    

    相关文章

      网友评论

          本文标题:pdb

          本文链接:https://www.haomeiwen.com/subject/etnefftx.html