17db96d56Sopenharmony_ci:mod:`cgitb` --- Traceback manager for CGI scripts
27db96d56Sopenharmony_ci==================================================
37db96d56Sopenharmony_ci
47db96d56Sopenharmony_ci.. module:: cgitb
57db96d56Sopenharmony_ci   :synopsis: Configurable traceback handler for CGI scripts.
67db96d56Sopenharmony_ci   :deprecated:
77db96d56Sopenharmony_ci
87db96d56Sopenharmony_ci.. moduleauthor:: Ka-Ping Yee <ping@lfw.org>
97db96d56Sopenharmony_ci.. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org>
107db96d56Sopenharmony_ci
117db96d56Sopenharmony_ci**Source code:** :source:`Lib/cgitb.py`
127db96d56Sopenharmony_ci
137db96d56Sopenharmony_ci.. index::
147db96d56Sopenharmony_ci   single: CGI; exceptions
157db96d56Sopenharmony_ci   single: CGI; tracebacks
167db96d56Sopenharmony_ci   single: exceptions; in CGI scripts
177db96d56Sopenharmony_ci   single: tracebacks; in CGI scripts
187db96d56Sopenharmony_ci
197db96d56Sopenharmony_ci.. deprecated-removed:: 3.11 3.13
207db96d56Sopenharmony_ci   The :mod:`cgitb` module is deprecated
217db96d56Sopenharmony_ci   (see :pep:`PEP 594 <594#cgitb>` for details).
227db96d56Sopenharmony_ci
237db96d56Sopenharmony_ci--------------
247db96d56Sopenharmony_ci
257db96d56Sopenharmony_ciThe :mod:`cgitb` module provides a special exception handler for Python scripts.
267db96d56Sopenharmony_ci(Its name is a bit misleading.  It was originally designed to display extensive
277db96d56Sopenharmony_citraceback information in HTML for CGI scripts.  It was later generalized to also
287db96d56Sopenharmony_cidisplay this information in plain text.)  After this module is activated, if an
297db96d56Sopenharmony_ciuncaught exception occurs, a detailed, formatted report will be displayed.  The
307db96d56Sopenharmony_cireport includes a traceback showing excerpts of the source code for each level,
317db96d56Sopenharmony_cias well as the values of the arguments and local variables to currently running
327db96d56Sopenharmony_cifunctions, to help you debug the problem.  Optionally, you can save this
337db96d56Sopenharmony_ciinformation to a file instead of sending it to the browser.
347db96d56Sopenharmony_ci
357db96d56Sopenharmony_ciTo enable this feature, simply add this to the top of your CGI script::
367db96d56Sopenharmony_ci
377db96d56Sopenharmony_ci   import cgitb
387db96d56Sopenharmony_ci   cgitb.enable()
397db96d56Sopenharmony_ci
407db96d56Sopenharmony_ciThe options to the :func:`enable` function control whether the report is
417db96d56Sopenharmony_cidisplayed in the browser and whether the report is logged to a file for later
427db96d56Sopenharmony_cianalysis.
437db96d56Sopenharmony_ci
447db96d56Sopenharmony_ci
457db96d56Sopenharmony_ci.. function:: enable(display=1, logdir=None, context=5, format="html")
467db96d56Sopenharmony_ci
477db96d56Sopenharmony_ci   .. index:: single: excepthook() (in module sys)
487db96d56Sopenharmony_ci
497db96d56Sopenharmony_ci   This function causes the :mod:`cgitb` module to take over the interpreter's
507db96d56Sopenharmony_ci   default handling for exceptions by setting the value of :attr:`sys.excepthook`.
517db96d56Sopenharmony_ci
527db96d56Sopenharmony_ci   The optional argument *display* defaults to ``1`` and can be set to ``0`` to
537db96d56Sopenharmony_ci   suppress sending the traceback to the browser. If the argument *logdir* is
547db96d56Sopenharmony_ci   present, the traceback reports are written to files.  The value of *logdir*
557db96d56Sopenharmony_ci   should be a directory where these files will be placed. The optional argument
567db96d56Sopenharmony_ci   *context* is the number of lines of context to display around the current line
577db96d56Sopenharmony_ci   of source code in the traceback; this defaults to ``5``. If the optional
587db96d56Sopenharmony_ci   argument *format* is ``"html"``, the output is formatted as HTML.  Any other
597db96d56Sopenharmony_ci   value forces plain text output.  The default value is ``"html"``.
607db96d56Sopenharmony_ci
617db96d56Sopenharmony_ci
627db96d56Sopenharmony_ci.. function:: text(info, context=5)
637db96d56Sopenharmony_ci
647db96d56Sopenharmony_ci   This function handles the exception described by *info* (a 3-tuple containing
657db96d56Sopenharmony_ci   the result of :func:`sys.exc_info`), formatting its traceback as text and
667db96d56Sopenharmony_ci   returning the result as a string. The optional argument *context* is the
677db96d56Sopenharmony_ci   number of lines of context to display around the current line of source code
687db96d56Sopenharmony_ci   in the traceback; this defaults to ``5``.
697db96d56Sopenharmony_ci
707db96d56Sopenharmony_ci
717db96d56Sopenharmony_ci.. function:: html(info, context=5)
727db96d56Sopenharmony_ci
737db96d56Sopenharmony_ci   This function handles the exception described by *info* (a 3-tuple containing
747db96d56Sopenharmony_ci   the result of :func:`sys.exc_info`), formatting its traceback as HTML and
757db96d56Sopenharmony_ci   returning the result as a string. The optional argument *context* is the
767db96d56Sopenharmony_ci   number of lines of context to display around the current line of source code
777db96d56Sopenharmony_ci   in the traceback; this defaults to ``5``.
787db96d56Sopenharmony_ci
797db96d56Sopenharmony_ci
807db96d56Sopenharmony_ci.. function:: handler(info=None)
817db96d56Sopenharmony_ci
827db96d56Sopenharmony_ci   This function handles an exception using the default settings (that is, show a
837db96d56Sopenharmony_ci   report in the browser, but don't log to a file). This can be used when you've
847db96d56Sopenharmony_ci   caught an exception and want to report it using :mod:`cgitb`.  The optional
857db96d56Sopenharmony_ci   *info* argument should be a 3-tuple containing an exception type, exception
867db96d56Sopenharmony_ci   value, and traceback object, exactly like the tuple returned by
877db96d56Sopenharmony_ci   :func:`sys.exc_info`.  If the *info* argument is not supplied, the current
887db96d56Sopenharmony_ci   exception is obtained from :func:`sys.exc_info`.
897db96d56Sopenharmony_ci
90