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