/Doc/library/cgitb.rst

http://unladen-swallow.googlecode.com/ · ReStructuredText · 65 lines · 47 code · 18 blank · 0 comment · 0 complexity · 8c714cc72cae9dad63cc72fc486abfff MD5 · raw file

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