]> git.ipfire.org Git - thirdparty/jinja.git/commitdiff
Correctly use buffer for else frame in loops. Fixes #669
authorArmin Ronacher <armin.ronacher@active-4.com>
Tue, 24 Jan 2017 19:16:02 +0000 (20:16 +0100)
committerArmin Ronacher <armin.ronacher@active-4.com>
Tue, 24 Jan 2017 19:16:02 +0000 (20:16 +0100)
CHANGES
jinja2/compiler.py
tests/test_regression.py

diff --git a/CHANGES b/CHANGES
index d7fa6b11170234f4e5cccd3d0b73f23ccaf52b98..faa319c09810ec94d849c906d36cf64d01e35e9b 100644 (file)
--- 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
 -------------
index 48f3210928224efb1f7a1289889fd803d696a6dd..0765675e5d64af1e427ddaf8adedff6440fd0f18 100644 (file)
@@ -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:
index e6a2f0c4c8702d88f71cc16842e7d1936a449a5e..f2314dee0459fba15bd55593c32a17a547dbdbb4 100644 (file)
@@ -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'