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