Added untested support for imports and includes in async mode
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 1259162 · Browse files · 3 files·+24 added−7 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+24 added−7 removed
- other:+24 added−7 removed
Provenance
- Commit
f61ffbf- Tree
f9df7f7- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
1259162
3 files changed+24 added−7 removed
modified:jinja2/asyncsupport.py+8 added
62 unmodified lines63 self._module = rv = await self.make_module_async()64 return rv65 66 67+@internalcode68+def get_default_module_impl(self):69+ if self.environment._async:70+ return self._get_default_module_async()71+ return self._get_default_module()72+73+74 def wrap_default_module(original_default_module):75 @internalcode76 def _get_default_module(self):77 if self.environment._async:17 unmodified lines95 Template.render = wrap_render_func(Template.render)96 Template._get_default_module = wrap_default_module(97 Template._get_default_module)98 Template._get_default_module_async = get_default_module_async99+ Template._get_default_module_impl = get_default_module_impl100 Template.make_module_async = make_module_async101 102 103 def patch_runtime():13 unmodified linesmodified:jinja2/compiler.py+12 added-7 removed
975 unmodified lines976 self.writeline('%s event in template.root_render_func('977 'template.new_context(context.parent, True, '978 'locals())):' % loop)979 else:980- self.writeline('for event in template._get_default_module()'981- '._body_stream:')980+ self.writeline('%s event in template._get_default_module_impl()'981+ '._body_stream:' % loop)982 983 self.indent()984 self.simple_write('event', frame)985 self.outdent()7 unmodified lines993 self.unoptimize_scope(frame)994 self.writeline('l_%s = ' % node.target, node)995 if frame.toplevel:996 self.write('context.vars[%r] = ' % node.target)997+ if self.environment._async:998+ self.write('await ')999 self.write('environment.get_template(')1000 self.visit(node.template, frame)1001 self.write(', %r).' % self.name)1002 if node.with_context:1001- self.write('make_module(context.parent, True, locals())')1003+ self.write('make_module%s(context.parent, True, locals())'1004+ % (self.environment._async and '_async' or ''))1005 else:1003- self.write('_get_default_module()')1006+ self.write('_get_default_module_impl()')1007 if frame.toplevel and not node.target.startswith('_'):1008 self.writeline('context.exported_vars.discard(%r)' % node.target)1009 frame.assigned_names.add(node.target)1010 1011 def visit_FromImport(self, node, frame):1012 """Visit named imports."""1013 self.newline(node)1011- self.write('included_template = environment.get_template(')1014+ self.write('included_template = %senvironment.get_template('1015+ % (self.environment._async and 'await ' or ''))1016 self.visit(node.template, frame)1017 self.write(', %r).' % self.name)1018 if node.with_context:1015- self.write('make_module(context.parent, True)')1019+ self.write('make_module%s(context.parent, True)'1020+ % (self.environment._async and '_async' or ''))1021 else:1017- self.write('_get_default_module()')1022+ self.write('_get_default_module_impl()')1023 1024 var_names = []1025 discarded_names = []1026 for name in node.names:691 unmodified linesmodified:jinja2/environment.py+4 added
1040 unmodified lines1041 return self._module1042 self._module = rv = self.make_module()1043 return rv1044 1045+ # This is what the compiler dispatches to from generated code. It1046+ # might get swapped out by the async support1047+ _get_default_module_impl = _get_default_module1048+1049 @property1050 def module(self):1051 """The template as module. This is used for imports in the1052 template runtime but is also useful if one wants to access179 unmodified lines