Made the env._async property public for filters
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 d8326d9 · Browse files · 3 files·+30 added−30 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+30 added−30 removed
- other:+30 added−30 removed
Provenance
- Commit
2fd008a- Tree
078d8a2- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
d8326d9
3 files changed+30 added−30 removed
modified:jinja2/asyncsupport.py+5 added-5 removed
47 unmodified lines48 yield loop.run_until_complete(async_gen.__anext__())49 except StopAsyncIteration:50 pass51 def generate(self, *args, **kwargs):52- if not self.environment._async:52+ if not self.environment.is_async:53 return original_generate(self, *args, **kwargs)54 return _convert_generator(self, asyncio.get_event_loop(), args, kwargs)55 return update_wrapper(generate, original_generate)56 57 58 async def render_async(self, *args, **kwargs):59- if not self.environment._async:59+ if not self.environment.is_async:60 raise RuntimeError('The environment was not created with async mode '61 'enabled.')62 63 vars = dict(*args, **kwargs)7 unmodified lines71 72 73 def wrap_render_func(original_render):74 def render(self, *args, **kwargs):75- if not self.environment._async:75+ if not self.environment.is_async:76 return original_render(self, *args, **kwargs)77 loop = asyncio.get_event_loop()78 return loop.run_until_complete(self.render_async(*args, **kwargs))79 return update_wrapper(render, original_render)8 unmodified lines88 return rv89 90 @internalcode91 def __call__(self):92- if not self._context.environment._async:92+ if not self._context.environment.is_async:93 return original_call(self)94 return async_call(self)95 96 return update_wrapper(__call__, original_call)9 unmodified lines106 107 def wrap_default_module(original_default_module):108 @internalcode109 def _get_default_module(self):110- if self.environment._async:110+ if self.environment.is_async:111 raise RuntimeError('Template module attribute is unavailable '112 'in async mode')113 return original_default_module(self)114 return _get_default_module115 unmodified linesmodified:jinja2/compiler.py+23 added-23 removed
647 unmodified lines648 # a = 42; b = lambda: a; del a649 self.writeline(' = '.join(to_delete) + ' = missing')650 651 def func(self, name):652- if self.environment._async:652+ if self.environment.is_async:653 return 'async def %s' % name654 return 'def %s' % name655 656 def function_scoping(self, node, frame, children=None,124 unmodified lines781 self.writeline('from jinja2.runtime import ' + ', '.join(exported))782 if not unoptimize_before_dead_code:783 self.writeline('dummy = lambda *x: None')784 785- if self.environment._async:785+ if self.environment.is_async:786 self.writeline('from jinja2.asyncsupport import auto_await, '787 'auto_aiter, make_async_loop_context')788 789 # if we want a deferred initialization we cannot move the98 unmodified lines888 self.indent()889 level += 1890 context = node.scoped and 'context.derived(locals())' or 'context'891 892- loop = self.environment._async and 'async for' or 'for'892+ loop = self.environment.is_async and 'async for' or 'for'893 self.writeline('%s event in context.blocks[%r][0](%s):' % (894 loop, node.name, context), node)895 self.indent()896 self.simple_write('event', frame)75 unmodified lines972 self.writeline('else:')973 self.indent()974 975 if node.with_context:976- loop = self.environment._async and 'async for' or 'for'976+ loop = self.environment.is_async and 'async for' or 'for'977 self.writeline('%s event in template.root_render_func('978 'template.new_context(context.parent, True, '979 'locals())):' % loop)980- elif self.environment._async:980+ elif self.environment.is_async:981 self.writeline('for event in (await '982 'template._get_default_module_async())'983 '._body_stream:')984 else:13 unmodified lines998 self.unoptimize_scope(frame)999 self.writeline('l_%s = ' % node.target, node)1000 if frame.toplevel:1001 self.write('context.vars[%r] = ' % node.target)1002- if self.environment._async:1002+ if self.environment.is_async:1003 self.write('await ')1004 self.write('environment.get_template(')1005 self.visit(node.template, frame)1006 self.write(', %r).' % self.name)1007 if node.with_context:1008 self.write('make_module%s(context.parent, True, locals())'1009- % (self.environment._async and '_async' or ''))1010- elif self.environment._async:1009+ % (self.environment.is_async and '_async' or ''))1010+ elif self.environment.is_async:1011 self.write('_get_default_module_async()')1012 else:1013 self.write('_get_default_module()')1014 if frame.toplevel and not node.target.startswith('_'):3 unmodified lines1018 def visit_FromImport(self, node, frame):1019 """Visit named imports."""1020 self.newline(node)1021 self.write('included_template = %senvironment.get_template('1022- % (self.environment._async and 'await ' or ''))1022+ % (self.environment.is_async and 'await ' or ''))1023 self.visit(node.template, frame)1024 self.write(', %r).' % self.name)1025 if node.with_context:1026 self.write('make_module%s(context.parent, True)'1027- % (self.environment._async and '_async' or ''))1028- elif self.environment._async:1027+ % (self.environment.is_async and '_async' or ''))1028+ elif self.environment.is_async:1029 self.write('_get_default_module_async()')1030 else:1031 self.write('_get_default_module()')1032 97 unmodified lines1130 " variable of the current loop. Because there is no parent "1131 "loop it's undefined. Happened in loop on %s" %1132 self.position(node)))1133 1134- self.writeline(self.environment._async and 'async for ' or 'for ', node)1134+ self.writeline(self.environment.is_async and 'async for ' or 'for ', node)1135 self.visit(node.target, loop_frame)1136 if extended_loop:1137- if self.environment._async:1137+ if self.environment.is_async:1138 self.write(', l_loop in await make_async_loop_context(')1139 else:1140 self.write(', l_loop in LoopContext(')1141 else:3 unmodified lines1145 # "outer frame".1146 if extended_loop and node.test is not None:1147 self.write('(')1148 self.visit(node.target, loop_frame)1149- self.write(self.environment._async and ' async for ' or ' for ')1149+ self.write(self.environment.is_async and ' async for ' or ' for ')1150 self.visit(node.target, loop_frame)1151 self.write(' in ')1152 if node.recursive:1153 self.write('reciter')1154 else:1155- if self.environment._async:1155+ if self.environment.is_async:1156 self.write('auto_aiter(')1157 self.visit(node.iter, loop_frame)1158- if self.environment._async:1158+ if self.environment.is_async:1159 self.write(')')1160 self.write(' if (')1161 test_frame = loop_frame.copy()1162 self.visit(node.test, test_frame)1 unmodified line1164 1165 elif node.recursive:1166 self.write('reciter')1167 else:1168- if self.environment._async and not extended_loop:1168+ if self.environment.is_async and not extended_loop:1169 self.write('auto_aiter(')1170 self.visit(node.iter, loop_frame)1171- if self.environment._async and not extended_loop:1171+ if self.environment.is_async and not extended_loop:1172 self.write(')')1173 1174 if node.recursive:1175 self.write(', loop_render_func, depth):')31 unmodified lines1207 if node.recursive:1208 self.return_buffer_contents(loop_frame)1209 self.outdent()1210 self.start_write(frame, node)1211- if self.environment._async:1211+ if self.environment.is_async:1212 self.write('await ')1213 self.write('loop(')1214- if self.environment._async:1214+ if self.environment.is_async:1215 self.write('auto_aiter(')1216 self.visit(node.iter, frame)1217- if self.environment._async:1217+ if self.environment.is_async:1218 self.write(')')1219 self.write(', loop)')1220 self.end_write(frame)1221 443 unmodified lines1665 write_expr2()1666 self.write(')')1667 1668 def visit_Call(self, node, frame, forward_caller=False):1669- if self.environment._async:1669+ if self.environment.is_async:1670 self.write('await auto_await(')1671 if self.environment.sandboxed:1672 self.write('environment.call(context, ')1673 else:1 unmodified line1675 self.visit(node.node, frame)1676 extra_kwargs = forward_caller and {'caller': 'caller'} or None1677 self.signature(node, frame, extra_kwargs)1678 self.write(')')1679- if self.environment._async:1679+ if self.environment.is_async:1680 self.write(')')1681 1682 def visit_Keyword(self, node, frame):1683 self.write(node.key + '=')63 unmodified linesmodified:jinja2/environment.py+2 added-2 removed
319 unmodified lines320 # load extensions321 self.extensions = load_extensions(self, extensions)322 323 self.enable_async = enable_async324- self._async = self.enable_async and have_async_gen324+ self.is_async = self.enable_async and have_async_gen325 326 _environment_sanity_check(self)327 328 def add_extension(self, extension):798 unmodified lines1127 """1128 1129 def __init__(self, template, context, body_stream=None):1130 if body_stream is None:1131- if context.environment._async:1131+ if context.environment.is_async:1132 raise RuntimeError('Async mode requires a body stream '1133 'to be passed to a template module. Use '1134 'the async methods of the API you are '1135 'using.')125 unmodified lines