# 標準ライブラリ cgitb.pyより抜粋
def html(einfo, context=5):
"""Return a nice HTML document describing a given traceback."""
etype, evalue, etb = einfo
if isinstance(etype, type):
etype = etype.__name__
pyver = 'Python ' + sys.version.split()[0] + ': ' + sys.executable
date = time.ctime(time.time())
head = '<body bgcolor="#f0f0f8">' + pydoc.html.heading(
'<big><big>%s</big></big>' %
strong(pydoc.html.escape(str(etype))),
'#ffffff', '#6622aa', pyver + '<br>' + date) + '''
<p>A problem occurred in a Python script. Here is the sequence of
function calls leading up to the error, in the order they occurred.</p>'''
indent = '<tt>' + small(' ' * 5) + ' </tt>'
frames = []
records = inspect.getinnerframes(etb, context)
for frame, file, lnum, func, lines, index in records:
if file:
file = os.path.abspath(file)
link = '<a href="file://%s">%s</a>' % (file, pydoc.html.escape(file))
else:
file = link = '?'
args, varargs, varkw, locals = inspect.getargvalues(frame)
call = ''
if func != '?':
call = 'in ' + strong(func) + \
inspect.formatargvalues(args, varargs, varkw, locals,
formatvalue=lambda value: '=' + pydoc.html.repr(value))
highlight = {}
def reader(lnum=[lnum]):
highlight[lnum[0]] = 1
try: return linecache.getline(file, lnum[0])
finally: lnum[0] += 1
vars = scanvars(reader, frame, locals)
rows = ['<tr><td bgcolor="#d8bbff">%s%s %s</td></tr>' %
('<big> </big>', link, call)]
if index is not None:
i = lnum - index
for line in lines:
num = small(' ' * (5-len(str(i))) + str(i)) + ' '
if i in highlight:
line = '<tt>=>%s%s</tt>' % (num, pydoc.html.preformat(line))
rows.append('<tr><td bgcolor="#ffccee">%s</td></tr>' % line)
else:
line = '<tt> %s%s</tt>' % (num, pydoc.html.preformat(line))
rows.append('<tr><td>%s</td></tr>' % grey(line))
i += 1
done, dump = {}, []
for name, where, value in vars:
if name in done: continue
done[name] = 1
if value is not __UNDEF__:
if where in ('global', 'builtin'):
name = ('<em>%s</em> ' % where) + strong(name)
elif where == 'local':
name = strong(name)
else:
name = where + strong(name.split('.')[-1])
dump.append('%s = %s' % (name, pydoc.html.repr(value)))
else:
dump.append(name + ' <em>undefined</em>')
rows.append('<tr><td>%s</td></tr>' % small(grey(', '.join(dump))))
frames.append('''
<table width="100%%" cellspacing=0 cellpadding=0 border=0>
%s</table>''' % '\n'.join(rows))
exception = ['<p>%s: %s' % (strong(pydoc.html.escape(str(etype))),
pydoc.html.escape(str(evalue)))]
for name in dir(evalue):
if name[:1] == '_': continue
value = pydoc.html.repr(getattr(evalue, name))
exception.append('\n<br>%s%s =\n%s' % (indent, name, value))
return head + ''.join(frames) + ''.join(exception) + '''
<!-- The above is a description of an error in a Python program, formatted
for a Web browser because the 'cgitb' module was enabled. In case you
are not reading this in a Web browser, here is the original traceback:
%s
-->
''' % pydoc.html.escape(
''.join(traceback.format_exception(etype, evalue, etb)))
IyDmqJnmupbjg6njgqTjg5bjg6njg6ogY2dpdGIucHnjgojjgormipznsosKCmRlZiBodG1sKGVpbmZvLCBjb250ZXh0PTUpOgogICAgIiIiUmV0dXJuIGEgbmljZSBIVE1MIGRvY3VtZW50IGRlc2NyaWJpbmcgYSBnaXZlbiB0cmFjZWJhY2suIiIiCiAgICBldHlwZSwgZXZhbHVlLCBldGIgPSBlaW5mbwogICAgaWYgaXNpbnN0YW5jZShldHlwZSwgdHlwZSk6CiAgICAgICAgZXR5cGUgPSBldHlwZS5fX25hbWVfXwogICAgcHl2ZXIgPSAnUHl0aG9uICcgKyBzeXMudmVyc2lvbi5zcGxpdCgpWzBdICsgJzogJyArIHN5cy5leGVjdXRhYmxlCiAgICBkYXRlID0gdGltZS5jdGltZSh0aW1lLnRpbWUoKSkKICAgIGhlYWQgPSAnPGJvZHkgYmdjb2xvcj0iI2YwZjBmOCI+JyArIHB5ZG9jLmh0bWwuaGVhZGluZygKICAgICAgICAnPGJpZz48YmlnPiVzPC9iaWc+PC9iaWc+JyAlCiAgICAgICAgc3Ryb25nKHB5ZG9jLmh0bWwuZXNjYXBlKHN0cihldHlwZSkpKSwKICAgICAgICAnI2ZmZmZmZicsICcjNjYyMmFhJywgcHl2ZXIgK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hpZ2hsaWdodCA9IHt9CiAgICAgICAgZGVmIHJlYWRlcihsbnVtPVtsbnVtXSk6CiAgICAgICAgICAgIGhpZ2hsaWdodFtsbnVtWzBdXSA9IDEKICAgICAgICAgICAgdHJ5OiByZXR1cm4gbGluZWNhY2hlLmdldGxpbmUoZmlsZSwgbG51bVswXSkKICAgICAgICAgICAgZmluYWxseTogbG51bVswXSArPSAxCiAgICAgICAgdmFycyA9IHNjYW52YXJzKHJlYWRlciwgZnJhbWUsIGxvY2FscykKCiAgICAgICAgcm93cyA9IFsnPHRyPjx0ZCBiZ2NvbG9yPSIjZDhiYmZmIj4lcyVzICVzPC90ZD48L3RyPicgJQogICAgICAgICAgICAgICAgKCc8YmlnPiZuYnNwOzwvYmlnPicsIGxpbmssIGNhbGwpXQogICAgICAgIGlmIGluZGV4IGlzIG5vdCBOb25lOgogICAgICAgICAgICBpID0gbG51bSAtIGluZGV4CiAgICAgICAgICAgIGZvciBsaW5lIGluIGxpbmVzOgogICAgICAgICAgICAgICAgbnVtID0gc21hbGwoJyZuYnNwOycgKiAoNS1sZW4oc3RyKGkpKSkgKyBzdHIoaSkpICsgJyZuYnNwOycKICAgICAgICAgICAgICAgIGlmIGkgaW4gaGlnaGxpZ2h0OgogICAgICAgICAgICAgICAgICAgIGxpbmUgPSAnPHR0Pj0mZ3Q7JXMlczwvdHQ+JyAlIChudW0sIHB5ZG9jLmh0bWwucHJlZm9ybWF0KGxpbmUpKQogICAgICAgICAgICAgICAgICAgIHJvd3MuYXBwZW5kKCc8dHI+PHRkIGJnY29sb3I9IiNmZmNjZWUiPiVzPC90ZD48L3RyPicgJSBsaW5lKQogICAgICAgICAgICAgICAgZWxzZToKICAgICAgICAgICAgICAgICAgICBsaW5lID0gJzx0dD4mbmJzcDsmbmJzcDslcyVzPC90dD4nICUgKG51bSwgcHlkb2MuaHRtbC5wcmVmb3JtYXQobGluZSkpCiAgICAgICAgICAgICAgICAgICAgcm93cy5hcHBlbmQoJzx0cj48dGQ+JXM8L3RkPjwvdHI+JyAlIGdyZXkobGluZSkpCiAgICAgICAgICAgICAgICBpICs9IDEKCiAgICAgICAgZG9uZSwgZHVtcCA9IHt9LCBbXQogICAgICAgIGZvciBuYW1lLCB3aGVyZSwgdmFsdWUgaW4gdmFyczoKICAgICAgICAgICAgaWYgbmFtZSBpbiBkb25lOiBjb250aW51ZQogICAgICAgICAgICBkb25lW25hbWVdID0gMQogICAgICAgICAgICBpZiB2YWx1ZSBpcyBub3QgX19VTkRFRl9fOgogICAgICAgICAgICAgICAgaWYgd2hlcmUgaW4gKCdnbG9iYWwnLCAnYnVpbHRpbicpOgogICAgICAgICAgICAgICAgICAgIG5hbWUgPSAoJzxlbT4lczwvZW0+ICcgJSB3aGVyZSkgKyBzdHJvbmcobmFtZSkKICAgICAgICAgICAgICAgIGVsaWYgd2hlcmUgPT0gJ2xvY2FsJzoKICAgICAgICAgICAgICAgICAgICBuYW1lID0gc3Ryb25nKG5hbWUpCiAgICAgICAgICAgICAgICBlbHNlOgogICAgICAgICAgICAgICAgICAgIG5hbWUgPSB3aGVyZSArIHN0cm9uZyhuYW1lLnNwbGl0KCcuJylbLTFdKQogICAgICAgICAgICAgICAgZHVtcC5hcHBlbmQoJyVzJm5ic3A7PSAlcycgJSAobmFtZSwgcHlkb2MuaHRtbC5yZXByKHZhbHVlKSkpCiAgICAgICAgICAgIGVsc2U6CiAgICAgICAgICAgICAgICBkdW1wLmFwcGVuZChuYW1lICsgJyA8ZW0+dW5kZWZpbmVkPC9lbT4nKQoKICAgICAgICByb3dzLmFwcGVuZCgnPHRyPjx0ZD4lczwvdGQ+PC90cj4nICUgc21hbGwoZ3JleSgnLCAnLmpvaW4oZHVtcCkpKSkKICAgICAgICBmcmFtZXMuYXBwZW5kKCcnJwo8dGFibGUgd2lkdGg9IjEwMCUlIiBjZWxsc3BhY2luZz0wIGNlbGxwYWRkaW5nPTAgYm9yZGVyPTA+CiVzPC90YWJsZT4nJycgJSAnXG4nLmpvaW4ocm93cykpC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