blog

PdbpPlease!

by

pdbpp is an awesome module that replaces pdb and makes debugging way nicer in Python. https://pypi.python.org/pypi/pdbpp/ A debugger is an extremely useful tool. I used to spend a lot of time with pdb. Then there was pdbpp and it has made debugging a much much nicer experience. For those who are not familiar with pdb, it’s a module you can use to set breakpoints in your Python code and then step around; execute Python; set further breakpoints etc. given:
[sourcecode language=”python”]
def doubleVals(d):
newD = {}
for key, val in d.iteritems():
newD[key] = val * 2
return newD
import pdb; pdb.set_trace()
a = {‘a’: 1, ‘b’:2}
a = doubleVals(a)
a.update({‘a’: 3, ‘c’: 4})
print a
[/sourcecode]
You can do this sort of thing: Software screen capture
[sourcecode language=”bash”]
$ python example.py

/home/fran/random/pdbpp_example/example.py(8)()
-> a = {‘a’: 1, ‘b’:2}
(Pdb) n
/home/fran/random/pdbpp_example/example.py(9)()
-> a = doubleVals(a)
(Pdb) s
–Call–
/home/fran/random/pdbpp_example/example.py(1)doubleVals()
-> def doubleVals(d):
(Pdb) l
1 -> def doubleVals(d):
2 newD = {}
3 for key, val in d.iteritems():
4 newD[key] = val * 2
5 return newD
6
7 import pdb; pdb.set_trace()
8 a = {‘a’: 1, ‘b’:2}
9 a = doubleVals(a)
10 a.update({‘a’: 3, ‘c’: 4})
11 print a
(Pdb) n
/home/fran/random/pdbpp_example/example.py(2)doubleVals()
-> newD = {}
(Pdb)
/home/fran/random/pdbpp_example/example.py(3)doubleVals()
-> for key, val in d.iteritems():
(Pdb)
/home/fran/random/pdbpp_example/example.py(4)doubleVals()
-> newD[key] = val * 2
(Pdb)
/home/fran/random/pdbpp_example/example.py(3)doubleVals()
-> for key, val in d.iteritems():
(Pdb) break 10
Breakpoint 1 at /home/fran/random/pdbpp_example/example.py:10
(Pdb) c
/home/fran/random/pdbpp_example/example.py(10)()
-> a.update({‘a’: 3, ‘c’: 4})
(Pdb) print a
{‘a’: 2, ‘b’: 4}
(Pdb) n
/home/fran/random/pdbpp_example/example.py(11)()
-> print a
(Pdb)
{‘a’: 3, ‘c’: 4, ‘b’: 4}
–Return–
/home/fran/random/pdbpp_example/example.py(11)()->None
-> print a
(Pdb) c
[/sourcecode]

and
[sourcecode language=”bash”]

/home/fran/random/pdbpp_example/example.py(8)()
-> a = {‘a’: 1, ‘b’:2}
(Pdb) n
/home/fran/random/pdbpp_example/example.py(9)()
-> a = doubleVals(a)
(Pdb) s
–Call–
/home/fran/random/pdbpp_example/example.py(1)doubleVals()
-> def doubleVals(d):
(Pdb) l
1 -> def doubleVals(d):
2 newD = {}
3 for key, val in d.iteritems():
4 newD[key] = val * 2
5 return newD
6
7 import pdb; pdb.set_trace()
8 a = {‘a’: 1, ‘b’:2}
9 a = doubleVals(a)
10 a.update({‘a’: 3, ‘c’: 4})
11 print a
(Pdb) w
/home/fran/random/pdbpp_example/example.py(9)()
-> a = doubleVals(a)
/home/fran/random/pdbpp_example/example.py(1)doubleVals()
-> def doubleVals(d):
(Pdb) u
/home/fran/random/pdbpp_example/example.py(9)()
-> a = doubleVals(a)
(Pdb) l
4 newD[key] = val * 2
5 return newD
6
7 import pdb; pdb.set_trace()
8 a = {‘a’: 1, ‘b’:2}
9 -> a = doubleVals(a)
10 a.update({‘a’: 3, ‘c’: 4})
11 print a
[EOF]
(Pdb) print a
{‘a’: 1, ‘b’: 2}
(Pdb) d
/home/fran/random/pdbpp_example/example.py(1)doubleVals()
-> def doubleVals(d):
(Pdb) print d
{‘a’: 1, ‘b’: 2}
[/sourcecode]

Where ‘n’ steps to the next line. ‘s’ steps in. ‘l’ lists around the current line of execution. ‘break’ adds breakpoints (in this case at line 10 in the module). ‘c’ continues until the next break. Hitting enter repeats whatever you did last. You can execute Python from the prompt so you can print things, or import pprint and pretty print them, or do anything you like. Examine the call stack with ‘w’ (where). Move up or down in the call stack with ‘u’ and ‘d’. pdbpp does all of this and more! Install with pip and do nothing else and now every time you hit a breakpoint you get pdbpp Software screen capture
For me the best things about pdbpp are the tab completion, ‘ll’ and the syntax highlighting. It also does other handy things like ‘source’ and ‘display’:
[sourcecode language=”bash”]
(Pdb++) source doubleVals
1 def doubleVals(d):
2 newD = {}
3 for key, val in d.iteritems():
4 newD[key] = val * 2
5 return newD
(Pdb++) display a
(Pdb++) n

/home/fran/random/pdbpp_example/example.py(9)()
-> a = doubleVals(a)
a: –> {‘a’: 1, ‘b’: 2}
(Pdb++) n
/home/fran/random/pdbpp_example/example.py(10)()
-> a.update({‘a’: 3, ‘c’: 4})
a: {‘a’: 1, ‘b’: 2} –> {‘a’: 2, ‘b’: 4}
(Pdb++)
[/sourcecode]

(these and more are described on the pypi page). A debugger is a useful tool and you should use good tools. Check it out.

+ more