exceptions.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. # mako/exceptions.py
  2. # Copyright (C) 2006-2016 the Mako authors and contributors <see AUTHORS file>
  3. #
  4. # This module is part of Mako and is released under
  5. # the MIT License: http://www.opensource.org/licenses/mit-license.php
  6. """exception classes"""
  7. import traceback
  8. import sys
  9. from mako import util, compat
  10. class MakoException(Exception):
  11. pass
  12. class RuntimeException(MakoException):
  13. pass
  14. def _format_filepos(lineno, pos, filename):
  15. if filename is None:
  16. return " at line: %d char: %d" % (lineno, pos)
  17. else:
  18. return " in file '%s' at line: %d char: %d" % (filename, lineno, pos)
  19. class CompileException(MakoException):
  20. def __init__(self, message, source, lineno, pos, filename):
  21. MakoException.__init__(
  22. self,
  23. message + _format_filepos(lineno, pos, filename))
  24. self.lineno = lineno
  25. self.pos = pos
  26. self.filename = filename
  27. self.source = source
  28. class SyntaxException(MakoException):
  29. def __init__(self, message, source, lineno, pos, filename):
  30. MakoException.__init__(
  31. self,
  32. message + _format_filepos(lineno, pos, filename))
  33. self.lineno = lineno
  34. self.pos = pos
  35. self.filename = filename
  36. self.source = source
  37. class UnsupportedError(MakoException):
  38. """raised when a retired feature is used."""
  39. class NameConflictError(MakoException):
  40. """raised when a reserved word is used inappropriately"""
  41. class TemplateLookupException(MakoException):
  42. pass
  43. class TopLevelLookupException(TemplateLookupException):
  44. pass
  45. class RichTraceback(object):
  46. """Pull the current exception from the ``sys`` traceback and extracts
  47. Mako-specific template information.
  48. See the usage examples in :ref:`handling_exceptions`.
  49. """
  50. def __init__(self, error=None, traceback=None):
  51. self.source, self.lineno = "", 0
  52. if error is None or traceback is None:
  53. t, value, tback = sys.exc_info()
  54. if error is None:
  55. error = value or t
  56. if traceback is None:
  57. traceback = tback
  58. self.error = error
  59. self.records = self._init(traceback)
  60. if isinstance(self.error, (CompileException, SyntaxException)):
  61. self.source = self.error.source
  62. self.lineno = self.error.lineno
  63. self._has_source = True
  64. self._init_message()
  65. @property
  66. def errorname(self):
  67. return compat.exception_name(self.error)
  68. def _init_message(self):
  69. """Find a unicode representation of self.error"""
  70. try:
  71. self.message = compat.text_type(self.error)
  72. except UnicodeError:
  73. try:
  74. self.message = str(self.error)
  75. except UnicodeEncodeError:
  76. # Fallback to args as neither unicode nor
  77. # str(Exception(u'\xe6')) work in Python < 2.6
  78. self.message = self.error.args[0]
  79. if not isinstance(self.message, compat.text_type):
  80. self.message = compat.text_type(self.message, 'ascii', 'replace')
  81. def _get_reformatted_records(self, records):
  82. for rec in records:
  83. if rec[6] is not None:
  84. yield (rec[4], rec[5], rec[2], rec[6])
  85. else:
  86. yield tuple(rec[0:4])
  87. @property
  88. def traceback(self):
  89. """Return a list of 4-tuple traceback records (i.e. normal python
  90. format) with template-corresponding lines remapped to the originating
  91. template.
  92. """
  93. return list(self._get_reformatted_records(self.records))
  94. @property
  95. def reverse_records(self):
  96. return reversed(self.records)
  97. @property
  98. def reverse_traceback(self):
  99. """Return the same data as traceback, except in reverse order.
  100. """
  101. return list(self._get_reformatted_records(self.reverse_records))
  102. def _init(self, trcback):
  103. """format a traceback from sys.exc_info() into 7-item tuples,
  104. containing the regular four traceback tuple items, plus the original
  105. template filename, the line number adjusted relative to the template
  106. source, and code line from that line number of the template."""
  107. import mako.template
  108. mods = {}
  109. rawrecords = traceback.extract_tb(trcback)
  110. new_trcback = []
  111. for filename, lineno, function, line in rawrecords:
  112. if not line:
  113. line = ''
  114. try:
  115. (line_map, template_lines) = mods[filename]
  116. except KeyError:
  117. try:
  118. info = mako.template._get_module_info(filename)
  119. module_source = info.code
  120. template_source = info.source
  121. template_filename = info.template_filename or filename
  122. except KeyError:
  123. # A normal .py file (not a Template)
  124. if not compat.py3k:
  125. try:
  126. fp = open(filename, 'rb')
  127. encoding = util.parse_encoding(fp)
  128. fp.close()
  129. except IOError:
  130. encoding = None
  131. if encoding:
  132. line = line.decode(encoding)
  133. else:
  134. line = line.decode('ascii', 'replace')
  135. new_trcback.append((filename, lineno, function, line,
  136. None, None, None, None))
  137. continue
  138. template_ln = 1
  139. source_map = mako.template.ModuleInfo.\
  140. get_module_source_metadata(
  141. module_source, full_line_map=True)
  142. line_map = source_map['full_line_map']
  143. template_lines = [line_ for line_ in
  144. template_source.split("\n")]
  145. mods[filename] = (line_map, template_lines)
  146. template_ln = line_map[lineno - 1]
  147. if template_ln <= len(template_lines):
  148. template_line = template_lines[template_ln - 1]
  149. else:
  150. template_line = None
  151. new_trcback.append((filename, lineno, function,
  152. line, template_filename, template_ln,
  153. template_line, template_source))
  154. if not self.source:
  155. for l in range(len(new_trcback) - 1, 0, -1):
  156. if new_trcback[l][5]:
  157. self.source = new_trcback[l][7]
  158. self.lineno = new_trcback[l][5]
  159. break
  160. else:
  161. if new_trcback:
  162. try:
  163. # A normal .py file (not a Template)
  164. fp = open(new_trcback[-1][0], 'rb')
  165. encoding = util.parse_encoding(fp)
  166. fp.seek(0)
  167. self.source = fp.read()
  168. fp.close()
  169. if encoding:
  170. self.source = self.source.decode(encoding)
  171. except IOError:
  172. self.source = ''
  173. self.lineno = new_trcback[-1][1]
  174. return new_trcback
  175. def text_error_template(lookup=None):
  176. """Provides a template that renders a stack trace in a similar format to
  177. the Python interpreter, substituting source template filenames, line
  178. numbers and code for that of the originating source template, as
  179. applicable.
  180. """
  181. import mako.template
  182. return mako.template.Template(r"""
  183. <%page args="error=None, traceback=None"/>
  184. <%!
  185. from mako.exceptions import RichTraceback
  186. %>\
  187. <%
  188. tback = RichTraceback(error=error, traceback=traceback)
  189. %>\
  190. Traceback (most recent call last):
  191. % for (filename, lineno, function, line) in tback.traceback:
  192. File "${filename}", line ${lineno}, in ${function or '?'}
  193. ${line | trim}
  194. % endfor
  195. ${tback.errorname}: ${tback.message}
  196. """)
  197. def _install_pygments():
  198. global syntax_highlight, pygments_html_formatter
  199. from mako.ext.pygmentplugin import syntax_highlight # noqa
  200. from mako.ext.pygmentplugin import pygments_html_formatter # noqa
  201. def _install_fallback():
  202. global syntax_highlight, pygments_html_formatter
  203. from mako.filters import html_escape
  204. pygments_html_formatter = None
  205. def syntax_highlight(filename='', language=None):
  206. return html_escape
  207. def _install_highlighting():
  208. try:
  209. _install_pygments()
  210. except ImportError:
  211. _install_fallback()
  212. _install_highlighting()
  213. def html_error_template():
  214. """Provides a template that renders a stack trace in an HTML format,
  215. providing an excerpt of code as well as substituting source template
  216. filenames, line numbers and code for that of the originating source
  217. template, as applicable.
  218. The template's default ``encoding_errors`` value is
  219. ``'htmlentityreplace'``. The template has two options. With the
  220. ``full`` option disabled, only a section of an HTML document is
  221. returned. With the ``css`` option disabled, the default stylesheet
  222. won't be included.
  223. """
  224. import mako.template
  225. return mako.template.Template(r"""
  226. <%!
  227. from mako.exceptions import RichTraceback, syntax_highlight,\
  228. pygments_html_formatter
  229. %>
  230. <%page args="full=True, css=True, error=None, traceback=None"/>
  231. % if full:
  232. <html>
  233. <head>
  234. <title>Mako Runtime Error</title>
  235. % endif
  236. % if css:
  237. <style>
  238. body { font-family:verdana; margin:10px 30px 10px 30px;}
  239. .stacktrace { margin:5px 5px 5px 5px; }
  240. .highlight { padding:0px 10px 0px 10px; background-color:#9F9FDF; }
  241. .nonhighlight { padding:0px; background-color:#DFDFDF; }
  242. .sample { padding:10px; margin:10px 10px 10px 10px;
  243. font-family:monospace; }
  244. .sampleline { padding:0px 10px 0px 10px; }
  245. .sourceline { margin:5px 5px 10px 5px; font-family:monospace;}
  246. .location { font-size:80%; }
  247. .highlight { white-space:pre; }
  248. .sampleline { white-space:pre; }
  249. % if pygments_html_formatter:
  250. ${pygments_html_formatter.get_style_defs()}
  251. .linenos { min-width: 2.5em; text-align: right; }
  252. pre { margin: 0; }
  253. .syntax-highlighted { padding: 0 10px; }
  254. .syntax-highlightedtable { border-spacing: 1px; }
  255. .nonhighlight { border-top: 1px solid #DFDFDF;
  256. border-bottom: 1px solid #DFDFDF; }
  257. .stacktrace .nonhighlight { margin: 5px 15px 10px; }
  258. .sourceline { margin: 0 0; font-family:monospace; }
  259. .code { background-color: #F8F8F8; width: 100%; }
  260. .error .code { background-color: #FFBDBD; }
  261. .error .syntax-highlighted { background-color: #FFBDBD; }
  262. % endif
  263. </style>
  264. % endif
  265. % if full:
  266. </head>
  267. <body>
  268. % endif
  269. <h2>Error !</h2>
  270. <%
  271. tback = RichTraceback(error=error, traceback=traceback)
  272. src = tback.source
  273. line = tback.lineno
  274. if src:
  275. lines = src.split('\n')
  276. else:
  277. lines = None
  278. %>
  279. <h3>${tback.errorname}: ${tback.message|h}</h3>
  280. % if lines:
  281. <div class="sample">
  282. <div class="nonhighlight">
  283. % for index in range(max(0, line-4),min(len(lines), line+5)):
  284. <%
  285. if pygments_html_formatter:
  286. pygments_html_formatter.linenostart = index + 1
  287. %>
  288. % if index + 1 == line:
  289. <%
  290. if pygments_html_formatter:
  291. old_cssclass = pygments_html_formatter.cssclass
  292. pygments_html_formatter.cssclass = 'error ' + old_cssclass
  293. %>
  294. ${lines[index] | syntax_highlight(language='mako')}
  295. <%
  296. if pygments_html_formatter:
  297. pygments_html_formatter.cssclass = old_cssclass
  298. %>
  299. % else:
  300. ${lines[index] | syntax_highlight(language='mako')}
  301. % endif
  302. % endfor
  303. </div>
  304. </div>
  305. % endif
  306. <div class="stacktrace">
  307. % for (filename, lineno, function, line) in tback.reverse_traceback:
  308. <div class="location">${filename}, line ${lineno}:</div>
  309. <div class="nonhighlight">
  310. <%
  311. if pygments_html_formatter:
  312. pygments_html_formatter.linenostart = lineno
  313. %>
  314. <div class="sourceline">${line | syntax_highlight(filename)}</div>
  315. </div>
  316. % endfor
  317. </div>
  318. % if full:
  319. </body>
  320. </html>
  321. % endif
  322. """, output_encoding=sys.getdefaultencoding(),
  323. encoding_errors='htmlentityreplace')