Skip to content
Myr

readded support for recursive for-loops

Before adoption: this commit came in with the history the project adopted, so no landing records it.

--HG-- branch : trunk

authored by Armin Ronacher · committed by Armin Ronacher · parent fdf9530 · Browse files · 2 files+74 added−11 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+74 added−11 removed

  • other:+74 added−11 removed

2 files+74 added−11 removed

modified:jinja2/compiler.py+66 added-10 removed
497 unmodified lines
498 aliases[name] = ident = self.temporary_identifier()499 self.writeline('%s = l_%s' % (ident, name))500 return aliases501502 def function_scoping(self, node, frame, children=None):502 def function_scoping(self, node, frame, children=None,503 find_special=True):504 """In Jinja a few statements require the help of anonymous505 functions. Those are currently macros and call blocks and in506 the future also recursive loops. As there is currently507 technical limitation that doesn't allow reading and writing a
34 unmodified lines
542 func_frame.identifiers.undeclared &543 func_frame.identifiers.declared544 )545546 # no special variables for this scope, abort early547 if not find_special:548 return func_frame549550 func_frame.accesses_kwargs = False551 func_frame.accesses_varargs = False552 func_frame.accesses_caller = False553 func_frame.arguments = args = ['l_' + x.name for x in node.args]
241 unmodified lines
795 if not alias.startswith('__'):796 self.writeline('context.exported_vars.discard(%r)' % alias)797798 def visit_For(self, node, frame):794 loop_frame = frame.inner()795 loop_frame.inspect(node.iter_child_nodes(exclude=('iter',)))796 extended_loop = bool(node.else_) or \799 # when calculating the nodes for the inner frame we have to exclude800 # the iterator contents from it801 children = node.iter_child_nodes(exclude=('iter',))802803 if node.recursive:804 loop_frame = self.function_scoping(node, frame, children,805 find_special=False)806 else:807 loop_frame = frame.inner()808 loop_frame.inspect(children)809810 extended_loop = node.recursive or node.else_ or \811 'loop' in loop_frame.identifiers.undeclared812 if extended_loop:813 loop_frame.identifiers.add_special('loop')814815 # if we don't have an recursive loop we have to find the shadowed816 # variables at that point817 if not node.recursive:801 aliases = self.collect_shadowed(loop_frame)818 aliases = self.collect_shadowed(loop_frame)819820 # otherwise we set up a buffer and add a function def821 else:822 loop_frame.buffer = buf = self.temporary_identifier()823 self.writeline('def loop(reciter, loop_render_func):', node)824 self.indent()825 self.writeline('%s = []' % buf, node)826 aliases = {}827828 self.pull_locals(loop_frame)829 if node.else_:830 self.writeline('l_loop = None')831
18 unmodified lines
850 self.visit(node.target, loop_frame)851 self.write(' for ')852 self.visit(node.target, loop_frame)853 self.write(' in ')854 if node.recursive:855 self.write('reciter')856 else:828 self.visit(node.iter, loop_frame)857 self.visit(node.iter, loop_frame)858 self.write(' if (')859 test_frame = loop_frame.copy()860 self.writeline('l_loop = ' + parent_loop)861 self.visit(node.test, test_frame)862 self.write('))')863864 elif node.recursive:865 self.write('reciter')866 else:867 self.visit(node.iter, loop_frame)868869 if node.recursive:870 self.write(', recurse=loop_render_func):')871 else:838 self.write(extended_loop and '):' or ':')872 self.write(extended_loop and '):' or ':')873874 # tests in not extended loops become a continue875 if not extended_loop and node.test is not None:876 self.indent()
18 unmodified lines
895 # reset the aliases if there are any.896 for name, alias in aliases.iteritems():897 self.writeline('l_%s = %s' % (name, alias))898899 # if the node was recursive we have to return the buffer contents900 # and start the iteration code901 if node.recursive:902 if self.environment.autoescape:903 self.writeline('return Markup(concat(%s))' % buf)904 else:905 self.writeline('return concat(%s)' % buf)906 self.outdent()907 if frame.buffer is None:908 self.writeline('yield loop(', node)909 else:910 self.writeline('%s.append(loop(' % frame.buffer, node)911 self.visit(node.iter, frame)912 self.write(', loop)')913 if frame.buffer is not None:914 self.write(')')915916 def visit_If(self, node, frame):917 if_frame = frame.soft()918 self.writeline('if ', node)919 self.visit(node.test, if_frame)
130 unmodified lines
1050 else:1051 body.append([const])10521053 # if we have less than 3 nodes or less than 6 and a buffer we1003 # yield or extend1054 # yield or extend/append1055 if len(body) < 3 or (frame.buffer is not None and len(body) < 6):1056 if frame.buffer is not None:1057 # for one item we append, for more we extend1058 if len(body) == 1:1059 self.writeline('%s.append(' % frame.buffer)1060 else:1006 self.writeline('%s.extend((' % frame.buffer)1061 self.writeline('%s.extend((' % frame.buffer)1062 for item in body:1063 if isinstance(item, list):1064 val = repr(concat(item))1065 if frame.buffer is None:
15 unmodified lines
1081 self.write(')' * close)1082 if frame.buffer is not None:1083 self.write(', ')1084 if frame.buffer is not None:1030 self.write('))')1085 # close the open parentheses1086 self.write(len(body) == 1 and ')' or '))')10871088 # otherwise we create a format string as this is faster in that case1089 else:1090 format = []
247 unmodified lines
modified:jinja2/runtime.py+8 added-1 removed
176 unmodified lines
177178class LoopContext(object):179 """A loop context for dynamic iteration."""180181 def __init__(self, iterable, enforce_length=False):181 def __init__(self, iterable, enforce_length=False, recurse=None):182 self._iterable = iterable183 self._next = iter(iterable).next184 self._length = None185 self._recurse = recurse186 self.index0 = -1187 if enforce_length:188 len(self)189
14 unmodified lines
204205 def __iter__(self):206 return self207208 def __call__(self, iterable):209 if self._recurse is None:210 raise TypeError('Tried to call non recursive loop. Maybe you '211 'forgot the "recursive" keyword.')212 return self._recurse(iterable, self._recurse)213214 def next(self):215 self.index0 += 1216 return self._next(), self217
203 unmodified lines