Skip to content
Myr

Merge pull request #1449 from amy-lei/loop-scoping

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

fix loop scoping bug

authored by David Lord · committed by GitHub · parents d4e5112, f3bc9c6 · Browse files · 3 files+15 added−0 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+15 added−0 removed

  • tests:+7 added−0 removed
  • other:+8 added−0 removed

3 files+15 added−0 removed

modified:src/jinja2/compiler.py+5 added
1288 unmodified lines
1289 self.write(")")1290 self.write(", loop)")1291 self.end_write(frame)12921293 # at the end of the iteration, clear any assignments made in the1294 # loop from the top level1295 if self._assign_stack:1296 self._assign_stack[-1].difference_update(loop_frame.symbols.stores)12971298 def visit_If(self, node: nodes.If, frame: Frame) -> None:1299 if_frame = frame.soft()1300 self.writeline("if ", node)1301 self.visit(node.test, if_frame)
658 unmodified lines
modified:tests/test_regression.py+7 added
744 unmodified lines
745746 tmpl = env.get_template("base")747 assert tmpl.render() == "42 y"748749 def test_nested_loop_scoping(self, env):750 tmpl = env.from_string(751 "{% set output %}{% for x in [1,2,3] %}hello{% endfor %}"752 "{% endset %}{{ output }}"753 )754 assert tmpl.render() == "hellohellohello"755756757@pytest.mark.parametrize("unicode_char", ["\N{FORM FEED}", "\x85"])758def test_unicode_whitespace(env, unicode_char):759 content = "Lorem ipsum\n" + unicode_char + "\nMore text"760 tmpl = env.from_string(content)
1 unmodified line
modified:CHANGES.rst+3 added
3 unmodified lines
4-------------56Unreleased78- Fix a loop scoping bug that caused assignments in nested loops9 to still be referenced outside of it. :issue:`1427`101112Version 3.0.113-------------1415Released 2021-05-18
874 unmodified lines