fork download
  1. # 標準ライブラリ cgitb.pyより抜粋
  2.  
  3. def html(einfo, context=5):
  4. """Return a nice HTML document describing a given traceback."""
  5. etype, evalue, etb = einfo
  6. if isinstance(etype, type):
  7. etype = etype.__name__
  8. pyver = 'Python ' + sys.version.split()[0] + ': ' + sys.executable
  9. date = time.ctime(time.time())
  10. head = '<body bgcolor="#f0f0f8">' + pydoc.html.heading(
  11. '<big><big>%s</big></big>' %
  12. strong(pydoc.html.escape(str(etype))),
  13. '#ffffff', '#6622aa', pyver + '<br>' + date) + '''
  14. <p>A problem occurred in a Python script. Here is the sequence of
  15. function calls leading up to the error, in the order they occurred.</p>'''
  16.  
  17. indent = '<tt>' + small('&nbsp;' * 5) + '&nbsp;</tt>'
  18. frames = []
  19. records = inspect.getinnerframes(etb, context)
  20. for frame, file, lnum, func, lines, index in records:
  21. if file:
  22. file = os.path.abspath(file)
  23. link = '<a href="file://%s">%s</a>' % (file, pydoc.html.escape(file))
  24. else:
  25. file = link = '?'
  26. args, varargs, varkw, locals = inspect.getargvalues(frame)
  27. call = ''
  28. if func != '?':
  29. call = 'in ' + strong(func) + \
  30. inspect.formatargvalues(args, varargs, varkw, locals,
  31. formatvalue=lambda value: '=' + pydoc.html.repr(value))
  32.  
  33. highlight = {}
  34. def reader(lnum=[lnum]):
  35. highlight[lnum[0]] = 1
  36. try: return linecache.getline(file, lnum[0])
  37. finally: lnum[0] += 1
  38. vars = scanvars(reader, frame, locals)
  39.  
  40. rows = ['<tr><td bgcolor="#d8bbff">%s%s %s</td></tr>' %
  41. ('<big>&nbsp;</big>', link, call)]
  42. if index is not None:
  43. i = lnum - index
  44. for line in lines:
  45. num = small('&nbsp;' * (5-len(str(i))) + str(i)) + '&nbsp;'
  46. if i in highlight:
  47. line = '<tt>=&gt;%s%s</tt>' % (num, pydoc.html.preformat(line))
  48. rows.append('<tr><td bgcolor="#ffccee">%s</td></tr>' % line)
  49. else:
  50. line = '<tt>&nbsp;&nbsp;%s%s</tt>' % (num, pydoc.html.preformat(line))
  51. rows.append('<tr><td>%s</td></tr>' % grey(line))
  52. i += 1
  53.  
  54. done, dump = {}, []
  55. for name, where, value in vars:
  56. if name in done: continue
  57. done[name] = 1
  58. if value is not __UNDEF__:
  59. if where in ('global', 'builtin'):
  60. name = ('<em>%s</em> ' % where) + strong(name)
  61. elif where == 'local':
  62. name = strong(name)
  63. else:
  64. name = where + strong(name.split('.')[-1])
  65. dump.append('%s&nbsp;= %s' % (name, pydoc.html.repr(value)))
  66. else:
  67. dump.append(name + ' <em>undefined</em>')
  68.  
  69. rows.append('<tr><td>%s</td></tr>' % small(grey(', '.join(dump))))
  70. frames.append('''
  71. <table width="100%%" cellspacing=0 cellpadding=0 border=0>
  72. %s</table>''' % '\n'.join(rows))
  73.  
  74. exception = ['<p>%s: %s' % (strong(pydoc.html.escape(str(etype))),
  75. pydoc.html.escape(str(evalue)))]
  76. for name in dir(evalue):
  77. if name[:1] == '_': continue
  78. value = pydoc.html.repr(getattr(evalue, name))
  79. exception.append('\n<br>%s%s&nbsp;=\n%s' % (indent, name, value))
  80.  
  81. return head + ''.join(frames) + ''.join(exception) + '''
  82.  
  83.  
  84. <!-- The above is a description of an error in a Python program, formatted
  85. for a Web browser because the 'cgitb' module was enabled. In case you
  86. are not reading this in a Web browser, here is the original traceback:
  87.  
  88. %s
  89. -->
  90. ''' % pydoc.html.escape(
  91. ''.join(traceback.format_exception(etype, evalue, etb)))
  92.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty