2007-08-15 11:28:22 -03:00
|
|
|
:mod:`cgitb` --- Traceback manager for CGI scripts
|
|
|
|
==================================================
|
|
|
|
|
|
|
|
.. module:: cgitb
|
|
|
|
:synopsis: Configurable traceback handler for CGI scripts.
|
2022-03-21 20:16:37 -03:00
|
|
|
:deprecated:
|
2016-06-11 16:02:54 -03:00
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
.. moduleauthor:: Ka-Ping Yee <ping@lfw.org>
|
|
|
|
.. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org>
|
|
|
|
|
2016-06-11 16:02:54 -03:00
|
|
|
**Source code:** :source:`Lib/cgitb.py`
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
.. index::
|
|
|
|
single: CGI; exceptions
|
|
|
|
single: CGI; tracebacks
|
|
|
|
single: exceptions; in CGI scripts
|
|
|
|
single: tracebacks; in CGI scripts
|
|
|
|
|
2022-03-21 20:16:37 -03:00
|
|
|
.. deprecated:: 3.11
|
|
|
|
The :mod:`cgitb` module is deprecated (see :pep:`594` for details).
|
|
|
|
|
2016-06-11 16:02:54 -03:00
|
|
|
--------------
|
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
The :mod:`cgitb` module provides a special exception handler for Python scripts.
|
|
|
|
(Its name is a bit misleading. It was originally designed to display extensive
|
|
|
|
traceback information in HTML for CGI scripts. It was later generalized to also
|
|
|
|
display this information in plain text.) After this module is activated, if an
|
|
|
|
uncaught exception occurs, a detailed, formatted report will be displayed. The
|
|
|
|
report includes a traceback showing excerpts of the source code for each level,
|
|
|
|
as well as the values of the arguments and local variables to currently running
|
|
|
|
functions, to help you debug the problem. Optionally, you can save this
|
|
|
|
information to a file instead of sending it to the browser.
|
|
|
|
|
Merged revisions 69803-69805,69840,69901,69905,69907,69924,69927,69987 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r69803 | georg.brandl | 2009-02-20 01:48:21 -0600 (Fri, 20 Feb 2009) | 1 line
#5327: fix a broken link by joining it.
........
r69804 | georg.brandl | 2009-02-20 02:22:21 -0600 (Fri, 20 Feb 2009) | 1 line
At least separate imports from other statements.
........
r69805 | georg.brandl | 2009-02-20 02:45:47 -0600 (Fri, 20 Feb 2009) | 2 lines
Fix punctuation.
........
r69840 | georg.brandl | 2009-02-21 13:09:40 -0600 (Sat, 21 Feb 2009) | 1 line
#5338, #5339: two types in the API manual.
........
r69901 | georg.brandl | 2009-02-23 05:24:46 -0600 (Mon, 23 Feb 2009) | 2 lines
#5349: C++ pure virtuals can also have an implementation.
........
r69905 | georg.brandl | 2009-02-23 09:51:27 -0600 (Mon, 23 Feb 2009) | 2 lines
#5352: str.count() counts non-overlapping instances.
........
r69907 | georg.brandl | 2009-02-23 12:33:48 -0600 (Mon, 23 Feb 2009) | 1 line
Fix grammar.
........
r69924 | benjamin.peterson | 2009-02-23 20:45:35 -0600 (Mon, 23 Feb 2009) | 1 line
update README on running tests
........
r69927 | neil.schemenauer | 2009-02-23 22:23:25 -0600 (Mon, 23 Feb 2009) | 1 line
Fix call to os.waitpid, it does not take keyword args.
........
r69987 | benjamin.peterson | 2009-02-25 18:30:11 -0600 (Wed, 25 Feb 2009) | 1 line
fix str.format()'s first arg #5371
........
2009-02-25 23:38:59 -04:00
|
|
|
To enable this feature, simply add this to the top of your CGI script::
|
2007-08-15 11:28:22 -03:00
|
|
|
|
Merged revisions 69803-69805,69840,69901,69905,69907,69924,69927,69987 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r69803 | georg.brandl | 2009-02-20 01:48:21 -0600 (Fri, 20 Feb 2009) | 1 line
#5327: fix a broken link by joining it.
........
r69804 | georg.brandl | 2009-02-20 02:22:21 -0600 (Fri, 20 Feb 2009) | 1 line
At least separate imports from other statements.
........
r69805 | georg.brandl | 2009-02-20 02:45:47 -0600 (Fri, 20 Feb 2009) | 2 lines
Fix punctuation.
........
r69840 | georg.brandl | 2009-02-21 13:09:40 -0600 (Sat, 21 Feb 2009) | 1 line
#5338, #5339: two types in the API manual.
........
r69901 | georg.brandl | 2009-02-23 05:24:46 -0600 (Mon, 23 Feb 2009) | 2 lines
#5349: C++ pure virtuals can also have an implementation.
........
r69905 | georg.brandl | 2009-02-23 09:51:27 -0600 (Mon, 23 Feb 2009) | 2 lines
#5352: str.count() counts non-overlapping instances.
........
r69907 | georg.brandl | 2009-02-23 12:33:48 -0600 (Mon, 23 Feb 2009) | 1 line
Fix grammar.
........
r69924 | benjamin.peterson | 2009-02-23 20:45:35 -0600 (Mon, 23 Feb 2009) | 1 line
update README on running tests
........
r69927 | neil.schemenauer | 2009-02-23 22:23:25 -0600 (Mon, 23 Feb 2009) | 1 line
Fix call to os.waitpid, it does not take keyword args.
........
r69987 | benjamin.peterson | 2009-02-25 18:30:11 -0600 (Wed, 25 Feb 2009) | 1 line
fix str.format()'s first arg #5371
........
2009-02-25 23:38:59 -04:00
|
|
|
import cgitb
|
|
|
|
cgitb.enable()
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
The options to the :func:`enable` function control whether the report is
|
|
|
|
displayed in the browser and whether the report is logged to a file for later
|
|
|
|
analysis.
|
|
|
|
|
|
|
|
|
2009-04-05 19:20:44 -03:00
|
|
|
.. function:: enable(display=1, logdir=None, context=5, format="html")
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
.. index:: single: excepthook() (in module sys)
|
|
|
|
|
|
|
|
This function causes the :mod:`cgitb` module to take over the interpreter's
|
|
|
|
default handling for exceptions by setting the value of :attr:`sys.excepthook`.
|
|
|
|
|
|
|
|
The optional argument *display* defaults to ``1`` and can be set to ``0`` to
|
|
|
|
suppress sending the traceback to the browser. If the argument *logdir* is
|
|
|
|
present, the traceback reports are written to files. The value of *logdir*
|
|
|
|
should be a directory where these files will be placed. The optional argument
|
|
|
|
*context* is the number of lines of context to display around the current line
|
|
|
|
of source code in the traceback; this defaults to ``5``. If the optional
|
|
|
|
argument *format* is ``"html"``, the output is formatted as HTML. Any other
|
|
|
|
value forces plain text output. The default value is ``"html"``.
|
|
|
|
|
|
|
|
|
2017-05-05 05:15:12 -03:00
|
|
|
.. function:: text(info, context=5)
|
|
|
|
|
|
|
|
This function handles the exception described by *info* (a 3-tuple containing
|
|
|
|
the result of :func:`sys.exc_info`), formatting its traceback as text and
|
|
|
|
returning the result as a string. The optional argument *context* is the
|
|
|
|
number of lines of context to display around the current line of source code
|
|
|
|
in the traceback; this defaults to ``5``.
|
|
|
|
|
|
|
|
|
|
|
|
.. function:: html(info, context=5)
|
|
|
|
|
|
|
|
This function handles the exception described by *info* (a 3-tuple containing
|
|
|
|
the result of :func:`sys.exc_info`), formatting its traceback as HTML and
|
|
|
|
returning the result as a string. The optional argument *context* is the
|
|
|
|
number of lines of context to display around the current line of source code
|
|
|
|
in the traceback; this defaults to ``5``.
|
|
|
|
|
|
|
|
|
2009-04-05 19:20:44 -03:00
|
|
|
.. function:: handler(info=None)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
This function handles an exception using the default settings (that is, show a
|
|
|
|
report in the browser, but don't log to a file). This can be used when you've
|
|
|
|
caught an exception and want to report it using :mod:`cgitb`. The optional
|
|
|
|
*info* argument should be a 3-tuple containing an exception type, exception
|
|
|
|
value, and traceback object, exactly like the tuple returned by
|
|
|
|
:func:`sys.exc_info`. If the *info* argument is not supplied, the current
|
|
|
|
exception is obtained from :func:`sys.exc_info`.
|
|
|
|
|