Skip to content
Myr

test_async.py

This file

Path

tests/test_async.py

660 lines · 21.7 KiB · Python

Last changed by

Merge pull request #1614 from pallets/async-env-filterDavid Lordbefore adoption20eb7f5

History of this file

Take it

The walk stopped after 200 commits: lines older than 6c1a62f point at it.

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

lines 1–2why, one step back
1import asyncio
2

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 3–4why, one step back
3import pytest
4

Merge pull request #1294 from Jafnee/chainableundefined-aiter-bugDavid Lordbefore adoption06c7c6f

line 5why, one step back
5from jinja2 import ChainableUndefined

Merge pull request #1133 from pallets/revert-renameDavid Lordbefore adoption15337f7

lines 6–8why, one step back
6from jinja2 import DictLoader
7from jinja2 import Environment8from jinja2 import Template

Merge pull request #1392 from pallets/inline-asyncDavid Lordbefore adoption9d4689b

line 9why, one step back
9from jinja2.async_utils import auto_aiter

Merge pull request #1133 from pallets/revert-renameDavid Lordbefore adoption15337f7

lines 10–12why, one step back
10from jinja2.exceptions import TemplateNotFound
11from jinja2.exceptions import TemplatesNotFound12from jinja2.exceptions import UndefinedError

Merge pull request #1363 from remidebette/add-native-async-supportDavid Lordbefore adoption59de6fb

line 13why, one step back
13from jinja2.nativetypes import NativeEnvironment

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

lines 14–16why, one step back
14
1516def test_basic_async():

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 17–20why, one step back
17 t = Template(
18 "{% for item in [1, 2, 3] %}[{{ item }}]{% endfor %}", enable_async=True19 )20

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

lines 21–23why, one step back
21 async def func():
22 return await t.render_async()23

Merge pull request #1534 from pallets/drop-python-3.6David Lordbefore adoptionb2bc7be

line 24why, one step back
24 rv = asyncio.run(func())

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

line 25why, one step back
25 assert rv == "[1][2][3]"

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

lines 26–28why, one step back
26
2728def test_await_on_calls():

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

line 29why, one step back
29 t = Template("{{ async_func() + normal_func() }}", enable_async=True)

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

lines 30–37why, one step back
30
31 async def async_func():32 return 423334 def normal_func():35 return 233637 async def func():

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

line 38why, one step back
38 return await t.render_async(async_func=async_func, normal_func=normal_func)

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

line 39why, one step back
39

Merge pull request #1534 from pallets/drop-python-3.6David Lordbefore adoptionb2bc7be

line 40why, one step back
40 rv = asyncio.run(func())

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

line 41why, one step back
41 assert rv == "65"

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

lines 42–44why, one step back
42
4344def test_await_on_calls_normal_render():

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

line 45why, one step back
45 t = Template("{{ async_func() + normal_func() }}", enable_async=True)

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

lines 46–52why, one step back
46
47 async def async_func():48 return 424950 def normal_func():51 return 2352

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 53–54why, one step back
53 rv = t.render(async_func=async_func, normal_func=normal_func)
54 assert rv == "65"

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

lines 55–57why, one step back
55
5657def test_await_and_macros():

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 58–61why, one step back
58 t = Template(
59 "{% macro foo(x) %}[{{ x }}][{{ async_func() }}]{% endmacro %}{{ foo(42) }}",60 enable_async=True,61 )

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

lines 62–68why, one step back
62
63 async def async_func():64 return 426566 async def func():67 return await t.render_async(async_func=async_func)68

Merge pull request #1534 from pallets/drop-python-3.6David Lordbefore adoptionb2bc7be

line 69why, one step back
69 rv = asyncio.run(func())

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

line 70why, one step back
70 assert rv == "[42][42]"

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

lines 71–73why, one step back
71
7273def test_async_blocks():

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 74–79why, one step back
74 t = Template(
75 "{% block foo %}<Test>{% endblock %}{{ self.foo() }}",76 enable_async=True,77 autoescape=True,78 )79

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

lines 80–82why, one step back
80 async def func():
81 return await t.render_async()82

Merge pull request #1534 from pallets/drop-python-3.6David Lordbefore adoptionb2bc7be

