Code

dce420e699b427d6b75ac3cedbe7290150029425
[roundup.git] / roundup / cgi / cgitb.py
1 #
2 # This module was written by Ka-Ping Yee, <ping@lfw.org>.
3
4 # $Id: cgitb.py,v 1.4 2002-09-09 05:28:48 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>&nbsp;</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>&nbsp;</tt>' % ('&nbsp;' * 5)
76     traceback = []
77     for frame, file, lnum, func, lines, index in inspect.trace(context):
78         if file is None:
79             link = '''&lt;file is None - probably inside <tt>eval</tt> or
80                     <tt>exec</tt>&gt;'''
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&nbsp;= %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 = '&nbsp;' * (5-len(str(i))) + str(i)
142             number = '<small><font color="#909090">%s</font></small>' % number
143             line = '<tt>%s&nbsp;%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&nbsp;= %s' % (indent, name, value))
163     return head + string.join(attribs) + string.join(traceback) + '<p>&nbsp;</p>'
165 def handler():
166     print breaker()
167     print html()
170 # $Log: not supported by cvs2svn $
171 # Revision 1.3  2002/09/06 07:23:29  richard
172 # tweak
174 # Revision 1.2  2002/09/06 07:21:31  richard
175 # much nicer error messages when there's a templating error
177 # Revision 1.1  2002/08/30 08:28:44  richard
178 # New CGI interface support
180 # Revision 1.10  2002/01/16 04:49:45  richard
181 # Handle a special case that the CGI interface tickles. I need to check if
182 # this needs fixing in python's core.
184 # Revision 1.9  2002/01/08 11:56:24  richard
185 # missed an import _
187 # Revision 1.8  2002/01/05 02:22:32  richard
188 # i18n'ification
190 # Revision 1.7  2001/11/22 15:46:42  jhermann
191 # Added module docstrings to all modules.
193 # Revision 1.6  2001/09/29 13:27:00  richard
194 # CGI interfaces now spit up a top-level index of all the instances they can
195 # serve.
197 # Revision 1.5  2001/08/07 00:24:42  richard
198 # stupid typo
200 # Revision 1.4  2001/08/07 00:15:51  richard
201 # Added the copyright/license notice to (nearly) all files at request of
202 # Bizar Software.
204 # Revision 1.3  2001/07/29 07:01:39  richard
205 # Added vim command to all source so that we don't get no steenkin' tabs :)
207 # Revision 1.2  2001/07/22 12:09:32  richard
208 # Final commit of Grande Splite
210 # Revision 1.1  2001/07/22 11:58:35  richard
211 # More Grande Splite
214 # vim: set filetype=python ts=4 sw=4 et si