Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to make a breakpoint on class member function of python? [duplicate]

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!

like image 608
user1058909 Avatar asked Nov 22 '11 01:11

user1058909


2 Answers

In pdb, the Python debugger, the breakpoint can be set with

b classname.methodname

after the class definition has been parsed.


For example,

% pdb ~/pybin/test.py

> /home/unutbu/pybin/test.py(4)<module>()
-> class Foo(object):
(Pdb) l
  1     #!/usr/bin/env python
  2     # coding: utf-8
  3     
  4  -> class Foo(object):
  5         def bar(self): pass
  6     
  7     foo=Foo()
  8     foo.bar()
[EOF]

Setting the breakpoint before parsing the class fails:

(Pdb) b Foo.bar
*** The specified object 'Foo.bar' is not a function
or was not found along sys.path.

but after parsing the class:

(Pdb) n
> /home/unutbu/pybin/test.py(7)<module>()
-> foo=Foo()
(Pdb) l
  2     # coding: utf-8
  3     
  4     class Foo(object):
  5         def bar(self): pass
  6     
  7  -> foo=Foo()
  8     foo.bar()
[EOF]

setting the breakpoint works:

(Pdb) b Foo.bar
Breakpoint 1 at /home/unutbu/pybin/test.py:5
(Pdb) 

(Pdb) r
> /home/unutbu/pybin/test.py(5)bar()
-> def bar(self): pass
like image 170
unutbu Avatar answered Nov 15 '22 17:11

unutbu


For a persistent break point, on the line before you want to break, you can also use:

import pdb; pdb.set_trace()

See the python doc for more detail.

like image 39
Nate Avatar answered Nov 15 '22 19:11

Nate