[Python-checkins] r52881 - sandbox/trunk/2to3/fix_has_key.py sandbox/trunk/2to3/fix_print.py sandbox/trunk/2to3/pytree.py

guido.van.rossum python-checkins at python.org
Fri Dec 1 00:23:28 CET 2006


Author: guido.van.rossum
Date: Fri Dec  1 00:23:28 2006
New Revision: 52881

Modified:
   sandbox/trunk/2to3/fix_has_key.py
   sandbox/trunk/2to3/fix_print.py
   sandbox/trunk/2to3/pytree.py
Log:
Change replace() API to not require passing in the parent.
Also, n.replace(None) deletes n from its parent.


Modified: sandbox/trunk/2to3/fix_has_key.py
==============================================================================
--- sandbox/trunk/2to3/fix_has_key.py	(original)
+++ sandbox/trunk/2to3/fix_has_key.py	Fri Dec  1 00:23:28 2006
@@ -101,7 +101,7 @@
     new = pytree.Node(syms.comparison,
                       (arg, n_in, pytree.Node(syms.power, nodes[:i])))
     # XXX Sometimes we need to parenthesize arg or new.  Later.
-    parent.parent.replace(parent, new)
+    parent.replace(new)
 
 def diff(fn, tree):
     f = open("@", "w")

Modified: sandbox/trunk/2to3/fix_print.py
==============================================================================
--- sandbox/trunk/2to3/fix_print.py	(original)
+++ sandbox/trunk/2to3/fix_print.py	Fri Dec  1 00:23:28 2006
@@ -94,7 +94,7 @@
     n_stmt = pytree.Node(syms.power, (n_print, n_trailer))
     n_stmt.set_prefix(node.get_prefix())
     print "Synthesized: >>>%s<<<" % n_stmt
-    node.parent.replace(node, n_stmt)
+    node.replace(n_stmt)
 
 
 def add_kwarg(l_nodes, s_kwd, n_expr):

Modified: sandbox/trunk/2to3/pytree.py
==============================================================================
--- sandbox/trunk/2to3/pytree.py	(original)
+++ sandbox/trunk/2to3/pytree.py	Fri Dec  1 00:23:28 2006
@@ -70,19 +70,24 @@
             return ""
         return self.children[0].get_prefix()
 
-    def replace(self, old, new):
+    def replace(self, new):
+        if self is new:
+            return
         l_children = []
         found = False
-        for ch in self.children:
-            if ch is old:
-                assert not found, (self.children, old, new)
-                l_children.append(new)
+        for ch in self.parent.children:
+            if ch is self:
+                assert not found, (self.parent.children, self, new)
+                if new is not None:
+                    l_children.append(new)
                 found = True
             else:
                 l_children.append(ch)
-        assert found, (self.children, old, new)
-        self.children = tuple(l_children)
-        new.parent = self
+        assert found, (self.children, self, new)
+        self.parent.children = tuple(l_children)
+        if new is not None:
+            new.parent = self.parent
+        self.parent = None
 
 
 class Leaf(Base):


More information about the Python-checkins mailing list