debug.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. # -*- coding: utf-8 -*-
  2. """
  3. jinja2.debug
  4. ~~~~~~~~~~~~
  5. Implements the debug interface for Jinja. This module does some pretty
  6. ugly stuff with the Python traceback system in order to achieve tracebacks
  7. with correct line numbers, locals and contents.
  8. :copyright: (c) 2010 by the Jinja Team.
  9. :license: BSD, see LICENSE for more details.
  10. """
  11. import sys
  12. import traceback
  13. from types import TracebackType
  14. from jinja2.utils import missing, internal_code
  15. from jinja2.exceptions import TemplateSyntaxError
  16. from jinja2._compat import iteritems, reraise, code_type
  17. # on pypy we can take advantage of transparent proxies
  18. try:
  19. from __pypy__ import tproxy
  20. except ImportError:
  21. tproxy = None
  22. # how does the raise helper look like?
  23. try:
  24. exec("raise TypeError, 'foo'")
  25. except SyntaxError:
  26. raise_helper = 'raise __jinja_exception__[1]'
  27. except TypeError:
  28. raise_helper = 'raise __jinja_exception__[0], __jinja_exception__[1]'
  29. class TracebackFrameProxy(object):
  30. """Proxies a traceback frame."""
  31. def __init__(self, tb):
  32. self.tb = tb
  33. self._tb_next = None
  34. @property
  35. def tb_next(self):
  36. return self._tb_next
  37. def set_next(self, next):
  38. if tb_set_next is not None:
  39. try:
  40. tb_set_next(self.tb, next and next.tb or None)
  41. except Exception:
  42. # this function can fail due to all the hackery it does
  43. # on various python implementations. We just catch errors
  44. # down and ignore them if necessary.
  45. pass
  46. self._tb_next = next
  47. @property
  48. def is_jinja_frame(self):
  49. return '__jinja_template__' in self.tb.tb_frame.f_globals
  50. def __getattr__(self, name):
  51. return getattr(self.tb, name)
  52. def make_frame_proxy(frame):
  53. proxy = TracebackFrameProxy(frame)
  54. if tproxy is None:
  55. return proxy
  56. def operation_handler(operation, *args, **kwargs):
  57. if operation in ('__getattribute__', '__getattr__'):
  58. return getattr(proxy, args[0])
  59. elif operation == '__setattr__':
  60. proxy.__setattr__(*args, **kwargs)
  61. else:
  62. return getattr(proxy, operation)(*args, **kwargs)
  63. return tproxy(TracebackType, operation_handler)
  64. class ProcessedTraceback(object):
  65. """Holds a Jinja preprocessed traceback for printing or reraising."""
  66. def __init__(self, exc_type, exc_value, frames):
  67. assert frames, 'no frames for this traceback?'
  68. self.exc_type = exc_type
  69. self.exc_value = exc_value
  70. self.frames = frames
  71. # newly concatenate the frames (which are proxies)
  72. prev_tb = None
  73. for tb in self.frames:
  74. if prev_tb is not None:
  75. prev_tb.set_next(tb)
  76. prev_tb = tb
  77. prev_tb.set_next(None)
  78. def render_as_text(self, limit=None):
  79. """Return a string with the traceback."""
  80. lines = traceback.format_exception(self.exc_type, self.exc_value,
  81. self.frames[0], limit=limit)
  82. return ''.join(lines).rstrip()
  83. def render_as_html(self, full=False):
  84. """Return a unicode string with the traceback as rendered HTML."""
  85. from jinja2.debugrenderer import render_traceback
  86. return u'%s\n\n<!--\n%s\n-->' % (
  87. render_traceback(self, full=full),
  88. self.render_as_text().decode('utf-8', 'replace')
  89. )
  90. @property
  91. def is_template_syntax_error(self):
  92. """`True` if this is a template syntax error."""
  93. return isinstance(self.exc_value, TemplateSyntaxError)
  94. @property
  95. def exc_info(self):
  96. """Exception info tuple with a proxy around the frame objects."""
  97. return self.exc_type, self.exc_value, self.frames[0]
  98. @property
  99. def standard_exc_info(self):
  100. """Standard python exc_info for re-raising"""
  101. tb = self.frames[0]
  102. # the frame will be an actual traceback (or transparent proxy) if
  103. # we are on pypy or a python implementation with support for tproxy
  104. if type(tb) is not TracebackType:
  105. tb = tb.tb
  106. return self.exc_type, self.exc_value, tb
  107. def make_traceback(exc_info, source_hint=None):
  108. """Creates a processed traceback object from the exc_info."""
  109. exc_type, exc_value, tb = exc_info
  110. if isinstance(exc_value, TemplateSyntaxError):
  111. exc_info = translate_syntax_error(exc_value, source_hint)
  112. initial_skip = 0
  113. else:
  114. initial_skip = 1
  115. return translate_exception(exc_info, initial_skip)
  116. def translate_syntax_error(error, source=None):
  117. """Rewrites a syntax error to please traceback systems."""
  118. error.source = source
  119. error.translated = True
  120. exc_info = (error.__class__, error, None)
  121. filename = error.filename
  122. if filename is None:
  123. filename = '<unknown>'
  124. return fake_exc_info(exc_info, filename, error.lineno)
  125. def translate_exception(exc_info, initial_skip=0):
  126. """If passed an exc_info it will automatically rewrite the exceptions
  127. all the way down to the correct line numbers and frames.
  128. """
  129. tb = exc_info[2]
  130. frames = []
  131. # skip some internal frames if wanted
  132. for x in range(initial_skip):
  133. if tb is not None:
  134. tb = tb.tb_next
  135. initial_tb = tb
  136. while tb is not None:
  137. # skip frames decorated with @internalcode. These are internal
  138. # calls we can't avoid and that are useless in template debugging
  139. # output.
  140. if tb.tb_frame.f_code in internal_code:
  141. tb = tb.tb_next
  142. continue
  143. # save a reference to the next frame if we override the current
  144. # one with a faked one.
  145. next = tb.tb_next
  146. # fake template exceptions
  147. template = tb.tb_frame.f_globals.get('__jinja_template__')
  148. if template is not None:
  149. lineno = template.get_corresponding_lineno(tb.tb_lineno)
  150. tb = fake_exc_info(exc_info[:2] + (tb,), template.filename,
  151. lineno)[2]
  152. frames.append(make_frame_proxy(tb))
  153. tb = next
  154. # if we don't have any exceptions in the frames left, we have to
  155. # reraise it unchanged.
  156. # XXX: can we backup here? when could this happen?
  157. if not frames:
  158. reraise(exc_info[0], exc_info[1], exc_info[2])
  159. return ProcessedTraceback(exc_info[0], exc_info[1], frames)
  160. def fake_exc_info(exc_info, filename, lineno):
  161. """Helper for `translate_exception`."""
  162. exc_type, exc_value, tb = exc_info
  163. # figure the real context out
  164. if tb is not None:
  165. real_locals = tb.tb_frame.f_locals.copy()
  166. ctx = real_locals.get('context')
  167. if ctx:
  168. locals = ctx.get_all()
  169. else:
  170. locals = {}
  171. for name, value in iteritems(real_locals):
  172. if name.startswith('l_') and value is not missing:
  173. locals[name[2:]] = value
  174. # if there is a local called __jinja_exception__, we get
  175. # rid of it to not break the debug functionality.
  176. locals.pop('__jinja_exception__', None)
  177. else:
  178. locals = {}
  179. # assamble fake globals we need
  180. globals = {
  181. '__name__': filename,
  182. '__file__': filename,
  183. '__jinja_exception__': exc_info[:2],
  184. # we don't want to keep the reference to the template around
  185. # to not cause circular dependencies, but we mark it as Jinja
  186. # frame for the ProcessedTraceback
  187. '__jinja_template__': None
  188. }
  189. # and fake the exception
  190. code = compile('\n' * (lineno - 1) + raise_helper, filename, 'exec')
  191. # if it's possible, change the name of the code. This won't work
  192. # on some python environments such as google appengine
  193. try:
  194. if tb is None:
  195. location = 'template'
  196. else:
  197. function = tb.tb_frame.f_code.co_name
  198. if function == 'root':
  199. location = 'top-level template code'
  200. elif function.startswith('block_'):
  201. location = 'block "%s"' % function[6:]
  202. else:
  203. location = 'template'
  204. code = code_type(0, code.co_nlocals, code.co_stacksize,
  205. code.co_flags, code.co_code, code.co_consts,
  206. code.co_names, code.co_varnames, filename,
  207. location, code.co_firstlineno,
  208. code.co_lnotab, (), ())
  209. except:
  210. pass
  211. # execute the code and catch the new traceback
  212. try:
  213. exec(code, globals, locals)
  214. except:
  215. exc_info = sys.exc_info()
  216. new_tb = exc_info[2].tb_next
  217. # return without this frame
  218. return exc_info[:2] + (new_tb,)
  219. def _init_ugly_crap():
  220. """This function implements a few ugly things so that we can patch the
  221. traceback objects. The function returned allows resetting `tb_next` on
  222. any python traceback object. Do not attempt to use this on non cpython
  223. interpreters
  224. """
  225. import ctypes
  226. from types import TracebackType
  227. # figure out side of _Py_ssize_t
  228. if hasattr(ctypes.pythonapi, 'Py_InitModule4_64'):
  229. _Py_ssize_t = ctypes.c_int64
  230. else:
  231. _Py_ssize_t = ctypes.c_int
  232. # regular python
  233. class _PyObject(ctypes.Structure):
  234. pass
  235. _PyObject._fields_ = [
  236. ('ob_refcnt', _Py_ssize_t),
  237. ('ob_type', ctypes.POINTER(_PyObject))
  238. ]
  239. # python with trace
  240. if hasattr(sys, 'getobjects'):
  241. class _PyObject(ctypes.Structure):
  242. pass
  243. _PyObject._fields_ = [
  244. ('_ob_next', ctypes.POINTER(_PyObject)),
  245. ('_ob_prev', ctypes.POINTER(_PyObject)),
  246. ('ob_refcnt', _Py_ssize_t),
  247. ('ob_type', ctypes.POINTER(_PyObject))
  248. ]
  249. class _Traceback(_PyObject):
  250. pass
  251. _Traceback._fields_ = [
  252. ('tb_next', ctypes.POINTER(_Traceback)),
  253. ('tb_frame', ctypes.POINTER(_PyObject)),
  254. ('tb_lasti', ctypes.c_int),
  255. ('tb_lineno', ctypes.c_int)
  256. ]
  257. def tb_set_next(tb, next):
  258. """Set the tb_next attribute of a traceback object."""
  259. if not (isinstance(tb, TracebackType) and
  260. (next is None or isinstance(next, TracebackType))):
  261. raise TypeError('tb_set_next arguments must be traceback objects')
  262. obj = _Traceback.from_address(id(tb))
  263. if tb.tb_next is not None:
  264. old = _Traceback.from_address(id(tb.tb_next))
  265. old.ob_refcnt -= 1
  266. if next is None:
  267. obj.tb_next = ctypes.POINTER(_Traceback)()
  268. else:
  269. next = _Traceback.from_address(id(next))
  270. next.ob_refcnt += 1
  271. obj.tb_next = ctypes.pointer(next)
  272. return tb_set_next
  273. # try to get a tb_set_next implementation if we don't have transparent
  274. # proxies.
  275. tb_set_next = None
  276. if tproxy is None:
  277. try:
  278. tb_set_next = _init_ugly_crap()
  279. except:
  280. pass
  281. del _init_ugly_crap