Mercurial > cpython-lru_cache-weakref
annotate Lib/cgitb.py @ 39940:dfd809dcf680 2.5
Bug #1446043: correctly raise a LookupError if an encoding name given
to encodings.search_function() contains a dot.
(backport from rev. 52075)
| author | Georg Brandl <georg@python.org> |
|---|---|
| date | Sat, 30 Sep 2006 11:22:35 +0000 |
| parents | 1c4d192f1568 |
| children | a01cb3df38dd 3667d1a8c916 |
| rev | line source |
|---|---|
|
29511
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
1 """More comprehensive traceback formatting for Python scripts. |
| 19365 | 2 |
| 3 To enable this module, do: | |
| 4 | |
| 5 import cgitb; cgitb.enable() | |
| 6 | |
|
29511
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
7 at the top of your script. The optional arguments to enable() are: |
| 19365 | 8 |
| 9 display - if true, tracebacks are displayed in the web browser | |
| 10 logdir - if set, tracebacks are written to files in this directory | |
| 19421 | 11 context - number of lines of source code to show for each stack frame |
|
29668
36a17904a3ed
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
29511
diff
changeset
|
12 format - 'text' or 'html' controls the output format |
| 19365 | 13 |
|
29511
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
14 By default, tracebacks are displayed but not saved, the context is 5 lines |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
15 and the output format is 'html' (for backwards compatibility with the |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
16 original use of this module) |
| 19365 | 17 |
| 18 Alternatively, if you have caught an exception and want cgitb to display it | |
|
29511
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
19 for you, call cgitb.handler(). The optional argument to handler() is a |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
20 3-item tuple (etype, evalue, etb) just like the value of sys.exc_info(). |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
21 The default handler displays output as HTML. |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
22 """ |
| 19365 | 23 |
| 24 __author__ = 'Ka-Ping Yee' | |
|
35742
1c4d192f1568
Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
35154
diff
changeset
|
25 |
| 19365 | 26 __version__ = '$Revision$' |
| 27 | |
|
21194
1519d6faab15
Add "file" argument to Hook constructor.
Ka-Ping Yee <ping@zesty.ca>
parents:
19421
diff
changeset
|
28 import sys |
|
1519d6faab15
Add "file" argument to Hook constructor.
Ka-Ping Yee <ping@zesty.ca>
parents:
19421
diff
changeset
|
29 |
| 19365 | 30 def reset(): |
| 31 """Return a string that resets the CGI and browser to a known state.""" | |
| 32 return '''<!--: spam | |
| 33 Content-Type: text/html | |
| 34 | |
| 19421 | 35 <body bgcolor="#f0f0f8"><font color="#f0f0f8" size="-5"> --> |
| 36 <body bgcolor="#f0f0f8"><font color="#f0f0f8" size="-5"> --> --> | |
| 19365 | 37 </font> </font> </font> </script> </object> </blockquote> </pre> |
| 38 </table> </table> </table> </table> </table> </font> </font> </font>''' | |
| 39 | |
| 19421 | 40 __UNDEF__ = [] # a special sentinel object |
|
32577
fef7911a5705
[Patch #987052 from Thomas Guettler]
Andrew M. Kuchling <amk@amk.ca>
parents:
32292
diff
changeset
|
41 def small(text): |
|
fef7911a5705
[Patch #987052 from Thomas Guettler]
Andrew M. Kuchling <amk@amk.ca>
parents:
32292
diff
changeset
|
42 if text: |
|
fef7911a5705
[Patch #987052 from Thomas Guettler]
Andrew M. Kuchling <amk@amk.ca>
parents:
32292
diff
changeset
|
43 return '<small>' + text + '</small>' |
|
fef7911a5705
[Patch #987052 from Thomas Guettler]
Andrew M. Kuchling <amk@amk.ca>
parents:
32292
diff
changeset
|
44 else: |
|
fef7911a5705
[Patch #987052 from Thomas Guettler]
Andrew M. Kuchling <amk@amk.ca>
parents:
32292
diff
changeset
|
45 return '' |
|
32736
c3a096bfbd6b
Whitespace normalization, via reindent.py.
Tim Peters <tim.peters@gmail.com>
parents:
32577
diff
changeset
|
46 |
|
32577
fef7911a5705
[Patch #987052 from Thomas Guettler]
Andrew M. Kuchling <amk@amk.ca>
parents:
32292
diff
changeset
|
47 def strong(text): |
|
fef7911a5705
[Patch #987052 from Thomas Guettler]
Andrew M. Kuchling <amk@amk.ca>
parents:
32292
diff
changeset
|
48 if text: |
|
fef7911a5705
[Patch #987052 from Thomas Guettler]
Andrew M. Kuchling <amk@amk.ca>
parents:
32292
diff
changeset
|
49 return '<strong>' + text + '</strong>' |
|
fef7911a5705
[Patch #987052 from Thomas Guettler]
Andrew M. Kuchling <amk@amk.ca>
parents:
32292
diff
changeset
|
50 else: |
|
fef7911a5705
[Patch #987052 from Thomas Guettler]
Andrew M. Kuchling <amk@amk.ca>
parents:
32292
diff
changeset
|
51 return '' |
|
32736
c3a096bfbd6b
Whitespace normalization, via reindent.py.
Tim Peters <tim.peters@gmail.com>
parents:
32577
diff
changeset
|
52 |
|
32577
fef7911a5705
[Patch #987052 from Thomas Guettler]
Andrew M. Kuchling <amk@amk.ca>
parents:
32292
diff
changeset
|
53 def grey(text): |
|
fef7911a5705
[Patch #987052 from Thomas Guettler]
Andrew M. Kuchling <amk@amk.ca>
parents:
32292
diff
changeset
|
54 if text: |
|
fef7911a5705
[Patch #987052 from Thomas Guettler]
Andrew M. Kuchling <amk@amk.ca>
parents:
32292
diff
changeset
|
55 return '<font color="#909090">' + text + '</font>' |
|
fef7911a5705
[Patch #987052 from Thomas Guettler]
Andrew M. Kuchling <amk@amk.ca>
parents:
32292
diff
changeset
|
56 else: |
|
fef7911a5705
[Patch #987052 from Thomas Guettler]
Andrew M. Kuchling <amk@amk.ca>
parents:
32292
diff
changeset
|
57 return '' |
| 19421 | 58 |
| 59 def lookup(name, frame, locals): | |
| 60 """Find the value for a given name in the given environment.""" | |
| 61 if name in locals: | |
| 62 return 'local', locals[name] | |
| 63 if name in frame.f_globals: | |
| 64 return 'global', frame.f_globals[name] | |
|
23998
dab8701bd3fa
Also look up variable names in __builtins__ if not found in globals.
Ka-Ping Yee <ping@zesty.ca>
parents:
21480
diff
changeset
|
65 if '__builtins__' in frame.f_globals: |
|
dab8701bd3fa
Also look up variable names in __builtins__ if not found in globals.
Ka-Ping Yee <ping@zesty.ca>
parents:
21480
diff
changeset
|
66 builtins = frame.f_globals['__builtins__'] |
|
dab8701bd3fa
Also look up variable names in __builtins__ if not found in globals.
Ka-Ping Yee <ping@zesty.ca>
parents:
21480
diff
changeset
|
67 if type(builtins) is type({}): |
|
dab8701bd3fa
Also look up variable names in __builtins__ if not found in globals.
Ka-Ping Yee <ping@zesty.ca>
parents:
21480
diff
changeset
|
68 if name in builtins: |
|
dab8701bd3fa
Also look up variable names in __builtins__ if not found in globals.
Ka-Ping Yee <ping@zesty.ca>
parents:
21480
diff
changeset
|
69 return 'builtin', builtins[name] |
|
dab8701bd3fa
Also look up variable names in __builtins__ if not found in globals.
Ka-Ping Yee <ping@zesty.ca>
parents:
21480
diff
changeset
|
70 else: |
|
dab8701bd3fa
Also look up variable names in __builtins__ if not found in globals.
Ka-Ping Yee <ping@zesty.ca>
parents:
21480
diff
changeset
|
71 if hasattr(builtins, name): |
|
dab8701bd3fa
Also look up variable names in __builtins__ if not found in globals.
Ka-Ping Yee <ping@zesty.ca>
parents:
21480
diff
changeset
|
72 return 'builtin', getattr(builtins, name) |
| 19421 | 73 return None, __UNDEF__ |
| 74 | |
| 75 def scanvars(reader, frame, locals): | |
| 76 """Scan one logical line of Python and look up values of variables used.""" | |
| 77 import tokenize, keyword | |
|
32263
b9c9e7e8be7d
[Bug #758665] cgitb.scanvars() fails because of an unititialized value variable. Patch from Robin Becker.
Andrew M. Kuchling <amk@amk.ca>
parents:
32001
diff
changeset
|
78 vars, lasttoken, parent, prefix, value = [], None, None, '', __UNDEF__ |
| 19421 | 79 for ttype, token, start, end, line in tokenize.generate_tokens(reader): |
| 80 if ttype == tokenize.NEWLINE: break | |
| 81 if ttype == tokenize.NAME and token not in keyword.kwlist: | |
| 82 if lasttoken == '.': | |
| 83 if parent is not __UNDEF__: | |
| 84 value = getattr(parent, token, __UNDEF__) | |
| 85 vars.append((prefix + token, prefix, value)) | |
| 86 else: | |
| 87 where, value = lookup(token, frame, locals) | |
| 88 vars.append((token, where, value)) | |
| 89 elif token == '.': | |
| 90 prefix += lasttoken + '.' | |
| 91 parent = value | |
| 92 else: | |
| 93 parent, prefix = None, '' | |
| 94 lasttoken = token | |
| 95 return vars | |
| 96 | |
| 97 def html((etype, evalue, etb), context=5): | |
| 98 """Return a nice HTML document describing a given traceback.""" | |
|
21194
1519d6faab15
Add "file" argument to Hook constructor.
Ka-Ping Yee <ping@zesty.ca>
parents:
19421
diff
changeset
|
99 import os, types, time, traceback, linecache, inspect, pydoc |
| 19365 | 100 |
| 101 if type(etype) is types.ClassType: | |
| 102 etype = etype.__name__ | |
| 103 pyver = 'Python ' + sys.version.split()[0] + ': ' + sys.executable | |
| 104 date = time.ctime(time.time()) | |
| 19421 | 105 head = '<body bgcolor="#f0f0f8">' + pydoc.html.heading( |
|
32577
fef7911a5705
[Patch #987052 from Thomas Guettler]
Andrew M. Kuchling <amk@amk.ca>
parents:
32292
diff
changeset
|
106 '<big><big>%s</big></big>' % |
|
fef7911a5705
[Patch #987052 from Thomas Guettler]
Andrew M. Kuchling <amk@amk.ca>
parents:
32292
diff
changeset
|
107 strong(pydoc.html.escape(str(etype))), |
| 19421 | 108 '#ffffff', '#6622aa', pyver + '<br>' + date) + ''' |
| 109 <p>A problem occurred in a Python script. Here is the sequence of | |
|
32577
fef7911a5705
[Patch #987052 from Thomas Guettler]
Andrew M. Kuchling <amk@amk.ca>
parents:
32292
diff
changeset
|
110 function calls leading up to the error, in the order they occurred.</p>''' |
| 19365 | 111 |
| 19421 | 112 indent = '<tt>' + small(' ' * 5) + ' </tt>' |
| 19365 | 113 frames = [] |
| 114 records = inspect.getinnerframes(etb, context) | |
| 115 for frame, file, lnum, func, lines, index in records: | |
|
35154
a31a770bb778
Prevent creating a HTML link to file://?/
Georg Brandl <georg@python.org>
parents:
34545
diff
changeset
|
116 if file: |
|
a31a770bb778
Prevent creating a HTML link to file://?/
Georg Brandl <georg@python.org>
parents:
34545
diff
changeset
|
117 file = os.path.abspath(file) |
|
a31a770bb778
Prevent creating a HTML link to file://?/
Georg Brandl <georg@python.org>
parents:
34545
diff
changeset
|
118 link = '<a href="file://%s">%s</a>' % (file, pydoc.html.escape(file)) |
|
a31a770bb778
Prevent creating a HTML link to file://?/
Georg Brandl <georg@python.org>
parents:
34545
diff
changeset
|
119 else: |
|
a31a770bb778
Prevent creating a HTML link to file://?/
Georg Brandl <georg@python.org>
parents:
34545
diff
changeset
|
120 file = link = '?' |
| 19365 | 121 args, varargs, varkw, locals = inspect.getargvalues(frame) |
| 19421 | 122 call = '' |
| 123 if func != '?': | |
| 124 call = 'in ' + strong(func) + \ | |
| 125 inspect.formatargvalues(args, varargs, varkw, locals, | |
| 126 formatvalue=lambda value: '=' + pydoc.html.repr(value)) | |
| 19365 | 127 |
| 19421 | 128 highlight = {} |
| 129 def reader(lnum=[lnum]): | |
| 130 highlight[lnum[0]] = 1 | |
| 131 try: return linecache.getline(file, lnum[0]) | |
| 132 finally: lnum[0] += 1 | |
| 133 vars = scanvars(reader, frame, locals) | |
| 19365 | 134 |
| 19421 | 135 rows = ['<tr><td bgcolor="#d8bbff">%s%s %s</td></tr>' % |
| 136 ('<big> </big>', link, call)] | |
| 19365 | 137 if index is not None: |
| 138 i = lnum - index | |
| 139 for line in lines: | |
| 19421 | 140 num = small(' ' * (5-len(str(i))) + str(i)) + ' ' |
| 141 line = '<tt>%s%s</tt>' % (num, pydoc.html.preformat(line)) | |
| 142 if i in highlight: | |
| 143 rows.append('<tr><td bgcolor="#ffccee">%s</td></tr>' % line) | |
| 144 else: | |
| 145 rows.append('<tr><td>%s</td></tr>' % grey(line)) | |
| 146 i += 1 | |
| 19365 | 147 |
| 19421 | 148 done, dump = {}, [] |
| 149 for name, where, value in vars: | |
| 150 if name in done: continue | |
| 151 done[name] = 1 | |
| 152 if value is not __UNDEF__: | |
|
34545
9d66fba78444
Replace list of constants with tuples of constants.
Raymond Hettinger <python@rcn.com>
parents:
32736
diff
changeset
|
153 if where in ('global', 'builtin'): |
|
23998
dab8701bd3fa
Also look up variable names in __builtins__ if not found in globals.
Ka-Ping Yee <ping@zesty.ca>
parents:
21480
diff
changeset
|
154 name = ('<em>%s</em> ' % where) + strong(name) |
|
dab8701bd3fa
Also look up variable names in __builtins__ if not found in globals.
Ka-Ping Yee <ping@zesty.ca>
parents:
21480
diff
changeset
|
155 elif where == 'local': |
|
dab8701bd3fa
Also look up variable names in __builtins__ if not found in globals.
Ka-Ping Yee <ping@zesty.ca>
parents:
21480
diff
changeset
|
156 name = strong(name) |
|
dab8701bd3fa
Also look up variable names in __builtins__ if not found in globals.
Ka-Ping Yee <ping@zesty.ca>
parents:
21480
diff
changeset
|
157 else: |
|
dab8701bd3fa
Also look up variable names in __builtins__ if not found in globals.
Ka-Ping Yee <ping@zesty.ca>
parents:
21480
diff
changeset
|
158 name = where + strong(name.split('.')[-1]) |
| 19421 | 159 dump.append('%s = %s' % (name, pydoc.html.repr(value))) |
| 160 else: | |
| 161 dump.append(name + ' <em>undefined</em>') | |
| 162 | |
| 163 rows.append('<tr><td>%s</td></tr>' % small(grey(', '.join(dump)))) | |
|
32577
fef7911a5705
[Patch #987052 from Thomas Guettler]
Andrew M. Kuchling <amk@amk.ca>
parents:
32292
diff
changeset
|
164 frames.append(''' |
| 19421 | 165 <table width="100%%" cellspacing=0 cellpadding=0 border=0> |
| 166 %s</table>''' % '\n'.join(rows)) | |
| 167 | |
|
31850
696f4ba9ddf7
[Bugfix candidate] Escape traceback type and value. There are probably additional cases where cgitb.py doesn't escape as paranoidly as it should (e.g. attribute names)
Andrew M. Kuchling <amk@amk.ca>
parents:
29668
diff
changeset
|
168 exception = ['<p>%s: %s' % (strong(pydoc.html.escape(str(etype))), |
|
696f4ba9ddf7
[Bugfix candidate] Escape traceback type and value. There are probably additional cases where cgitb.py doesn't escape as paranoidly as it should (e.g. attribute names)
Andrew M. Kuchling <amk@amk.ca>
parents:
29668
diff
changeset
|
169 pydoc.html.escape(str(evalue)))] |
| 19365 | 170 if type(evalue) is types.InstanceType: |
| 171 for name in dir(evalue): | |
|
23998
dab8701bd3fa
Also look up variable names in __builtins__ if not found in globals.
Ka-Ping Yee <ping@zesty.ca>
parents:
21480
diff
changeset
|
172 if name[:1] == '_': continue |
| 19365 | 173 value = pydoc.html.repr(getattr(evalue, name)) |
| 174 exception.append('\n<br>%s%s =\n%s' % (indent, name, value)) | |
| 175 | |
| 176 import traceback | |
| 177 return head + ''.join(frames) + ''.join(exception) + ''' | |
| 178 | |
| 179 | |
| 19421 | 180 <!-- The above is a description of an error in a Python program, formatted |
| 181 for a Web browser because the 'cgitb' module was enabled. In case you | |
| 182 are not reading this in a Web browser, here is the original traceback: | |
| 19365 | 183 |
| 184 %s | |
| 185 --> | |
| 19421 | 186 ''' % ''.join(traceback.format_exception(etype, evalue, etb)) |
| 19365 | 187 |
|
29511
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
188 def text((etype, evalue, etb), context=5): |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
189 """Return a plain text document describing a given traceback.""" |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
190 import os, types, time, traceback, linecache, inspect, pydoc |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
191 |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
192 if type(etype) is types.ClassType: |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
193 etype = etype.__name__ |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
194 pyver = 'Python ' + sys.version.split()[0] + ': ' + sys.executable |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
195 date = time.ctime(time.time()) |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
196 head = "%s\n%s\n%s\n" % (str(etype), pyver, date) + ''' |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
197 A problem occurred in a Python script. Here is the sequence of |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
198 function calls leading up to the error, in the order they occurred. |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
199 ''' |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
200 |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
201 frames = [] |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
202 records = inspect.getinnerframes(etb, context) |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
203 for frame, file, lnum, func, lines, index in records: |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
204 file = file and os.path.abspath(file) or '?' |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
205 args, varargs, varkw, locals = inspect.getargvalues(frame) |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
206 call = '' |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
207 if func != '?': |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
208 call = 'in ' + func + \ |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
209 inspect.formatargvalues(args, varargs, varkw, locals, |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
210 formatvalue=lambda value: '=' + pydoc.text.repr(value)) |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
211 |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
212 highlight = {} |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
213 def reader(lnum=[lnum]): |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
214 highlight[lnum[0]] = 1 |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
215 try: return linecache.getline(file, lnum[0]) |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
216 finally: lnum[0] += 1 |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
217 vars = scanvars(reader, frame, locals) |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
218 |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
219 rows = [' %s %s' % (file, call)] |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
220 if index is not None: |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
221 i = lnum - index |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
222 for line in lines: |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
223 num = '%5d ' % i |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
224 rows.append(num+line.rstrip()) |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
225 i += 1 |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
226 |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
227 done, dump = {}, [] |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
228 for name, where, value in vars: |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
229 if name in done: continue |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
230 done[name] = 1 |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
231 if value is not __UNDEF__: |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
232 if where == 'global': name = 'global ' + name |
|
32292
e1345ce275f7
Another nit found by Neal Norwitz using pychecker. This was caused by a
Skip Montanaro <skip@pobox.com>
parents:
32263
diff
changeset
|
233 elif where != 'local': name = where + name.split('.')[-1] |
|
29511
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
234 dump.append('%s = %s' % (name, pydoc.text.repr(value))) |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
235 else: |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
236 dump.append(name + ' undefined') |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
237 |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
238 rows.append('\n'.join(dump)) |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
239 frames.append('\n%s\n' % '\n'.join(rows)) |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
240 |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
241 exception = ['%s: %s' % (str(etype), str(evalue))] |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
242 if type(evalue) is types.InstanceType: |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
243 for name in dir(evalue): |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
244 value = pydoc.text.repr(getattr(evalue, name)) |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
245 exception.append('\n%s%s = %s' % (" "*4, name, value)) |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
246 |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
247 import traceback |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
248 return head + ''.join(frames) + ''.join(exception) + ''' |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
249 |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
250 The above is a description of an error in a Python program. Here is |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
251 the original traceback: |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
252 |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
253 %s |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
254 ''' % ''.join(traceback.format_exception(etype, evalue, etb)) |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
255 |
| 19365 | 256 class Hook: |
| 19421 | 257 """A hook to replace sys.excepthook that shows tracebacks in HTML.""" |
| 258 | |
|
29511
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
259 def __init__(self, display=1, logdir=None, context=5, file=None, |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
260 format="html"): |
| 19365 | 261 self.display = display # send tracebacks to browser if true |
| 262 self.logdir = logdir # log tracebacks to files if not None | |
| 19421 | 263 self.context = context # number of source code lines per frame |
|
21194
1519d6faab15
Add "file" argument to Hook constructor.
Ka-Ping Yee <ping@zesty.ca>
parents:
19421
diff
changeset
|
264 self.file = file or sys.stdout # place to send the output |
|
29511
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
265 self.format = format |
| 19365 | 266 |
| 267 def __call__(self, etype, evalue, etb): | |
| 268 self.handle((etype, evalue, etb)) | |
| 269 | |
| 270 def handle(self, info=None): | |
| 271 info = info or sys.exc_info() | |
|
29511
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
272 if self.format == "html": |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
273 self.file.write(reset()) |
| 19365 | 274 |
|
29511
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
275 formatter = (self.format=="html") and html or text |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
276 plain = False |
| 19365 | 277 try: |
|
29511
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
278 doc = formatter(info, self.context) |
| 19365 | 279 except: # just in case something goes wrong |
| 280 import traceback | |
|
29511
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
281 doc = ''.join(traceback.format_exception(*info)) |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
282 plain = True |
| 19365 | 283 |
| 284 if self.display: | |
|
29511
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
285 if plain: |
| 19365 | 286 doc = doc.replace('&', '&').replace('<', '<') |
|
21194
1519d6faab15
Add "file" argument to Hook constructor.
Ka-Ping Yee <ping@zesty.ca>
parents:
19421
diff
changeset
|
287 self.file.write('<pre>' + doc + '</pre>\n') |
| 19365 | 288 else: |
|
21194
1519d6faab15
Add "file" argument to Hook constructor.
Ka-Ping Yee <ping@zesty.ca>
parents:
19421
diff
changeset
|
289 self.file.write(doc + '\n') |
| 19365 | 290 else: |
|
21194
1519d6faab15
Add "file" argument to Hook constructor.
Ka-Ping Yee <ping@zesty.ca>
parents:
19421
diff
changeset
|
291 self.file.write('<p>A problem occurred in a Python script.\n') |
| 19365 | 292 |
| 293 if self.logdir is not None: | |
| 19421 | 294 import os, tempfile |
|
32001
2cc714977479
[Bug #945063] Get file extension correct. (2.3 bugfix candidate)
Andrew M. Kuchling <amk@amk.ca>
parents:
31850
diff
changeset
|
295 suffix = ['.txt', '.html'][self.format=="html"] |
|
29511
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
296 (fd, path) = tempfile.mkstemp(suffix=suffix, dir=self.logdir) |
| 19365 | 297 try: |
|
24710
11c701038805
Massive changes from SF 589982 (tempfile.py rewrite, by Zack
Guido van Rossum <guido@python.org>
parents:
23998
diff
changeset
|
298 file = os.fdopen(fd, 'w') |
| 19365 | 299 file.write(doc) |
| 300 file.close() | |
|
21194
1519d6faab15
Add "file" argument to Hook constructor.
Ka-Ping Yee <ping@zesty.ca>
parents:
19421
diff
changeset
|
301 msg = '<p> %s contains the description of this error.' % path |
| 19365 | 302 except: |
|
21194
1519d6faab15
Add "file" argument to Hook constructor.
Ka-Ping Yee <ping@zesty.ca>
parents:
19421
diff
changeset
|
303 msg = '<p> Tried to save traceback to %s, but failed.' % path |
|
1519d6faab15
Add "file" argument to Hook constructor.
Ka-Ping Yee <ping@zesty.ca>
parents:
19421
diff
changeset
|
304 self.file.write(msg + '\n') |
|
1519d6faab15
Add "file" argument to Hook constructor.
Ka-Ping Yee <ping@zesty.ca>
parents:
19421
diff
changeset
|
305 try: |
|
1519d6faab15
Add "file" argument to Hook constructor.
Ka-Ping Yee <ping@zesty.ca>
parents:
19421
diff
changeset
|
306 self.file.flush() |
|
1519d6faab15
Add "file" argument to Hook constructor.
Ka-Ping Yee <ping@zesty.ca>
parents:
19421
diff
changeset
|
307 except: pass |
| 19365 | 308 |
| 309 handler = Hook().handle | |
|
29511
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
310 def enable(display=1, logdir=None, context=5, format="html"): |
| 19421 | 311 """Install an exception handler that formats tracebacks as HTML. |
| 312 | |
| 313 The optional argument 'display' can be set to 0 to suppress sending the | |
| 314 traceback to the browser, and 'logdir' can be set to a directory to cause | |
| 315 tracebacks to be written to files there.""" | |
|
29511
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
316 sys.excepthook = Hook(display=display, logdir=logdir, |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
317 context=context, format=format) |
