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

collin.winter python-checkins at python.org
Wed Mar 21 21:56:36 CET 2007


Author: collin.winter
Date: Wed Mar 21 21:56:31 2007
New Revision: 54498

Modified:
   sandbox/trunk/2to3/pytree.py
   sandbox/trunk/2to3/tests/test_pytree.py
Log:
Add a prefix keyword argument to Node and Leaf constructors.

Modified: sandbox/trunk/2to3/pytree.py
==============================================================================
--- sandbox/trunk/2to3/pytree.py	(original)
+++ sandbox/trunk/2to3/pytree.py	Wed Mar 21 21:56:31 2007
@@ -137,7 +137,7 @@
 
     """Concrete implementation for interior nodes."""
 
-    def __init__(self, type, children, context=None):
+    def __init__(self, type, children, context=None, prefix=None):
         """Initializer.
 
         Takes a type constant (a symbol number >= 256), a sequence of
@@ -151,6 +151,8 @@
         for ch in self.children:
             assert ch.parent is None, repr(ch)
             ch.parent = self
+        if prefix is not None:
+            self.set_prefix(prefix)
 
     def __repr__(self):
         """Returns a canonical string representation."""
@@ -207,7 +209,7 @@
     lineno = 0   # Line where this token starts in the input
     column = 0   # Column where this token tarts in the input
 
-    def __init__(self, type, value, context=None):
+    def __init__(self, type, value, context=None, prefix=None):
         """Initializer.
 
         Takes a type constant (a token number < 256), a string value,
@@ -218,6 +220,8 @@
             self.prefix, (self.lineno, self.column) = context
         self.type = type
         self.value = value
+        if prefix is not None:
+            self.prefix = prefix
 
     def __repr__(self):
         """Returns a canonical string representation."""

Modified: sandbox/trunk/2to3/tests/test_pytree.py
==============================================================================
--- sandbox/trunk/2to3/tests/test_pytree.py	(original)
+++ sandbox/trunk/2to3/tests/test_pytree.py	Wed Mar 21 21:56:31 2007
@@ -163,6 +163,22 @@
         self.failUnless(n1.was_changed)
         self.failUnless(n2.was_changed)
         self.failIf(l1.was_changed)
+        
+    def testLeafConstructorPrefix(self):
+        for prefix in ("xyz_", ""):
+            l1 = pytree.Leaf(100, "self", prefix=prefix)
+            self.failUnless(str(l1), prefix + "self")
+            self.assertEqual(l1.get_prefix(), prefix)
+        
+    def testNodeConstructorPrefix(self):
+        for prefix in ("xyz_", ""):
+            l1 = pytree.Leaf(100, "self")
+            l2 = pytree.Leaf(100, "foo", prefix="_")
+            n1 = pytree.Node(1000, [l1, l2], prefix=prefix)
+            self.failUnless(str(n1), prefix + "self_foo")
+            self.assertEqual(n1.get_prefix(), prefix)
+            self.assertEqual(l1.get_prefix(), prefix)
+            self.assertEqual(l2.get_prefix(), "_")
 
 
 class TestPatterns(support.TestCase):


More information about the Python-checkins mailing list