Mercurial > p > roundup > code
comparison roundup/cgi/cgitb.py @ 6201:70e6b053193b
pep8 fixes
| author | John Rouillard <rouilj@ieee.org> |
|---|---|
| date | Fri, 19 Jun 2020 21:39:17 -0400 |
| parents | 883c9e90b403 |
| children | 3129d73e8535 |
comparison
equal
deleted
inserted
replaced
| 6200:718f205dbe50 | 6201:70e6b053193b |
|---|---|
| 5 """Extended CGI traceback handler by Ka-Ping Yee, <ping@lfw.org>. | 5 """Extended CGI traceback handler by Ka-Ping Yee, <ping@lfw.org>. |
| 6 """ | 6 """ |
| 7 from __future__ import print_function | 7 from __future__ import print_function |
| 8 __docformat__ = 'restructuredtext' | 8 __docformat__ = 'restructuredtext' |
| 9 | 9 |
| 10 import sys, os, keyword, linecache, tokenize, inspect, cgi | 10 import sys, os, keyword, linecache, tokenize, inspect |
| 11 import pydoc, traceback | 11 import pydoc, traceback |
| 12 | 12 |
| 13 from roundup.anypy.html import html_escape | 13 from roundup.anypy.html import html_escape |
| 14 | 14 |
| 15 from roundup.cgi import templating, TranslationService | 15 from roundup.cgi import TranslationService |
| 16 from roundup.anypy.strings import s2b | 16 from roundup.anypy.strings import s2b |
| 17 | |
| 17 | 18 |
| 18 def get_translator(i18n=None): | 19 def get_translator(i18n=None): |
| 19 """Return message translation function (gettext) | 20 """Return message translation function (gettext) |
| 20 | 21 |
| 21 Parameters: | 22 Parameters: |
| 28 TranslationService. | 29 TranslationService. |
| 29 | 30 |
| 30 """ | 31 """ |
| 31 try: | 32 try: |
| 32 return i18n.gettext | 33 return i18n.gettext |
| 33 except: | 34 except AttributeError: |
| 34 return TranslationService.get_translation().gettext | 35 return TranslationService.get_translation().gettext |
| 36 | |
| 35 | 37 |
| 36 def breaker(): | 38 def breaker(): |
| 37 return ('<body bgcolor="white">' + | 39 return ('<body bgcolor="white">' + |
| 38 '<font color="white" size="-5"> > </font> ' + | 40 '<font color="white" size="-5"> > </font> ' + |
| 39 '</table>' * 5) | 41 '</table>' * 5) |
| 40 | 42 |
| 43 | |
| 41 def niceDict(indent, dict): | 44 def niceDict(indent, dict): |
| 42 l = [] | 45 l = [] |
| 43 for k in sorted(dict): | 46 for k in sorted(dict): |
| 44 v = dict[k] | 47 v = dict[k] |
| 45 l.append('<tr><td><strong>%s</strong></td><td>%s</td></tr>'%(k, | 48 l.append('<tr><td><strong>%s</strong></td><td>%s</td></tr>' % (k, |
| 46 html_escape(repr(v)))) | 49 html_escape(repr(v)))) |
| 47 return '\n'.join(l) | 50 return '\n'.join(l) |
| 51 | |
| 48 | 52 |
| 49 def pt_html(context=5, i18n=None): | 53 def pt_html(context=5, i18n=None): |
| 50 _ = get_translator(i18n) | 54 _ = get_translator(i18n) |
| 51 esc = html_escape | 55 esc = html_escape |
| 52 exc_info = [esc(str(value)) for value in sys.exc_info()[:2]] | 56 exc_info = [esc(str(value)) for value in sys.exc_info()[:2]] |
| 53 l = [_('<h1>Templating Error</h1>\n' | 57 l = [_('<h1>Templating Error</h1>\n' |
| 54 '<p><b>%(exc_type)s</b>: %(exc_value)s</p>\n' | 58 '<p><b>%(exc_type)s</b>: %(exc_value)s</p>\n' |
| 55 '<p class="help">Debugging information follows</p>' | 59 '<p class="help">Debugging information follows</p>' |
| 56 ) % {'exc_type': exc_info[0], 'exc_value': exc_info[1]}, | 60 ) % {'exc_type': exc_info[0], 'exc_value': exc_info[1]}, |
| 57 '<ol>',] | 61 '<ol>', ] |
| 58 from roundup.cgi.PageTemplates.Expressions import TraversalError | 62 from roundup.cgi.PageTemplates.Expressions import TraversalError |
| 59 t = inspect.trace(context) | 63 t = inspect.trace(context) |
| 60 t.reverse() | 64 t.reverse() |
| 61 for frame, file, lnum, func, lines, index in t: | 65 for frame, file, lnum, func, lines, index in t: |
| 62 args, varargs, varkw, locals = inspect.getargvalues(frame) | 66 args, varargs, varkw, locals = inspect.getargvalues(frame) |
| 64 ti = locals['__traceback_info__'] | 68 ti = locals['__traceback_info__'] |
| 65 if isinstance(ti, TraversalError): | 69 if isinstance(ti, TraversalError): |
| 66 s = [] | 70 s = [] |
| 67 for name, info in ti.path: | 71 for name, info in ti.path: |
| 68 s.append(_('<li>"%(name)s" (%(info)s)</li>') | 72 s.append(_('<li>"%(name)s" (%(info)s)</li>') |
| 69 % {'name': name, 'info': esc(repr(info))}) | 73 % {'name': name, 'info': esc(repr(info))}) |
| 70 s = '\n'.join(s) | 74 s = '\n'.join(s) |
| 71 l.append(_('<li>Looking for "%(name)s", ' | 75 l.append(_('<li>Looking for "%(name)s", ' |
| 72 'current path:<ol>%(path)s</ol></li>' | 76 'current path:<ol>%(path)s</ol></li>' |
| 73 ) % {'name': ti.name, 'path': s}) | 77 ) % {'name': ti.name, 'path': s}) |
| 74 else: | 78 else: |
| 75 l.append(_('<li>In %s</li>') % esc(str(ti))) | 79 l.append(_('<li>In %s</li>') % esc(str(ti))) |
| 76 if '__traceback_supplement__' in locals: | 80 if '__traceback_supplement__' in locals: |
| 77 ts = locals['__traceback_supplement__'] | 81 ts = locals['__traceback_supplement__'] |
| 78 if len(ts) == 2: | 82 if len(ts) == 2: |
| 80 s = _('A problem occurred in your template "%s".') \ | 84 s = _('A problem occurred in your template "%s".') \ |
| 81 % str(context.id) | 85 % str(context.id) |
| 82 if context._v_errors: | 86 if context._v_errors: |
| 83 s = s + '<br>' + '<br>'.join( | 87 s = s + '<br>' + '<br>'.join( |
| 84 [esc(x) for x in context._v_errors]) | 88 [esc(x) for x in context._v_errors]) |
| 85 l.append('<li>%s</li>'%s) | 89 l.append('<li>%s</li>' % s) |
| 86 elif len(ts) == 3: | 90 elif len(ts) == 3: |
| 87 supp, context, info = ts | 91 supp, context, info = ts |
| 88 l.append(_(''' | 92 l.append(_(''' |
| 89 <li>While evaluating the %(info)r expression on line %(line)d | 93 <li>While evaluating the %(info)r expression on line %(line)d |
| 90 <table class="otherinfo" style="font-size: 90%%"> | 94 <table class="otherinfo" style="font-size: 90%%"> |
| 108 traceback.format_exception(*sys.exc_info()) | 112 traceback.format_exception(*sys.exc_info()) |
| 109 )))) | 113 )))) |
| 110 l.append('<p> </p>') | 114 l.append('<p> </p>') |
| 111 return '\n'.join(l) | 115 return '\n'.join(l) |
| 112 | 116 |
| 117 | |
| 113 def html(context=5, i18n=None): | 118 def html(context=5, i18n=None): |
| 114 _ = get_translator(i18n) | 119 _ = get_translator(i18n) |
| 115 etype, evalue = sys.exc_info()[0], sys.exc_info()[1] | 120 etype, evalue = sys.exc_info()[0], sys.exc_info()[1] |
| 116 if type(etype) is type: | 121 if type(etype) is type: |
| 117 etype = etype.__name__ | 122 etype = etype.__name__ |
| 120 _('<font size=+1><strong>%(exc_type)s</strong>: %(exc_value)s</font>') | 125 _('<font size=+1><strong>%(exc_type)s</strong>: %(exc_value)s</font>') |
| 121 % {'exc_type': etype, 'exc_value': evalue}, | 126 % {'exc_type': etype, 'exc_value': evalue}, |
| 122 '#ffffff', '#777777', pyver) | 127 '#ffffff', '#777777', pyver) |
| 123 | 128 |
| 124 head = head + (_('<p>A problem occurred while running a Python script. ' | 129 head = head + (_('<p>A problem occurred while running a Python script. ' |
| 125 'Here is the sequence of function calls leading up to ' | 130 'Here is the sequence of function calls leading up to ' |
| 126 'the error, with the most recent (innermost) call first. ' | 131 'the error, with the most recent (innermost) call first. ' |
| 127 'The exception attributes are:')) | 132 'The exception attributes are:')) |
| 128 | 133 |
| 129 indent = '<tt><small>%s</small> </tt>' % (' ' * 5) | 134 indent = '<tt><small>%s</small> </tt>' % (' ' * 5) |
| 130 traceback = [] | 135 traceback = [] |
| 131 for frame, file, lnum, func, lines, index in inspect.trace(context): | 136 for frame, file, lnum, func, lines, index in inspect.trace(context): |
| 132 if file is None: | 137 if file is None: |
| 133 link = _("<file is None - probably inside <tt>eval</tt> " | 138 link = _("<file is None - probably inside <tt>eval</tt> " |
| 134 "or <tt>exec</tt>>") | 139 "or <tt>exec</tt>>") |
| 135 else: | 140 else: |
| 136 file = os.path.abspath(file) | 141 file = os.path.abspath(file) |
| 137 link = '<a href="file:%s">%s</a>' % (file, pydoc.html.escape(file)) | 142 link = '<a href="file:%s">%s</a>' % (file, pydoc.html.escape(file)) |
| 138 args, varargs, varkw, locals = inspect.getargvalues(frame) | 143 args, varargs, varkw, locals = inspect.getargvalues(frame) |
| 139 if func == '?': | 144 if func == '?': |
| 140 call = '' | 145 call = '' |
| 141 else: | 146 else: |
| 142 call = _('in <strong>%s</strong>') % func + inspect.formatargvalues( | 147 call = _('in <strong>%s</strong>') % \ |
| 143 args, varargs, varkw, locals, | 148 func + inspect.formatargvalues( |
| 144 formatvalue=lambda value: '=' + pydoc.html.repr(value)) | 149 args, varargs, varkw, locals, |
| 150 formatvalue=lambda value: '=' + pydoc.html.repr(value)) | |
| 145 | 151 |
| 146 level = ''' | 152 level = ''' |
| 147 <table width="100%%" bgcolor="#dddddd" cellspacing=0 cellpadding=2 border=0> | 153 <table width="100%%" bgcolor="#dddddd" cellspacing=0 cellpadding=2 border=0> |
| 148 <tr><td>%s %s</td></tr></table>''' % (link, call) | 154 <tr><td>%s %s</td></tr></table>''' % (link, call) |
| 149 | 155 |
| 151 traceback.append('<p>' + level) | 157 traceback.append('<p>' + level) |
| 152 continue | 158 continue |
| 153 | 159 |
| 154 # do a file inspection | 160 # do a file inspection |
| 155 names = [] | 161 names = [] |
| 162 | |
| 156 def tokeneater(type, token, start, end, line, names=names): | 163 def tokeneater(type, token, start, end, line, names=names): |
| 157 if type == tokenize.NAME and token not in keyword.kwlist: | 164 if type == tokenize.NAME and token not in keyword.kwlist: |
| 158 if token not in names: | 165 if token not in names: |
| 159 names.append(token) | 166 names.append(token) |
| 160 if type == tokenize.NEWLINE: raise IndexError | 167 if type == tokenize.NEWLINE: raise IndexError |
| 168 | |
| 161 def linereader(file=file, lnum=[lnum]): | 169 def linereader(file=file, lnum=[lnum]): |
| 162 line = s2b(linecache.getline(file, lnum[0])) | 170 line = s2b(linecache.getline(file, lnum[0])) |
| 163 lnum[0] = lnum[0] + 1 | 171 lnum[0] = lnum[0] + 1 |
| 164 return line | 172 return line |
| 165 | 173 |
| 190 if name in frame.f_globals: | 198 if name in frame.f_globals: |
| 191 value = pydoc.html.repr(frame.f_globals[name]) | 199 value = pydoc.html.repr(frame.f_globals[name]) |
| 192 else: | 200 else: |
| 193 value = _('<em>undefined</em>') | 201 value = _('<em>undefined</em>') |
| 194 name = '<em>global</em> <strong>%s</strong>' % name | 202 name = '<em>global</em> <strong>%s</strong>' % name |
| 195 lvals.append('%s = %s'%(name, value)) | 203 lvals.append('%s = %s' % (name, value)) |
| 196 if lvals: | 204 if lvals: |
| 197 lvals = ', '.join(lvals) | 205 lvals = ', '.join(lvals) |
| 198 lvals = indent + '<small><font color="#909090">%s'\ | 206 lvals = indent + '<small><font color="#909090">%s'\ |
| 199 '</font></small><br>'%lvals | 207 '</font></small><br>' % lvals |
| 200 else: | 208 else: |
| 201 lvals = '' | 209 lvals = '' |
| 202 | 210 |
| 203 excerpt = [] | 211 excerpt = [] |
| 204 i = lnum - index | 212 i = lnum - index |
| 224 value = pydoc.html.repr(getattr(evalue, name)) | 232 value = pydoc.html.repr(getattr(evalue, name)) |
| 225 attribs.append('<br>%s%s = %s' % (indent, name, value)) | 233 attribs.append('<br>%s%s = %s' % (indent, name, value)) |
| 226 | 234 |
| 227 return head + ' '.join(attribs) + ' '.join(traceback) + '<p> </p>' | 235 return head + ' '.join(attribs) + ' '.join(traceback) + '<p> </p>' |
| 228 | 236 |
| 237 | |
| 229 def handler(): | 238 def handler(): |
| 230 print(breaker()) | 239 print(breaker()) |
| 231 print(html()) | 240 print(html()) |
| 232 | 241 |
| 233 # vim: set filetype=python ts=4 sw=4 et si : | 242 # vim: set filetype=python ts=4 sw=4 et si : |
