From 8a672512e7275985b8bc4be24b470c99a1be79ec Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Mon, 5 Apr 2010 18:43:07 +0200 Subject: [PATCH] Fixed a scoping bug that caused the immutable scoping rule to be ignored in a few edge cases. This clauses #376. --HG-- branch : trunk --- jinja2/compiler.py | 6 +++--- jinja2/testsuite/regression.py | 13 +++++++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) 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) -%}