Mercurial > p > roundup > code
comparison roundup/cgi/cgitb.py @ 985:55ab0c5b49f9
New CGI interface support
| author | Richard Jones <richard@users.sourceforge.net> |
|---|---|
| date | Fri, 30 Aug 2002 08:28:44 +0000 |
| parents | |
| children | 0f9aa62917bd |
comparison
equal
deleted
inserted
replaced
| 984:a3a8ac7b8c40 | 985:55ab0c5b49f9 |
|---|---|
| 1 # | |
| 2 # This module was written by Ka-Ping Yee, <ping@lfw.org>. | |
| 3 # | |
| 4 # $Id: cgitb.py,v 1.1 2002-08-30 08:28:44 richard Exp $ | |
| 5 | |
| 6 __doc__ = """ | |
| 7 Extended CGI traceback handler by Ka-Ping Yee, <ping@lfw.org>. | |
| 8 """ | |
| 9 | |
| 10 import sys, os, types, string, keyword, linecache, tokenize, inspect, pydoc | |
| 11 | |
| 12 from roundup.i18n import _ | |
| 13 | |
| 14 def breaker(): | |
| 15 return ('<body bgcolor="white">' + | |
| 16 '<font color="white" size="-5"> > </font> ' + | |
| 17 '</table>' * 5) | |
| 18 | |
| 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>'%(etype, evalue), | |
| 26 '#ffffff', '#777777', pyver) | |
| 27 | |
| 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:')) | |
| 32 | |
| 33 indent = '<tt><small>%s</small> </tt>' % (' ' * 5) | |
| 34 traceback = [] | |
| 35 for frame, file, lnum, func, lines, index in inspect.trace(context): | |
| 36 if file is None: | |
| 37 link = '<file is None - probably inside <tt>eval</tt> or <tt>exec</tt>>' | |
| 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)) | |
| 48 | |
| 49 level = ''' | |
| 50 <table width="100%%" bgcolor="#dddddd" cellspacing=0 cellpadding=2 border=0> | |
| 51 <tr><td>%s %s</td></tr></table>''' % (link, call) | |
| 52 | |
| 53 if index is None or file is None: | |
| 54 traceback.append('<p>' + level) | |
| 55 continue | |
| 56 | |
| 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 | |
| 68 | |
| 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 = %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 = '' | |
| 93 | |
| 94 excerpt = [] | |
| 95 i = lnum - index | |
| 96 for line in lines: | |
| 97 number = ' ' * (5-len(str(i))) + str(i) | |
| 98 number = '<small><font color="#909090">%s</font></small>' % number | |
| 99 line = '<tt>%s %s</tt>' % (number, pydoc.html.preformat(line)) | |
| 100 if i == lnum: | |
| 101 line = ''' | |
| 102 <table width="100%%" bgcolor="#white" 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')) | |
| 109 | |
| 110 traceback.reverse() | |
| 111 | |
| 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 = %s' % (indent, name, value)) | |
| 118 | |
| 119 return head + string.join(attribs) + string.join(traceback) + '<p> </p>' | |
| 120 | |
| 121 def handler(): | |
| 122 print breaker() | |
| 123 print html() | |
| 124 | |
| 125 # | |
| 126 # $Log: not supported by cvs2svn $ | |
| 127 # Revision 1.10 2002/01/16 04:49:45 richard | |
| 128 # Handle a special case that the CGI interface tickles. I need to check if | |
| 129 # this needs fixing in python's core. | |
| 130 # | |
| 131 # Revision 1.9 2002/01/08 11:56:24 richard | |
| 132 # missed an import _ | |
| 133 # | |
| 134 # Revision 1.8 2002/01/05 02:22:32 richard | |
| 135 # i18n'ification | |
| 136 # | |
| 137 # Revision 1.7 2001/11/22 15:46:42 jhermann | |
| 138 # Added module docstrings to all modules. | |
| 139 # | |
| 140 # Revision 1.6 2001/09/29 13:27:00 richard | |
| 141 # CGI interfaces now spit up a top-level index of all the instances they can | |
| 142 # serve. | |
| 143 # | |
| 144 # Revision 1.5 2001/08/07 00:24:42 richard | |
| 145 # stupid typo | |
| 146 # | |
| 147 # Revision 1.4 2001/08/07 00:15:51 richard | |
| 148 # Added the copyright/license notice to (nearly) all files at request of | |
| 149 # Bizar Software. | |
| 150 # | |
| 151 # Revision 1.3 2001/07/29 07:01:39 richard | |
| 152 # Added vim command to all source so that we don't get no steenkin' tabs :) | |
| 153 # | |
| 154 # Revision 1.2 2001/07/22 12:09:32 richard | |
| 155 # Final commit of Grande Splite | |
| 156 # | |
| 157 # Revision 1.1 2001/07/22 11:58:35 richard | |
| 158 # More Grande Splite | |
| 159 # | |
| 160 # | |
| 161 # vim: set filetype=python ts=4 sw=4 et si |
