cgitb.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. """More comprehensive traceback formatting for Python scripts.
  2. To enable this module, do:
  3. import cgitb; cgitb.enable()
  4. at the top of your script. The optional arguments to enable() are:
  5. display - if true, tracebacks are displayed in the web browser
  6. logdir - if set, tracebacks are written to files in this directory
  7. context - number of lines of source code to show for each stack frame
  8. format - 'text' or 'html' controls the output format
  9. By default, tracebacks are displayed but not saved, the context is 5 lines
  10. and the output format is 'html' (for backwards compatibility with the
  11. original use of this module)
  12. Alternatively, if you have caught an exception and want cgitb to display it
  13. for you, call cgitb.handler(). The optional argument to handler() is a
  14. 3-item tuple (etype, evalue, etb) just like the value of sys.exc_info().
  15. The default handler displays output as HTML.
  16. """
  17. import inspect
  18. import keyword
  19. import linecache
  20. import os
  21. import pydoc
  22. import sys
  23. import tempfile
  24. import time
  25. import tokenize
  26. import traceback
  27. import warnings
  28. from html import escape as html_escape
  29. warnings._deprecated(__name__, remove=(3, 13))
  30. def reset():
  31. """Return a string that resets the CGI and browser to a known state."""
  32. return '''<!--: spam
  33. Content-Type: text/html
  34. <body bgcolor="#f0f0f8"><font color="#f0f0f8" size="-5"> -->
  35. <body bgcolor="#f0f0f8"><font color="#f0f0f8" size="-5"> --> -->
  36. </font> </font> </font> </script> </object> </blockquote> </pre>
  37. </table> </table> </table> </table> </table> </font> </font> </font>'''
  38. __UNDEF__ = [] # a special sentinel object
  39. def small(text):
  40. if text:
  41. return '<small>' + text + '</small>'
  42. else:
  43. return ''
  44. def strong(text):
  45. if text:
  46. return '<strong>' + text + '</strong>'
  47. else:
  48. return ''
  49. def grey(text):
  50. if text:
  51. return '<font color="#909090">' + text + '</font>'
  52. else:
  53. return ''
  54. def lookup(name, frame, locals):
  55. """Find the value for a given name in the given environment."""
  56. if name in locals:
  57. return 'local', locals[name]
  58. if name in frame.f_globals:
  59. return 'global', frame.f_globals[name]
  60. if '__builtins__' in frame.f_globals:
  61. builtins = frame.f_globals['__builtins__']
  62. if isinstance(builtins, dict):
  63. if name in builtins:
  64. return 'builtin', builtins[name]
  65. else:
  66. if hasattr(builtins, name):
  67. return 'builtin', getattr(builtins, name)
  68. return None, __UNDEF__
  69. def scanvars(reader, frame, locals):
  70. """Scan one logical line of Python and look up values of variables used."""
  71. vars, lasttoken, parent, prefix, value = [], None, None, '', __UNDEF__
  72. for ttype, token, start, end, line in tokenize.generate_tokens(reader):
  73. if ttype == tokenize.NEWLINE: break
  74. if ttype == tokenize.NAME and token not in keyword.kwlist:
  75. if lasttoken == '.':
  76. if parent is not __UNDEF__:
  77. value = getattr(parent, token, __UNDEF__)
  78. vars.append((prefix + token, prefix, value))
  79. else:
  80. where, value = lookup(token, frame, locals)
  81. vars.append((token, where, value))
  82. elif token == '.':
  83. prefix += lasttoken + '.'
  84. parent = value
  85. else:
  86. parent, prefix = None, ''
  87. lasttoken = token
  88. return vars
  89. def html(einfo, context=5):
  90. """Return a nice HTML document describing a given traceback."""
  91. etype, evalue, etb = einfo
  92. if isinstance(etype, type):
  93. etype = etype.__name__
  94. pyver = 'Python ' + sys.version.split()[0] + ': ' + sys.executable
  95. date = time.ctime(time.time())
  96. head = f'''
  97. <body bgcolor="#f0f0f8">
  98. <table width="100%" cellspacing=0 cellpadding=2 border=0 summary="heading">
  99. <tr bgcolor="#6622aa">
  100. <td valign=bottom>&nbsp;<br>
  101. <font color="#ffffff" face="helvetica, arial">&nbsp;<br>
  102. <big><big><strong>{html_escape(str(etype))}</strong></big></big></font></td>
  103. <td align=right valign=bottom>
  104. <font color="#ffffff" face="helvetica, arial">{pyver}<br>{date}</font></td>
  105. </tr></table>
  106. <p>A problem occurred in a Python script. Here is the sequence of
  107. function calls leading up to the error, in the order they occurred.</p>'''
  108. indent = '<tt>' + small('&nbsp;' * 5) + '&nbsp;</tt>'
  109. frames = []
  110. records = inspect.getinnerframes(etb, context)
  111. for frame, file, lnum, func, lines, index in records:
  112. if file:
  113. file = os.path.abspath(file)
  114. link = '<a href="file://%s">%s</a>' % (file, pydoc.html.escape(file))
  115. else:
  116. file = link = '?'
  117. args, varargs, varkw, locals = inspect.getargvalues(frame)
  118. call = ''
  119. if func != '?':
  120. call = 'in ' + strong(pydoc.html.escape(func))
  121. if func != "<module>":
  122. call += inspect.formatargvalues(args, varargs, varkw, locals,
  123. formatvalue=lambda value: '=' + pydoc.html.repr(value))
  124. highlight = {}
  125. def reader(lnum=[lnum]):
  126. highlight[lnum[0]] = 1
  127. try: return linecache.getline(file, lnum[0])
  128. finally: lnum[0] += 1
  129. vars = scanvars(reader, frame, locals)
  130. rows = ['<tr><td bgcolor="#d8bbff">%s%s %s</td></tr>' %
  131. ('<big>&nbsp;</big>', link, call)]
  132. if index is not None:
  133. i = lnum - index
  134. for line in lines:
  135. num = small('&nbsp;' * (5-len(str(i))) + str(i)) + '&nbsp;'
  136. if i in highlight:
  137. line = '<tt>=&gt;%s%s</tt>' % (num, pydoc.html.preformat(line))
  138. rows.append('<tr><td bgcolor="#ffccee">%s</td></tr>' % line)
  139. else:
  140. line = '<tt>&nbsp;&nbsp;%s%s</tt>' % (num, pydoc.html.preformat(line))
  141. rows.append('<tr><td>%s</td></tr>' % grey(line))
  142. i += 1
  143. done, dump = {}, []
  144. for name, where, value in vars:
  145. if name in done: continue
  146. done[name] = 1
  147. if value is not __UNDEF__:
  148. if where in ('global', 'builtin'):
  149. name = ('<em>%s</em> ' % where) + strong(name)
  150. elif where == 'local':
  151. name = strong(name)
  152. else:
  153. name = where + strong(name.split('.')[-1])
  154. dump.append('%s&nbsp;= %s' % (name, pydoc.html.repr(value)))
  155. else:
  156. dump.append(name + ' <em>undefined</em>')
  157. rows.append('<tr><td>%s</td></tr>' % small(grey(', '.join(dump))))
  158. frames.append('''
  159. <table width="100%%" cellspacing=0 cellpadding=0 border=0>
  160. %s</table>''' % '\n'.join(rows))
  161. exception = ['<p>%s: %s' % (strong(pydoc.html.escape(str(etype))),
  162. pydoc.html.escape(str(evalue)))]
  163. for name in dir(evalue):
  164. if name[:1] == '_': continue
  165. value = pydoc.html.repr(getattr(evalue, name))
  166. exception.append('\n<br>%s%s&nbsp;=\n%s' % (indent, name, value))
  167. return head + ''.join(frames) + ''.join(exception) + '''
  168. <!-- The above is a description of an error in a Python program, formatted
  169. for a web browser because the 'cgitb' module was enabled. In case you
  170. are not reading this in a web browser, here is the original traceback:
  171. %s
  172. -->
  173. ''' % pydoc.html.escape(
  174. ''.join(traceback.format_exception(etype, evalue, etb)))
  175. def text(einfo, context=5):
  176. """Return a plain text document describing a given traceback."""
  177. etype, evalue, etb = einfo
  178. if isinstance(etype, type):
  179. etype = etype.__name__
  180. pyver = 'Python ' + sys.version.split()[0] + ': ' + sys.executable
  181. date = time.ctime(time.time())
  182. head = "%s\n%s\n%s\n" % (str(etype), pyver, date) + '''
  183. A problem occurred in a Python script. Here is the sequence of
  184. function calls leading up to the error, in the order they occurred.
  185. '''
  186. frames = []
  187. records = inspect.getinnerframes(etb, context)
  188. for frame, file, lnum, func, lines, index in records:
  189. file = file and os.path.abspath(file) or '?'
  190. args, varargs, varkw, locals = inspect.getargvalues(frame)
  191. call = ''
  192. if func != '?':
  193. call = 'in ' + func
  194. if func != "<module>":
  195. call += inspect.formatargvalues(args, varargs, varkw, locals,
  196. formatvalue=lambda value: '=' + pydoc.text.repr(value))
  197. highlight = {}
  198. def reader(lnum=[lnum]):
  199. highlight[lnum[0]] = 1
  200. try: return linecache.getline(file, lnum[0])
  201. finally: lnum[0] += 1
  202. vars = scanvars(reader, frame, locals)
  203. rows = [' %s %s' % (file, call)]
  204. if index is not None:
  205. i = lnum - index
  206. for line in lines:
  207. num = '%5d ' % i
  208. rows.append(num+line.rstrip())
  209. i += 1
  210. done, dump = {}, []
  211. for name, where, value in vars:
  212. if name in done: continue
  213. done[name] = 1
  214. if value is not __UNDEF__:
  215. if where == 'global': name = 'global ' + name
  216. elif where != 'local': name = where + name.split('.')[-1]
  217. dump.append('%s = %s' % (name, pydoc.text.repr(value)))
  218. else:
  219. dump.append(name + ' undefined')
  220. rows.append('\n'.join(dump))
  221. frames.append('\n%s\n' % '\n'.join(rows))
  222. exception = ['%s: %s' % (str(etype), str(evalue))]
  223. for name in dir(evalue):
  224. value = pydoc.text.repr(getattr(evalue, name))
  225. exception.append('\n%s%s = %s' % (" "*4, name, value))
  226. return head + ''.join(frames) + ''.join(exception) + '''
  227. The above is a description of an error in a Python program. Here is
  228. the original traceback:
  229. %s
  230. ''' % ''.join(traceback.format_exception(etype, evalue, etb))
  231. class Hook:
  232. """A hook to replace sys.excepthook that shows tracebacks in HTML."""
  233. def __init__(self, display=1, logdir=None, context=5, file=None,
  234. format="html"):
  235. self.display = display # send tracebacks to browser if true
  236. self.logdir = logdir # log tracebacks to files if not None
  237. self.context = context # number of source code lines per frame
  238. self.file = file or sys.stdout # place to send the output
  239. self.format = format
  240. def __call__(self, etype, evalue, etb):
  241. self.handle((etype, evalue, etb))
  242. def handle(self, info=None):
  243. info = info or sys.exc_info()
  244. if self.format == "html":
  245. self.file.write(reset())
  246. formatter = (self.format=="html") and html or text
  247. plain = False
  248. try:
  249. doc = formatter(info, self.context)
  250. except: # just in case something goes wrong
  251. doc = ''.join(traceback.format_exception(*info))
  252. plain = True
  253. if self.display:
  254. if plain:
  255. doc = pydoc.html.escape(doc)
  256. self.file.write('<pre>' + doc + '</pre>\n')
  257. else:
  258. self.file.write(doc + '\n')
  259. else:
  260. self.file.write('<p>A problem occurred in a Python script.\n')
  261. if self.logdir is not None:
  262. suffix = ['.txt', '.html'][self.format=="html"]
  263. (fd, path) = tempfile.mkstemp(suffix=suffix, dir=self.logdir)
  264. try:
  265. with os.fdopen(fd, 'w') as file:
  266. file.write(doc)
  267. msg = '%s contains the description of this error.' % path
  268. except:
  269. msg = 'Tried to save traceback to %s, but failed.' % path
  270. if self.format == 'html':
  271. self.file.write('<p>%s</p>\n' % msg)
  272. else:
  273. self.file.write(msg + '\n')
  274. try:
  275. self.file.flush()
  276. except: pass
  277. handler = Hook().handle
  278. def enable(display=1, logdir=None, context=5, format="html"):
  279. """Install an exception handler that formats tracebacks as HTML.
  280. The optional argument 'display' can be set to 0 to suppress sending the
  281. traceback to the browser, and 'logdir' can be set to a directory to cause
  282. tracebacks to be written to files there."""
  283. sys.excepthook = Hook(display=display, logdir=logdir,
  284. context=context, format=format)