Skip to content

Commit

Permalink
TemplateSyntaxError from included template has source
Browse files Browse the repository at this point in the history
  • Loading branch information
davidism committed Dec 3, 2019
1 parent 09ddf2d commit af8a474
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Expand Up @@ -90,6 +90,8 @@ Unreleased
``loop``. :issue:`860`
- Constant folding during compilation is applied to some node types
that were previously overlooked. :issue:`733`
- ``TemplateSyntaxError.source`` is not empty when raised from an
included template. :issue:`457`


Version 2.10.3
Expand Down
10 changes: 4 additions & 6 deletions jinja2/debug.py
Expand Up @@ -21,14 +21,10 @@ def rewrite_traceback_stack(source=None):
:return: A :meth:`sys.exc_info` tuple that can be re-raised.
"""
exc_type, exc_value, tb = sys.exc_info()
# The new stack of traceback objects, to be joined together by
# tb_set_next later.
stack = []

if isinstance(exc_value, TemplateSyntaxError):
exc_value.source = source
# The exception doesn't need to output location info manually.
if isinstance(exc_value, TemplateSyntaxError) and not exc_value.translated:
exc_value.translated = True
exc_value.source = source

try:
# Remove the old traceback on Python 3, otherwise the frames
Expand All @@ -46,6 +42,8 @@ def rewrite_traceback_stack(source=None):
# Skip the frame for the render function.
tb = tb.tb_next

stack = []

# Build the stack of traceback object, replacing any in template
# code with the source file and line information.
while tb is not None:
Expand Down
22 changes: 17 additions & 5 deletions tests/test_debug.py
Expand Up @@ -14,6 +14,8 @@
import sys
from traceback import format_exception

from jinja2 import ChoiceLoader
from jinja2 import DictLoader
from jinja2 import Environment, TemplateSyntaxError


Expand Down Expand Up @@ -51,13 +53,9 @@ def test():
''')

def test_syntax_error(self, fs_env):
# XXX: the .*? is necessary for python3 which does not hide
# some of the stack frames we don't want to show. Not sure
# what's up with that, but that is not that critical. Should
# be fixed though.
self.assert_traceback_matches(lambda: fs_env.get_template('syntaxerror.html'), r'''(?sm)
File ".*?syntaxerror.html", line 4, in (template|<module>)
\{% endif %\}.*?
\{% endif %\}
(jinja2\.exceptions\.)?TemplateSyntaxError: Encountered unknown tag 'endif'. Jinja was looking for the following tags: 'endfor' or 'else'. The innermost block that needs to be closed is 'for'.
''')

Expand All @@ -70,6 +68,20 @@ def test():
(jinja2\.exceptions\.)?TemplateSyntaxError: wtf
line 42''')

def test_include_syntax_error_source(self, filesystem_loader):
e = Environment(loader=ChoiceLoader(
[
filesystem_loader,
DictLoader({"inc": "a\n{% include 'syntaxerror.html' %}\nb"}),
]
))
t = e.get_template("inc")

with pytest.raises(TemplateSyntaxError) as exc_info:
t.render()

assert exc_info.value.source is not None

def test_local_extraction(self):
from jinja2.debug import get_template_locals
from jinja2.runtime import missing
Expand Down

0 comments on commit af8a474

Please sign in to comment.