Merge remote-tracking branch 'origin/3.0.x'
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by David Lord · committed by David Lord · parents 3aeefed, 024fa10 · Browse files · 9 files·+61 added−8 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
9 files+61 added−8 removed
- tests:+37 added−1 removed
- other:+24 added−7 removed
Provenance
- Commit
35c69e9- Tree
132e6b7- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent 1
3aeefed- Parent 2
024fa10
9 files changed+61 added−8 removed
modified:src/jinja2/compiler.py+6 added-1 removed
554 unmodified lines555 self.tests,556 visitor.tests,557 "tests",558 ):559- for name in names:559+ for name in sorted(names):560 if name not in id_map:561 id_map[name] = self.temporary_identifier()562 563 # add check during runtime that dependencies used inside of executed725 unmodified lines1289 self.write(")")1290 self.write(", loop)")1291 self.end_write(frame)1292 1293+ # 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)1297+1298 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 linesmodified:src/jinja2/idtracking.py+1 added-1 removed
147 unmodified lines148 rv: t.Dict[str, str] = {}149 node: t.Optional["Symbols"] = self150 151 while node is not None:152- for name in node.stores:152+ for name in sorted(node.stores):153 if name not in rv:154 rv[name] = self.find_ref(name) # type: ignore155 156 node = node.parent162 unmodified linesmodified:src/jinja2/runtime.py+2 added-1 removed
913 unmodified lines914 __truediv__ = __rtruediv__ = _fail_with_undefined_error915 __floordiv__ = __rfloordiv__ = _fail_with_undefined_error916 __mod__ = __rmod__ = _fail_with_undefined_error917 __pos__ = __neg__ = _fail_with_undefined_error918- __call__ = __getitem__ = __contains__ = _fail_with_undefined_error918+ __call__ = __getitem__ = _fail_with_undefined_error919 __lt__ = __le__ = __gt__ = __ge__ = _fail_with_undefined_error920 __int__ = __float__ = __complex__ = _fail_with_undefined_error921 __pow__ = __rpow__ = _fail_with_undefined_error922 167 unmodified lines1090 1091 __slots__ = ()1092 __iter__ = __str__ = __len__ = Undefined._fail_with_undefined_error1093 __eq__ = __ne__ = __bool__ = __hash__ = Undefined._fail_with_undefined_error1094+ __contains__ = Undefined._fail_with_undefined_error1095 1096 1097 # Remove slots attributes, after the metaclass is applied they are1098 # unneeded and contain wrong data for subclasses.6 unmodified linesmodified:tests/test_api.py+2 added-1 removed
314 unmodified lines315 assert env.from_string("{{ missing is not defined }}").render() == "True"316 assert env.from_string("{{ foo.missing }}").render(foo=42) == ""317 assert env.from_string("{{ not missing }}").render() == "True"318 pytest.raises(UndefinedError, env.from_string("{{ missing - 1}}").render)319- pytest.raises(UndefinedError, env.from_string("{{ 'foo' in missing }}").render)319+ assert env.from_string("{{ 'foo' in missing }}").render() == "False"320 und1 = Undefined(name="x")321 und2 = Undefined(name="y")322 assert und1 == und2323 assert und1 != 4250 unmodified lines374 env = Environment(undefined=StrictUndefined)375 pytest.raises(UndefinedError, env.from_string("{{ missing }}").render)376 pytest.raises(UndefinedError, env.from_string("{{ missing.attribute }}").render)377 pytest.raises(UndefinedError, env.from_string("{{ missing|list }}").render)378+ pytest.raises(UndefinedError, env.from_string("{{ 'foo' in missing }}").render)379 assert env.from_string("{{ missing is not defined }}").render() == "True"380 pytest.raises(381 UndefinedError, env.from_string("{{ foo.missing }}").render, foo=42382 )52 unmodified linesadded:tests/test_compile.py+28 added
1+import os2+import re3+4+from jinja2.environment import Environment5+from jinja2.loaders import DictLoader6+7+8+def test_filters_deterministic(tmp_path):9+ src = "".join(f"{{{{ {i}|filter{i} }}}}" for i in range(10))10+ env = Environment(loader=DictLoader({"foo": src}))11+ env.filters.update(dict.fromkeys((f"filter{i}" for i in range(10)), lambda: None))12+ env.compile_templates(tmp_path, zip=None)13+ name = os.listdir(tmp_path)[0]14+ content = (tmp_path / name).read_text("utf8")15+ expect = [f"filters['filter{i}']" for i in range(10)]16+ found = re.findall(r"filters\['filter\d']", content)17+ assert found == expect18+19+20+def test_import_as_with_context_deterministic(tmp_path):21+ src = "\n".join(f'{{% import "bar" as bar{i} with context %}}' for i in range(10))22+ env = Environment(loader=DictLoader({"foo": src}))23+ env.compile_templates(tmp_path, zip=None)24+ name = os.listdir(tmp_path)[0]25+ content = (tmp_path / name).read_text("utf8")26+ expect = [f"'bar{i}': " for i in range(10)]27+ found = re.findall(r"'bar\d': ", content)[:10]28+ assert found == expectmodified:tests/test_regression.py+7 added
744 unmodified lines745 746 tmpl = env.get_template("base")747 assert tmpl.render() == "42 y"748 749+ 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"755 756+757 @pytest.mark.parametrize("unicode_char", ["\N{FORM FEED}", "\x85"])758 def test_unicode_whitespace(env, unicode_char):759 content = "Lorem ipsum\n" + unicode_char + "\nMore text"760 tmpl = env.from_string(content)1 unmodified linemodified:CHANGES.rst+6 added-1 removed
7 unmodified lines8 9 Version 3.0.210 -------------11 12-Unreleased12+- Fix a loop scoping bug that caused assignments in nested loops13+ to still be referenced outside of it. :issue:`1427`14+- Make ``compile_templates`` deterministic for filter and import15+ names. :issue:`1452, 1453`16+- Revert an unintended change that caused ``Undefined`` to act like17+ ``StrictUndefined`` for the ``in`` operator. :issue:`1448`18 19 20 Version 3.0.121 -------------876 unmodified linesmodified:CONTRIBUTING.rst+8 added-2 removed
90 unmodified lines91 default Pallets remote is "origin".92 93 .. code-block:: text94 95- git remote add fork https://github.com/{username}/jinja95+ $ git remote add fork https://github.com/{username}/jinja96 97 - Create a virtualenv.98 99 .. code-block:: text6 unmodified lines106 .. code-block:: text107 108 > env\Scripts\activate109 110+- Upgrade pip and setuptools.111+112+ .. code-block:: text113+114+ $ python -m pip install --upgrade pip setuptools115+116 - Install the development dependencies, then install Jinja in editable117 mode.118 119 .. code-block:: text23 unmodified lines143 144 .. code-block:: text145 146 $ git fetch origin141- $ git checkout -b your-branch-name origin/1.1.x147+ $ git checkout -b your-branch-name origin/3.0.x148 149 If you're submitting a feature addition or change, branch off of the150 "main" branch.151 71 unmodified linesmodified:README.rst+1 added-1 removed
33 unmodified lines34 .. code-block:: text35 36 $ pip install -U Jinja237 38-.. _pip: https://pip.pypa.io/en/stable/quickstart/38+.. _pip: https://pip.pypa.io/en/stable/getting-started/39 40 41 In A Nutshell42 -------------36 unmodified lines