line 83why, one step back
83 rv = asyncio.run(func())

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

line 84why, one step back
84 assert rv == "<Test><Test>"

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

lines 85–87why, one step back
85
8687def test_async_generate():

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

line 88why, one step back
88 t = Template("{% for x in [1, 2, 3] %}{{ x }}{% endfor %}", enable_async=True)

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

line 89why, one step back
89 rv = list(t.generate())

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

line 90why, one step back
90 assert rv == ["1", "2", "3"]

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

lines 91–93why, one step back
91
9293def test_async_iteration_in_templates():

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 94–95why, one step back
94 t = Template("{% for x in rng %}{{ x }}{% endfor %}", enable_async=True)
95

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

lines 96–98why, one step back
96 async def async_iterator():
97 for item in [1, 2, 3]:98 yield item

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

line 99why, one step back
99

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

line 100why, one step back
100 rv = list(t.generate(rng=async_iterator()))

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

line 101why, one step back
101 assert rv == ["1", "2", "3"]

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

lines 102–114why, one step back
102
103104def test_async_iteration_in_templates_extended():105 t = Template(106 "{% for x in rng %}{{ loop.index0 }}/{{ x }}{% endfor %}", enable_async=True107 )108 stream = t.generate(rng=auto_aiter(range(1, 4)))109 assert next(stream) == "0"110 assert "".join(stream) == "/11/22/3"111112113@pytest.fixture114def test_env_async():

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 115–125why, one step back
115 env = Environment(
116 loader=DictLoader(117 dict(118 module="{% macro test() %}[{{ foo }}|{{ bar }}]{% endmacro %}",119 header="[{{ foo }}|{{ 23 }}]",120 o_printer="({{ o }})",121 )122 ),123 enable_async=True,124 )125 env.globals["bar"] = 23

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

lines 126–128why, one step back
126 return env
127128

Merge pull request #1136 from pallets/drop-python2David Lordbefore adoption3915eb5

line 129why, one step back
129class TestAsyncImports:

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

lines 130–131why, one step back
130 def test_context_imports(self, test_env_async):
131 t = test_env_async.from_string('{% import "module" as m %}{{ m.test() }}')

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

line 132why, one step back
132 assert t.render(foo=42) == "[|23]"

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

lines 133–135why, one step back
133 t = test_env_async.from_string(
134 '{% import "module" as m without context %}{{ m.test() }}'135 )

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

line 136why, one step back
136 assert t.render(foo=42) == "[|23]"

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

lines 137–139why, one step back
137 t = test_env_async.from_string(
138 '{% import "module" as m with context %}{{ m.test() }}'139 )

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

line 140why, one step back
140 assert t.render(foo=42) == "[42|23]"

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

line 141why, one step back
141 t = test_env_async.from_string('{% from "module" import test %}{{ test() }}')

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

line 142why, one step back
142 assert t.render(foo=42) == "[|23]"

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

lines 143–145why, one step back
143 t = test_env_async.from_string(
144 '{% from "module" import test without context %}{{ test() }}'145 )

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

line 146why, one step back
146 assert t.render(foo=42) == "[|23]"

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

lines 147–149why, one step back
147 t = test_env_async.from_string(
148 '{% from "module" import test with context %}{{ test() }}'149 )

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

line 150why, one step back
150 assert t.render(foo=42) == "[42|23]"

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

lines 151–159why, one step back
151
152 def test_trailing_comma(self, test_env_async):153 test_env_async.from_string('{% from "foo" import bar, baz with context %}')154 test_env_async.from_string('{% from "foo" import bar, baz, with context %}')155 test_env_async.from_string('{% from "foo" import bar, with context %}')156 test_env_async.from_string('{% from "foo" import bar, with, context %}')157 test_env_async.from_string('{% from "foo" import bar, with with context %}')158159 def test_exports(self, test_env_async):

Merge pull request #1534 from pallets/drop-python-3.6David Lordbefore adoptionb2bc7be

