[Python-checkins] r56388 - in sandbox/trunk/2to3: pytree.py tests/test_pytree.py

collin.winter python-checkins at python.org
Sun Jul 15 00:34:18 CEST 2007


Author: collin.winter
Date: Sun Jul 15 00:34:18 2007
New Revision: 56388

Modified:
   sandbox/trunk/2to3/   (props changed)
   sandbox/trunk/2to3/pytree.py
   sandbox/trunk/2to3/tests/test_pytree.py
Log:
Remove the ability to remove a node by passing None to replace(); add the ability to replace a node with a list of nodes.


Modified: sandbox/trunk/2to3/pytree.py
==============================================================================
--- sandbox/trunk/2to3/pytree.py	(original)
+++ sandbox/trunk/2to3/pytree.py	Sun Jul 15 00:34:18 2007
@@ -101,28 +101,26 @@
         raise NotImplementedError
 
     def replace(self, new):
-        """Replaces this node with a new one in the parent.
-
-        This can also be used to remove this node from the parent by
-        passing None.
-        """
+        """Replaces this node with a new one in the parent."""
         assert self.parent is not None, str(self)
-        assert new is None or new.parent is None, str(new)
+        assert new is not None
+        if not isinstance(new, list):
+            new = [new]
         l_children = []
         found = False
         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)
+                    l_children.extend(new)
                 found = True
             else:
                 l_children.append(ch)
         assert found, (self.children, self, new)
-        self.changed()
-        self.parent.children = tuple(l_children)
-        if new is not None:
-            new.parent = self.parent
+        self.parent.changed()
+        self.parent.children = l_children
+        for x in new:
+            x.parent = self.parent
         self.parent = None
 
     def get_lineno(self):

Modified: sandbox/trunk/2to3/tests/test_pytree.py
==============================================================================
--- sandbox/trunk/2to3/tests/test_pytree.py	(original)
+++ sandbox/trunk/2to3/tests/test_pytree.py	Sun Jul 15 00:34:18 2007
@@ -140,12 +140,24 @@
         l3 = pytree.Leaf(100, "bar")
         n1 = pytree.Node(1000, [l1, l2, l3])
         self.assertEqual(n1.children, [l1, l2, l3])
+        self.failUnless(isinstance(n1.children, list))
         self.failIf(n1.was_changed)
         l2new = pytree.Leaf(100, "-")
         l2.replace(l2new)
-        self.assertEqual(n1.children, (l1, l2new, l3))
+        self.assertEqual(n1.children, [l1, l2new, l3])
+        self.failUnless(isinstance(n1.children, list))
         self.failUnless(n1.was_changed)
 
+    def testReplaceWithList(self):
+        l1 = pytree.Leaf(100, "foo")
+        l2 = pytree.Leaf(100, "+")
+        l3 = pytree.Leaf(100, "bar")
+        n1 = pytree.Node(1000, [l1, l2, l3])
+
+        l2.replace([pytree.Leaf(100, "*"), pytree.Leaf(100, "*")])
+        self.assertEqual(str(n1), "foo**bar")
+        self.failUnless(isinstance(n1.children, list))
+
     def testConvert(self):
         # XXX
         pass


More information about the Python-checkins mailing list