Merge pull request #1112 from pallets/include-syntax-error-source
Before adoption: this commit came in with the history the project adopted, so no landing records it.
TemplateSyntaxError from included template has source
authored by David Lord · committed by GitHub · parents 09ddf2d, 2b0d1ed · 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
Provenance
- Commit
28f12c0- Tree
23762a1- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
09ddf2d- Parent 2
2b0d1ed
3 files changed+25 added−10 removed
modified:jinja2/debug.py+4 added-6 removed
19 unmodified lines20 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 = []24 28- 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 = source28 29 try:30 # Remove the old traceback on Python 3, otherwise the frames31 # from the compiler still show up.9 unmodified lines41 else:42 # Skip the frame for the render function.43 tb = tb.tb_next44 45+ stack = []46+47 # 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 internal216 unmodified linesmodified:tests/test_debug.py+19 added-4 removed
12 unmodified lines13 import re14 import sys15 from traceback import format_exception16 17+from jinja2 import ChoiceLoader18+from jinja2 import DictLoader19 from jinja2 import Environment, TemplateSyntaxError20 21 22 @pytest.fixture29 unmodified lines52 ZeroDivisionError: (int(eger)? )?division (or modulo )?by zero53 ''')54 55 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 lines70 raise TemplateSyntaxError\('wtf', 42\)71 (jinja2\.exceptions\.)?TemplateSyntaxError: wtf72 line 42''')73 74+ 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")82+83+ with pytest.raises(TemplateSyntaxError) as exc_info:84+ t.render()85+86+ assert exc_info.value.source is not None87+88 def test_local_extraction(self):89 from jinja2.debug import get_template_locals90 from jinja2.runtime import missing91 locals = get_template_locals({12 unmodified linesmodified:CHANGES.rst+2 added
88 unmodified lines89 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`95 96 97 Version 2.10.398 --------------629 unmodified lines