lines 160–161why, one step back
160 coro = test_env_async.from_string(
161 """

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

lines 162–167why, one step back
162 {% macro toplevel() %}...{% endmacro %}
163 {% macro __private() %}...{% endmacro %}164 {% set variable = 42 %}165 {% for item in [1] %}166 {% macro notthere() %}{% endmacro %}167 {% endfor %}

Merge pull request #1534 from pallets/drop-python-3.6David Lordbefore adoptionb2bc7be

lines 168–171why, one step back
168 """
169 )._get_default_module_async()170 m = asyncio.run(coro)171 assert asyncio.run(m.toplevel()) == "..."

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

line 172why, one step back
172 assert not hasattr(m, "__missing")

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

line 173why, one step back
173 assert m.variable == 42

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

line 174why, one step back
174 assert not hasattr(m, "notthere")

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

line 175why, one step back
175

Merge branch '3.0.x'David Lordbefore adoptionf20a9c9

lines 176–198why, one step back
176 def test_import_with_globals(self, test_env_async):
177 t = test_env_async.from_string(178 '{% import "module" as m %}{{ m.test() }}', globals={"foo": 42}179 )180 assert t.render() == "[42|23]"181182 t = test_env_async.from_string('{% import "module" as m %}{{ m.test() }}')183 assert t.render() == "[|23]"184185 def test_import_with_globals_override(self, test_env_async):186 t = test_env_async.from_string(187 '{% set foo = 41 %}{% import "module" as m %}{{ m.test() }}',188 globals={"foo": 42},189 )190 assert t.render() == "[42|23]"191192 def test_from_import_with_globals(self, test_env_async):193 t = test_env_async.from_string(194 '{% from "module" import test %}{{ test() }}',195 globals={"foo": 42},196 )197 assert t.render() == "[42|23]"198

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

line 199why, one step back
199

Merge pull request #1136 from pallets/drop-python2David Lordbefore adoption3915eb5

line 200why, one step back
200class TestAsyncIncludes:

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

lines 201–202why, one step back
201 def test_context_include(self, test_env_async):
202 t = test_env_async.from_string('{% include "header" %}')

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

line 203why, one step back
203 assert t.render(foo=42) == "[42|23]"

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

line 204why, one step back
204 t = test_env_async.from_string('{% include "header" with context %}')

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

line 205why, one step back
205 assert t.render(foo=42) == "[42|23]"

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

line 206why, one step back
206 t = test_env_async.from_string('{% include "header" without context %}')

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

line 207why, one step back
207 assert t.render(foo=42) == "[|23]"

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

lines 208–210why, one step back
208
209 def test_choice_includes(self, test_env_async):210 t = test_env_async.from_string('{% include ["missing", "header"] %}')

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

line 211why, one step back
211 assert t.render(foo=42) == "[42|23]"

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

lines 212–215why, one step back
212
213 t = test_env_async.from_string(214 '{% include ["missing", "missing2"] ignore missing %}'215 )

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

line 216why, one step back
216 assert t.render(foo=42) == ""

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

lines 217–222why, one step back
217
218 t = test_env_async.from_string('{% include ["missing", "missing2"] %}')219 pytest.raises(TemplateNotFound, t.render)220 with pytest.raises(TemplatesNotFound) as e:221 t.render()222

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 223–224why, one step back
223 assert e.value.templates == ["missing", "missing2"]
224 assert e.value.name == "missing2"

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

lines 225–226why, one step back
225
226 def test_includes(t, **ctx):

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 227–228why, one step back
227 ctx["foo"] = 42
228 assert t.render(ctx) == "[42|23]"

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

lines 229–231why, one step back
229
230 t = test_env_async.from_string('{% include ["missing", "header"] %}')231 test_includes(t)

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 232–233why, one step back
232 t = test_env_async.from_string("{% include x %}")
233 test_includes(t, x=["missing", "header"])

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

line 234why, one step back
234 t = test_env_async.from_string('{% include [x, "header"] %}')

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 235–241why, one step back
235 test_includes(t, x="missing")
236 t = test_env_async.from_string("{% include x %}")237 test_includes(t, x="header")238 t = test_env_async.from_string("{% include x %}")239 test_includes(t, x="header")240 t = test_env_async.from_string("{% include [x] %}")241 test_includes(t, x="header")

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

lines 242–245why, one step back
242
243 def test_include_ignoring_missing(self, test_env_async):244 t = test_env_async.from_string('{% include "missing" %}')245 pytest.raises(TemplateNotFound, t.render)

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 246–250why, one step back
246 for extra in "", "with context", "without context":
247 t = test_env_async.from_string(248 '{% include "missing" ignore missing ' + extra + " %}"249 )250 assert t.render() == ""

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

lines 251–252why, one step back
251
252 def test_context_include_with_overrides(self, test_env_async):

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 253–260why, one step back
253 env = Environment(
254 loader=DictLoader(255 dict(256 main="{% for item in [1, 2, 3] %}{% include 'item' %}{% endfor %}",257 item="{{ item }}",258 )259 )260 )

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

lines 261–263why, one step back
261 assert env.get_template("main").render() == "123"
262263 def test_unoptimized_scopes(self, test_env_async):

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 264–265why, one step back
264 t = test_env_async.from_string(
265 """

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

lines 266–272why, one step back
266 {% macro outer(o) %}
267 {% macro inner() %}268 {% include "o_printer" %}269 {% endmacro %}270 {{ inner() }}271 {% endmacro %}272 {{ outer("FOO") }}

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 273–275why, one step back
273 """
274 )275 assert t.render().strip() == "(FOO)"

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

lines 276–277why, one step back
276
277 def test_unoptimized_scopes_autoescape(self):

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

line 278why, one step back
278 env = Environment(

Merge pull request #1310 from pallets/pre-commit-ci-update-configDavid Lordbefore adoption1cad38c

line 279why, one step back
279 loader=DictLoader({"o_printer": "({{ o }})"}),

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 280–284why, one step back
280 autoescape=True,
281 enable_async=True,282 )283 t = env.from_string(284 """

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

lines 285–291why, one step back
285 {% macro outer(o) %}
286 {% macro inner() %}287 {% include "o_printer" %}288 {% endmacro %}289 {{ inner() }}290 {% endmacro %}291 {{ outer("FOO") }}

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 292–294why, one step back
292 """
293 )294 assert t.render().strip() == "(FOO)"

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

lines 295–296why, one step back
295
296

Merge pull request #1136 from pallets/drop-python2David Lordbefore adoption3915eb5

line 297why, one step back
297class TestAsyncForLoop:

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

line 298why, one step back
298 def test_simple(self, test_env_async):

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 299–300why, one step back
299 tmpl = test_env_async.from_string("{% for item in seq %}{{ item }}{% endfor %}")
300 assert tmpl.render(seq=list(range(10))) == "0123456789"

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

lines 301–303why, one step back
301
302 def test_else(self, test_env_async):303 tmpl = test_env_async.from_string(

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 304–306why, one step back
304 "{% for item in seq %}XXX{% else %}...{% endfor %}"
305 )306 assert tmpl.render() == "..."

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

lines 307–308why, one step back
307
308 def test_empty_blocks(self, test_env_async):

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 309–312why, one step back
309 tmpl = test_env_async.from_string(
310 "<{% for item in seq %}{% else %}{% endfor %}>"311 )312 assert tmpl.render() == "<>"

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

lines 313–326why, one step back
313
314 @pytest.mark.parametrize(315 "transform", [lambda x: x, iter, reversed, lambda x: (i for i in x), auto_aiter]316 )317 def test_context_vars(self, test_env_async, transform):318 t = test_env_async.from_string(319 "{% for item in seq %}{{ loop.index }}|{{ loop.index0 }}"320 "|{{ loop.revindex }}|{{ loop.revindex0 }}|{{ loop.first }}"321 "|{{ loop.last }}|{{ loop.length }}\n{% endfor %}"322 )323 out = t.render(seq=transform([42, 24]))324 assert out == "1|0|2|1|True|False|2\n2|1|1|0|False|True|2\n"325326 def test_cycling(self, test_env_async):

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 327–328why, one step back
327 tmpl = test_env_async.from_string(
328 """{% for item in seq %}{{

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

line 329why, one step back
329 loop.cycle('<1>', '<2>') }}{% endfor %}{%

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 330–333why, one step back
330 for item in seq %}{{ loop.cycle(*through) }}{% endfor %}"""
331 )332 output = tmpl.render(seq=list(range(4)), through=("<1>", "<2>"))333 assert output == "<1><2>" * 4

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

lines 334–335why, one step back
334
335 def test_lookaround(self, test_env_async):

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 336–337why, one step back
336 tmpl = test_env_async.from_string(
337 """{% for item in seq -%}

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

lines 338–339why, one step back
338 {{ loop.previtem|default('x') }}-{{ item }}-{{
339 loop.nextitem|default('x') }}|

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 340–341why, one step back
340 {%- endfor %}"""
341 )

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

line 342why, one step back
342 output = tmpl.render(seq=list(range(4)))

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

line 343why, one step back
343 assert output == "x-0-1|0-1-2|1-2-3|2-3-x|"

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

lines 344–345why, one step back
344
345 def test_changed(self, test_env_async):

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 346–347why, one step back
346 tmpl = test_env_async.from_string(
347 """{% for item in seq -%}

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

line 348why, one step back
348 {{ loop.changed(item) }},

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 349–350why, one step back
349 {%- endfor %}"""
350 )

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

line 351why, one step back
351 output = tmpl.render(seq=[None, None, 1, 2, 2, 3, 4, 4, 4])

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

line 352why, one step back
352 assert output == "True,False,True,True,False,True,True,False,False,"

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

lines 353–354why, one step back
353
354 def test_scope(self, test_env_async):

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

line 355why, one step back
355 tmpl = test_env_async.from_string("{% for item in seq %}{% endfor %}{{ item }}")

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

lines 356–360why, one step back
356 output = tmpl.render(seq=list(range(10)))
357 assert not output358359 def test_varlen(self, test_env_async):360 def inner():

Merge pull request #1136 from pallets/drop-python2David Lordbefore adoption3915eb5

line 361why, one step back
361 yield from range(5)

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 362–365why, one step back
362
363 tmpl = test_env_async.from_string(364 "{% for item in iter %}{{ item }}{% endfor %}"365 )

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

line 366why, one step back
366 output = tmpl.render(iter=inner())

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

line 367why, one step back
367 assert output == "01234"

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

lines 368–369why, one step back
368
369 def test_noniter(self, test_env_async):

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

line 370why, one step back
370 tmpl = test_env_async.from_string("{% for item in none %}...{% endfor %}")

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

lines 371–373why, one step back
371 pytest.raises(TypeError, tmpl.render)
372373 def test_recursive(self, test_env_async):

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 374–375why, one step back
374 tmpl = test_env_async.from_string(
375 """{% for item in seq recursive -%}

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

line 376why, one step back
376 [{{ item.a }}{% if item.b %}<{{ loop(item.b) }}>{% endif %}]

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 377–388why, one step back
377 {%- endfor %}"""
378 )379 assert (380 tmpl.render(381 seq=[382 dict(a=1, b=[dict(a=1), dict(a=2)]),383 dict(a=2, b=[dict(a=1), dict(a=2)]),384 dict(a=3, b=[dict(a="a")]),385 ]386 )387 == "[1<[1][2]>][2<[1][2]>][3<[a]>]"388 )

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

lines 389–390why, one step back
389
390 def test_recursive_lookaround(self, test_env_async):

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 391–392why, one step back
391 tmpl = test_env_async.from_string(
392 """{% for item in seq recursive -%}

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

lines 393–395why, one step back
393 [{{ loop.previtem.a if loop.previtem is defined else 'x' }}.{{
394 item.a }}.{{ loop.nextitem.a if loop.nextitem is defined else 'x'395 }}{% if item.b %}<{{ loop(item.b) }}>{% endif %}]

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 396–407why, one step back
396 {%- endfor %}"""
397 )398 assert (399 tmpl.render(400 seq=[401 dict(a=1, b=[dict(a=1), dict(a=2)]),402 dict(a=2, b=[dict(a=1), dict(a=2)]),403 dict(a=3, b=[dict(a="a")]),404 ]405 )406 == "[x.1.2<[x.1.2][1.2.x]>][1.2.3<[x.1.2][1.2.x]>][2.3.x<[x.a.x]>]"407 )

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

