Code

oops
[roundup.git] / roundup / cgitb.py
1 #
2 # This module was written by Ka-Ping Yee, <ping@lfw.org>.
3
4 # $Id: cgitb.py,v 1.10 2002-01-16 04:49:45 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, pydoc
12 from i18n import _
14 def breaker():
15     return ('<body bgcolor="#f0f0ff">' +
16             '<font color="#f0f0ff" size="-5"> > </font> ' +
17             '</table>' * 5)
19 def html(context=5):
20     etype, evalue = sys.exc_type, sys.exc_value
21     if type(etype) is types.ClassType:
22         etype = etype.__name__
23     pyver = 'Python ' + string.split(sys.version)[0] + '<br>' + sys.executable
24     head = pydoc.html.heading(
25         '<font size=+1><strong>%s</strong>: %s</font>'%(str(etype), str(evalue)),
26         '#ffffff', '#aa55cc', pyver)
28     head = head + (_('<p>A problem occurred while running a Python script. '
29                    'Here is the sequence of function calls leading up to '
30                    'the error, with the most recent (innermost) call first. '
31                    'The exception attributes are:'))
33     indent = '<tt><small>%s</small>&nbsp;</tt>' % ('&nbsp;' * 5)
34     traceback = []
35     for frame, file, lnum, func, lines, index in inspect.trace(context):
36         if file is None:
37             link = '&lt;file is None - probably inside <tt>eval</tt> or <tt>exec</tt>&gt;'
38         else:
39             file = os.path.abspath(file)
40             link = '<a href="file:%s">%s</a>' % (file, pydoc.html.escape(file))
41         args, varargs, varkw, locals = inspect.getargvalues(frame)
42         if func == '?':
43             call = ''
44         else:
45             call = 'in <strong>%s</strong>' % func + inspect.formatargvalues(
46                     args, varargs, varkw, locals,
47                     formatvalue=lambda value: '=' + pydoc.html.repr(value))
49         level = '''
50 <table width="100%%" bgcolor="#d8bbff" cellspacing=0 cellpadding=2 border=0>
51 <tr><td>%s %s</td></tr></table>''' % (link, call)
53         if index is None or file is None:
54             traceback.append('<p>' + level)
55             continue
57         # do a fil inspection
58         names = []
59         def tokeneater(type, token, start, end, line, names=names):
60             if type == tokenize.NAME and token not in keyword.kwlist:
61                 if token not in names:
62                     names.append(token)
63             if type == tokenize.NEWLINE: raise IndexError
64         def linereader(file=file, lnum=[lnum]):
65             line = linecache.getline(file, lnum[0])
66             lnum[0] = lnum[0] + 1
67             return line
69         try:
70             tokenize.tokenize(linereader, tokeneater)
71         except IndexError: pass
72         lvals = []
73         for name in names:
74             if name in frame.f_code.co_varnames:
75                 if locals.has_key(name):
76                     value = pydoc.html.repr(locals[name])
77                 else:
78                     value = _('<em>undefined</em>')
79                 name = '<strong>%s</strong>' % name
80             else:
81                 if frame.f_globals.has_key(name):
82                     value = pydoc.html.repr(frame.f_globals[name])
83                 else:
84                     value = _('<em>undefined</em>')
85                 name = '<em>global</em> <strong>%s</strong>' % name
86             lvals.append('%s&nbsp;= %s' % (name, value))
87         if lvals:
88             lvals = string.join(lvals, ', ')
89             lvals = indent + '''
90 <small><font color="#909090">%s</font></small><br>''' % lvals
91         else:
92             lvals = ''
94         excerpt = []
95         i = lnum - index
96         for line in lines:
97             number = '&nbsp;' * (5-len(str(i))) + str(i)
98             number = '<small><font color="#909090">%s</font></small>' % number
99             line = '<tt>%s&nbsp;%s</tt>' % (number, pydoc.html.preformat(line))
100             if i == lnum:
101                 line = '''
102 <table width="100%%" bgcolor="#ffccee" cellspacing=0 cellpadding=0 border=0>
103 <tr><td>%s</td></tr></table>''' % line
104             excerpt.append('\n' + line)
105             if i == lnum:
106                 excerpt.append(lvals)
107             i = i + 1
108         traceback.append('<p>' + level + string.join(excerpt, '\n'))
110     traceback.reverse()
112     exception = '<p><strong>%s</strong>: %s' % (str(etype), str(evalue))
113     attribs = []
114     if type(evalue) is types.InstanceType:
115         for name in dir(evalue):
116             value = pydoc.html.repr(getattr(evalue, name))
117             attribs.append('<br>%s%s&nbsp;= %s' % (indent, name, value))
119     return head + string.join(attribs) + string.join(traceback) + '<p>&nbsp;</p>'
121 def handler():
122     print breaker()
123     print html()
126 # $Log: not supported by cvs2svn $
127 # Revision 1.9  2002/01/08 11:56:24  richard
128 # missed an import _
130 # Revision 1.8  2002/01/05 02:22:32  richard
131 # i18n'ification
133 # Revision 1.7  2001/11/22 15:46:42  jhermann
134 # Added module docstrings to all modules.
136 # Revision 1.6  2001/09/29 13:27:00  richard
137 # CGI interfaces now spit up a top-level index of all the instances they can
138 # serve.
140 # Revision 1.5  2001/08/07 00:24:42  richard
141 # stupid typo
143 # Revision 1.4  2001/08/07 00:15:51  richard
144 # Added the copyright/license notice to (nearly) all files at request of
145 # Bizar Software.
147 # Revision 1.3  2001/07/29 07:01:39  richard
148 # Added vim command to all source so that we don't get no steenkin' tabs :)
150 # Revision 1.2  2001/07/22 12:09:32  richard
151 # Final commit of Grande Splite
153 # Revision 1.1  2001/07/22 11:58:35  richard
154 # More Grande Splite
157 # vim: set filetype=python ts=4 sw=4 et si