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