Added another testcase and fixed a bug with the volatile scoping.
authorArmin Ronacher <armin.ronacher@active-4.com>
Sun, 14 Mar 2010 23:26:05 +0000 (00:26 +0100)
committerArmin Ronacher <armin.ronacher@active-4.com>
Sun, 14 Mar 2010 23:26:05 +0000 (00:26 +0100)
--HG--
branch : trunk

jinja2/compiler.py
jinja2/testsuite/ext.py

index 5f355a99b6a88fa10b25f29195ab86d1e11b7da9..3bc6d354fee587df6d49690751b642b44669fc55 100644 (file)
@@ -1318,6 +1318,7 @@ class CodeGenerator(NodeVisitor):
                 if frame.eval_ctx.volatile:
                     self.write('(context.eval_ctx.autoescape and'
                                ' escape or to_string)(')
+                    close += 1
                 elif frame.eval_ctx.autoescape:
                     self.write('escape(')
                     close += 1
@@ -1613,7 +1614,7 @@ class CodeGenerator(NodeVisitor):
             try:
                 val = keyword.value.as_const(frame.eval_ctx)
             except nodes.Impossible:
-                frame.volatile = True
+                frame.eval_ctx.volatile = True
             else:
                 setattr(frame.eval_ctx, keyword.key, val)
 
index 09b2b857af15f5d45974e334ad163ce582012928..f8458b1f381e9cdf4dc2a0a4f1af62fc72bf4a39 100644 (file)
@@ -306,6 +306,19 @@ class AutoEscapeTestCase(JinjaTestCase):
                                '{% endautoescape %}{{ x }}{{ "<y>" }}')
         assert tmpl.render(x=1) == '&lt;x&gt;1<y>'
 
+    def test_volatile_scoping(self):
+        env = Environment(extensions=['jinja2.ext.autoescape'])
+        tmpl = env.from_string('''
+        {% autoescape val %}
+            {% macro foo(x) %}
+                [{{ x }}]
+            {% endmacro %}
+            {{ foo().__class__.__name__ }}
+        {% endautoescape %}
+        ''')
+        assert tmpl.render(val=True).strip() == 'Markup'
+        assert tmpl.render(val=False).strip() == unicode.__name__
+
 
 def suite():
     suite = unittest.TestSuite()