blob: 854ea95c00e5fbe3e113bf2fe728b75bd97b458a [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001
2:mod:`cgitb` --- Traceback manager for CGI scripts
3==================================================
4
5.. module:: cgitb
6 :synopsis: Configurable traceback handler for CGI scripts.
7.. moduleauthor:: Ka-Ping Yee <ping@lfw.org>
8.. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org>
9
10
Georg Brandl116aa622007-08-15 14:28:22 +000011.. index::
12 single: CGI; exceptions
13 single: CGI; tracebacks
14 single: exceptions; in CGI scripts
15 single: tracebacks; in CGI scripts
16
17The :mod:`cgitb` module provides a special exception handler for Python scripts.
18(Its name is a bit misleading. It was originally designed to display extensive
19traceback information in HTML for CGI scripts. It was later generalized to also
20display this information in plain text.) After this module is activated, if an
21uncaught exception occurs, a detailed, formatted report will be displayed. The
22report includes a traceback showing excerpts of the source code for each level,
23as well as the values of the arguments and local variables to currently running
24functions, to help you debug the problem. Optionally, you can save this
25information to a file instead of sending it to the browser.
26
Benjamin Petersonad3d5c22009-02-26 03:38:59 +000027To enable this feature, simply add this to the top of your CGI script::
Georg Brandl116aa622007-08-15 14:28:22 +000028
Benjamin Petersonad3d5c22009-02-26 03:38:59 +000029 import cgitb
30 cgitb.enable()
Georg Brandl116aa622007-08-15 14:28:22 +000031
32The options to the :func:`enable` function control whether the report is
33displayed in the browser and whether the report is logged to a file for later
34analysis.
35
36
37.. function:: enable([display[, logdir[, context[, format]]]])
38
39 .. index:: single: excepthook() (in module sys)
40
41 This function causes the :mod:`cgitb` module to take over the interpreter's
42 default handling for exceptions by setting the value of :attr:`sys.excepthook`.
43
44 The optional argument *display* defaults to ``1`` and can be set to ``0`` to
45 suppress sending the traceback to the browser. If the argument *logdir* is
46 present, the traceback reports are written to files. The value of *logdir*
47 should be a directory where these files will be placed. The optional argument
48 *context* is the number of lines of context to display around the current line
49 of source code in the traceback; this defaults to ``5``. If the optional
50 argument *format* is ``"html"``, the output is formatted as HTML. Any other
51 value forces plain text output. The default value is ``"html"``.
52
53
54.. function:: handler([info])
55
56 This function handles an exception using the default settings (that is, show a
57 report in the browser, but don't log to a file). This can be used when you've
58 caught an exception and want to report it using :mod:`cgitb`. The optional
59 *info* argument should be a 3-tuple containing an exception type, exception
60 value, and traceback object, exactly like the tuple returned by
61 :func:`sys.exc_info`. If the *info* argument is not supplied, the current
62 exception is obtained from :func:`sys.exc_info`.
63