exceptions.py 12 KB

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