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