1 #
2 # This module was written by Ka-Ping Yee, <ping@lfw.org>.
3 #
4 # $Id: cgitb.py,v 1.5 2002-09-10 01:07:05 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
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('%s%s: %r'%(indent,k,v))
24 return '\n'.join(l)
26 def pt_html(context=5):
27 import cgi
28 etype, evalue = sys.exc_type, sys.exc_value
29 if type(etype) is types.ClassType:
30 etype = etype.__name__
31 pyver = 'Python ' + string.split(sys.version)[0] + '<br>' + sys.executable
32 head = pydoc.html.heading(
33 '<font size=+1><strong>%s</strong>: %s</font>'%(etype, evalue),
34 '#ffffff', '#777777', pyver)
36 head = head + _('<p>A problem occurred in your template</p><pre>')
38 l = []
39 for frame, file, lnum, func, lines, index in inspect.trace(context):
40 args, varargs, varkw, locals = inspect.getargvalues(frame)
41 if locals.has_key('__traceback_info__'):
42 ti = locals['__traceback_info__']
43 l.append(str(ti))
44 if locals.has_key('__traceback_supplement__'):
45 ts = locals['__traceback_supplement__']
46 if len(ts) == 2:
47 supp, context = ts
48 l.append('in template %r'%context.id)
49 elif len(ts) == 3:
50 supp, context, info = ts
51 l.append('in expression %r\n current variables:\n%s\n%s\n'%(info,
52 niceDict(' ', context.global_vars),
53 niceDict(' ', context.local_vars)))
54 # context._scope_stack))
56 l.append('\n')
57 l.append(''.join(traceback.format_exception(etype, evalue,
58 sys.exc_traceback)))
59 return head + cgi.escape('\n'.join(l)) + '</pre><p> </p>'
61 def html(context=5):
62 etype, evalue = sys.exc_type, sys.exc_value
63 if type(etype) is types.ClassType:
64 etype = etype.__name__
65 pyver = 'Python ' + string.split(sys.version)[0] + '<br>' + sys.executable
66 head = pydoc.html.heading(
67 '<font size=+1><strong>%s</strong>: %s</font>'%(etype, evalue),
68 '#ffffff', '#777777', pyver)
70 head = head + (_('<p>A problem occurred while running a Python script. '
71 'Here is the sequence of function calls leading up to '
72 'the error, with the most recent (innermost) call first. '
73 'The exception attributes are:'))
75 indent = '<tt><small>%s</small> </tt>' % (' ' * 5)
76 traceback = []
77 for frame, file, lnum, func, lines, index in inspect.trace(context):
78 if file is None:
79 link = '''<file is None - probably inside <tt>eval</tt> or
80 <tt>exec</tt>>'''
81 else:
82 file = os.path.abspath(file)
83 link = '<a href="file:%s">%s</a>' % (file, pydoc.html.escape(file))
84 args, varargs, varkw, locals = inspect.getargvalues(frame)
85 if func == '?':
86 call = ''
87 else:
88 call = 'in <strong>%s</strong>' % func + inspect.formatargvalues(
89 args, varargs, varkw, locals,
90 formatvalue=lambda value: '=' + pydoc.html.repr(value))
92 level = '''
93 <table width="100%%" bgcolor="#dddddd" cellspacing=0 cellpadding=2 border=0>
94 <tr><td>%s %s</td></tr></table>''' % (link, call)
96 if index is None or file is None:
97 traceback.append('<p>' + level)
98 continue
100 # do a file inspection
101 names = []
102 def tokeneater(type, token, start, end, line, names=names):
103 if type == tokenize.NAME and token not in keyword.kwlist:
104 if token not in names:
105 names.append(token)
106 if type == tokenize.NEWLINE: raise IndexError
107 def linereader(file=file, lnum=[lnum]):
108 line = linecache.getline(file, lnum[0])
109 lnum[0] = lnum[0] + 1
110 return line
112 try:
113 tokenize.tokenize(linereader, tokeneater)
114 except IndexError:
115 pass
116 lvals = []
117 for name in names:
118 if name in frame.f_code.co_varnames:
119 if locals.has_key(name):
120 value = pydoc.html.repr(locals[name])
121 else:
122 value = _('<em>undefined</em>')
123 name = '<strong>%s</strong>' % name
124 else:
125 if frame.f_globals.has_key(name):
126 value = pydoc.html.repr(frame.f_globals[name])
127 else:
128 value = _('<em>undefined</em>')
129 name = '<em>global</em> <strong>%s</strong>' % name
130 lvals.append('%s = %s'%(name, value))
131 if lvals:
132 lvals = string.join(lvals, ', ')
133 lvals = indent + '<small><font color="#909090">%s'\
134 '</font></small><br>'%lvals
135 else:
136 lvals = ''
138 excerpt = []
139 i = lnum - index
140 for line in lines:
141 number = ' ' * (5-len(str(i))) + str(i)
142 number = '<small><font color="#909090">%s</font></small>' % number
143 line = '<tt>%s %s</tt>' % (number, pydoc.html.preformat(line))
144 if i == lnum:
145 line = '''
146 <table width="100%%" bgcolor="#white" cellspacing=0 cellpadding=0 border=0>
147 <tr><td>%s</td></tr></table>''' % line
148 excerpt.append('\n' + line)
149 if i == lnum:
150 excerpt.append(lvals)
151 i = i + 1
152 traceback.append('<p>' + level + string.join(excerpt, '\n'))
154 traceback.reverse()
156 exception = '<p><strong>%s</strong>: %s' % (str(etype), str(evalue))
157 attribs = []
158 if type(evalue) is types.InstanceType:
159 for name in dir(evalue):
160 value = pydoc.html.repr(getattr(evalue, name))
161 attribs.append('<br>%s%s = %s' % (indent, name, value))
163 return head + string.join(attribs) + string.join(traceback) + '<p> </p>'
165 def handler():
166 print breaker()
167 print html()
169 # vim: set filetype=python ts=4 sw=4 et si