From: Armin Ronacher Date: Mon, 5 Apr 2010 16:43:07 +0000 (+0200) Subject: Fixed a scoping bug that caused the immutable scoping rule to be ignored X-Git-Tag: 2.4~6 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=8a672512e7275985b8bc4be24b470c99a1be79ec;p=jinja2.git Fixed a scoping bug that caused the immutable scoping rule to be ignored in a few edge cases. This clauses #376. --HG-- branch : trunk --- diff --git a/jinja2/compiler.py b/jinja2/compiler.py index 117bd8c..0d608a3 100644 --- a/jinja2/compiler.py +++ b/jinja2/compiler.py @@ -293,8 +293,7 @@ class FrameIdentifierVisitor(NodeVisitor): self.visit(node.test) real_identifiers = self.identifiers - old_names = real_identifiers.declared | \ - real_identifiers.declared_locally | \ + old_names = real_identifiers.declared_locally | \ real_identifiers.declared_parameter def inner_visit(nodes): @@ -315,7 +314,8 @@ class FrameIdentifierVisitor(NodeVisitor): # the differences between the two branches are also pulled as # undeclared variables - real_identifiers.undeclared.update(body.symmetric_difference(else_)) + real_identifiers.undeclared.update(body.symmetric_difference(else_) - + real_identifiers.declared) # remember those that are declared. real_identifiers.declared_locally.update(body | else_) diff --git a/jinja2/testsuite/regression.py b/jinja2/testsuite/regression.py index 12e09ff..a39c3cb 100644 --- a/jinja2/testsuite/regression.py +++ b/jinja2/testsuite/regression.py @@ -162,6 +162,19 @@ class BugTestCase(JinjaTestCase): ''') assert t.render(a=0, b=False, c=42, d=42.0) == '1111C' + def test_stacked_locals_scoping_bug_twoframe(self): + t = Template(''' + {% set x = 1 %} + {% for item in foo %} + {% if item == 1 %} + {% set x = 2 %} + {% endif %} + {% endfor %} + {{ x }} + ''') + rv = t.render(foo=[1]).strip() + assert rv == u'1' + def test_call_with_args(self): t = Template("""{% macro dump_users(users) -%}