Mercurial > cpython-lru_cache-weakref
annotate Lib/cgitb.py @ 38664:7619216bbc19 2.4
Bug #1502728: Correctly link against librt library on HP-UX.
(backport from rev. 46743)
| author | Georg Brandl <georg@python.org> |
|---|---|
| date | Thu, 08 Jun 2006 12:54:16 +0000 |
| parents | f5473a762b50 |
| children | 26349a64ef70 |
| 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' | |
| 25 __version__ = '$Revision$' | |
| 26 | |
|
21194
1519d6faab15
Add "file" argument to Hook constructor.
Ka-Ping Yee <ping@zesty.ca>
parents:
19421
diff
changeset
|
27 import sys |
|
1519d6faab15
Add "file" argument to Hook constructor.
Ka-Ping Yee <ping@zesty.ca>
parents:
19421
diff
changeset
|
28 |
| 19365 | 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 | |
| 19421 | 34 <body bgcolor="#f0f0f8"><font color="#f0f0f8" size="-5"> --> |
| 35 <body bgcolor="#f0f0f8"><font color="#f0f0f8" size="-5"> --> --> | |
| 19365 | 36 </font> </font> </font> </script> </object> </blockquote> </pre> |
| 37 </table> </table> </table> </table> </table> </font> </font> </font>''' | |
| 38 | |
| 19421 | 39 __UNDEF__ = [] # a special sentinel object |
|
32577
fef7911a5705
[Patch #987052 from Thomas Guettler]
Andrew M. Kuchling <amk@amk.ca>
parents:
32292
diff
changeset
|
40 def small(text): |
|
fef7911a5705
[Patch #987052 from Thomas Guettler]
Andrew M. Kuchling <amk@amk.ca>
parents:
32292
diff
changeset
|
41 if text: |
|
fef7911a5705
[Patch #987052 from Thomas Guettler]
Andrew M. Kuchling <amk@amk.ca>
parents:
32292
diff
changeset
|
42 return '<small>' + text + '</small>' |
|
fef7911a5705
[Patch #987052 from Thomas Guettler]
Andrew M. Kuchling <amk@amk.ca>
parents:
32292
diff
changeset
|
43 else: |
|
fef7911a5705
[Patch #987052 from Thomas Guettler]
Andrew M. Kuchling <amk@amk.ca>
parents:
32292
diff
changeset
|
44 return '' |
|
32736
c3a096bfbd6b
Whitespace normalization, via reindent.py.
Tim Peters <tim.peters@gmail.com>
parents:
32577
diff
changeset
|
45 |
|
32577
fef7911a5705
[Patch #987052 from Thomas Guettler]
Andrew M. Kuchling <amk@amk.ca>
parents:
32292
diff
changeset
|
46 def strong(text): |
|
fef7911a5705
[Patch #987052 from Thomas Guettler]
Andrew M. Kuchling <amk@amk.ca>
parents:
32292
diff
changeset
|
47 if text: |
|
fef7911a5705
[Patch #987052 from Thomas Guettler]
Andrew M. Kuchling <amk@amk.ca>
parents:
32292
diff
changeset
|
48 return '<strong>' + text + '</strong>' |
|
fef7911a5705
[Patch #987052 from Thomas Guettler]
Andrew M. Kuchling <amk@amk.ca>
parents:
32292
diff
changeset
|
49 else: |
|
fef7911a5705
[Patch #987052 from Thomas Guettler]
Andrew M. Kuchling <amk@amk.ca>
parents:
32292
diff
changeset
|
50 return '' |
|
32736
c3a096bfbd6b
Whitespace normalization, via reindent.py.
Tim Peters <tim.peters@gmail.com>
parents:
32577
diff
changeset
|
51 |
|
32577
fef7911a5705
[Patch #987052 from Thomas Guettler]
Andrew M. Kuchling <amk@amk.ca>
parents:
32292
diff
changeset
|
52 def grey(text): |
|
fef7911a5705
[Patch #987052 from Thomas Guettler]
Andrew M. Kuchling <amk@amk.ca>
parents:
32292
diff
changeset
|
53 if text: |
|
fef7911a5705
[Patch #987052 from Thomas Guettler]
Andrew M. Kuchling <amk@amk.ca>
parents:
32292
diff
changeset
|
54 return '<font color="#909090">' + text + '</font>' |
|
fef7911a5705
[Patch #987052 from Thomas Guettler]
Andrew M. Kuchling <amk@amk.ca>
parents:
32292
diff
changeset
|
55 else: |
|
fef7911a5705
[Patch #987052 from Thomas Guettler]
Andrew M. Kuchling <amk@amk.ca>
parents:
32292
diff
changeset
|
56 return '' |
| 19421 | 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] | |
|
23998
dab8701bd3fa
Also look up variable names in __builtins__ if not found in globals.
Ka-Ping Yee <ping@zesty.ca>
parents:
21480
diff
changeset
|
64 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
|
65 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
|
66 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
|
67 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
|
68 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
|
69 else: |
|
dab8701bd3fa
Also look up variable names in __builtins__ if not found in globals.
Ka-Ping Yee <ping@zesty.ca>
parents:
21480
diff
changeset
|
70 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
|
71 return 'builtin', getattr(builtins, name) |
| 19421 | 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 | |
|
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
|
77 vars, lasttoken, parent, prefix, value = [], None, None, '', __UNDEF__ |
| 19421 | 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.""" | |
|
21194
1519d6faab15
Add "file" argument to Hook constructor.
Ka-Ping Yee <ping@zesty.ca>
parents:
19421
diff
changeset
|
98 import os, types, time, traceback, linecache, inspect, pydoc |
| 19365 | 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()) | |
| 19421 | 104 head = '<body bgcolor="#f0f0f8">' + pydoc.html.heading( |
|
32577
fef7911a5705
[Patch #987052 from Thomas Guettler]
Andrew M. Kuchling <amk@amk.ca>
parents:
32292
diff
changeset
|
105 '<big><big>%s</big></big>' % |
|
fef7911a5705
[Patch #987052 from Thomas Guettler]
Andrew M. Kuchling <amk@amk.ca>
parents:
32292
diff
changeset
|
106 strong(pydoc.html.escape(str(etype))), |
| 19421 | 107 '#ffffff', '#6622aa', pyver + '<br>' + date) + ''' |
| 108 <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
|
109 function calls leading up to the error, in the order they occurred.</p>''' |
| 19365 | 110 |
| 19421 | 111 indent = '<tt>' + small(' ' * 5) + ' </tt>' |
| 19365 | 112 frames = [] |
| 113 records = inspect.getinnerframes(etb, context) | |
| 114 for frame, file, lnum, func, lines, index in records: | |
|
35155
f5473a762b50
Backport: Prevent creating a HTML link to file://?/
Georg Brandl <georg@python.org>
parents:
32736
diff
changeset
|
115 if file: |
|
f5473a762b50
Backport: Prevent creating a HTML link to file://?/
Georg Brandl <georg@python.org>
parents:
32736
diff
changeset
|
116 file = os.path.abspath(file) |
|
f5473a762b50
Backport: Prevent creating a HTML link to file://?/
Georg Brandl <georg@python.org>
parents:
32736
diff
changeset
|
117 link = '<a href="file://%s">%s</a>' % (file, pydoc.html.escape(file)) |
|
f5473a762b50
Backport: Prevent creating a HTML link to file://?/
Georg Brandl <georg@python.org>
parents:
32736
diff
changeset
|
118 else: |
|
f5473a762b50
Backport: Prevent creating a HTML link to file://?/
Georg Brandl <georg@python.org>
parents:
32736
diff
changeset
|
119 file = link = '?' |
| 19365 | 120 args, varargs, varkw, locals = inspect.getargvalues(frame) |
| 19421 | 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)) | |
| 19365 | 126 |
| 19421 | 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) | |
| 19365 | 133 |
| 19421 | 134 rows = ['<tr><td bgcolor="#d8bbff">%s%s %s</td></tr>' % |
| 135 ('<big> </big>', link, call)] | |
| 19365 | 136 if index is not None: |
| 137 i = lnum - index | |
| 138 for line in lines: | |
| 19421 | 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 | |
| 19365 | 146 |
| 19421 | 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__: | |
|
23998
dab8701bd3fa
Also look up variable names in __builtins__ if not found in globals.
Ka-Ping Yee <ping@zesty.ca>
parents:
21480
diff
changeset
|
152 if where in ['global', 'builtin']: |
|
dab8701bd3fa
Also look up variable names in __builtins__ if not found in globals.
Ka-Ping Yee <ping@zesty.ca>
parents:
21480
diff
changeset
|
153 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
|
154 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
|
155 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
|
156 else: |
|
dab8701bd3fa
Also look up variable names in __builtins__ if not found in globals.
Ka-Ping Yee <ping@zesty.ca>
parents:
21480
diff
changeset
|
157 name = where + strong(name.split('.')[-1]) |
| 19421 | 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)))) | |
|
32577
fef7911a5705
[Patch #987052 from Thomas Guettler]
Andrew M. Kuchling <amk@amk.ca>
parents:
32292
diff
changeset
|
163 frames.append(''' |
| 19421 | 164 <table width="100%%" cellspacing=0 cellpadding=0 border=0> |
| 165 %s</table>''' % '\n'.join(rows)) | |
| 166 | |
|
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
|
167 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
|
168 pydoc.html.escape(str(evalue)))] |
| 19365 | 169 if type(evalue) is types.InstanceType: |
| 170 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
|
171 if name[:1] == '_': continue |
| 19365 | 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 | |
| 19421 | 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: | |
| 19365 | 182 |
| 183 %s | |
| 184 --> | |
| 19421 | 185 ''' % ''.join(traceback.format_exception(etype, evalue, etb)) |
| 19365 | 186 |
|
29511
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
187 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
|
188 """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
|
189 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
|
190 |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
191 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
|
192 etype = etype.__name__ |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
193 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
|
194 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
|
195 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
|
196 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
|
197 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
|
198 ''' |
|
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 frames = [] |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
201 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
|
202 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
|
203 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
|
204 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
|
205 call = '' |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
206 if func != '?': |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
207 call = 'in ' + func + \ |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
208 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
|
209 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
|
210 |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
211 highlight = {} |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
212 def reader(lnum=[lnum]): |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
213 highlight[lnum[0]] = 1 |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
214 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
|
215 finally: lnum[0] += 1 |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
216 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
|
217 |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
218 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
|
219 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
|
220 i = lnum - index |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
221 for line in lines: |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
222 num = '%5d ' % i |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
223 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
|
224 i += 1 |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
225 |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
226 done, dump = {}, [] |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
227 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
|
228 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
|
229 done[name] = 1 |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
230 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
|
231 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
|
232 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
|
233 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
|
234 else: |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
235 dump.append(name + ' undefined') |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
236 |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
237 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
|
238 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
|
239 |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
240 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
|
241 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
|
242 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
|
243 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
|
244 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
|
245 |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
246 import traceback |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
247 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
|
248 |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
249 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
|
250 the original traceback: |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
251 |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
252 %s |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
253 ''' % ''.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
|
254 |
| 19365 | 255 class Hook: |
| 19421 | 256 """A hook to replace sys.excepthook that shows tracebacks in HTML.""" |
| 257 | |
|
29511
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
258 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
|
259 format="html"): |
| 19365 | 260 self.display = display # send tracebacks to browser if true |
| 261 self.logdir = logdir # log tracebacks to files if not None | |
| 19421 | 262 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
|
263 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
|
264 self.format = format |
| 19365 | 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() | |
|
29511
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
271 if self.format == "html": |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
272 self.file.write(reset()) |
| 19365 | 273 |
|
29511
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
274 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
|
275 plain = False |
| 19365 | 276 try: |
|
29511
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
277 doc = formatter(info, self.context) |
| 19365 | 278 except: # just in case something goes wrong |
| 279 import traceback | |
|
29511
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
280 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
|
281 plain = True |
| 19365 | 282 |
| 283 if self.display: | |
|
29511
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
284 if plain: |
| 19365 | 285 doc = doc.replace('&', '&').replace('<', '<') |
|
21194
1519d6faab15
Add "file" argument to Hook constructor.
Ka-Ping Yee <ping@zesty.ca>
parents:
19421
diff
changeset
|
286 self.file.write('<pre>' + doc + '</pre>\n') |
| 19365 | 287 else: |
|
21194
1519d6faab15
Add "file" argument to Hook constructor.
Ka-Ping Yee <ping@zesty.ca>
parents:
19421
diff
changeset
|
288 self.file.write(doc + '\n') |
| 19365 | 289 else: |
|
21194
1519d6faab15
Add "file" argument to Hook constructor.
Ka-Ping Yee <ping@zesty.ca>
parents:
19421
diff
changeset
|
290 self.file.write('<p>A problem occurred in a Python script.\n') |
| 19365 | 291 |
| 292 if self.logdir is not None: | |
| 19421 | 293 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
|
294 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
|
295 (fd, path) = tempfile.mkstemp(suffix=suffix, dir=self.logdir) |
| 19365 | 296 try: |
|
24710
11c701038805
Massive changes from SF 589982 (tempfile.py rewrite, by Zack
Guido van Rossum <guido@python.org>
parents:
23998
diff
changeset
|
297 file = os.fdopen(fd, 'w') |
| 19365 | 298 file.write(doc) |
| 299 file.close() | |
|
21194
1519d6faab15
Add "file" argument to Hook constructor.
Ka-Ping Yee <ping@zesty.ca>
parents:
19421
diff
changeset
|
300 msg = '<p> %s contains the description of this error.' % path |
| 19365 | 301 except: |
|
21194
1519d6faab15
Add "file" argument to Hook constructor.
Ka-Ping Yee <ping@zesty.ca>
parents:
19421
diff
changeset
|
302 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
|
303 self.file.write(msg + '\n') |
|
1519d6faab15
Add "file" argument to Hook constructor.
Ka-Ping Yee <ping@zesty.ca>
parents:
19421
diff
changeset
|
304 try: |
|
1519d6faab15
Add "file" argument to Hook constructor.
Ka-Ping Yee <ping@zesty.ca>
parents:
19421
diff
changeset
|
305 self.file.flush() |
|
1519d6faab15
Add "file" argument to Hook constructor.
Ka-Ping Yee <ping@zesty.ca>
parents:
19421
diff
changeset
|
306 except: pass |
| 19365 | 307 |
| 308 handler = Hook().handle | |
|
29511
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
309 def enable(display=1, logdir=None, context=5, format="html"): |
| 19421 | 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.""" | |
|
29511
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
315 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
|
316 context=context, format=format) |
