PageRenderTime 58ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/flask/Lib/site-packages/jinja2/debug.py

https://gitlab.com/nachee1325/todoprv
Python | 350 lines | 254 code | 43 blank | 53 comment | 49 complexity | 664255cb0cff6ea6de505f48717b45a3 MD5 | raw file
  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, CodeType
  14. from jinja2.utils import missing, internal_code
  15. from jinja2.exceptions import TemplateSyntaxError
  16. from jinja2._compat import iteritems, reraise, PY2
  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. if PY2:
  205. code = CodeType(0, code.co_nlocals, code.co_stacksize,
  206. code.co_flags, code.co_code, code.co_consts,
  207. code.co_names, code.co_varnames, filename,
  208. location, code.co_firstlineno,
  209. code.co_lnotab, (), ())
  210. else:
  211. code = CodeType(0, code.co_kwonlyargcount,
  212. code.co_nlocals, code.co_stacksize,
  213. code.co_flags, code.co_code, code.co_consts,
  214. code.co_names, code.co_varnames, filename,
  215. location, code.co_firstlineno,
  216. code.co_lnotab, (), ())
  217. except Exception as e:
  218. pass
  219. # execute the code and catch the new traceback
  220. try:
  221. exec(code, globals, locals)
  222. except:
  223. exc_info = sys.exc_info()
  224. new_tb = exc_info[2].tb_next
  225. # return without this frame
  226. return exc_info[:2] + (new_tb,)
  227. def _init_ugly_crap():
  228. """This function implements a few ugly things so that we can patch the
  229. traceback objects. The function returned allows resetting `tb_next` on
  230. any python traceback object. Do not attempt to use this on non cpython
  231. interpreters
  232. """
  233. import ctypes
  234. from types import TracebackType
  235. if PY2:
  236. # figure out size of _Py_ssize_t for Python 2:
  237. if hasattr(ctypes.pythonapi, 'Py_InitModule4_64'):
  238. _Py_ssize_t = ctypes.c_int64
  239. else:
  240. _Py_ssize_t = ctypes.c_int
  241. else:
  242. # platform ssize_t on Python 3
  243. _Py_ssize_t = ctypes.c_ssize_t
  244. # regular python
  245. class _PyObject(ctypes.Structure):
  246. pass
  247. _PyObject._fields_ = [
  248. ('ob_refcnt', _Py_ssize_t),
  249. ('ob_type', ctypes.POINTER(_PyObject))
  250. ]
  251. # python with trace
  252. if hasattr(sys, 'getobjects'):
  253. class _PyObject(ctypes.Structure):
  254. pass
  255. _PyObject._fields_ = [
  256. ('_ob_next', ctypes.POINTER(_PyObject)),
  257. ('_ob_prev', ctypes.POINTER(_PyObject)),
  258. ('ob_refcnt', _Py_ssize_t),
  259. ('ob_type', ctypes.POINTER(_PyObject))
  260. ]
  261. class _Traceback(_PyObject):
  262. pass
  263. _Traceback._fields_ = [
  264. ('tb_next', ctypes.POINTER(_Traceback)),
  265. ('tb_frame', ctypes.POINTER(_PyObject)),
  266. ('tb_lasti', ctypes.c_int),
  267. ('tb_lineno', ctypes.c_int)
  268. ]
  269. def tb_set_next(tb, next):
  270. """Set the tb_next attribute of a traceback object."""
  271. if not (isinstance(tb, TracebackType) and
  272. (next is None or isinstance(next, TracebackType))):
  273. raise TypeError('tb_set_next arguments must be traceback objects')
  274. obj = _Traceback.from_address(id(tb))
  275. if tb.tb_next is not None:
  276. old = _Traceback.from_address(id(tb.tb_next))
  277. old.ob_refcnt -= 1
  278. if next is None:
  279. obj.tb_next = ctypes.POINTER(_Traceback)()
  280. else:
  281. next = _Traceback.from_address(id(next))
  282. next.ob_refcnt += 1
  283. obj.tb_next = ctypes.pointer(next)
  284. return tb_set_next
  285. # try to get a tb_set_next implementation if we don't have transparent
  286. # proxies.
  287. tb_set_next = None
  288. if tproxy is None:
  289. try:
  290. tb_set_next = _init_ugly_crap()
  291. except:
  292. pass
  293. del _init_ugly_crap