1.6.7 Debugging Code

If you are using the web.error module from a command line or supporting webserver such as scripts/webserver.py in the source directory you can raise a web.error.Breakpoint Exception and it will be caught and provide a prompt from which you can debug your code.

#!/usr/bin/env python

# show python where the modules are
import sys; sys.path.append('../'); sys.path.append('../../../') 

import web.error; web.error.handle('debug')

print "Setting value to 5"
value = 5
print "Raising a Breakpoint so you can inspect value"
raise web.error.Breakpoint
print "The program has exited so this will not be printed"

This code provides a prompt that can be used as follows:

> python "command-web-error-debug.py"
Setting value to 5
Raising a Breakpoint so you can inspect value
> y:\doc\src\lib\command-web-error-debug.py(11)?()
-> raise web.error.Breakpoint
(Pdb) value
(Pdb) exit

The prompt uses the pdb module. To exit the debugger type exit and press Return. Code after the web.error.Breakpoint Exception was raised is not executed.

See Also:

The Python Debugger To find out more about the Python Debugger see the documentation for the pdb module distributed with Python.