Debugging in Python django using pdb
Debugger Module Contents
The pdb
module contains the debugger. pdb
contains one class, Pdb
, which inherits from bdb.Bdb
. The debugger documentation mentions six functions, which create an interactive debugging session:
pdb.run(statement[, globals[, locals]])
pdb.runeval(expression[, globals[, locals]])
pdb.runcall(function[, argument, ...])
pdb.set_trace()
pdb.post_mortem(traceback)
pdb.pm()
All six functions provide a slightly different mechanism for dropping a user into the debugger.
pdb.run(statement[, globals[, locals]])
pdb.run()
executes the string statement
under the debugger's control. Global and local dictionaries are optional parameters:
#!/usr/bin/env python
import pdb
def test_debugger(some_int):
print "start some_int>>", some_int
return_int = 10 / some_int
print "end some_int>>", some_int
return return_int
if __name__ == "__main__":
pdb.run("test_debugger(0)")
pdb.runeval(expression[, globals[, locals]])
pdb.runeval()
is identical to pdb.run()
, except that pdb.runeval()
returns the value of the evaluated string expression
:
#!/usr/bin/env python
import pdb
def test_debugger(some_int):
print "start some_int>>", some_int
return_int = 10 / some_int
print "end some_int>>", some_int
return return_int
if __name__ == "__main__":
pdb.runeval("test_debugger(0)")
pdb.runcall(function[, argument, ...])
pdb.runcall()
calls the specified function
and passes any specified arguments to it:
#!/usr/bin/env python
import pdb
def test_debugger(some_int):
print "start some_int>>", some_int
return_int = 10 / some_int
print "end some_int>>", some_int
return return_int
if __name__ == "__main__":
pdb.runcall(test_debugger, 0)
pdb.set_trace()
pdb.set_trace()
drops the code into the debugger when execution hits it:
#!/usr/bin/env python
import pdb
def test_debugger(some_int):
pdb.set_trace()
print "start some_int>>", some_int
return_int = 10 / some_int
print "end some_int>>", some_int
return return_int
if __name__ == "__main__":
test_debugger(0)
pdb.post_mortem(traceback)
pdb.post_mortem()
performs postmortem debugging of the specified traceback
:
#!/usr/bin/env python
import pdb
def test_debugger(some_int):
print "start some_int>>", some_int
return_int = 10 / some_int
print "end some_int>>", some_int
return return_int
if __name__ == "__main__":
try:
test_debugger(0)
except:
import sys
tb = sys.exc_info()[2]
pdb.post_mortem(tb)
pdb.pm()
pdb.pm()
performs postmortem debugging of the traceback contained in sys.last_traceback
:
#!/usr/bin/env python
import pdb
import sys
def test_debugger(some_int):
print "start some_int>>", some_int
return_int = 10 / some_int
print "end some_int>>", some_int
return return_int
def do_debugger(type, value, tb):
pdb.pm()
if __name__ == "__main__":
sys.excepthook = do_debugger
test_debugger(0)
Read More ...
No comments:
Post a Comment