Skip to content

Commit

Permalink
Merge branch '2.9-maintenance'
Browse files Browse the repository at this point in the history
  • Loading branch information
mitsuhiko committed Jan 8, 2017
2 parents 1e11737 + 8172db0 commit c1b4469
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
16 changes: 14 additions & 2 deletions jinja2/idtracking.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,22 @@ def copy(self):
return rv

def store(self, name):
# We already have that name locally, so we can just bail
self.stores.add(name)

# If we have not see the name referenced yet, we need to figure
# out what to set it to.
if name not in self.refs:
# If there is a parent scope we check if the name has a
# reference there. If it does it means we might have to alias
# to a variable there.
if self.parent is not None:
outer_ref = self.parent.find_ref(name)
if outer_ref is not None:
self._define_ref(name, load=(VAR_LOAD_ALIAS, outer_ref))
return

# Otherwise we can just set it to undefined.
self._define_ref(name, load=(VAR_LOAD_UNDEFINED, None))
self.stores.add(name)

def declare_parameter(self, name):
self.stores.add(name)
Expand Down
9 changes: 9 additions & 0 deletions tests/test_core_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,15 @@ def test_unpacking(self, env):
'{{ a }}|{{ b }}|{{ c }}{% endfor %}')
assert tmpl.render() == '1|2|3'

def test_intended_scoping_with_set(self, env):
tmpl = env.from_string('{% for item in seq %}{{ x }}'
'{% set x = item %}{{ x }}{% endfor %}')
assert tmpl.render(x=0, seq=[1, 2, 3]) == '010203'

tmpl = env.from_string('{% set x = 9 %}{% for item in seq %}{{ x }}'
'{% set x = item %}{{ x }}{% endfor %}')
assert tmpl.render(x=0, seq=[1, 2, 3]) == '919293'


@pytest.mark.core_tags
@pytest.mark.if_condition
Expand Down

0 comments on commit c1b4469

Please sign in to comment.