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