Wednesday, July 22, 2009

The python debugger

Python has a debugging tool very similar to the gdb. This post would show how to start the debugger and the basic commands.

Starting the debugger
There are mainly two ways to start the debugger.
One is in the Python interpreter:

The debugger’s prompt is (Pdb). Typical usage to run a program under control of the debugger is:

>>> import pdb
>>> import mymodule
>>> pdb.run('mymodule.test()')
> (0)?()
(Pdb) continue
> (1)?()
(Pdb) continue
NameError: 'spam'
> (1)?()
(Pdb)

The other is from the command lines:

pdb.py can also be invoked as a script to debug other scripts.
For example:python -m pdb myscript.py
Some basic commands include:
1. Setting and removing breakpoints:
break(b) + line no, clear (cl) + break + breakid
2. checking all the breakpoints:
break
3. step into:
step(s)
4. step over:
next (n)
5. go to next breakpoint:
continue(c)
6. go to the end of the function:
r
7. jump to certain place
jump(j)
8. list the current code
list(l)
9. change the variable value of the code:
as Python is a script language, the value can be changed in the running time. just assign a new value to the language!

10. printing
p
how to print the lists, dictionares, sets?
print is a statement in the Python, not in the pdb module.

11. How to inspect the statement inside a loop?
Using conditions
condition bpnumber. Here condition is any expression that could be evaluated. It must be evaluated as true if the breakpoint takes effect.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.