Skip to content
Myr

exceptions.py

This file

Path

src/jinja2/exceptions.py

166 lines · 4.9 KiB · Python

Last changed by

drop end of life python versions (#2096)David Lordbefore adoption49c48a0

History of this file

Take it

The walk stopped after 200 commits: lines older than c580dd0 point at it.

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

lines 1–6why, one step back
1import typing as t
23if t.TYPE_CHECKING:4 from .runtime import Undefined56

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 7–9why, one step back
7class TemplateError(Exception):
8 """Baseclass for all template errors."""9

drop end of life python versions (#2096)David Lordbefore adoption49c48a0

line 10why, one step back
10 def __init__(self, message: str | None = None) -> None:

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 11–13why, one step back
11 super().__init__(message)
1213 @property

drop end of life python versions (#2096)David Lordbefore adoption49c48a0

line 14why, one step back
14 def message(self) -> str | None:

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

line 15why, one step back
15 return self.args[0] if self.args else None

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 16–27why, one step back
16
1718class TemplateNotFound(IOError, LookupError, TemplateError):19 """Raised if a template does not exist.2021 .. versionchanged:: 2.1122 If the given name is :class:`Undefined` and no message was23 provided, an :exc:`UndefinedError` is raised.24 """2526 # Silence the Python warning about message being deprecated since27 # it's not valid here.

drop end of life python versions (#2096)David Lordbefore adoption49c48a0

line 28why, one step back
28 message: str | None = None

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

line 29why, one step back
29

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

lines 30–31why, one step back
30 def __init__(
31 self,

drop end of life python versions (#2096)David Lordbefore adoption49c48a0

lines 32–33why, one step back
32 name: t.Union[str, "Undefined"] | None,
33 message: str | None = None,

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

line 34why, one step back
34 ) -> None:

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 35–48why, one step back
35 IOError.__init__(self, name)
3637 if message is None:38 from .runtime import Undefined3940 if isinstance(name, Undefined):41 name._fail_with_undefined_error()4243 message = name4445 self.message = message46 self.name = name47 self.templates = [name]48

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

lines 49–50why, one step back
49 def __str__(self) -> str:
50 return str(self.message)

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 51–64why, one step back
51
5253class TemplatesNotFound(TemplateNotFound):54 """Like :class:`TemplateNotFound` but raised if multiple templates55 are selected. This is a subclass of :class:`TemplateNotFound`56 exception, so just catching the base exception will catch both.5758 .. versionchanged:: 2.1159 If a name in the list of names is :class:`Undefined`, a message60 about it being undefined is shown rather than the empty string.6162 .. versionadded:: 2.263 """64

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

lines 65–67why, one step back
65 def __init__(
66 self,67 names: t.Sequence[t.Union[str, "Undefined"]] = (),

drop end of life python versions (#2096)David Lordbefore adoption49c48a0

line 68why, one step back
68 message: str | None = None,

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

line 69why, one step back
69 ) -> None:

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 70–80why, one step back
70 if message is None:
71 from .runtime import Undefined7273 parts = []7475 for name in names:76 if isinstance(name, Undefined):77 parts.append(name._undefined_message)78 else:79 parts.append(name)80

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

lines 81–84why, one step back
81 parts_str = ", ".join(map(str, parts))
82 message = f"none of the templates given were found: {parts_str}"8384 super().__init__(names[-1] if names else None, message)

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 85–90why, one step back
85 self.templates = list(names)
868788class TemplateSyntaxError(TemplateError):89 """Raised to tell the user that there is a problem with the template."""90

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

lines 91–94why, one step back
91 def __init__(
92 self,93 message: str,94 lineno: int,

drop end of life python versions (#2096)David Lordbefore adoption49c48a0

lines 95–96why, one step back
95 name: str | None = None,
96 filename: str | None = None,

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

lines 97–98why, one step back
97 ) -> None:
98 super().__init__(message)

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 99–101why, one step back
99 self.lineno = lineno
100 self.name = name101 self.filename = filename

drop end of life python versions (#2096)David Lordbefore adoption49c48a0

line 102why, one step back
102 self.source: str | None = None

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 103–107why, one step back
103
104 # this is set to True if the debug.translate_syntax_error105 # function translated the syntax error into a new traceback106 self.translated = False107

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

line 108why, one step back
108 def __str__(self) -> str:

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 109–110why, one step back
109 # for translated errors we only return the message
110 if self.translated:

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

line 111why, one step back
111 return t.cast(str, self.message)

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 112–117why, one step back
112
113 # otherwise attach some stuff114 location = f"line {self.lineno}"115 name = self.filename or self.name116 if name:117 location = f'File "{name}", {location}'

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

line 118why, one step back
118 lines = [t.cast(str, self.message), " " + location]

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 119–124why, one step back
119
120 # if the source is set, add the line to the output121 if self.source is not None:122 try:123 line = self.source.splitlines()[self.lineno - 1]124 except IndexError:

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

lines 125–126why, one step back
125 pass
126 else:

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 127–130why, one step back
127 lines.append(" " + line.strip())
128129 return "\n".join(lines)130

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

line 131why, one step back
131 def __reduce__(self): # type: ignore

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 132–166why, one step back
132 # https://bugs.python.org/issue1692335 Exceptions that take
133 # multiple required arguments have problems with pickling.134 # Without this, raises TypeError: __init__() missing 1 required135 # positional argument: 'lineno'136 return self.__class__, (self.message, self.lineno, self.name, self.filename)137138139class TemplateAssertionError(TemplateSyntaxError):140 """Like a template syntax error, but covers cases where something in the141 template caused an error at compile time that wasn't necessarily caused142 by a syntax error. However it's a direct subclass of143 :exc:`TemplateSyntaxError` and has the same attributes.144 """145146147class TemplateRuntimeError(TemplateError):148 """A generic runtime error in the template engine. Under some situations149 Jinja may raise this exception.150 """151152153class UndefinedError(TemplateRuntimeError):154 """Raised if a template tries to operate on :class:`Undefined`."""155156157class SecurityError(TemplateRuntimeError):158 """Raised if a template tries to do something insecure if the159 sandbox is enabled.160 """161162163class FilterArgumentError(TemplateRuntimeError):164 """This error is raised if a filter was called with inappropriate165 arguments166 """