From 0ecb859b7650f504ba694ed678d79be9d3b35c87 Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Wed, 9 Apr 2008 16:29:47 +0200 Subject: [PATCH] added broken copy method --HG-- branch : trunk --- jinja2/nodes.py | 24 ++++++++++++++++++++++++ jinja2/optimizer.py | 9 ++++----- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/jinja2/nodes.py b/jinja2/nodes.py index 72edeb6..911f9fc 100644 --- a/jinja2/nodes.py +++ b/jinja2/nodes.py @@ -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 diff --git a/jinja2/optimizer.py b/jinja2/optimizer.py index 13b0fbc..e16961f 100644 --- a/jinja2/optimizer.py +++ b/jinja2/optimizer.py @@ -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: -- 2.26.2