Skip to content
Myr

TemplateSyntaxError from included template has source

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by David Lord · committed by David Lord · parent 09ddf2d · Browse files · 3 files+25 added−10 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+25 added−10 removed

  • tests:+19 added−4 removed
  • other:+6 added−6 removed

3 files+25 added−10 removed

modified:jinja2/debug.py+4 added-6 removed
19 unmodified lines
20 known.21 :return: A :meth:`sys.exc_info` tuple that can be re-raised.22 """23 exc_type, exc_value, tb = sys.exc_info()24 # The new stack of traceback objects, to be joined together by25 # tb_set_next later.26 stack = []2428 if isinstance(exc_value, TemplateSyntaxError):29 exc_value.source = source30 # The exception doesn't need to output location info manually.25 if isinstance(exc_value, TemplateSyntaxError) and not exc_value.translated:26 exc_value.translated = True27 exc_value.source = source2829 try:30 # Remove the old traceback on Python 3, otherwise the frames31 # from the compiler still show up.
9 unmodified lines
41 else:42 # Skip the frame for the render function.43 tb = tb.tb_next4445 stack = []4647 # Build the stack of traceback object, replacing any in template48 # code with the source file and line information.49 while tb is not None:50 # Skip frames decorated with @internalcode. These are internal
216 unmodified lines
modified:tests/test_debug.py+19 added-4 removed
12 unmodified lines
13import re14import sys15from traceback import format_exception1617from jinja2 import ChoiceLoader18from jinja2 import DictLoader19from jinja2 import Environment, TemplateSyntaxError202122@pytest.fixture
29 unmodified lines
52ZeroDivisionError: (int(eger)? )?division (or modulo )?by zero53''')5455 def test_syntax_error(self, fs_env):54 # XXX: the .*? is necessary for python3 which does not hide55 # some of the stack frames we don't want to show. Not sure56 # what's up with that, but that is not that critical. Should57 # be fixed though.56 # The trailing .*? is for PyPy 2 and 3, which don't seem to57 # clear the exception's original traceback, leaving the syntax58 # error in the middle of other compiler frames.59 self.assert_traceback_matches(lambda: fs_env.get_template('syntaxerror.html'), r'''(?sm)60 File ".*?syntaxerror.html", line 4, in (template|<module>)61 \{% endif %\}.*?62(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'.
7 unmodified lines
70 raise TemplateSyntaxError\('wtf', 42\)71(jinja2\.exceptions\.)?TemplateSyntaxError: wtf72 line 42''')7374 def test_include_syntax_error_source(self, filesystem_loader):75 e = Environment(loader=ChoiceLoader(76 [77 filesystem_loader,78 DictLoader({"inc": "a\n{% include 'syntaxerror.html' %}\nb"}),79 ]80 ))81 t = e.get_template("inc")8283 with pytest.raises(TemplateSyntaxError) as exc_info:84 t.render()8586 assert exc_info.value.source is not None8788 def test_local_extraction(self):89 from jinja2.debug import get_template_locals90 from jinja2.runtime import missing91 locals = get_template_locals({
12 unmodified lines
modified:CHANGES.rst+2 added
88 unmodified lines
89 extensions to get the current context and local variables such as90 ``loop``. :issue:`860`91- Constant folding during compilation is applied to some node types92 that were previously overlooked. :issue:`733`93- ``TemplateSyntaxError.source`` is not empty when raised from an94 included template. :issue:`457`959697Version 2.10.398--------------
629 unmodified lines