debug.py
This file
Path
src/jinja2/debug.py
172 lines · 5.6 KiB · Python
Last changed by
drop end of life python versions (#2096)David Lordbefore adoption49c48a0
History of this fileTake it
The walk stopped after 200 commits: lines older than c580dd0 point at it.
update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
line 1why, one step back1import sysMerge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 2why, one step back2import typing as tupdate building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
line 3why, one step back3from types import CodeTypeMerge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 4why, one step back4from types import TracebackTypeupdate building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
line 5why, one step back5Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 6why, one step back6from .exceptions import TemplateSyntaxErrorupdate building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 7–9why, one step back7from .utils import internal_code8from .utils import missing9Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
lines 10–11why, one step back10if t.TYPE_CHECKING:11 from .runtime import Contextupdate building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
line 12why, one step back12Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 13why, one step back13drop end of life python versions (#2096)David Lordbefore adoption49c48a0
line 14why, one step back14def rewrite_traceback_stack(source: str | None = None) -> BaseException:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 15–25why, one step back15 """Rewrite the current exception to replace any tracebacks from16 within compiled template code with tracebacks that look like they17 came from the template source.1819 This must be called within an ``except`` block.2021 :param source: For ``TemplateSyntaxError``, the original source if22 known.23 :return: The original exception with the rewritten traceback.24 """25 _, exc_value, tb = sys.exc_info()Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
lines 26–27why, one step back26 exc_value = t.cast(BaseException, exc_value)27 tb = t.cast(TracebackType, tb)update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 28–69why, one step back2829 if isinstance(exc_value, TemplateSyntaxError) and not exc_value.translated:30 exc_value.translated = True31 exc_value.source = source32 # Remove the old traceback, otherwise the frames from the33 # compiler still show up.34 exc_value.with_traceback(None)35 # Outside of runtime, so the frame isn't executing template36 # code, but it still needs to point at the template.37 tb = fake_traceback(38 exc_value, None, exc_value.filename or "<unknown>", exc_value.lineno39 )40 else:41 # Skip the frame for the render function.42 tb = tb.tb_next4344 stack = []4546 # Build the stack of traceback object, replacing any in template47 # code with the source file and line information.48 while tb is not None:49 # Skip frames decorated with @internalcode. These are internal50 # calls that aren't useful in template debugging output.51 if tb.tb_frame.f_code in internal_code:52 tb = tb.tb_next53 continue5455 template = tb.tb_frame.f_globals.get("__jinja_template__")5657 if template is not None:58 lineno = template.get_corresponding_lineno(tb.tb_lineno)59 fake_tb = fake_traceback(exc_value, tb, template.filename, lineno)60 stack.append(fake_tb)61 else:62 stack.append(tb)6364 tb = tb.tb_next6566 tb_next = None6768 # Assign tb_next in reverse to avoid circular references.69 for tb in reversed(stack):Merge pull request #1534 from pallets/drop-python-3.6David Lordbefore adoptionb2bc7be
lines 70–71why, one step back70 tb.tb_next = tb_next71 tb_next = tbupdate building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 72–75why, one step back7273 return exc_value.with_traceback(tb_next)7475Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 76why, one step back76def fake_traceback( # type: ignoredrop end of life python versions (#2096)David Lordbefore adoption49c48a0
line 77why, one step back77 exc_value: BaseException, tb: TracebackType | None, filename: str, lineno: intMerge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 78why, one step back78) -> TracebackType:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 79–104why, one step back79 """Produce a new traceback object that looks like it came from the80 template source instead of the compiled code. The filename, line81 number, and location name will point to the template, and the local82 variables will be the current template context.8384 :param exc_value: The original exception to be re-raised to create85 the new traceback.86 :param tb: The original traceback to get the local variables and87 code info from.88 :param filename: The template filename.89 :param lineno: The line number in the template source.90 """91 if tb is not None:92 # Replace the real locals with the context that would be93 # available at that point in the template.94 locals = get_template_locals(tb.tb_frame.f_locals)95 locals.pop("__jinja_exception__", None)96 else:97 locals = {}9899 globals = {100 "__name__": filename,101 "__file__": filename,102 "__jinja_exception__": exc_value,103 }104 # Raise an exception at the correct line number.Merge branch '3.0.x'David Lordbefore adoptiona42b291
lines 105–107why, one step back105 code: CodeType = compile(106 "\n" * (lineno - 1) + "raise __jinja_exception__", filename, "exec"107 )update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 108–110why, one step back108109 # Build a new code object that points to the template file and110 # replaces the location with a block name.Merge branch '3.0.x'David Lordbefore adoptiona42b291
line 111why, one step back111 location = "template"update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
line 112why, one step back112Merge branch '3.0.x'David Lordbefore adoptiona42b291
lines 113–114why, one step back113 if tb is not None:114 function = tb.tb_frame.f_code.co_nameupdate building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
line 115why, one step back115Merge branch '3.0.x'David Lordbefore adoptiona42b291
lines 116–119why, one step back116 if function == "root":117 location = "top-level template code"118 elif function.startswith("block_"):119 location = f"block {function[6:]!r}"update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
line 120why, one step back120drop support for Python 3.7David Lordbefore adoptionfcd3d3b
line 121why, one step back121 code = code.replace(co_name=location)update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 122–127why, one step back122123 # Execute the new code, which is guaranteed to raise, and return124 # the new traceback without this frame.125 try:126 exec(code, globals, locals)127 except BaseException:Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 128why, one step back128 return sys.exc_info()[2].tb_next # type: ignoreupdate building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 129–130why, one step back129130Updated dependency management to uv (#2093)David Lordbefore adoption05f5d74
line 131why, one step back131def get_template_locals(real_locals: t.Mapping[str, t.Any]) -> dict[str, t.Any]:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 132–135why, one step back132 """Based on the runtime locals, get the context that would be133 available at that point in the template.134 """135 # Start with the current template context.drop end of life python versions (#2096)David Lordbefore adoption49c48a0
line 136why, one step back136 ctx: Context | None = real_locals.get("context")update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
line 137why, one step back137Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 138why, one step back138 if ctx is not None:Updated dependency management to uv (#2093)David Lordbefore adoption05f5d74
line 139why, one step back139 data: dict[str, t.Any] = ctx.get_all().copy()update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 140–146why, one step back140 else:141 data = {}142143 # Might be in a derived context that only sets local variables144 # rather than pushing a context. Local variables follow the scheme145 # l_depth_name. Find the highest-depth local that has a value for146 # each name.Updated dependency management to uv (#2093)David Lordbefore adoption05f5d74
line 147why, one step back147 local_overrides: dict[str, tuple[int, t.Any]] = {}update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 148–154why, one step back148149 for name, value in real_locals.items():150 if not name.startswith("l_") or value is missing:151 # Not a template variable, or no longer relevant.152 continue153154 try:Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
lines 155–156why, one step back155 _, depth_str, name = name.split("_", 2)156 depth = int(depth_str)update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 157–172why, one step back157 except ValueError:158 continue159160 cur_depth = local_overrides.get(name, (-1,))[0]161162 if cur_depth < depth:163 local_overrides[name] = (depth, value)164165 # Modify the context with any derived context.166 for name, (_, value) in local_overrides.items():167 if value is missing:168 data.pop(name, None)169 else:170 data[name] = value171172 return data