First pass on implementing async default module
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 55e2c4a · Browse files · 2 files·+39 added−2 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+39 added−2 removed
- other:+39 added−2 removed
Provenance
- Commit
95d495a- Tree
24b25b3- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
55e2c4a
2 files changed+39 added−2 removed
modified:jinja2/asyncsupport.py+31 added
1 unmodified line2 import asyncio3 import inspect4 5 from jinja2.utils import concat, internalcode, concat, Markup6+from jinja2.environment import TemplateModule7 8 9 async def concat_async(async_gen):10 rv = []44 unmodified lines55 56 return __call__57 58 59+@internalcode60+async def get_default_module_async(self):61+ if self._module is not None:62+ return self._module63+ self._module = rv = await self.make_module_async()64+ return rv65+66+67+def wrap_default_module(original_default_module):68+ @internalcode69+ def _get_default_module(self):70+ if self.environment._async:71+ raise RuntimeError('Template module attribute is unavailable '72+ 'in async mode')73+ return original_default_module(self)74+ return _get_default_module75+76+77+async def make_module_async(self, vars=None, shared=False, locals=None):78+ context = self.new_context(vars, shared, locals)79+ body_stream = []80+ async for item in template.root_render_func(context):81+ body_stream.append(item)82+ return TemplateModule(self, context, body_stream)83+84+85 def patch_template():86 from jinja2 import Template87 Template.render_async = render_async88 Template.render = wrap_render_func(Template.render)89+ Template._get_default_module = wrap_default_module(90+ Template._get_default_module)91+ Template._get_default_module_async = get_default_module_async92+ Template.make_module_async = make_module_async93 94 95 def patch_runtime():96 from jinja2.runtime import BlockReference12 unmodified linesmodified:jinja2/environment.py+8 added-2 removed
1033 unmodified lines1034 as for the :meth:`new_context` method.1035 """1036 return TemplateModule(self, self.new_context(vars, shared, locals))1037 1038+ @internalcode1039 def _get_default_module(self):1040 if self._module is not None:1041 return self._module1042 self._module = rv = self.make_module()49 unmodified lines1092 template are available as attributes on this object. Additionally1093 converting it into an unicode- or bytestrings renders the contents.1094 """1095 1095- def __init__(self, template, context):1096- self._body_stream = list(template.root_render_func(context))1096+ def __init__(self, template, context, body_stream=None):1097+ if body_stream is None:1098+ if context.environment._async:1099+ raise RuntimeError('Async mode requires a body stream '1100+ 'to be passed in.')1101+ body_stream = list(template.root_render_func(context))1102+ self._body_stream = body_stream1103 self.__dict__.update(context.get_exported())1104 self.__name__ = template.name1105 1106 def __html__(self):119 unmodified lines