blob: 621d3257c605a7b03ab76b2be3a7d738d396fffe [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 Drakee0889702001-12-18 15:51:55 +000012\index{exception!in CGI scripts}
Fred Drake6e70e8b2001-12-04 22:47:42 +000013\index{traceback!in CGI scripts}
14
15The \module{cgitb} module provides a special exception handler for CGI
Fred Drakee0889702001-12-18 15:51:55 +000016scripts. After this module is activated using the \function{enable()}
17function, if an uncaught exception occurs, a detailed, formatted
18report will be sent to the Web browser. The report includes a
19traceback showing excerpts of the source code for each level, as well
20as the values of the arguments and local variables to currently
21running functions, to help you debug the problem. Optionally, you can
22save this information to a file instead of sending it to the browser.
23
24To enable this feature, simply add one line to the top of your CGI script:
25
26\begin{verbatim}
27import cgitb; cgitb.enable()
28\end{verbatim}
29
30The options to the \function{enable()} function control whether the
31report is displayed in the browser and whether the report is logged
32to a file for later analysis.
Fred Drake6e70e8b2001-12-04 22:47:42 +000033
34
35\begin{funcdesc}{enable}{\optional{display\optional{, logdir\optional{,
36 context}}}}
37 This function causes the \module{cgitb} module to take over the
Fred Drakee0889702001-12-18 15:51:55 +000038 interpreter's default handling for exceptions by setting the
Fred Drake6e70e8b2001-12-04 22:47:42 +000039 value of \code{\refmodule{sys}.excepthook}.
Fred Drakee0889702001-12-18 15:51:55 +000040 \withsubitem{(in module sys)}{\ttindex{excepthook()}}
Fred Drake6e70e8b2001-12-04 22:47:42 +000041
Fred Drakee0889702001-12-18 15:51:55 +000042 The optional argument \var{display} defaults to \code{1} and can be set
43 to \code{0} to suppress sending the traceback to the browser.
44 If the argument \var{logdir} is present, the traceback reports are
45 written to files. The value of \var{logdir} should be a directory
46 where these files will be placed.
47 The optional argument \var{context} is the number of lines of
48 context to display around the current line of source code in the
49 traceback; this defaults to \code{5}.
Fred Drake6e70e8b2001-12-04 22:47:42 +000050\end{funcdesc}
51
52\begin{funcdesc}{handler}{\optional{info}}
Fred Drakee0889702001-12-18 15:51:55 +000053 This function handles an exception using the default settings
54 (that is, show a report in the browser, but don't log to a file).
55 This can be used when you've caught an exception and want to
56 report it using \module{cgitb}. The optional \var{info} argument
57 should be a 3-tuple containing an exception type, exception
58 value, and traceback object, exactly like the tuple returned by
59 \code{\refmodule{sys}.exc_info()}. If the \var{info} argument
60 is not supplied, the current exception is obtained from
61 \code{\refmodule{sys}.exc_info()}.
Fred Drake6e70e8b2001-12-04 22:47:42 +000062\end{funcdesc}