added broken copy method
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 cb3bd65 · Browse files · 2 files·+28 added−5 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+28 added−5 removed
- other:+28 added−5 removed
Provenance
- Commit
0ecb859- Tree
7c9d8ef- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
cb3bd65
2 files changed+28 added−5 removed
modified:jinja2/nodes.py+24 added
110 unmodified lines111 yield child112 for result in child.find_all(node_type):113 yield result114 115+ def copy(self):116+ """Return a deep copy of the node."""117+ result = object.__new__(self.__class__)118+ for field, value in self.iter_fields():119+ if isinstance(value, Node):120+ new_value = value.copy()121+ elif isinstance(value, list):122+ new_value = []123+ for item in new_value:124+ if isinstance(item, Node):125+ item = item.copy()126+ else:127+ item = copy(item)128+ new_value.append(item)129+ else:130+ new_value = copy(new_value)131+ setattr(result, field, new_value)132+ for attr in self.attributes:133+ try:134+ setattr(result, attr, getattr(self, attr))135+ except AttributeError:136+ pass137+ return result138+139 def set_ctx(self, ctx):140 """Reset the context of a node and all child nodes. Per default the141 parser will all generate nodes that have a 'load' context as it's the142 most common one. This method is used in the parser to set assignment413 unmodified linesmodified:jinja2/optimizer.py+4 added-5 removed
17 unmodified lines18 19 :copyright: Copyright 2008 by Christoph Hack.20 :license: GNU GPL.21 """22-from copy import deepcopy22 from jinja2 import nodes23 from jinja2.visitor import NodeVisitor, NodeTransformer24 from jinja2.runtime import subscribe, LoopContext25 74 unmodified lines100 try:101 for loop, item in LoopContext(iterable, parent, True):102 context['loop'] = loop.make_static()103 assign(node.target, item)105- result.extend(self.visit(n, context)106- for n in deepcopy(node.body))104+ result.extend(self.visit(n.copy(), context)105+ for n in node.body)106 iterated = True107 if not iterated and node.else_:109- result.extend(self.visit(n, context)110- for n in deepcopy(node.else_))108+ result.extend(self.visit(n.copy(), context)109+ for n in node.else_)110 except nodes.Impossible:111 return node112 finally:113 context.pop()74 unmodified lines