lines 408–409why, one step back
408
409 def test_recursive_depth0(self, test_env_async):

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 410–423why, one step back
410 tmpl = test_env_async.from_string(
411 "{% for item in seq recursive %}[{{ loop.depth0 }}:{{ item.a }}"412 "{% if item.b %}<{{ loop(item.b) }}>{% endif %}]{% endfor %}"413 )414 assert (415 tmpl.render(416 seq=[417 dict(a=1, b=[dict(a=1), dict(a=2)]),418 dict(a=2, b=[dict(a=1), dict(a=2)]),419 dict(a=3, b=[dict(a="a")]),420 ]421 )422 == "[0:1<[1:1][1:2]>][0:2<[1:1][1:2]>][0:3<[1:a]>]"423 )

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

lines 424–425why, one step back
424
425 def test_recursive_depth(self, test_env_async):

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 426–439why, one step back
426 tmpl = test_env_async.from_string(
427 "{% for item in seq recursive %}[{{ loop.depth }}:{{ item.a }}"428 "{% if item.b %}<{{ loop(item.b) }}>{% endif %}]{% endfor %}"429 )430 assert (431 tmpl.render(432 seq=[433 dict(a=1, b=[dict(a=1), dict(a=2)]),434 dict(a=2, b=[dict(a=1), dict(a=2)]),435 dict(a=3, b=[dict(a="a")]),436 ]437 )438 == "[1:1<[2:1][2:2]>][1:2<[2:1][2:2]>][1:3<[2:a]>]"439 )

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

lines 440–441why, one step back
440
441 def test_looploop(self, test_env_async):

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 442–443why, one step back
442 tmpl = test_env_async.from_string(
443 """{% for row in table %}

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

lines 444–447why, one step back
444 {%- set rowloop = loop -%}
445 {% for cell in row -%}446 [{{ rowloop.index }}|{{ loop.index }}]447 {%- endfor %}

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 448–450why, one step back
448 {%- endfor %}"""
449 )450 assert tmpl.render(table=["ab", "cd"]) == "[1|1][1|2][2|1][2|2]"

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

lines 451–452why, one step back
451
452 def test_reversed_bug(self, test_env_async):

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 453–458why, one step back
453 tmpl = test_env_async.from_string(
454 "{% for i in items %}{{ i }}"455 "{% if not loop.last %}"456 ",{% endif %}{% endfor %}"457 )458 assert tmpl.render(items=reversed([3, 2, 1])) == "1,2,3"

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

lines 459–460why, one step back
459
460 def test_loop_errors(self, test_env_async):

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 461–464why, one step back
461 tmpl = test_env_async.from_string(
462 """{% for item in [1] if loop.index463 == 0 %}...{% endfor %}"""464 )

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

line 465why, one step back
465 pytest.raises(UndefinedError, tmpl.render)

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 466–470why, one step back
466 tmpl = test_env_async.from_string(
467 """{% for item in [] %}...{% else468 %}{{ loop }}{% endfor %}"""469 )470 assert tmpl.render() == ""

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

lines 471–472why, one step back
471
472 def test_loop_filter(self, test_env_async):

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 473–478why, one step back
473 tmpl = test_env_async.from_string(
474 "{% for item in range(10) if item is even %}[{{ item }}]{% endfor %}"475 )476 assert tmpl.render() == "[0][2][4][6][8]"477 tmpl = test_env_async.from_string(478 """

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

line 479why, one step back
479 {%- for item in range(10) if item is even %}[{{

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 480–482why, one step back
480 loop.index }}:{{ item }}]{% endfor %}"""
481 )482 assert tmpl.render() == "[1:0][2:2][3:4][4:6][5:8]"

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

lines 483–485why, one step back
483
484 def test_scoped_special_var(self, test_env_async):485 t = test_env_async.from_string(

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 486–489why, one step back
486 "{% for s in seq %}[{{ loop.first }}{% for c in s %}"
487 "|{{ loop.first }}{% endfor %}]{% endfor %}"488 )489 assert t.render(seq=("ab", "cd")) == "[True|True|False][False|True|False]"

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

