Mercurial > cpython-lru_cache-weakref
annotate Lib/cgitb.py @ 80661:94de77bd0b4b 2.7
Issue #16194: document imp.load_dynamic problems
when read several extension modules from the same binary object file.
| author | Andrew Svetlov <andrew.svetlov@gmail.com> |
|---|---|
| date | Thu, 29 Nov 2012 19:51:28 +0200 |
| parents | 715e5b337c91 |
| children |
| 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. |
|
52649
76d61c12623c
The cgitb module had imports in its functions. This can cause deadlock with the
Brett Cannon <bcannon@gmail.com>
parents:
41719
diff
changeset
|
22 |
|
29511
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
23 """ |
|
52649
76d61c12623c
The cgitb module had imports in its functions. This can cause deadlock with the
Brett Cannon <bcannon@gmail.com>
parents:
41719
diff
changeset
|
24 import inspect |
|
76d61c12623c
The cgitb module had imports in its functions. This can cause deadlock with the
Brett Cannon <bcannon@gmail.com>
parents:
41719
diff
changeset
|
25 import keyword |
|
76d61c12623c
The cgitb module had imports in its functions. This can cause deadlock with the
Brett Cannon <bcannon@gmail.com>
parents:
41719
diff
changeset
|
26 import linecache |
|
76d61c12623c
The cgitb module had imports in its functions. This can cause deadlock with the
Brett Cannon <bcannon@gmail.com>
parents:
41719
diff
changeset
|
27 import os |
|
76d61c12623c
The cgitb module had imports in its functions. This can cause deadlock with the
Brett Cannon <bcannon@gmail.com>
parents:
41719
diff
changeset
|
28 import pydoc |
|
21194
1519d6faab15
Add "file" argument to Hook constructor.
Ka-Ping Yee <ping@zesty.ca>
parents:
19421
diff
changeset
|
29 import sys |
|
52649
76d61c12623c
The cgitb module had imports in its functions. This can cause deadlock with the
Brett Cannon <bcannon@gmail.com>
parents:
41719
diff
changeset
|
30 import tempfile |
|
76d61c12623c
The cgitb module had imports in its functions. This can cause deadlock with the
Brett Cannon <bcannon@gmail.com>
parents:
41719
diff
changeset
|
31 import time |
|
76d61c12623c
The cgitb module had imports in its functions. This can cause deadlock with the
Brett Cannon <bcannon@gmail.com>
parents:
41719
diff
changeset
|
32 import tokenize |
|
76d61c12623c
The cgitb module had imports in its functions. This can cause deadlock with the
Brett Cannon <bcannon@gmail.com>
parents:
41719
diff
changeset
|
33 import traceback |
|
76d61c12623c
The cgitb module had imports in its functions. This can cause deadlock with the
Brett Cannon <bcannon@gmail.com>
parents:
41719
diff
changeset
|
34 import types |
|
21194
1519d6faab15
Add "file" argument to Hook constructor.
Ka-Ping Yee <ping@zesty.ca>
parents:
19421
diff
changeset
|
35 |
| 19365 | 36 def reset(): |
| 37 """Return a string that resets the CGI and browser to a known state.""" | |
| 38 return '''<!--: spam | |
| 39 Content-Type: text/html | |
| 40 | |
| 19421 | 41 <body bgcolor="#f0f0f8"><font color="#f0f0f8" size="-5"> --> |
| 42 <body bgcolor="#f0f0f8"><font color="#f0f0f8" size="-5"> --> --> | |
| 19365 | 43 </font> </font> </font> </script> </object> </blockquote> </pre> |
| 44 </table> </table> </table> </table> </table> </font> </font> </font>''' | |
| 45 | |
| 19421 | 46 __UNDEF__ = [] # a special sentinel object |
|
32577
fef7911a5705
[Patch #987052 from Thomas Guettler]
Andrew M. Kuchling <amk@amk.ca>
parents:
32292
diff
changeset
|
47 def small(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 '<small>' + text + '</small>' |
|
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 strong(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 '<strong>' + text + '</strong>' |
|
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 '' |
|
32736
c3a096bfbd6b
Whitespace normalization, via reindent.py.
Tim Peters <tim.peters@gmail.com>
parents:
32577
diff
changeset
|
58 |
|
32577
fef7911a5705
[Patch #987052 from Thomas Guettler]
Andrew M. Kuchling <amk@amk.ca>
parents:
32292
diff
changeset
|
59 def grey(text): |
|
fef7911a5705
[Patch #987052 from Thomas Guettler]
Andrew M. Kuchling <amk@amk.ca>
parents:
32292
diff
changeset
|
60 if text: |
|
fef7911a5705
[Patch #987052 from Thomas Guettler]
Andrew M. Kuchling <amk@amk.ca>
parents:
32292
diff
changeset
|
61 return '<font color="#909090">' + text + '</font>' |
|
fef7911a5705
[Patch #987052 from Thomas Guettler]
Andrew M. Kuchling <amk@amk.ca>
parents:
32292
diff
changeset
|
62 else: |
|
fef7911a5705
[Patch #987052 from Thomas Guettler]
Andrew M. Kuchling <amk@amk.ca>
parents:
32292
diff
changeset
|
63 return '' |
| 19421 | 64 |
| 65 def lookup(name, frame, locals): | |
| 66 """Find the value for a given name in the given environment.""" | |
| 67 if name in locals: | |
| 68 return 'local', locals[name] | |
| 69 if name in frame.f_globals: | |
| 70 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
|
71 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
|
72 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
|
73 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
|
74 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
|
75 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
|
76 else: |
|
dab8701bd3fa
Also look up variable names in __builtins__ if not found in globals.
Ka-Ping Yee <ping@zesty.ca>
parents:
21480
diff
changeset
|
77 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
|
78 return 'builtin', getattr(builtins, name) |
| 19421 | 79 return None, __UNDEF__ |
| 80 | |
| 81 def scanvars(reader, frame, locals): | |
| 82 """Scan one logical line of Python and look up values of variables used.""" | |
|
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
|
83 vars, lasttoken, parent, prefix, value = [], None, None, '', __UNDEF__ |
| 19421 | 84 for ttype, token, start, end, line in tokenize.generate_tokens(reader): |
| 85 if ttype == tokenize.NEWLINE: break | |
| 86 if ttype == tokenize.NAME and token not in keyword.kwlist: | |
| 87 if lasttoken == '.': | |
| 88 if parent is not __UNDEF__: | |
| 89 value = getattr(parent, token, __UNDEF__) | |
| 90 vars.append((prefix + token, prefix, value)) | |
| 91 else: | |
| 92 where, value = lookup(token, frame, locals) | |
| 93 vars.append((token, where, value)) | |
| 94 elif token == '.': | |
| 95 prefix += lasttoken + '.' | |
| 96 parent = value | |
| 97 else: | |
| 98 parent, prefix = None, '' | |
| 99 lasttoken = token | |
| 100 return vars | |
| 101 | |
|
63579
ac29f70c61db
Merged revisions 79558 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
41719
diff
changeset
|
102 def html(einfo, context=5): |
| 19421 | 103 """Return a nice HTML document describing a given traceback.""" |
|
63579
ac29f70c61db
Merged revisions 79558 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
41719
diff
changeset
|
104 etype, evalue, etb = einfo |
| 19365 | 105 if type(etype) is types.ClassType: |
| 106 etype = etype.__name__ | |
| 107 pyver = 'Python ' + sys.version.split()[0] + ': ' + sys.executable | |
| 108 date = time.ctime(time.time()) | |
| 19421 | 109 head = '<body bgcolor="#f0f0f8">' + pydoc.html.heading( |
|
32577
fef7911a5705
[Patch #987052 from Thomas Guettler]
Andrew M. Kuchling <amk@amk.ca>
parents:
32292
diff
changeset
|
110 '<big><big>%s</big></big>' % |
|
fef7911a5705
[Patch #987052 from Thomas Guettler]
Andrew M. Kuchling <amk@amk.ca>
parents:
32292
diff
changeset
|
111 strong(pydoc.html.escape(str(etype))), |
| 19421 | 112 '#ffffff', '#6622aa', pyver + '<br>' + date) + ''' |
| 113 <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
|
114 function calls leading up to the error, in the order they occurred.</p>''' |
| 19365 | 115 |
| 19421 | 116 indent = '<tt>' + small(' ' * 5) + ' </tt>' |
| 19365 | 117 frames = [] |
| 118 records = inspect.getinnerframes(etb, context) | |
| 119 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
|
120 if file: |
|
a31a770bb778
Prevent creating a HTML link to file://?/
Georg Brandl <georg@python.org>
parents:
34545
diff
changeset
|
121 file = os.path.abspath(file) |
|
a31a770bb778
Prevent creating a HTML link to file://?/
Georg Brandl <georg@python.org>
parents:
34545
diff
changeset
|
122 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
|
123 else: |
|
a31a770bb778
Prevent creating a HTML link to file://?/
Georg Brandl <georg@python.org>
parents:
34545
diff
changeset
|
124 file = link = '?' |
| 19365 | 125 args, varargs, varkw, locals = inspect.getargvalues(frame) |
| 19421 | 126 call = '' |
| 127 if func != '?': | |
| 128 call = 'in ' + strong(func) + \ | |
| 129 inspect.formatargvalues(args, varargs, varkw, locals, | |
| 130 formatvalue=lambda value: '=' + pydoc.html.repr(value)) | |
| 19365 | 131 |
| 19421 | 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) | |
| 19365 | 138 |
| 19421 | 139 rows = ['<tr><td bgcolor="#d8bbff">%s%s %s</td></tr>' % |
| 140 ('<big> </big>', link, call)] | |
| 19365 | 141 if index is not None: |
| 142 i = lnum - index | |
| 143 for line in lines: | |
| 19421 | 144 num = small(' ' * (5-len(str(i))) + str(i)) + ' ' |
| 145 if i in highlight: | |
|
52888
a16a0456978f
#602893: add indicator for current line in cgitb that doesnt rely on styling alone.
Georg Brandl <georg@python.org>
parents:
52649
diff
changeset
|
146 line = '<tt>=>%s%s</tt>' % (num, pydoc.html.preformat(line)) |
| 19421 | 147 rows.append('<tr><td bgcolor="#ffccee">%s</td></tr>' % line) |
| 148 else: | |
|
52888
a16a0456978f
#602893: add indicator for current line in cgitb that doesnt rely on styling alone.
Georg Brandl <georg@python.org>
parents:
52649
diff
changeset
|
149 line = '<tt> %s%s</tt>' % (num, pydoc.html.preformat(line)) |
| 19421 | 150 rows.append('<tr><td>%s</td></tr>' % grey(line)) |
| 151 i += 1 | |
| 19365 | 152 |
| 19421 | 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__: | |
|
34545
9d66fba78444
Replace list of constants with tuples of constants.
Raymond Hettinger <python@rcn.com>
parents:
32736
diff
changeset
|
158 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
|
159 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
|
160 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
|
161 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
|
162 else: |
|
dab8701bd3fa
Also look up variable names in __builtins__ if not found in globals.
Ka-Ping Yee <ping@zesty.ca>
parents:
21480
diff
changeset
|
163 name = where + strong(name.split('.')[-1]) |
| 19421 | 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)))) | |
|
32577
fef7911a5705
[Patch #987052 from Thomas Guettler]
Andrew M. Kuchling <amk@amk.ca>
parents:
32292
diff
changeset
|
169 frames.append(''' |
| 19421 | 170 <table width="100%%" cellspacing=0 cellpadding=0 border=0> |
| 171 %s</table>''' % '\n'.join(rows)) | |
| 172 | |
|
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
|
173 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
|
174 pydoc.html.escape(str(evalue)))] |
|
41438
a01cb3df38dd
Exceptions are no longer old-style instances. Fix accordingly.
Georg Brandl <georg@python.org>
parents:
35742
diff
changeset
|
175 if isinstance(evalue, BaseException): |
| 19365 | 176 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
|
177 if name[:1] == '_': continue |
| 19365 | 178 value = pydoc.html.repr(getattr(evalue, name)) |
| 179 exception.append('\n<br>%s%s =\n%s' % (indent, name, value)) | |
| 180 | |
| 181 return head + ''.join(frames) + ''.join(exception) + ''' | |
| 182 | |
| 183 | |
| 19421 | 184 <!-- The above is a description of an error in a Python program, formatted |
| 185 for a Web browser because the 'cgitb' module was enabled. In case you | |
| 186 are not reading this in a Web browser, here is the original traceback: | |
| 19365 | 187 |
| 188 %s | |
| 189 --> | |
|
41719
3e1eb7af4ea0
HTML-escape the plain traceback in cgitb's HTML output, to prevent
Georg Brandl <georg@python.org>
parents:
41438
diff
changeset
|
190 ''' % pydoc.html.escape( |
|
3e1eb7af4ea0
HTML-escape the plain traceback in cgitb's HTML output, to prevent
Georg Brandl <georg@python.org>
parents:
41438
diff
changeset
|
191 ''.join(traceback.format_exception(etype, evalue, etb))) |
| 19365 | 192 |
|
63579
ac29f70c61db
Merged revisions 79558 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
41719
diff
changeset
|
193 def text(einfo, context=5): |
|
29511
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
194 """Return a plain text document describing a given traceback.""" |
|
63579
ac29f70c61db
Merged revisions 79558 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
41719
diff
changeset
|
195 etype, evalue, etb = einfo |
|
29511
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
196 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
|
197 etype = etype.__name__ |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
198 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
|
199 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
|
200 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
|
201 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
|
202 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
|
203 ''' |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
204 |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
205 frames = [] |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
206 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
|
207 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
|
208 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
|
209 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
|
210 call = '' |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
211 if func != '?': |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
212 call = 'in ' + func + \ |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
213 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
|
214 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
|
215 |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
216 highlight = {} |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
217 def reader(lnum=[lnum]): |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
218 highlight[lnum[0]] = 1 |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
219 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
|
220 finally: lnum[0] += 1 |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
221 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
|
222 |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
223 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
|
224 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
|
225 i = lnum - index |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
226 for line in lines: |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
227 num = '%5d ' % i |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
228 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
|
229 i += 1 |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
230 |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
231 done, dump = {}, [] |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
232 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
|
233 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
|
234 done[name] = 1 |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
235 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
|
236 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
|
237 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
|
238 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
|
239 else: |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
240 dump.append(name + ' undefined') |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
241 |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
242 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
|
243 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
|
244 |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
245 exception = ['%s: %s' % (str(etype), str(evalue))] |
|
41438
a01cb3df38dd
Exceptions are no longer old-style instances. Fix accordingly.
Georg Brandl <georg@python.org>
parents:
35742
diff
changeset
|
246 if isinstance(evalue, BaseException): |
|
29511
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
247 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
|
248 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
|
249 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
|
250 |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
251 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
|
252 |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
253 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
|
254 the original traceback: |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
255 |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
256 %s |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
257 ''' % ''.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
|
258 |
| 19365 | 259 class Hook: |
| 19421 | 260 """A hook to replace sys.excepthook that shows tracebacks in HTML.""" |
| 261 | |
|
29511
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
262 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
|
263 format="html"): |
| 19365 | 264 self.display = display # send tracebacks to browser if true |
| 265 self.logdir = logdir # log tracebacks to files if not None | |
| 19421 | 266 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
|
267 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
|
268 self.format = format |
| 19365 | 269 |
| 270 def __call__(self, etype, evalue, etb): | |
| 271 self.handle((etype, evalue, etb)) | |
| 272 | |
| 273 def handle(self, info=None): | |
| 274 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
|
275 if self.format == "html": |
|
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
276 self.file.write(reset()) |
| 19365 | 277 |
|
29511
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
278 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
|
279 plain = False |
| 19365 | 280 try: |
|
29511
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
281 doc = formatter(info, self.context) |
| 19365 | 282 except: # just in case something goes wrong |
|
29511
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
283 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
|
284 plain = True |
| 19365 | 285 |
| 286 if self.display: | |
|
29511
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
287 if plain: |
| 19365 | 288 doc = doc.replace('&', '&').replace('<', '<') |
|
21194
1519d6faab15
Add "file" argument to Hook constructor.
Ka-Ping Yee <ping@zesty.ca>
parents:
19421
diff
changeset
|
289 self.file.write('<pre>' + doc + '</pre>\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(doc + '\n') |
| 19365 | 292 else: |
|
21194
1519d6faab15
Add "file" argument to Hook constructor.
Ka-Ping Yee <ping@zesty.ca>
parents:
19421
diff
changeset
|
293 self.file.write('<p>A problem occurred in a Python script.\n') |
| 19365 | 294 |
| 295 if self.logdir is not None: | |
|
32001
2cc714977479
[Bug #945063] Get file extension correct. (2.3 bugfix candidate)
Andrew M. Kuchling <amk@amk.ca>
parents:
31850
diff
changeset
|
296 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
|
297 (fd, path) = tempfile.mkstemp(suffix=suffix, dir=self.logdir) |
|
79971
715e5b337c91
#12890: don't emit <p> tags in text mode when logdir specified.
R David Murray <rdmurray@bitdance.com>
parents:
68256
diff
changeset
|
298 |
| 19365 | 299 try: |
|
24710
11c701038805
Massive changes from SF 589982 (tempfile.py rewrite, by Zack
Guido van Rossum <guido@python.org>
parents:
23998
diff
changeset
|
300 file = os.fdopen(fd, 'w') |
| 19365 | 301 file.write(doc) |
| 302 file.close() | |
|
79971
715e5b337c91
#12890: don't emit <p> tags in text mode when logdir specified.
R David Murray <rdmurray@bitdance.com>
parents:
68256
diff
changeset
|
303 msg = '%s contains the description of this error.' % path |
| 19365 | 304 except: |
|
79971
715e5b337c91
#12890: don't emit <p> tags in text mode when logdir specified.
R David Murray <rdmurray@bitdance.com>
parents:
68256
diff
changeset
|
305 msg = 'Tried to save traceback to %s, but failed.' % path |
|
715e5b337c91
#12890: don't emit <p> tags in text mode when logdir specified.
R David Murray <rdmurray@bitdance.com>
parents:
68256
diff
changeset
|
306 |
|
715e5b337c91
#12890: don't emit <p> tags in text mode when logdir specified.
R David Murray <rdmurray@bitdance.com>
parents:
68256
diff
changeset
|
307 if self.format == 'html': |
|
715e5b337c91
#12890: don't emit <p> tags in text mode when logdir specified.
R David Murray <rdmurray@bitdance.com>
parents:
68256
diff
changeset
|
308 self.file.write('<p>%s</p>\n' % msg) |
|
715e5b337c91
#12890: don't emit <p> tags in text mode when logdir specified.
R David Murray <rdmurray@bitdance.com>
parents:
68256
diff
changeset
|
309 else: |
|
715e5b337c91
#12890: don't emit <p> tags in text mode when logdir specified.
R David Murray <rdmurray@bitdance.com>
parents:
68256
diff
changeset
|
310 self.file.write(msg + '\n') |
|
21194
1519d6faab15
Add "file" argument to Hook constructor.
Ka-Ping Yee <ping@zesty.ca>
parents:
19421
diff
changeset
|
311 try: |
|
1519d6faab15
Add "file" argument to Hook constructor.
Ka-Ping Yee <ping@zesty.ca>
parents:
19421
diff
changeset
|
312 self.file.flush() |
|
1519d6faab15
Add "file" argument to Hook constructor.
Ka-Ping Yee <ping@zesty.ca>
parents:
19421
diff
changeset
|
313 except: pass |
| 19365 | 314 |
| 315 handler = Hook().handle | |
|
29511
d1acf89d1c49
SF Patch 569574 - enhancements to cgitb for plain text display
Skip Montanaro <skip@pobox.com>
parents:
26912
diff
changeset
|
316 def enable(display=1, logdir=None, context=5, format="html"): |
| 19421 | 317 """Install an exception handler that formats tracebacks as HTML. |
| 318 | |
| 319 The optional argument 'display' can be set to 0 to suppress sending the | |
| 320 traceback to the browser, and 'logdir' can be set to a directory to cause | |
| 321 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
|
322 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
|
323 context=context, format=format) |
