Skip to content
Myr

Basic async support for blocks

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

authored by Armin Ronacher · committed by Armin Ronacher · parent 21ad817 · Browse files · 3 files+49 added−9 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+49 added−9 removed

  • tests:+11 added−0 removed
  • other:+38 added−9 removed

3 files+49 added−9 removed

modified:jinja2/asyncsupport.py+34 added-7 removed
1import sys2import asyncio3import inspect45from jinja2.utils import concat5from jinja2.utils import concat, internalcode, concat, Markup678async def concat_async(async_gen):9 rv = []10 async def collect():11 async for event in async_gen:12 rv.append(event)13 await collect()14 return concat(rv)151617async def render_async(self, *args, **kwargs):18 if not self.environment._async:19 raise RuntimeError('The environment was not created with async mode '20 'enabled.')2122 vars = dict(*args, **kwargs)23 ctx = self.new_context(vars)15 rv = []16 async def collect():17 async for event in self.root_render_func(ctx):18 rv.append(event)2425 try:21 await collect()22 return concat(rv)26 return await concat_async(self.root_render_func(ctx))27 except Exception:28 exc_info = sys.exc_info()29 return self.environment.handle_exception(exc_info, True)30
6 unmodified lines
37 return loop.run_until_complete(self.render_async(self, *args, **kwargs))38 return render394041def wrap_block_reference_call(original_call):42 @internalcode43 async def async_call(self):44 rv = await concat_async(self._stack[self._depth](self._context))45 if self._context.eval_ctx.autoescape:46 rv = Markup(rv)47 return rv4849 @internalcode50 def __call__(self):51 if not self._context.environment._async:52 return original_call(self)53 return async_call(self)5455 return __call__565758def patch_template():59 from jinja2 import Template60 Template.render_async = render_async61 Template.render = wrap_render_func(Template.render)626364def patch_runtime():65 from jinja2.runtime import BlockReference66 BlockReference.__call__ = wrap_block_reference_call(BlockReference.__call__)676869def patch_all():70 patch_template()71 patch_runtime()727374async def auto_await(value):75 if inspect.isawaitable(value):
2 unmodified lines
modified:jinja2/compiler.py+4 added-2 removed
885 unmodified lines
886 self.writeline('if parent_template is None:')887 self.indent()888 level += 1889 context = node.scoped and 'context.derived(locals())' or 'context'890891 loop = self.environment._async and 'async for' or 'for'890 self.writeline('for event in context.blocks[%r][0](%s):' % (891 node.name, context), node)892 self.writeline('%s event in context.blocks[%r][0](%s):' % (893 loop, node.name, context), node)894 self.indent()895 self.simple_write('event', frame)896 self.outdent(level)897
813 unmodified lines
modified:tests/test_async.py+11 added
53 unmodified lines
54 return await t.render_async(async_func=async_func)5556 rv = run(func)57 assert rv == '[42][42]'585960@pytest.mark.skipif(not have_async_gen, reason='No async generators')61def test_async_blocks():62 t = Template('{% block foo %}<Test>{% endblock %}{{ self.foo() }}',63 enable_async=True, autoescape=True)64 async def func():65 return await t.render_async()6667 rv = run(func)68 assert rv == '<Test><Test>'