Skip to content
Myr

Fix bug involving calling set on a template parameter within all branches of an if block (#1665)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by David Lord · committed by GitHub · parents fbc3a69, 66587ce · Browse files · 3 files+20 added−10 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+20 added−10 removed

  • tests:+10 added−0 removed
  • other:+10 added−10 removed

3 files+20 added−10 removed

modified:src/jinja2/idtracking.py+7 added-10 removed
119 unmodified lines
120 if self.find_ref(name) is None:121 self._define_ref(name, load=(VAR_LOAD_RESOLVE, name))122123 def branch_update(self, branch_symbols: t.Sequence["Symbols"]) -> None:124 stores: t.Dict[str, int] = {}124 stores: t.Set[str] = set()125126 for branch in branch_symbols:126 for target in branch.stores:127 if target in self.stores:128 continue129 stores[target] = stores.get(target, 0) + 1127 stores.update(branch.stores)128129 stores.difference_update(self.stores)130131 for sym in branch_symbols:132 self.refs.update(sym.refs)133 self.loads.update(sym.loads)134 self.stores.update(sym.stores)135136 for name, branch_count in stores.items():137 if branch_count == len(branch_symbols):138 continue139140 target = self.find_ref(name) # type: ignore136 for name in stores:137 target = self.find_ref(name)138 assert target is not None, "should not happen"139140 if self.parent is not None:141 outer_target = self.parent.find_ref(name)
177 unmodified lines
modified:tests/test_regression.py+10 added
748 unmodified lines
749 )750 assert tmpl.render() == "foo"751752753def test_load_parameter_when_set_in_all_if_branches(env):754 tmpl = env.from_string(755 "{% if True %}{{ a.b }}{% set a = 1 %}"756 "{% elif False %}{% set a = 2 %}"757 "{% else %}{% set a = 3 %}{% endif %}"758 "{{ a }}"759 )760 assert tmpl.render(a={"b": 0}) == "01"761762763@pytest.mark.parametrize("unicode_char", ["\N{FORM FEED}", "\x85"])764def test_unicode_whitespace(env, unicode_char):765 content = "Lorem ipsum\n" + unicode_char + "\nMore text"766 tmpl = env.from_string(content)
1 unmodified line
modified:CHANGES.rst+3 added
43 unmodified lines
44- Tests decorated with `@pass_context`` can be used with the ``|select``45 filter. :issue:`1624`46- Using ``set`` for multiple assignment (``a, b = 1, 2``) does not fail when the47 target is a namespace attribute. :issue:`1413`48- Using ``set`` in all branches of ``{% if %}{% elif %}{% else %}`` blocks49 does not cause the variable to be considered initially undefined.50 :issue:`1253`515253Version 3.1.454-------------
994 unmodified lines