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
Provenance
- Commit
2b4d44b- Tree
8c928b2- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
21ad817
3 files changed+49 added−9 removed
modified:jinja2/asyncsupport.py+34 added-7 removed
1 import sys2 import asyncio3 import inspect4 5-from jinja2.utils import concat5+from jinja2.utils import concat, internalcode, concat, Markup6 7 8+async 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)15+16+17 async 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.')21 22 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)24 25 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 lines37 return loop.run_until_complete(self.render_async(self, *args, **kwargs))38 return render39 40 41+def 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 rv48+49+ @internalcode50+ def __call__(self):51+ if not self._context.environment._async:52+ return original_call(self)53+ return async_call(self)54+55+ return __call__56+57+58 def patch_template():59 from jinja2 import Template60 Template.render_async = render_async61 Template.render = wrap_render_func(Template.render)62 63 64+def patch_runtime():65+ from jinja2.runtime import BlockReference66+ BlockReference.__call__ = wrap_block_reference_call(BlockReference.__call__)67+68+69 def patch_all():70 patch_template()71+ patch_runtime()72 73 74 async def auto_await(value):75 if inspect.isawaitable(value):2 unmodified linesmodified:jinja2/compiler.py+4 added-2 removed
885 unmodified lines886 self.writeline('if parent_template is None:')887 self.indent()888 level += 1889 context = node.scoped and 'context.derived(locals())' or 'context'890+891+ 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 linesmodified:tests/test_async.py+11 added
53 unmodified lines54 return await t.render_async(async_func=async_func)55 56 rv = run(func)57 assert rv == '[42][42]'58+59+60+@pytest.mark.skipif(not have_async_gen, reason='No async generators')61+def 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()66+67+ rv = run(func)68+ assert rv == '<Test><Test>'