Support yield from if we can use it
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 46025fb · Browse files · 1 file·+42 added−16 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+42 added−16 removed
- other:+42 added−16 removed
Provenance
- Commit
e5e3e61- Tree
0e0eb3c- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
46025fb
1 file changed+42 added−16 removed
modified:jinja2/compiler.py+42 added-16 removed
47 unmodified lines48 code_features.append('generator_stop')49 except SyntaxError:50 pass51 52+# does this python version support yield from?53+try:54+ exec('def f(): yield from x()')55+except SyntaxError:56+ supports_yield_from = False57+else:58+ supports_yield_from = True59 60+61 def generate(node, environment, name, filename, stream=None,62 defer_init=False):63 """Generate the python source for a node tree."""64 if not isinstance(node, nodes.Template):561 unmodified lines626 if not self.has_known_extends:627 self.indent()628 self.writeline('if parent_template is not None:')629 self.indent()630+ if supports_yield_from:631+ self.writeline('yield from parent_template.'632+ 'root_render_func(context)')633+ else:622- self.writeline('for event in parent_template.'623- 'root_render_func(context):')624- self.indent()625- self.writeline('yield event')626- self.outdent(2 + (not self.has_known_extends))634+ self.writeline('for event in parent_template.'635+ 'root_render_func(context):')636+ self.indent()637+ self.writeline('yield event')638+ self.outdent()639+ self.outdent(1 + (not self.has_known_extends))640 641 # at this point we now have the blocks collected and can visit them too.642 for name, block in iteritems(self.blocks):643 self.writeline('%s(context, missing=missing%s):' %30 unmodified lines674 in self.debug_info))675 676 def visit_Block(self, node, frame):677 """Call a block and register it for the template."""665- level = 1678+ level = 0679 if frame.toplevel:680 # if we know that we are a child template, there is no need to681 # check if we are one682 if self.has_known_extends:4 unmodified lines687 level += 1688 context = node.scoped and (689 'context.derived(%s)' % self.dump_local_context(frame)) or 'context'690 691+ if supports_yield_from and not self.environment.is_async:692+ self.writeline('yield from context.blocks[%r][0](%s)' % (693+ node.name, context), node)694+ else:678- loop = self.environment.is_async and 'async for' or 'for'679- self.writeline('%s event in context.blocks[%r][0](%s):' % (680- loop, node.name, context), node)681- self.indent()682- self.simple_write('event', frame)695+ loop = self.environment.is_async and 'async for' or 'for'696+ self.writeline('%s event in context.blocks[%r][0](%s):' % (697+ loop, node.name, context), node)698+ self.indent()699+ self.simple_write('event', frame)700+ self.outdent()701+702 self.outdent(level)703 704 def visit_Extends(self, node, frame):705 """Calls the extender."""68 unmodified lines774 self.outdent()775 self.writeline('else:')776 self.indent()777 778+ skip_event_yield = False779 if node.with_context:780 loop = self.environment.is_async and 'async for' or 'for'781 self.writeline('%s event in template.root_render_func('782 'template.new_context(context.parent, True, '2 unmodified lines785 self.writeline('for event in (await '786 'template._get_default_module_async())'787 '._body_stream:')788 else:789+ if supports_yield_from:790+ self.writeline('yield from template._get_default_module()'791+ '._body_stream')792+ skip_event_yield = True793+ else:769- self.writeline('for event in template._get_default_module()'770- '._body_stream:')794+ self.writeline('for event in template._get_default_module()'795+ '._body_stream:')796 797+ if not skip_event_yield:772- self.indent()773- self.simple_write('event', frame)774- self.outdent()798+ self.indent()799+ self.simple_write('event', frame)800+ self.outdent()801 802 if node.ignore_missing:803 self.outdent()804 737 unmodified lines