added broken copy method
authorArmin Ronacher <armin.ronacher@active-4.com>
Wed, 9 Apr 2008 14:29:47 +0000 (16:29 +0200)
committerArmin Ronacher <armin.ronacher@active-4.com>
Wed, 9 Apr 2008 14:29:47 +0000 (16:29 +0200)
--HG--
branch : trunk

jinja2/nodes.py
jinja2/optimizer.py

index 72edeb62a73c5a9a46cb5c44db5eee472fbfe23d..911f9fc0dc5223f7847f62d528b93d8a7abecbe7 100644 (file)
@@ -112,6 +112,30 @@ class Node(object):
             for result in child.find_all(node_type):
                 yield result
 
+    def copy(self):
+        """Return a deep copy of the node."""
+        result = object.__new__(self.__class__)
+        for field, value in self.iter_fields():
+            if isinstance(value, Node):
+                new_value = value.copy()
+            elif isinstance(value, list):
+                new_value = []
+                for item in new_value:
+                    if isinstance(item, Node):
+                        item = item.copy()
+                    else:
+                        item = copy(item)
+                    new_value.append(item)
+            else:
+                new_value = copy(new_value)
+            setattr(result, field, new_value)
+        for attr in self.attributes:
+            try:
+                setattr(result, attr, getattr(self, attr))
+            except AttributeError:
+                pass
+        return result
+
     def set_ctx(self, ctx):
         """Reset the context of a node and all child nodes.  Per default the
         parser will all generate nodes that have a 'load' context as it's the
index 13b0fbc1e7d752864171e3b01eaf73b81bd418d6..e16961f737cf02db3a8314634baee6e3bfacf690 100644 (file)
@@ -19,7 +19,6 @@
     :copyright: Copyright 2008 by Christoph Hack.
     :license: GNU GPL.
 """
-from copy import deepcopy
 from jinja2 import nodes
 from jinja2.visitor import NodeVisitor, NodeTransformer
 from jinja2.runtime import subscribe, LoopContext
@@ -102,12 +101,12 @@ class Optimizer(NodeTransformer):
                 for loop, item in LoopContext(iterable, parent, True):
                     context['loop'] = loop.make_static()
                     assign(node.target, item)
-                    result.extend(self.visit(n, context)
-                                  for n in deepcopy(node.body))
+                    result.extend(self.visit(n.copy(), context)
+                                  for n in node.body)
                     iterated = True
                 if not iterated and node.else_:
-                    result.extend(self.visit(n, context)
-                                  for n in deepcopy(node.else_))
+                    result.extend(self.visit(n.copy(), context)
+                                  for n in node.else_)
             except nodes.Impossible:
                 return node
         finally: