blob: c686fe6a6c5ce267a492904d3972583620b3126a [file] [log] [blame]
Fred Drake6e70e8b2001-12-04 22:47:42 +00001\section{\module{cgitb} ---
2 Traceback manager for CGI scripts}
3
4\declaremodule{standard}{cgitb}
5\modulesynopsis{Configurable traceback handler for CGI scripts.}
Fred Drakee0889702001-12-18 15:51:55 +00006\moduleauthor{Ka-Ping Yee}{ping@lfw.org}
Fred Drake6e70e8b2001-12-04 22:47:42 +00007\sectionauthor{Fred L. Drake, Jr.}{fdrake@acm.org}
8
Fred Drakee0889702001-12-18 15:51:55 +00009\versionadded{2.2}
10\index{CGI!exceptions}
Fred Drake6e70e8b2001-12-04 22:47:42 +000011\index{CGI!tracebacks}
Fred Drake34a37b82001-12-20 17:13:09 +000012\index{exceptions!in CGI scripts}
13\index{tracebacks!in CGI scripts}
Fred Drake6e70e8b2001-12-04 22:47:42 +000014
Skip Montanaro364ca402003-06-17 12:58:31 +000015The \module{cgitb} module provides a special exception handler for Python
Johannes Gijsbers592ef612004-08-26 10:40:12 +000016scripts. (Its name is a bit misleading. It was originally designed to
Skip Montanaro364ca402003-06-17 12:58:31 +000017display extensive traceback information in HTML for CGI scripts. It was
18later generalized to also display this information in plain text.) After
19this module is activated, if an uncaught exception occurs, a detailed,
20formatted report will be displayed. The report
Fred Drake34a37b82001-12-20 17:13:09 +000021includes a traceback showing excerpts of the source code for each level,
22as well as the values of the arguments and local variables to currently
Fred Drakee0889702001-12-18 15:51:55 +000023running functions, to help you debug the problem. Optionally, you can
24save this information to a file instead of sending it to the browser.
25
26To enable this feature, simply add one line to the top of your CGI script:
27
28\begin{verbatim}
29import cgitb; cgitb.enable()
30\end{verbatim}
31
32The options to the \function{enable()} function control whether the
33report is displayed in the browser and whether the report is logged
34to a file for later analysis.
Fred Drake6e70e8b2001-12-04 22:47:42 +000035
36
37\begin{funcdesc}{enable}{\optional{display\optional{, logdir\optional{,
Skip Montanaro364ca402003-06-17 12:58:31 +000038 context\optional{, format}}}}}
Fred Drake6e70e8b2001-12-04 22:47:42 +000039 This function causes the \module{cgitb} module to take over the
Fred Drakee0889702001-12-18 15:51:55 +000040 interpreter's default handling for exceptions by setting the
Fred Drake6e70e8b2001-12-04 22:47:42 +000041 value of \code{\refmodule{sys}.excepthook}.
Fred Drakee0889702001-12-18 15:51:55 +000042 \withsubitem{(in module sys)}{\ttindex{excepthook()}}
Fred Drake6e70e8b2001-12-04 22:47:42 +000043
Fred Drakee0889702001-12-18 15:51:55 +000044 The optional argument \var{display} defaults to \code{1} and can be set
45 to \code{0} to suppress sending the traceback to the browser.
46 If the argument \var{logdir} is present, the traceback reports are
47 written to files. The value of \var{logdir} should be a directory
48 where these files will be placed.
49 The optional argument \var{context} is the number of lines of
50 context to display around the current line of source code in the
51 traceback; this defaults to \code{5}.
Skip Montanaro364ca402003-06-17 12:58:31 +000052 If the optional argument \var{format} is \code{"html"}, the output is
53 formatted as HTML. Any other value forces plain text output. The default
54 value is \code{"html"}.
Fred Drake6e70e8b2001-12-04 22:47:42 +000055\end{funcdesc}
56
57\begin{funcdesc}{handler}{\optional{info}}
Fred Drakee0889702001-12-18 15:51:55 +000058 This function handles an exception using the default settings
59 (that is, show a report in the browser, but don't log to a file).
60 This can be used when you've caught an exception and want to
61 report it using \module{cgitb}. The optional \var{info} argument
62 should be a 3-tuple containing an exception type, exception
63 value, and traceback object, exactly like the tuple returned by
64 \code{\refmodule{sys}.exc_info()}. If the \var{info} argument
65 is not supplied, the current exception is obtained from
66 \code{\refmodule{sys}.exc_info()}.
Fred Drake6e70e8b2001-12-04 22:47:42 +000067\end{funcdesc}