lines 490–491why, one step back
490
491 def test_scoped_loop_var(self, test_env_async):

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 492–501why, one step back
492 t = test_env_async.from_string(
493 "{% for x in seq %}{{ loop.first }}"494 "{% for y in seq %}{% endfor %}{% endfor %}"495 )496 assert t.render(seq="ab") == "TrueFalse"497 t = test_env_async.from_string(498 "{% for x in seq %}{% for y in seq %}"499 "{{ loop.first }}{% endfor %}{% endfor %}"500 )501 assert t.render(seq="ab") == "TrueFalseTrueFalse"

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

lines 502–503why, one step back
502
503 def test_recursive_empty_loop_iter(self, test_env_async):

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 504–505why, one step back
504 t = test_env_async.from_string(
505 """

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

line 506why, one step back
506 {%- for item in foo recursive -%}{%- endfor -%}

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 507–509why, one step back
507 """
508 )509 assert t.render(dict(foo=[])) == ""

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

lines 510–511why, one step back
510
511 def test_call_in_loop(self, test_env_async):

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 512–513why, one step back
512 t = test_env_async.from_string(
513 """

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

lines 514–522why, one step back
514 {%- macro do_something() -%}
515 [{{ caller() }}]516 {%- endmacro %}517518 {%- for i in [1, 2, 3] %}519 {%- call do_something() -%}520 {{ i }}521 {%- endcall %}522 {%- endfor -%}

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 523–525why, one step back
523 """
524 )525 assert t.render() == "[1][2][3]"

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

lines 526–527why, one step back
526
527 def test_scoping_bug(self, test_env_async):

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 528–529why, one step back
528 t = test_env_async.from_string(
529 """

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

lines 530–532why, one step back
530 {%- for item in foo %}...{{ item }}...{% endfor %}
531 {%- macro item(a) %}...{{ a }}...{% endmacro %}532 {{- item(2) -}}

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 533–535why, one step back
533 """
534 )535 assert t.render(foo=(1,)) == "...1......2..."

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

lines 536–537why, one step back
536
537 def test_unpacking(self, test_env_async):

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 538–541why, one step back
538 tmpl = test_env_async.from_string(
539 "{% for a, b, c in [[1, 2, 3]] %}{{ a }}|{{ b }}|{{ c }}{% endfor %}"540 )541 assert tmpl.render() == "1|2|3"

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

lines 542–543why, one step back
542
543 def test_recursive_loop_filter(self, test_env_async):

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 544–545why, one step back
544 t = test_env_async.from_string(
545 """

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

lines 546–552why, one step back
546 <?xml version="1.0" encoding="UTF-8"?>
547 <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">548 {%- for page in [site.root] if page.url != this recursive %}549 <url><loc>{{ page.url }}</loc></url>550 {{- loop(page.children) }}551 {%- endfor %}552 </urlset>

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 553–558why, one step back
553 """
554 )555 sm = t.render(556 this="/foo",557 site={"root": {"url": "/", "children": [{"url": "/foo"}, {"url": "/bar"}]}},558 )

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

lines 559–562why, one step back
559 lines = [x.strip() for x in sm.splitlines() if x.strip()]
560 assert lines == [561 '<?xml version="1.0" encoding="UTF-8"?>',562 '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">',

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 563–565why, one step back
563 "<url><loc>/</loc></url>",
564 "<url><loc>/bar</loc></url>",565 "</urlset>",

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

lines 566–568why, one step back
566 ]
567568 def test_nonrecursive_loop_filter(self, test_env_async):

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 569–570why, one step back
569 t = test_env_async.from_string(
570 """

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

lines 571–576why, one step back
571 <?xml version="1.0" encoding="UTF-8"?>
572 <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">573 {%- for page in items if page.url != this %}574 <url><loc>{{ page.url }}</loc></url>575 {%- endfor %}576 </urlset>

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 577–581why, one step back
577 """
578 )579 sm = t.render(580 this="/foo", items=[{"url": "/"}, {"url": "/foo"}, {"url": "/bar"}]581 )

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

