From: Jafnee Date: Wed, 23 Sep 2020 10:34:58 +0000 (+0800) Subject: add Undefined.__aiter__ X-Git-Tag: 3.0.0rc1~17^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3242428c52852718abe3929481a2446c9e43482f;p=thirdparty%2Fjinja.git add Undefined.__aiter__ --- diff --git a/CHANGES.rst b/CHANGES.rst index 2dccbfc3..45ff1444 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -54,6 +54,7 @@ Unreleased :issue:`1170` - ``Undefined.__contains__`` (``in``) raises an ``UndefinedError`` instead of a ``TypeError``. :issue:`1198` +- ``Undefined`` is iterable in an async environment. :issue:`1294` Version 2.11.3 diff --git a/src/jinja2/runtime.py b/src/jinja2/runtime.py index ca49db76..c3d6fa4c 100644 --- a/src/jinja2/runtime.py +++ b/src/jinja2/runtime.py @@ -778,6 +778,10 @@ class Undefined: def __iter__(self): yield from () + async def __aiter__(self): + for _ in (): + yield + def __bool__(self): return False diff --git a/tests/test_async.py b/tests/test_async.py index 5cc8ba68..44b4bf13 100644 --- a/tests/test_async.py +++ b/tests/test_async.py @@ -2,6 +2,7 @@ import asyncio import pytest +from jinja2 import ChainableUndefined from jinja2 import DictLoader from jinja2 import Environment from jinja2 import Template @@ -588,3 +589,16 @@ def test_namespace_awaitable(test_env_async): assert actual == "Bar" run(_test()) + + +def test_chainable_undefined_aiter(): + async def _test(): + t = Template( + "{% for x in a['b']['c'] %}{{ x }}{% endfor %}", + enable_async=True, + undefined=ChainableUndefined, + ) + rv = await t.render_async(a={}) + assert rv == "" + + run(_test())