PageRenderTime 18ms CodeModel.GetById 14ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

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