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