程式扎記: [ Python 常見問題 ] pdb : How to make a breakpoint on class member function of python?

標籤

2015年5月25日 星期一

[ Python 常見問題 ] pdb : How to make a breakpoint on class member function of python?

Source From Here
Question
I used b "classname:function" or "b classname::function", and those didn't work. now I use "b linenum" as a workaround.but as I modifiy my code frequently, linenum changes.So how to make a breakpoint on class member function in python? I google it && read the python manual, and there's no direct answer.thanks!

How-To
In pdb, the Python debugger, the breakpoint can be set with:
b(reak) [[filename:]lineno | function[, condition]]
With a lineno argument, set a break there in the current file. With a function argument, set a break at the first executable statement within that function. The line number may be prefixed with a filename and a colon, to specify a breakpoint in another file (probably one that hasn’t been loaded yet). The file is searched on sys.path. Note that each breakpoint is assigned a number to which all the other breakpoint commands refer.

If a second argument is present, it is an expression which must evaluate to true before the breakpoint is honored.

Without argument, list all breaks, including for each breakpoint, the number of times that breakpoint has been hit, the current ignore count, and the associated condition if any.

b(reak) classname.methodname
After the class definition has been parsed.

For example,
  1. % pdb ~/pybin/test.py  
  2.   
  3. > /home/unutbu/pybin/test.py(4)()  
  4. -> class Foo(object):  
  5. (Pdb) l  
  6.   1     #!/usr/bin/env python  
  7.   2     # coding: utf-8  
  8.   3       
  9.   4  -> class Foo(object):  
  10.   5         def bar(self): pass  
  11.   6       
  12.   7     foo=Foo()  
  13.   8     foo.bar()  
  14. [EOF]  
Setting the breakpoint before parsing the class fails:
  1. (Pdb) b Foo.bar  
  2. *** The specified object 'Foo.bar' is not a function  
  3. or was not found along sys.path.  
but after parsing the class:
  1. (Pdb) n  
  2. > /home/unutbu/pybin/test.py(7)()  
  3. -> foo=Foo()  
  4. (Pdb) l  
  5.   2     # coding: utf-8  
  6.   3       
  7.   4     class Foo(object):  
  8.   5         def bar(self): pass  
  9.   6       
  10.   7  -> foo=Foo()  
  11.   8     foo.bar()  
  12. [EOF]  
setting the breakpoint works:
  1. (Pdb) b Foo.bar  
  2. Breakpoint 1 at /home/unutbu/pybin/test.py:5  
  3. (Pdb)   
  4.   
  5. (Pdb) r  
  6. > /home/unutbu/pybin/test.py(5)bar()  
  7. -> def bar(self): pass  


沒有留言:

張貼留言

網誌存檔

關於我自己

我的相片
Where there is a will, there is a way!