Skip to content
Myr

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

2 files+39 added−2 removed

modified:jinja2/asyncsupport.py+31 added
1 unmodified line
2import asyncio3import inspect45from jinja2.utils import concat, internalcode, concat, Markup6from jinja2.environment import TemplateModule789async def concat_async(async_gen):10 rv = []
44 unmodified lines
5556 return __call__575859@internalcode60async 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 rv656667def 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_module757677async 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)838485def 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_async939495def patch_runtime():96 from jinja2.runtime import BlockReference
12 unmodified lines
modified:jinja2/environment.py+8 added-2 removed
1033 unmodified lines
1034 as for the :meth:`new_context` method.1035 """1036 return TemplateModule(self, self.new_context(vars, shared, locals))10371038 @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 lines
1092 template are available as attributes on this object. Additionally1093 converting it into an unicode- or bytestrings renders the contents.1094 """10951095 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.name11051106 def __html__(self):
119 unmodified lines