cgitb — Traceback manager for CGI scripts¶
Source code:Lib/cgitb.py
Thecgitb module provides a special exception handler for Python scripts.(Its name is a bit misleading. It was originally designed to display extensivetraceback information in HTML for CGI scripts. It was later generalized to alsodisplay this information in plain text.) After this module is activated, if anuncaught exception occurs, a detailed, formatted report will be displayed. Thereport includes a traceback showing excerpts of the source code for each level,as well as the values of the arguments and local variables to currently runningfunctions, to help you debug the problem. Optionally, you can save thisinformation to a file instead of sending it to the browser.
To enable this feature, simply add this to the top of your CGI script:
importcgitbcgitb.enable()
The options to theenable() function control whether the report isdisplayed in the browser and whether the report is logged to a file for lateranalysis.
cgitb.enable(display=1,logdir=None,context=5,format="html")¶This function causes the
cgitbmodule to take over the interpreter’sdefault handling for exceptions by setting the value ofsys.excepthook.The optional argumentdisplay defaults to
1and can be set to0tosuppress sending the traceback to the browser. If the argumentlogdir ispresent, the traceback reports are written to files. The value oflogdirshould be a directory where these files will be placed. The optional argumentcontext is the number of lines of context to display around the current lineof source code in the traceback; this defaults to5. If the optionalargumentformat is"html", the output is formatted as HTML. Any othervalue forces plain text output. The default value is"html".
cgitb.text(info,context=5)¶This function handles the exception described byinfo (a 3-tuple containingthe result of
sys.exc_info()), formatting its traceback as text andreturning the result as a string. The optional argumentcontext is thenumber of lines of context to display around the current line of source codein the traceback; this defaults to5.
cgitb.html(info,context=5)¶This function handles the exception described byinfo (a 3-tuple containingthe result of
sys.exc_info()), formatting its traceback as HTML andreturning the result as a string. The optional argumentcontext is thenumber of lines of context to display around the current line of source codein the traceback; this defaults to5.
cgitb.handler(info=None)¶This function handles an exception using the default settings (that is, show areport in the browser, but don’t log to a file). This can be used when you’vecaught an exception and want to report it using
cgitb. The optionalinfo argument should be a 3-tuple containing an exception type, exceptionvalue, and traceback object, exactly like the tuple returned bysys.exc_info(). If theinfo argument is not supplied, the currentexception is obtained fromsys.exc_info().