debug.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. import sys
  2. from types import CodeType
  3. from . import TemplateSyntaxError
  4. from ._compat import PYPY
  5. from .utils import internal_code
  6. from .utils import missing
  7. def rewrite_traceback_stack(source=None):
  8. """Rewrite the current exception to replace any tracebacks from
  9. within compiled template code with tracebacks that look like they
  10. came from the template source.
  11. This must be called within an ``except`` block.
  12. :param exc_info: A :meth:`sys.exc_info` tuple. If not provided,
  13. the current ``exc_info`` is used.
  14. :param source: For ``TemplateSyntaxError``, the original source if
  15. known.
  16. :return: A :meth:`sys.exc_info` tuple that can be re-raised.
  17. """
  18. exc_type, exc_value, tb = sys.exc_info()
  19. if isinstance(exc_value, TemplateSyntaxError) and not exc_value.translated:
  20. exc_value.translated = True
  21. exc_value.source = source
  22. try:
  23. # Remove the old traceback on Python 3, otherwise the frames
  24. # from the compiler still show up.
  25. exc_value.with_traceback(None)
  26. except AttributeError:
  27. pass
  28. # Outside of runtime, so the frame isn't executing template
  29. # code, but it still needs to point at the template.
  30. tb = fake_traceback(
  31. exc_value, None, exc_value.filename or "<unknown>", exc_value.lineno
  32. )
  33. else:
  34. # Skip the frame for the render function.
  35. tb = tb.tb_next
  36. stack = []
  37. # Build the stack of traceback object, replacing any in template
  38. # code with the source file and line information.
  39. while tb is not None:
  40. # Skip frames decorated with @internalcode. These are internal
  41. # calls that aren't useful in template debugging output.
  42. if tb.tb_frame.f_code in internal_code:
  43. tb = tb.tb_next
  44. continue
  45. template = tb.tb_frame.f_globals.get("__jinja_template__")
  46. if template is not None:
  47. lineno = template.get_corresponding_lineno(tb.tb_lineno)
  48. fake_tb = fake_traceback(exc_value, tb, template.filename, lineno)
  49. stack.append(fake_tb)
  50. else:
  51. stack.append(tb)
  52. tb = tb.tb_next
  53. tb_next = None
  54. # Assign tb_next in reverse to avoid circular references.
  55. for tb in reversed(stack):
  56. tb_next = tb_set_next(tb, tb_next)
  57. return exc_type, exc_value, tb_next
  58. def fake_traceback(exc_value, tb, filename, lineno):
  59. """Produce a new traceback object that looks like it came from the
  60. template source instead of the compiled code. The filename, line
  61. number, and location name will point to the template, and the local
  62. variables will be the current template context.
  63. :param exc_value: The original exception to be re-raised to create
  64. the new traceback.
  65. :param tb: The original traceback to get the local variables and
  66. code info from.
  67. :param filename: The template filename.
  68. :param lineno: The line number in the template source.
  69. """
  70. if tb is not None:
  71. # Replace the real locals with the context that would be
  72. # available at that point in the template.
  73. locals = get_template_locals(tb.tb_frame.f_locals)
  74. locals.pop("__jinja_exception__", None)
  75. else:
  76. locals = {}
  77. globals = {
  78. "__name__": filename,
  79. "__file__": filename,
  80. "__jinja_exception__": exc_value,
  81. }
  82. # Raise an exception at the correct line number.
  83. code = compile("\n" * (lineno - 1) + "raise __jinja_exception__", filename, "exec")
  84. # Build a new code object that points to the template file and
  85. # replaces the location with a block name.
  86. try:
  87. location = "template"
  88. if tb is not None:
  89. function = tb.tb_frame.f_code.co_name
  90. if function == "root":
  91. location = "top-level template code"
  92. elif function.startswith("block_"):
  93. location = 'block "%s"' % function[6:]
  94. # Collect arguments for the new code object. CodeType only
  95. # accepts positional arguments, and arguments were inserted in
  96. # new Python versions.
  97. code_args = []
  98. for attr in (
  99. "argcount",
  100. "posonlyargcount", # Python 3.8
  101. "kwonlyargcount", # Python 3
  102. "nlocals",
  103. "stacksize",
  104. "flags",
  105. "code", # codestring
  106. "consts", # constants
  107. "names",
  108. "varnames",
  109. ("filename", filename),
  110. ("name", location),
  111. "firstlineno",
  112. "lnotab",
  113. "freevars",
  114. "cellvars",
  115. ):
  116. if isinstance(attr, tuple):
  117. # Replace with given value.
  118. code_args.append(attr[1])
  119. continue
  120. try:
  121. # Copy original value if it exists.
  122. code_args.append(getattr(code, "co_" + attr))
  123. except AttributeError:
  124. # Some arguments were added later.
  125. continue
  126. code = CodeType(*code_args)
  127. except Exception:
  128. # Some environments such as Google App Engine don't support
  129. # modifying code objects.
  130. pass
  131. # Execute the new code, which is guaranteed to raise, and return
  132. # the new traceback without this frame.
  133. try:
  134. exec(code, globals, locals)
  135. except BaseException:
  136. return sys.exc_info()[2].tb_next
  137. def get_template_locals(real_locals):
  138. """Based on the runtime locals, get the context that would be
  139. available at that point in the template.
  140. """
  141. # Start with the current template context.
  142. ctx = real_locals.get("context")
  143. if ctx:
  144. data = ctx.get_all().copy()
  145. else:
  146. data = {}
  147. # Might be in a derived context that only sets local variables
  148. # rather than pushing a context. Local variables follow the scheme
  149. # l_depth_name. Find the highest-depth local that has a value for
  150. # each name.
  151. local_overrides = {}
  152. for name, value in real_locals.items():
  153. if not name.startswith("l_") or value is missing:
  154. # Not a template variable, or no longer relevant.
  155. continue
  156. try:
  157. _, depth, name = name.split("_", 2)
  158. depth = int(depth)
  159. except ValueError:
  160. continue
  161. cur_depth = local_overrides.get(name, (-1,))[0]
  162. if cur_depth < depth:
  163. local_overrides[name] = (depth, value)
  164. # Modify the context with any derived context.
  165. for name, (_, value) in local_overrides.items():
  166. if value is missing:
  167. data.pop(name, None)
  168. else:
  169. data[name] = value
  170. return data
  171. if sys.version_info >= (3, 7):
  172. # tb_next is directly assignable as of Python 3.7
  173. def tb_set_next(tb, tb_next):
  174. tb.tb_next = tb_next
  175. return tb
  176. elif PYPY:
  177. # PyPy might have special support, and won't work with ctypes.
  178. try:
  179. import tputil
  180. except ImportError:
  181. # Without tproxy support, use the original traceback.
  182. def tb_set_next(tb, tb_next):
  183. return tb
  184. else:
  185. # With tproxy support, create a proxy around the traceback that
  186. # returns the new tb_next.
  187. def tb_set_next(tb, tb_next):
  188. def controller(op):
  189. if op.opname == "__getattribute__" and op.args[0] == "tb_next":
  190. return tb_next
  191. return op.delegate()
  192. return tputil.make_proxy(controller, obj=tb)
  193. else:
  194. # Use ctypes to assign tb_next at the C level since it's read-only
  195. # from Python.
  196. import ctypes
  197. class _CTraceback(ctypes.Structure):
  198. _fields_ = [
  199. # Extra PyObject slots when compiled with Py_TRACE_REFS.
  200. ("PyObject_HEAD", ctypes.c_byte * object().__sizeof__()),
  201. # Only care about tb_next as an object, not a traceback.
  202. ("tb_next", ctypes.py_object),
  203. ]
  204. def tb_set_next(tb, tb_next):
  205. c_tb = _CTraceback.from_address(id(tb))
  206. # Clear out the old tb_next.
  207. if tb.tb_next is not None:
  208. c_tb_next = ctypes.py_object(tb.tb_next)
  209. c_tb.tb_next = ctypes.py_object()
  210. ctypes.pythonapi.Py_DecRef(c_tb_next)
  211. # Assign the new tb_next.
  212. if tb_next is not None:
  213. c_tb_next = ctypes.py_object(tb_next)
  214. ctypes.pythonapi.Py_IncRef(c_tb_next)
  215. c_tb.tb_next = c_tb_next
  216. return tb