lines 582–585why, one step back
582 lines = [x.strip() for x in sm.splitlines() if x.strip()]
583 assert lines == [584 '<?xml version="1.0" encoding="UTF-8"?>',585 '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">',

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

lines 586–588why, one step back
586 "<url><loc>/</loc></url>",
587 "<url><loc>/bar</loc></url>",588 "</urlset>",

Merge pull request #1109 from pallets/visit-output-refactorDavid Lordbefore adoption6c1a62f

lines 589–592why, one step back
589 ]
590591 def test_bare_async(self, test_env_async):592 t = test_env_async.from_string('{% extends "header" %}')

Merge pull request #1125 from pallets/styleDavid Lordbefore adoption42d4a4e

line 593why, one step back
593 assert t.render(foo=42) == "[42|23]"

Merge branch '2.11.x'David Lordbefore adoption547e6e3

lines 594–597why, one step back
594
595 def test_awaitable_property_slicing(self, test_env_async):596 t = test_env_async.from_string("{% for x in a.b[:1] %}{{ x }}{% endfor %}")597 assert t.render(a=dict(b=[1, 2, 3])) == "1"

Merge branch '2.11.x'David Lordbefore adoption0a37031

lines 598–606why, one step back
598
599600def test_namespace_awaitable(test_env_async):601 async def _test():602 t = test_env_async.from_string(603 '{% set ns = namespace(foo="Bar") %}{{ ns.foo }}'604 )605 actual = await t.render_async()606 assert actual == "Bar"

Merge pull request #1294 from Jafnee/chainableundefined-aiter-bugDavid Lordbefore adoption06c7c6f

line 607why, one step back
607

Merge pull request #1534 from pallets/drop-python-3.6David Lordbefore adoptionb2bc7be

line 608why, one step back
608 asyncio.run(_test())

Merge pull request #1294 from Jafnee/chainableundefined-aiter-bugDavid Lordbefore adoption06c7c6f

lines 609–619why, one step back
609
610611def test_chainable_undefined_aiter():612 async def _test():613 t = Template(614 "{% for x in a['b']['c'] %}{{ x }}{% endfor %}",615 enable_async=True,616 undefined=ChainableUndefined,617 )618 rv = await t.render_async(a={})619 assert rv == ""

Merge pull request #1363 from remidebette/add-native-async-supportDavid Lordbefore adoption59de6fb

line 620why, one step back
620

Merge pull request #1534 from pallets/drop-python-3.6David Lordbefore adoptionb2bc7be

line 621why, one step back
621 asyncio.run(_test())

Merge pull request #1363 from remidebette/add-native-async-supportDavid Lordbefore adoption59de6fb

lines 622–634why, one step back
622
623624@pytest.fixture625def async_native_env():626 return NativeEnvironment(enable_async=True)627628629def test_native_async(async_native_env):630 async def _test():631 t = async_native_env.from_string("{{ x }}")632 rv = await t.render_async(x=23)633 assert rv == 23634

Merge pull request #1534 from pallets/drop-python-3.6David Lordbefore adoptionb2bc7be

line 635why, one step back
635 asyncio.run(_test())

Merge pull request #1363 from remidebette/add-native-async-supportDavid Lordbefore adoption59de6fb

lines 636–642why, one step back
636
637638def test_native_list_async(async_native_env):639 async def _test():640 t = async_native_env.from_string("{{ x }}")641 rv = await t.render_async(x=list(range(3)))642 assert rv == [0, 1, 2]

Merge branch '2.11.x'David Lordbefore adoption0a37031

line 643why, one step back
643

Merge pull request #1534 from pallets/drop-python-3.6David Lordbefore adoptionb2bc7be

line 644why, one step back
644 asyncio.run(_test())

Merge pull request #1614 from pallets/async-env-filterDavid Lordbefore adoption20eb7f5

lines 645–660why, one step back
645
646647def test_getitem_after_filter():648 env = Environment(enable_async=True)649 env.filters["add_each"] = lambda v, x: [i + x for i in v]650 t = env.from_string("{{ (a|add_each(2))[1:] }}")651 out = t.render(a=range(3))652 assert out == "[3, 4]"653654655def test_getitem_after_call():656 env = Environment(enable_async=True)657 env.globals["add_each"] = lambda v, x: [i + x for i in v]658 t = env.from_string("{{ add_each(a, 2)[1:] }}")659 out = t.render(a=range(3))660 assert out == "[3, 4]"