From: Armin Ronacher Date: Tue, 24 Jan 2017 19:16:02 +0000 (+0100) Subject: Correctly use buffer for else frame in loops. Fixes #669 X-Git-Tag: 2.9.5~5 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ca124c5cfe69ea695ac4caa05d0acb611de52adf;p=thirdparty%2Fjinja.git Correctly use buffer for else frame in loops. Fixes #669 --- diff --git a/CHANGES b/CHANGES index d7fa6b11..faa319c0 100644 --- a/CHANGES +++ b/CHANGES @@ -10,6 +10,9 @@ Version 2.9.5 - Added back support for custom contexts that override the old `resolve` method since it was hard for people to spot that this could cause a regression. +- Correctly use the buffer for the else block of for loops. This caused + invalid syntax errors to be caused on 2.x and completely wrong behavior + on Python 3 (#669) Version 2.9.4 ------------- diff --git a/jinja2/compiler.py b/jinja2/compiler.py index 48f32109..0765675e 100644 --- a/jinja2/compiler.py +++ b/jinja2/compiler.py @@ -1040,6 +1040,9 @@ class CodeGenerator(NodeVisitor): self.indent() self.buffer(loop_frame) + # Use the same buffer for the else frame + else_frame.buffer = loop_frame.buffer + # make sure the loop variable is a special one and raise a template # assertion error if a loop tries to write to loop if extended_loop: diff --git a/tests/test_regression.py b/tests/test_regression.py index e6a2f0c4..f2314dee 100644 --- a/tests/test_regression.py +++ b/tests/test_regression.py @@ -520,3 +520,9 @@ class TestBug(object): assert x.resolve_or_missing('foo') == 42 assert x.resolve_or_missing('bar') == 23 assert x.resolve_or_missing('baz') is missing + + def test_recursive_loop_bug(self, env): + tmpl = env.from_string(''' + {%- for value in values recursive %}1{% else %}0{% endfor -%} + ''') + assert tmpl.render(values=[]) == '0'