Skip to content
Myr

NativeEnvironment supports async mode

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

authored by remidebette · committed by David Lord · parent 06c7c6f · Browse files · 3 files+39 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+39 added−0 removed

  • tests:+24 added−0 removed
  • other:+15 added−0 removed

3 files+39 added−0 removed

modified:src/jinja2/nativetypes.py+14 added
91 unmodified lines
92 return native_concat(self.root_render_func(self.new_context(vars)))93 except Exception:94 return self.environment.handle_exception()9596 async def render_async(self, *args, **kwargs):97 if not self.environment.is_async:98 raise RuntimeError(99 "The environment was not created with async mode enabled."100 )101102 vars = dict(*args, **kwargs)103 ctx = self.new_context(vars)104105 try:106 return native_concat([n async for n in self.root_render_func(ctx)])107 except Exception:108 return self.environment.handle_exception()109110111NativeEnvironment.template_class = NativeTemplate
modified:tests/test_async.py+24 added
8 unmodified lines
9from jinja2.asyncsupport import auto_aiter10from jinja2.exceptions import TemplateNotFound11from jinja2.exceptions import TemplatesNotFound12from jinja2.exceptions import UndefinedError13from jinja2.nativetypes import NativeEnvironment141516def run(coro):17 loop = asyncio.get_event_loop()
584 unmodified lines
602 rv = await t.render_async(a={})603 assert rv == ""604605 run(_test())606607608@pytest.fixture609def async_native_env():610 return NativeEnvironment(enable_async=True)611612613def test_native_async(async_native_env):614 async def _test():615 t = async_native_env.from_string("{{ x }}")616 rv = await t.render_async(x=23)617 assert rv == 23618619 run(_test())620621622def test_native_list_async(async_native_env):623 async def _test():624 t = async_native_env.from_string("{{ x }}")625 rv = await t.render_async(x=list(range(3)))626 assert rv == [0, 1, 2]627628 run(_test())
modified:CHANGES.rst+1 added
53 unmodified lines
54 :issue:`1170`55- ``Undefined.__contains__`` (``in``) raises an ``UndefinedError``56 instead of a ``TypeError``. :issue:`1198`57- ``Undefined`` is iterable in an async environment. :issue:`1294`58- ``NativeEnvironment`` supports async mode. :issue:`1362`596061Version 2.11.362--------------
780 unmodified lines