1 #
2 # This module was written by Ka-Ping Yee, <ping@lfw.org>.
3 #
4 # $Id: cgitb.py,v 1.10 2004-02-11 23:55:09 richard Exp $
6 """Extended CGI traceback handler by Ka-Ping Yee, <ping@lfw.org>.
7 """
8 __docformat__ = 'restructuredtext'
10 import sys, os, types, string, keyword, linecache, tokenize, inspect, cgi
11 import pydoc, traceback
13 from roundup.i18n import _
15 def breaker():
16 return ('<body bgcolor="white">' +
17 '<font color="white" size="-5"> > </font> ' +
18 '</table>' * 5)
20 def niceDict(indent, dict):
21 l = []
22 for k,v in dict.items():
23 l.append('<tr><td><strong>%s</strong></td><td>%s</td></tr>'%(k,
24 cgi.escape(repr(v))))
25 return '\n'.join(l)
27 def pt_html(context=5):
28 esc = cgi.escape
29 l = ['<h1>Templating Error</h1>',
30 '<p><b>%s</b>: %s</p>'%(esc(str(sys.exc_type)),
31 esc(str(sys.exc_value))),
32 '<p class="help">Debugging information follows</p>',
33 '<ol>',]
34 from roundup.cgi.PageTemplates.Expressions import TraversalError
35 t = inspect.trace(context)
36 t.reverse()
37 for frame, file, lnum, func, lines, index in t:
38 args, varargs, varkw, locals = inspect.getargvalues(frame)
39 if locals.has_key('__traceback_info__'):
40 ti = locals['__traceback_info__']
41 if isinstance(ti, TraversalError):
42 s = []
43 for name, info in ti.path:
44 s.append('<li>"%s" (%s)</li>'%(name, esc(repr(info))))
45 s = '\n'.join(s)
46 l.append('<li>Looking for "%s", current path:<ol>%s</ol></li>'%(
47 ti.name, s))
48 else:
49 l.append('<li>In %s</li>'%esc(str(ti)))
50 if locals.has_key('__traceback_supplement__'):
51 ts = locals['__traceback_supplement__']
52 if len(ts) == 2:
53 supp, context = ts
54 s = 'A problem occurred in your template "%s".'%str(context.id)
55 if context._v_errors:
56 s = s + '<br>' + '<br>'.join(
57 [esc(x) for x in context._v_errors])
58 l.append('<li>%s</li>'%s)
59 elif len(ts) == 3:
60 supp, context, info = ts
61 l.append('''
62 <li>While evaluating the %r expression on line %d
63 <table class="otherinfo" style="font-size: 90%%">
64 <tr><th colspan="2" class="header">Current variables:</th></tr>
65 %s
66 %s
67 </table></li>
68 '''%(info, context.position[0], niceDict(' ', context.global_vars),
69 niceDict(' ', context.local_vars)))
71 l.append('''
72 </ol>
73 <table style="font-size: 80%%; color: gray">
74 <tr><th class="header" align="left">Full traceback:</th></tr>
75 <tr><td><pre>%s</pre></td></tr>
76 </table>'''%cgi.escape(''.join(traceback.format_exception(sys.exc_type,
77 sys.exc_value, sys.exc_traceback))))
78 l.append('<p> </p>')
79 return '\n'.join(l)
81 def html(context=5):
82 etype, evalue = sys.exc_type, sys.exc_value
83 if type(etype) is types.ClassType:
84 etype = etype.__name__
85 pyver = 'Python ' + string.split(sys.version)[0] + '<br>' + sys.executable
86 head = pydoc.html.heading(
87 '<font size=+1><strong>%s</strong>: %s</font>'%(etype, evalue),
88 '#ffffff', '#777777', pyver)
90 head = head + (_('<p>A problem occurred while running a Python script. '
91 'Here is the sequence of function calls leading up to '
92 'the error, with the most recent (innermost) call first. '
93 'The exception attributes are:'))
95 indent = '<tt><small>%s</small> </tt>' % (' ' * 5)
96 traceback = []
97 for frame, file, lnum, func, lines, index in inspect.trace(context):
98 if file is None:
99 link = '''<file is None - probably inside <tt>eval</tt> or
100 <tt>exec</tt>>'''
101 else:
102 file = os.path.abspath(file)
103 link = '<a href="file:%s">%s</a>' % (file, pydoc.html.escape(file))
104 args, varargs, varkw, locals = inspect.getargvalues(frame)
105 if func == '?':
106 call = ''
107 else:
108 call = 'in <strong>%s</strong>' % func + inspect.formatargvalues(
109 args, varargs, varkw, locals,
110 formatvalue=lambda value: '=' + pydoc.html.repr(value))
112 level = '''
113 <table width="100%%" bgcolor="#dddddd" cellspacing=0 cellpadding=2 border=0>
114 <tr><td>%s %s</td></tr></table>''' % (link, call)
116 if index is None or file is None:
117 traceback.append('<p>' + level)
118 continue
120 # do a file inspection
121 names = []
122 def tokeneater(type, token, start, end, line, names=names):
123 if type == tokenize.NAME and token not in keyword.kwlist:
124 if token not in names:
125 names.append(token)
126 if type == tokenize.NEWLINE: raise IndexError
127 def linereader(file=file, lnum=[lnum]):
128 line = linecache.getline(file, lnum[0])
129 lnum[0] = lnum[0] + 1
130 return line
132 try:
133 tokenize.tokenize(linereader, tokeneater)
134 except IndexError:
135 pass
136 lvals = []
137 for name in names:
138 if name in frame.f_code.co_varnames:
139 if locals.has_key(name):
140 value = pydoc.html.repr(locals[name])
141 else:
142 value = _('<em>undefined</em>')
143 name = '<strong>%s</strong>' % name
144 else:
145 if frame.f_globals.has_key(name):
146 value = pydoc.html.repr(frame.f_globals[name])
147 else:
148 value = _('<em>undefined</em>')
149 name = '<em>global</em> <strong>%s</strong>' % name
150 lvals.append('%s = %s'%(name, value))
151 if lvals:
152 lvals = string.join(lvals, ', ')
153 lvals = indent + '<small><font color="#909090">%s'\
154 '</font></small><br>'%lvals
155 else:
156 lvals = ''
158 excerpt = []
159 i = lnum - index
160 for line in lines:
161 number = ' ' * (5-len(str(i))) + str(i)
162 number = '<small><font color="#909090">%s</font></small>' % number
163 line = '<tt>%s %s</tt>' % (number, pydoc.html.preformat(line))
164 if i == lnum:
165 line = '''
166 <table width="100%%" bgcolor="white" cellspacing=0 cellpadding=0 border=0>
167 <tr><td>%s</td></tr></table>''' % line
168 excerpt.append('\n' + line)
169 if i == lnum:
170 excerpt.append(lvals)
171 i = i + 1
172 traceback.append('<p>' + level + string.join(excerpt, '\n'))
174 traceback.reverse()
176 exception = '<p><strong>%s</strong>: %s' % (str(etype), str(evalue))
177 attribs = []
178 if type(evalue) is types.InstanceType:
179 for name in dir(evalue):
180 value = pydoc.html.repr(getattr(evalue, name))
181 attribs.append('<br>%s%s = %s' % (indent, name, value))
183 return head + string.join(attribs) + string.join(traceback) + '<p> </p>'
185 def handler():
186 print breaker()
187 print html()
189 # vim: set filetype=python ts=4 sw=4 et si