Raised to tell the user that there is a problem with the template.
| 86 | |
| 87 | |
| 88 | class TemplateSyntaxError(TemplateError): |
| 89 | """Raised to tell the user that there is a problem with the template.""" |
| 90 | |
| 91 | def __init__( |
| 92 | self, |
| 93 | message: str, |
| 94 | lineno: int, |
| 95 | name: t.Optional[str] = None, |
| 96 | filename: t.Optional[str] = None, |
| 97 | ) -> None: |
| 98 | super().__init__(message) |
| 99 | self.lineno = lineno |
| 100 | self.name = name |
| 101 | self.filename = filename |
| 102 | self.source: t.Optional[str] = None |
| 103 | |
| 104 | # this is set to True if the debug.translate_syntax_error |
| 105 | # function translated the syntax error into a new traceback |
| 106 | self.translated = False |
| 107 | |
| 108 | def __str__(self) -> str: |
| 109 | # for translated errors we only return the message |
| 110 | if self.translated: |
| 111 | return t.cast(str, self.message) |
| 112 | |
| 113 | # otherwise attach some stuff |
| 114 | location = f"line {self.lineno}" |
| 115 | name = self.filename or self.name |
| 116 | if name: |
| 117 | location = f'File "{name}", {location}' |
| 118 | lines = [t.cast(str, self.message), " " + location] |
| 119 | |
| 120 | # if the source is set, add the line to the output |
| 121 | if self.source is not None: |
| 122 | try: |
| 123 | line = self.source.splitlines()[self.lineno - 1] |
| 124 | except IndexError: |
| 125 | pass |
| 126 | else: |
| 127 | lines.append(" " + line.strip()) |
| 128 | |
| 129 | return "\n".join(lines) |
| 130 | |
| 131 | def __reduce__(self): # type: ignore |
| 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 required |
| 135 | # positional argument: 'lineno' |
| 136 | return self.__class__, (self.message, self.lineno, self.name, self.filename) |
| 137 | |
| 138 | |
| 139 | class TemplateAssertionError(TemplateSyntaxError): |
no outgoing calls