Skip Montanaro | 364ca40 | 2003-06-17 12:58:31 +0000 | [diff] [blame] | 1 | """More comprehensive traceback formatting for Python scripts. |
Ka-Ping Yee | 6b5a48d | 2001-08-18 04:04:50 +0000 | [diff] [blame] | 2 | |
| 3 | To enable this module, do: |
| 4 | |
| 5 | import cgitb; cgitb.enable() |
| 6 | |
Skip Montanaro | 364ca40 | 2003-06-17 12:58:31 +0000 | [diff] [blame] | 7 | at the top of your script. The optional arguments to enable() are: |
Ka-Ping Yee | 6b5a48d | 2001-08-18 04:04:50 +0000 | [diff] [blame] | 8 | |
| 9 | display - if true, tracebacks are displayed in the web browser |
| 10 | logdir - if set, tracebacks are written to files in this directory |
Ka-Ping Yee | 8320597 | 2001-08-21 06:53:01 +0000 | [diff] [blame] | 11 | context - number of lines of source code to show for each stack frame |
Tim Peters | 478c105 | 2003-06-29 05:46:54 +0000 | [diff] [blame] | 12 | format - 'text' or 'html' controls the output format |
Ka-Ping Yee | 6b5a48d | 2001-08-18 04:04:50 +0000 | [diff] [blame] | 13 | |
Skip Montanaro | 364ca40 | 2003-06-17 12:58:31 +0000 | [diff] [blame] | 14 | By default, tracebacks are displayed but not saved, the context is 5 lines |
| 15 | and the output format is 'html' (for backwards compatibility with the |
| 16 | original use of this module) |
Ka-Ping Yee | 6b5a48d | 2001-08-18 04:04:50 +0000 | [diff] [blame] | 17 | |
| 18 | Alternatively, if you have caught an exception and want cgitb to display it |
Skip Montanaro | 364ca40 | 2003-06-17 12:58:31 +0000 | [diff] [blame] | 19 | for you, call cgitb.handler(). The optional argument to handler() is a |
| 20 | 3-item tuple (etype, evalue, etb) just like the value of sys.exc_info(). |
| 21 | The default handler displays output as HTML. |
Brett Cannon | f47e84c | 2009-04-01 16:06:01 +0000 | [diff] [blame] | 22 | |
Skip Montanaro | 364ca40 | 2003-06-17 12:58:31 +0000 | [diff] [blame] | 23 | """ |
Brett Cannon | f47e84c | 2009-04-01 16:06:01 +0000 | [diff] [blame] | 24 | import inspect |
| 25 | import keyword |
| 26 | import linecache |
| 27 | import os |
| 28 | import pydoc |
Ka-Ping Yee | fa78d0f | 2001-12-04 18:45:17 +0000 | [diff] [blame] | 29 | import sys |
Brett Cannon | f47e84c | 2009-04-01 16:06:01 +0000 | [diff] [blame] | 30 | import tempfile |
| 31 | import time |
| 32 | import tokenize |
| 33 | import traceback |
Ka-Ping Yee | fa78d0f | 2001-12-04 18:45:17 +0000 | [diff] [blame] | 34 | |
Ka-Ping Yee | 6b5a48d | 2001-08-18 04:04:50 +0000 | [diff] [blame] | 35 | def reset(): |
| 36 | """Return a string that resets the CGI and browser to a known state.""" |
| 37 | return '''<!--: spam |
| 38 | Content-Type: text/html |
| 39 | |
Ka-Ping Yee | 8320597 | 2001-08-21 06:53:01 +0000 | [diff] [blame] | 40 | <body bgcolor="#f0f0f8"><font color="#f0f0f8" size="-5"> --> |
| 41 | <body bgcolor="#f0f0f8"><font color="#f0f0f8" size="-5"> --> --> |
Ka-Ping Yee | 6b5a48d | 2001-08-18 04:04:50 +0000 | [diff] [blame] | 42 | </font> </font> </font> </script> </object> </blockquote> </pre> |
| 43 | </table> </table> </table> </table> </table> </font> </font> </font>''' |
| 44 | |
Ka-Ping Yee | 8320597 | 2001-08-21 06:53:01 +0000 | [diff] [blame] | 45 | __UNDEF__ = [] # a special sentinel object |
Andrew M. Kuchling | 5fcefdb | 2004-07-10 14:14:51 +0000 | [diff] [blame] | 46 | def small(text): |
| 47 | if text: |
| 48 | return '<small>' + text + '</small>' |
| 49 | else: |
| 50 | return '' |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 51 | |
Andrew M. Kuchling | 5fcefdb | 2004-07-10 14:14:51 +0000 | [diff] [blame] | 52 | def strong(text): |
| 53 | if text: |
| 54 | return '<strong>' + text + '</strong>' |
| 55 | else: |
| 56 | return '' |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 57 | |
Andrew M. Kuchling | 5fcefdb | 2004-07-10 14:14:51 +0000 | [diff] [blame] | 58 | def grey(text): |
| 59 | if text: |
| 60 | return '<font color="#909090">' + text + '</font>' |
| 61 | else: |
| 62 | return '' |
Ka-Ping Yee | 8320597 | 2001-08-21 06:53:01 +0000 | [diff] [blame] | 63 | |
| 64 | def lookup(name, frame, locals): |
| 65 | """Find the value for a given name in the given environment.""" |
| 66 | if name in locals: |
| 67 | return 'local', locals[name] |
| 68 | if name in frame.f_globals: |
| 69 | return 'global', frame.f_globals[name] |
Ka-Ping Yee | 711cad7 | 2002-06-26 07:10:56 +0000 | [diff] [blame] | 70 | if '__builtins__' in frame.f_globals: |
| 71 | builtins = frame.f_globals['__builtins__'] |
| 72 | if type(builtins) is type({}): |
| 73 | if name in builtins: |
| 74 | return 'builtin', builtins[name] |
| 75 | else: |
| 76 | if hasattr(builtins, name): |
| 77 | return 'builtin', getattr(builtins, name) |
Ka-Ping Yee | 8320597 | 2001-08-21 06:53:01 +0000 | [diff] [blame] | 78 | return None, __UNDEF__ |
| 79 | |
| 80 | def scanvars(reader, frame, locals): |
| 81 | """Scan one logical line of Python and look up values of variables used.""" |
Andrew M. Kuchling | 26f6bdf | 2004-06-05 19:15:34 +0000 | [diff] [blame] | 82 | vars, lasttoken, parent, prefix, value = [], None, None, '', __UNDEF__ |
Ka-Ping Yee | 8320597 | 2001-08-21 06:53:01 +0000 | [diff] [blame] | 83 | for ttype, token, start, end, line in tokenize.generate_tokens(reader): |
| 84 | if ttype == tokenize.NEWLINE: break |
| 85 | if ttype == tokenize.NAME and token not in keyword.kwlist: |
| 86 | if lasttoken == '.': |
| 87 | if parent is not __UNDEF__: |
| 88 | value = getattr(parent, token, __UNDEF__) |
| 89 | vars.append((prefix + token, prefix, value)) |
| 90 | else: |
| 91 | where, value = lookup(token, frame, locals) |
| 92 | vars.append((token, where, value)) |
| 93 | elif token == '.': |
| 94 | prefix += lasttoken + '.' |
| 95 | parent = value |
| 96 | else: |
| 97 | parent, prefix = None, '' |
| 98 | lasttoken = token |
| 99 | return vars |
| 100 | |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 101 | def html(einfo, context=5): |
Ka-Ping Yee | 8320597 | 2001-08-21 06:53:01 +0000 | [diff] [blame] | 102 | """Return a nice HTML document describing a given traceback.""" |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 103 | etype, evalue, etb = einfo |
Guido van Rossum | 1325790 | 2007-06-07 23:15:56 +0000 | [diff] [blame] | 104 | if isinstance(etype, type): |
Ka-Ping Yee | 6b5a48d | 2001-08-18 04:04:50 +0000 | [diff] [blame] | 105 | etype = etype.__name__ |
| 106 | pyver = 'Python ' + sys.version.split()[0] + ': ' + sys.executable |
| 107 | date = time.ctime(time.time()) |
Ka-Ping Yee | 8320597 | 2001-08-21 06:53:01 +0000 | [diff] [blame] | 108 | head = '<body bgcolor="#f0f0f8">' + pydoc.html.heading( |
Andrew M. Kuchling | 5fcefdb | 2004-07-10 14:14:51 +0000 | [diff] [blame] | 109 | '<big><big>%s</big></big>' % |
| 110 | strong(pydoc.html.escape(str(etype))), |
Ka-Ping Yee | 8320597 | 2001-08-21 06:53:01 +0000 | [diff] [blame] | 111 | '#ffffff', '#6622aa', pyver + '<br>' + date) + ''' |
| 112 | <p>A problem occurred in a Python script. Here is the sequence of |
Andrew M. Kuchling | 5fcefdb | 2004-07-10 14:14:51 +0000 | [diff] [blame] | 113 | function calls leading up to the error, in the order they occurred.</p>''' |
Ka-Ping Yee | 6b5a48d | 2001-08-18 04:04:50 +0000 | [diff] [blame] | 114 | |
Ka-Ping Yee | 8320597 | 2001-08-21 06:53:01 +0000 | [diff] [blame] | 115 | indent = '<tt>' + small(' ' * 5) + ' </tt>' |
Ka-Ping Yee | 6b5a48d | 2001-08-18 04:04:50 +0000 | [diff] [blame] | 116 | frames = [] |
| 117 | records = inspect.getinnerframes(etb, context) |
| 118 | for frame, file, lnum, func, lines, index in records: |
Georg Brandl | 07c81d9 | 2005-06-26 21:57:55 +0000 | [diff] [blame] | 119 | if file: |
| 120 | file = os.path.abspath(file) |
| 121 | link = '<a href="file://%s">%s</a>' % (file, pydoc.html.escape(file)) |
| 122 | else: |
| 123 | file = link = '?' |
Ka-Ping Yee | 6b5a48d | 2001-08-18 04:04:50 +0000 | [diff] [blame] | 124 | args, varargs, varkw, locals = inspect.getargvalues(frame) |
Ka-Ping Yee | 8320597 | 2001-08-21 06:53:01 +0000 | [diff] [blame] | 125 | call = '' |
| 126 | if func != '?': |
sblondon | 8cf4b34 | 2018-05-09 11:39:32 +0200 | [diff] [blame] | 127 | call = 'in ' + strong(pydoc.html.escape(func)) |
| 128 | if func != "<module>": |
| 129 | call += inspect.formatargvalues(args, varargs, varkw, locals, |
Ka-Ping Yee | 8320597 | 2001-08-21 06:53:01 +0000 | [diff] [blame] | 130 | formatvalue=lambda value: '=' + pydoc.html.repr(value)) |
Ka-Ping Yee | 6b5a48d | 2001-08-18 04:04:50 +0000 | [diff] [blame] | 131 | |
Ka-Ping Yee | 8320597 | 2001-08-21 06:53:01 +0000 | [diff] [blame] | 132 | highlight = {} |
| 133 | def reader(lnum=[lnum]): |
| 134 | highlight[lnum[0]] = 1 |
| 135 | try: return linecache.getline(file, lnum[0]) |
| 136 | finally: lnum[0] += 1 |
| 137 | vars = scanvars(reader, frame, locals) |
Ka-Ping Yee | 6b5a48d | 2001-08-18 04:04:50 +0000 | [diff] [blame] | 138 | |
Ka-Ping Yee | 8320597 | 2001-08-21 06:53:01 +0000 | [diff] [blame] | 139 | rows = ['<tr><td bgcolor="#d8bbff">%s%s %s</td></tr>' % |
| 140 | ('<big> </big>', link, call)] |
Ka-Ping Yee | 6b5a48d | 2001-08-18 04:04:50 +0000 | [diff] [blame] | 141 | if index is not None: |
| 142 | i = lnum - index |
| 143 | for line in lines: |
Ka-Ping Yee | 8320597 | 2001-08-21 06:53:01 +0000 | [diff] [blame] | 144 | num = small(' ' * (5-len(str(i))) + str(i)) + ' ' |
Ka-Ping Yee | 8320597 | 2001-08-21 06:53:01 +0000 | [diff] [blame] | 145 | if i in highlight: |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 146 | line = '<tt>=>%s%s</tt>' % (num, pydoc.html.preformat(line)) |
Ka-Ping Yee | 8320597 | 2001-08-21 06:53:01 +0000 | [diff] [blame] | 147 | rows.append('<tr><td bgcolor="#ffccee">%s</td></tr>' % line) |
| 148 | else: |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 149 | line = '<tt> %s%s</tt>' % (num, pydoc.html.preformat(line)) |
Ka-Ping Yee | 8320597 | 2001-08-21 06:53:01 +0000 | [diff] [blame] | 150 | rows.append('<tr><td>%s</td></tr>' % grey(line)) |
| 151 | i += 1 |
Ka-Ping Yee | 6b5a48d | 2001-08-18 04:04:50 +0000 | [diff] [blame] | 152 | |
Ka-Ping Yee | 8320597 | 2001-08-21 06:53:01 +0000 | [diff] [blame] | 153 | done, dump = {}, [] |
| 154 | for name, where, value in vars: |
| 155 | if name in done: continue |
| 156 | done[name] = 1 |
| 157 | if value is not __UNDEF__: |
Raymond Hettinger | dbecd93 | 2005-02-06 06:57:08 +0000 | [diff] [blame] | 158 | if where in ('global', 'builtin'): |
Ka-Ping Yee | 711cad7 | 2002-06-26 07:10:56 +0000 | [diff] [blame] | 159 | name = ('<em>%s</em> ' % where) + strong(name) |
| 160 | elif where == 'local': |
| 161 | name = strong(name) |
| 162 | else: |
| 163 | name = where + strong(name.split('.')[-1]) |
Ka-Ping Yee | 8320597 | 2001-08-21 06:53:01 +0000 | [diff] [blame] | 164 | dump.append('%s = %s' % (name, pydoc.html.repr(value))) |
| 165 | else: |
| 166 | dump.append(name + ' <em>undefined</em>') |
| 167 | |
| 168 | rows.append('<tr><td>%s</td></tr>' % small(grey(', '.join(dump)))) |
Andrew M. Kuchling | 5fcefdb | 2004-07-10 14:14:51 +0000 | [diff] [blame] | 169 | frames.append(''' |
Ka-Ping Yee | 8320597 | 2001-08-21 06:53:01 +0000 | [diff] [blame] | 170 | <table width="100%%" cellspacing=0 cellpadding=0 border=0> |
| 171 | %s</table>''' % '\n'.join(rows)) |
| 172 | |
Andrew M. Kuchling | b67c943 | 2004-03-31 20:17:56 +0000 | [diff] [blame] | 173 | exception = ['<p>%s: %s' % (strong(pydoc.html.escape(str(etype))), |
| 174 | pydoc.html.escape(str(evalue)))] |
Georg Brandl | 57b39e0 | 2007-04-11 19:24:50 +0000 | [diff] [blame] | 175 | for name in dir(evalue): |
| 176 | if name[:1] == '_': continue |
| 177 | value = pydoc.html.repr(getattr(evalue, name)) |
| 178 | exception.append('\n<br>%s%s =\n%s' % (indent, name, value)) |
Ka-Ping Yee | 6b5a48d | 2001-08-18 04:04:50 +0000 | [diff] [blame] | 179 | |
Ka-Ping Yee | 6b5a48d | 2001-08-18 04:04:50 +0000 | [diff] [blame] | 180 | return head + ''.join(frames) + ''.join(exception) + ''' |
| 181 | |
| 182 | |
Ka-Ping Yee | 8320597 | 2001-08-21 06:53:01 +0000 | [diff] [blame] | 183 | <!-- The above is a description of an error in a Python program, formatted |
Miss Islington (bot) | 6fc1efa | 2021-07-26 15:34:32 -0700 | [diff] [blame] | 184 | for a web browser because the 'cgitb' module was enabled. In case you |
| 185 | are not reading this in a web browser, here is the original traceback: |
Ka-Ping Yee | 6b5a48d | 2001-08-18 04:04:50 +0000 | [diff] [blame] | 186 | |
| 187 | %s |
| 188 | --> |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 189 | ''' % pydoc.html.escape( |
| 190 | ''.join(traceback.format_exception(etype, evalue, etb))) |
Ka-Ping Yee | 6b5a48d | 2001-08-18 04:04:50 +0000 | [diff] [blame] | 191 | |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 192 | def text(einfo, context=5): |
Skip Montanaro | 364ca40 | 2003-06-17 12:58:31 +0000 | [diff] [blame] | 193 | """Return a plain text document describing a given traceback.""" |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 194 | etype, evalue, etb = einfo |
Guido van Rossum | 1325790 | 2007-06-07 23:15:56 +0000 | [diff] [blame] | 195 | if isinstance(etype, type): |
Skip Montanaro | 364ca40 | 2003-06-17 12:58:31 +0000 | [diff] [blame] | 196 | etype = etype.__name__ |
| 197 | pyver = 'Python ' + sys.version.split()[0] + ': ' + sys.executable |
| 198 | date = time.ctime(time.time()) |
| 199 | head = "%s\n%s\n%s\n" % (str(etype), pyver, date) + ''' |
| 200 | A problem occurred in a Python script. Here is the sequence of |
| 201 | function calls leading up to the error, in the order they occurred. |
| 202 | ''' |
| 203 | |
| 204 | frames = [] |
| 205 | records = inspect.getinnerframes(etb, context) |
| 206 | for frame, file, lnum, func, lines, index in records: |
| 207 | file = file and os.path.abspath(file) or '?' |
| 208 | args, varargs, varkw, locals = inspect.getargvalues(frame) |
| 209 | call = '' |
| 210 | if func != '?': |
sblondon | 8cf4b34 | 2018-05-09 11:39:32 +0200 | [diff] [blame] | 211 | call = 'in ' + func |
| 212 | if func != "<module>": |
| 213 | call += inspect.formatargvalues(args, varargs, varkw, locals, |
Skip Montanaro | 364ca40 | 2003-06-17 12:58:31 +0000 | [diff] [blame] | 214 | formatvalue=lambda value: '=' + pydoc.text.repr(value)) |
| 215 | |
| 216 | highlight = {} |
| 217 | def reader(lnum=[lnum]): |
| 218 | highlight[lnum[0]] = 1 |
| 219 | try: return linecache.getline(file, lnum[0]) |
| 220 | finally: lnum[0] += 1 |
| 221 | vars = scanvars(reader, frame, locals) |
| 222 | |
| 223 | rows = [' %s %s' % (file, call)] |
| 224 | if index is not None: |
| 225 | i = lnum - index |
| 226 | for line in lines: |
| 227 | num = '%5d ' % i |
| 228 | rows.append(num+line.rstrip()) |
| 229 | i += 1 |
| 230 | |
| 231 | done, dump = {}, [] |
| 232 | for name, where, value in vars: |
| 233 | if name in done: continue |
| 234 | done[name] = 1 |
| 235 | if value is not __UNDEF__: |
| 236 | if where == 'global': name = 'global ' + name |
Skip Montanaro | 1c0228a | 2004-06-07 11:20:40 +0000 | [diff] [blame] | 237 | elif where != 'local': name = where + name.split('.')[-1] |
Skip Montanaro | 364ca40 | 2003-06-17 12:58:31 +0000 | [diff] [blame] | 238 | dump.append('%s = %s' % (name, pydoc.text.repr(value))) |
| 239 | else: |
| 240 | dump.append(name + ' undefined') |
| 241 | |
| 242 | rows.append('\n'.join(dump)) |
| 243 | frames.append('\n%s\n' % '\n'.join(rows)) |
| 244 | |
| 245 | exception = ['%s: %s' % (str(etype), str(evalue))] |
Georg Brandl | 57b39e0 | 2007-04-11 19:24:50 +0000 | [diff] [blame] | 246 | for name in dir(evalue): |
| 247 | value = pydoc.text.repr(getattr(evalue, name)) |
| 248 | exception.append('\n%s%s = %s' % (" "*4, name, value)) |
Skip Montanaro | 364ca40 | 2003-06-17 12:58:31 +0000 | [diff] [blame] | 249 | |
Skip Montanaro | 364ca40 | 2003-06-17 12:58:31 +0000 | [diff] [blame] | 250 | return head + ''.join(frames) + ''.join(exception) + ''' |
| 251 | |
| 252 | The above is a description of an error in a Python program. Here is |
| 253 | the original traceback: |
| 254 | |
| 255 | %s |
| 256 | ''' % ''.join(traceback.format_exception(etype, evalue, etb)) |
| 257 | |
Ka-Ping Yee | 6b5a48d | 2001-08-18 04:04:50 +0000 | [diff] [blame] | 258 | class Hook: |
Ka-Ping Yee | 8320597 | 2001-08-21 06:53:01 +0000 | [diff] [blame] | 259 | """A hook to replace sys.excepthook that shows tracebacks in HTML.""" |
| 260 | |
Skip Montanaro | 364ca40 | 2003-06-17 12:58:31 +0000 | [diff] [blame] | 261 | def __init__(self, display=1, logdir=None, context=5, file=None, |
| 262 | format="html"): |
Ka-Ping Yee | 6b5a48d | 2001-08-18 04:04:50 +0000 | [diff] [blame] | 263 | self.display = display # send tracebacks to browser if true |
| 264 | self.logdir = logdir # log tracebacks to files if not None |
Ka-Ping Yee | 8320597 | 2001-08-21 06:53:01 +0000 | [diff] [blame] | 265 | self.context = context # number of source code lines per frame |
Ka-Ping Yee | fa78d0f | 2001-12-04 18:45:17 +0000 | [diff] [blame] | 266 | self.file = file or sys.stdout # place to send the output |
Skip Montanaro | 364ca40 | 2003-06-17 12:58:31 +0000 | [diff] [blame] | 267 | self.format = format |
Ka-Ping Yee | 6b5a48d | 2001-08-18 04:04:50 +0000 | [diff] [blame] | 268 | |
| 269 | def __call__(self, etype, evalue, etb): |
Ka-Ping Yee | 6b5a48d | 2001-08-18 04:04:50 +0000 | [diff] [blame] | 270 | self.handle((etype, evalue, etb)) |
| 271 | |
| 272 | def handle(self, info=None): |
Ka-Ping Yee | 6b5a48d | 2001-08-18 04:04:50 +0000 | [diff] [blame] | 273 | info = info or sys.exc_info() |
Skip Montanaro | 364ca40 | 2003-06-17 12:58:31 +0000 | [diff] [blame] | 274 | if self.format == "html": |
| 275 | self.file.write(reset()) |
Ka-Ping Yee | 6b5a48d | 2001-08-18 04:04:50 +0000 | [diff] [blame] | 276 | |
Skip Montanaro | 364ca40 | 2003-06-17 12:58:31 +0000 | [diff] [blame] | 277 | formatter = (self.format=="html") and html or text |
| 278 | plain = False |
Ka-Ping Yee | 6b5a48d | 2001-08-18 04:04:50 +0000 | [diff] [blame] | 279 | try: |
Skip Montanaro | 364ca40 | 2003-06-17 12:58:31 +0000 | [diff] [blame] | 280 | doc = formatter(info, self.context) |
Ka-Ping Yee | 6b5a48d | 2001-08-18 04:04:50 +0000 | [diff] [blame] | 281 | except: # just in case something goes wrong |
Skip Montanaro | 364ca40 | 2003-06-17 12:58:31 +0000 | [diff] [blame] | 282 | doc = ''.join(traceback.format_exception(*info)) |
| 283 | plain = True |
Ka-Ping Yee | 6b5a48d | 2001-08-18 04:04:50 +0000 | [diff] [blame] | 284 | |
| 285 | if self.display: |
Skip Montanaro | 364ca40 | 2003-06-17 12:58:31 +0000 | [diff] [blame] | 286 | if plain: |
sblondon | 7d68bfa | 2018-04-29 19:48:33 +0200 | [diff] [blame] | 287 | doc = pydoc.html.escape(doc) |
Ka-Ping Yee | fa78d0f | 2001-12-04 18:45:17 +0000 | [diff] [blame] | 288 | self.file.write('<pre>' + doc + '</pre>\n') |
Ka-Ping Yee | 6b5a48d | 2001-08-18 04:04:50 +0000 | [diff] [blame] | 289 | else: |
Ka-Ping Yee | fa78d0f | 2001-12-04 18:45:17 +0000 | [diff] [blame] | 290 | self.file.write(doc + '\n') |
Ka-Ping Yee | 6b5a48d | 2001-08-18 04:04:50 +0000 | [diff] [blame] | 291 | else: |
Ka-Ping Yee | fa78d0f | 2001-12-04 18:45:17 +0000 | [diff] [blame] | 292 | self.file.write('<p>A problem occurred in a Python script.\n') |
Ka-Ping Yee | 6b5a48d | 2001-08-18 04:04:50 +0000 | [diff] [blame] | 293 | |
| 294 | if self.logdir is not None: |
Andrew M. Kuchling | 30633c9 | 2004-05-06 13:13:44 +0000 | [diff] [blame] | 295 | suffix = ['.txt', '.html'][self.format=="html"] |
Skip Montanaro | 364ca40 | 2003-06-17 12:58:31 +0000 | [diff] [blame] | 296 | (fd, path) = tempfile.mkstemp(suffix=suffix, dir=self.logdir) |
R David Murray | 252cd0e | 2012-10-27 14:42:47 -0400 | [diff] [blame] | 297 | |
Ka-Ping Yee | 6b5a48d | 2001-08-18 04:04:50 +0000 | [diff] [blame] | 298 | try: |
Serhiy Storchaka | 46ba6c8 | 2015-04-04 11:01:02 +0300 | [diff] [blame] | 299 | with os.fdopen(fd, 'w') as file: |
| 300 | file.write(doc) |
R David Murray | 252cd0e | 2012-10-27 14:42:47 -0400 | [diff] [blame] | 301 | msg = '%s contains the description of this error.' % path |
Ka-Ping Yee | 6b5a48d | 2001-08-18 04:04:50 +0000 | [diff] [blame] | 302 | except: |
R David Murray | 252cd0e | 2012-10-27 14:42:47 -0400 | [diff] [blame] | 303 | msg = 'Tried to save traceback to %s, but failed.' % path |
| 304 | |
| 305 | if self.format == 'html': |
| 306 | self.file.write('<p>%s</p>\n' % msg) |
| 307 | else: |
| 308 | self.file.write(msg + '\n') |
Ka-Ping Yee | fa78d0f | 2001-12-04 18:45:17 +0000 | [diff] [blame] | 309 | try: |
| 310 | self.file.flush() |
| 311 | except: pass |
Ka-Ping Yee | 6b5a48d | 2001-08-18 04:04:50 +0000 | [diff] [blame] | 312 | |
| 313 | handler = Hook().handle |
Skip Montanaro | 364ca40 | 2003-06-17 12:58:31 +0000 | [diff] [blame] | 314 | def enable(display=1, logdir=None, context=5, format="html"): |
Ka-Ping Yee | 8320597 | 2001-08-21 06:53:01 +0000 | [diff] [blame] | 315 | """Install an exception handler that formats tracebacks as HTML. |
| 316 | |
| 317 | The optional argument 'display' can be set to 0 to suppress sending the |
| 318 | traceback to the browser, and 'logdir' can be set to a directory to cause |
| 319 | tracebacks to be written to files there.""" |
Skip Montanaro | 364ca40 | 2003-06-17 12:58:31 +0000 | [diff] [blame] | 320 | sys.excepthook = Hook(display=display, logdir=logdir, |
| 321 | context=context, format=format) |