- ``Undefined.__contains__`` (``in``) raises an ``UndefinedError``
instead of a ``TypeError``. :issue:`1198`
- ``Undefined`` is iterable in an async environment. :issue:`1294`
+- ``NativeEnvironment`` supports async mode. :issue:`1362`
Version 2.11.3
except Exception:
return self.environment.handle_exception()
+ async def render_async(self, *args, **kwargs):
+ if not self.environment.is_async:
+ raise RuntimeError(
+ "The environment was not created with async mode enabled."
+ )
+
+ vars = dict(*args, **kwargs)
+ ctx = self.new_context(vars)
+
+ try:
+ return native_concat([n async for n in self.root_render_func(ctx)])
+ except Exception:
+ return self.environment.handle_exception()
+
NativeEnvironment.template_class = NativeTemplate
from jinja2.exceptions import TemplateNotFound
from jinja2.exceptions import TemplatesNotFound
from jinja2.exceptions import UndefinedError
+from jinja2.nativetypes import NativeEnvironment
def run(coro):
assert rv == ""
run(_test())
+
+
+@pytest.fixture
+def async_native_env():
+ return NativeEnvironment(enable_async=True)
+
+
+def test_native_async(async_native_env):
+ async def _test():
+ t = async_native_env.from_string("{{ x }}")
+ rv = await t.render_async(x=23)
+ assert rv == 23
+
+ run(_test())
+
+
+def test_native_list_async(async_native_env):
+ async def _test():
+ t = async_native_env.from_string("{{ x }}")
+ rv = await t.render_async(x=list(range(3)))
+ assert rv == [0, 1, 2]
+
+ run(_test())