From: Dag Sverre Seljebotn Date: Fri, 16 May 2008 16:09:01 +0000 (+0200) Subject: Added Node.clone_node utility. X-Git-Tag: 0.9.8rc1~11^2~10^2~21 X-Git-Url: http://git.tremily.us/gitweb.cgi?a=commitdiff_plain;h=6f5104eb2f342a7fae4942465282200fb7eea9ae;p=cython.git Added Node.clone_node utility. A method for cloning nodes. I expect this one to work on all descandants, but it can be overriden if a node has special needs. It seems natural to put such core functionality in the node classes rather than in a visitor. --- diff --git a/Cython/Compiler/Nodes.py b/Cython/Compiler/Nodes.py index 352ee77e..c44fc00f 100644 --- a/Cython/Compiler/Nodes.py +++ b/Cython/Compiler/Nodes.py @@ -2,7 +2,7 @@ # Pyrex - Parse tree nodes # -import string, sys, os, time +import string, sys, os, time, copy import Code from Errors import error, warning, InternalError @@ -150,6 +150,19 @@ class Node(object): If you override get_child_accessors then this method is not used.""" return self.child_attrs + def clone_node(self): + """Clone the node. This is defined as a shallow copy, except for member lists + amongst the child attributes (from get_child_accessors) which are also + copied. Lists containing child nodes are thus seen as a way for the node + to hold multiple children directly; the list is not treated as a seperate + level in the tree.""" + c = copy.copy(self) + for acc in c.get_child_accessors(): + value = acc.get() + if isinstance(value, list): + acc.set([x for x in value]) + return c + # # There are 4 phases of parse tree processing, applied in order to