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