From: Armin Ronacher Date: Wed, 28 Dec 2016 21:51:56 +0000 (+0100) Subject: Made the env._async property public for filters X-Git-Tag: 2.9~58 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2fd008a796115de31bbd0642f677d96f1efede87;p=thirdparty%2Fjinja.git Made the env._async property public for filters --- diff --git a/jinja2/asyncsupport.py b/jinja2/asyncsupport.py index f109beea..1cdda833 100644 --- a/jinja2/asyncsupport.py +++ b/jinja2/asyncsupport.py @@ -49,14 +49,14 @@ def wrap_generate_func(original_generate): except StopAsyncIteration: pass def generate(self, *args, **kwargs): - if not self.environment._async: + if not self.environment.is_async: return original_generate(self, *args, **kwargs) return _convert_generator(self, asyncio.get_event_loop(), args, kwargs) return update_wrapper(generate, original_generate) async def render_async(self, *args, **kwargs): - if not self.environment._async: + if not self.environment.is_async: raise RuntimeError('The environment was not created with async mode ' 'enabled.') @@ -72,7 +72,7 @@ async def render_async(self, *args, **kwargs): def wrap_render_func(original_render): def render(self, *args, **kwargs): - if not self.environment._async: + if not self.environment.is_async: return original_render(self, *args, **kwargs) loop = asyncio.get_event_loop() return loop.run_until_complete(self.render_async(*args, **kwargs)) @@ -89,7 +89,7 @@ def wrap_block_reference_call(original_call): @internalcode def __call__(self): - if not self._context.environment._async: + if not self._context.environment.is_async: return original_call(self) return async_call(self) @@ -107,7 +107,7 @@ async def get_default_module_async(self): def wrap_default_module(original_default_module): @internalcode def _get_default_module(self): - if self.environment._async: + if self.environment.is_async: raise RuntimeError('Template module attribute is unavailable ' 'in async mode') return original_default_module(self) diff --git a/jinja2/compiler.py b/jinja2/compiler.py index 40f145e0..c01d5b1a 100644 --- a/jinja2/compiler.py +++ b/jinja2/compiler.py @@ -649,7 +649,7 @@ class CodeGenerator(NodeVisitor): self.writeline(' = '.join(to_delete) + ' = missing') def func(self, name): - if self.environment._async: + if self.environment.is_async: return 'async def %s' % name return 'def %s' % name @@ -782,7 +782,7 @@ class CodeGenerator(NodeVisitor): if not unoptimize_before_dead_code: self.writeline('dummy = lambda *x: None') - if self.environment._async: + if self.environment.is_async: self.writeline('from jinja2.asyncsupport import auto_await, ' 'auto_aiter, make_async_loop_context') @@ -889,7 +889,7 @@ class CodeGenerator(NodeVisitor): level += 1 context = node.scoped and 'context.derived(locals())' or 'context' - loop = self.environment._async and 'async for' or 'for' + loop = self.environment.is_async and 'async for' or 'for' self.writeline('%s event in context.blocks[%r][0](%s):' % ( loop, node.name, context), node) self.indent() @@ -973,11 +973,11 @@ class CodeGenerator(NodeVisitor): self.indent() if node.with_context: - loop = self.environment._async and 'async for' or 'for' + loop = self.environment.is_async and 'async for' or 'for' self.writeline('%s event in template.root_render_func(' 'template.new_context(context.parent, True, ' 'locals())):' % loop) - elif self.environment._async: + elif self.environment.is_async: self.writeline('for event in (await ' 'template._get_default_module_async())' '._body_stream:') @@ -999,15 +999,15 @@ class CodeGenerator(NodeVisitor): self.writeline('l_%s = ' % node.target, node) if frame.toplevel: self.write('context.vars[%r] = ' % node.target) - if self.environment._async: + if self.environment.is_async: self.write('await ') self.write('environment.get_template(') self.visit(node.template, frame) self.write(', %r).' % self.name) if node.with_context: self.write('make_module%s(context.parent, True, locals())' - % (self.environment._async and '_async' or '')) - elif self.environment._async: + % (self.environment.is_async and '_async' or '')) + elif self.environment.is_async: self.write('_get_default_module_async()') else: self.write('_get_default_module()') @@ -1019,13 +1019,13 @@ class CodeGenerator(NodeVisitor): """Visit named imports.""" self.newline(node) self.write('included_template = %senvironment.get_template(' - % (self.environment._async and 'await ' or '')) + % (self.environment.is_async and 'await ' or '')) self.visit(node.template, frame) self.write(', %r).' % self.name) if node.with_context: self.write('make_module%s(context.parent, True)' - % (self.environment._async and '_async' or '')) - elif self.environment._async: + % (self.environment.is_async and '_async' or '')) + elif self.environment.is_async: self.write('_get_default_module_async()') else: self.write('_get_default_module()') @@ -1131,10 +1131,10 @@ class CodeGenerator(NodeVisitor): "loop it's undefined. Happened in loop on %s" % self.position(node))) - self.writeline(self.environment._async and 'async for ' or 'for ', node) + self.writeline(self.environment.is_async and 'async for ' or 'for ', node) self.visit(node.target, loop_frame) if extended_loop: - if self.environment._async: + if self.environment.is_async: self.write(', l_loop in await make_async_loop_context(') else: self.write(', l_loop in LoopContext(') @@ -1146,16 +1146,16 @@ class CodeGenerator(NodeVisitor): if extended_loop and node.test is not None: self.write('(') self.visit(node.target, loop_frame) - self.write(self.environment._async and ' async for ' or ' for ') + self.write(self.environment.is_async and ' async for ' or ' for ') self.visit(node.target, loop_frame) self.write(' in ') if node.recursive: self.write('reciter') else: - if self.environment._async: + if self.environment.is_async: self.write('auto_aiter(') self.visit(node.iter, loop_frame) - if self.environment._async: + if self.environment.is_async: self.write(')') self.write(' if (') test_frame = loop_frame.copy() @@ -1165,10 +1165,10 @@ class CodeGenerator(NodeVisitor): elif node.recursive: self.write('reciter') else: - if self.environment._async and not extended_loop: + if self.environment.is_async and not extended_loop: self.write('auto_aiter(') self.visit(node.iter, loop_frame) - if self.environment._async and not extended_loop: + if self.environment.is_async and not extended_loop: self.write(')') if node.recursive: @@ -1208,13 +1208,13 @@ class CodeGenerator(NodeVisitor): self.return_buffer_contents(loop_frame) self.outdent() self.start_write(frame, node) - if self.environment._async: + if self.environment.is_async: self.write('await ') self.write('loop(') - if self.environment._async: + if self.environment.is_async: self.write('auto_aiter(') self.visit(node.iter, frame) - if self.environment._async: + if self.environment.is_async: self.write(')') self.write(', loop)') self.end_write(frame) @@ -1666,7 +1666,7 @@ class CodeGenerator(NodeVisitor): self.write(')') def visit_Call(self, node, frame, forward_caller=False): - if self.environment._async: + if self.environment.is_async: self.write('await auto_await(') if self.environment.sandboxed: self.write('environment.call(context, ') @@ -1676,7 +1676,7 @@ class CodeGenerator(NodeVisitor): extra_kwargs = forward_caller and {'caller': 'caller'} or None self.signature(node, frame, extra_kwargs) self.write(')') - if self.environment._async: + if self.environment.is_async: self.write(')') def visit_Keyword(self, node, frame): diff --git a/jinja2/environment.py b/jinja2/environment.py index afe7686d..759ecb97 100644 --- a/jinja2/environment.py +++ b/jinja2/environment.py @@ -321,7 +321,7 @@ class Environment(object): self.extensions = load_extensions(self, extensions) self.enable_async = enable_async - self._async = self.enable_async and have_async_gen + self.is_async = self.enable_async and have_async_gen _environment_sanity_check(self) @@ -1128,7 +1128,7 @@ class TemplateModule(object): def __init__(self, template, context, body_stream=None): if body_stream is None: - if context.environment._async: + if context.environment.is_async: raise RuntimeError('Async mode requires a body stream ' 'to be passed to a template module. Use ' 'the async methods of the API you are '