[Python-checkins] r54722 - in sandbox/trunk/2to3: fixes/fix_except.py fixes/fix_intern.py fixes/fix_raise.py fixes/fix_throw.py fixes/util.py pytree.py tests/test_pytree.py tests/test_util.py

collin.winter python-checkins at python.org
Mon Apr 9 03:49:31 CEST 2007


Author: collin.winter
Date: Mon Apr  9 03:49:30 2007
New Revision: 54722

Modified:
   sandbox/trunk/2to3/fixes/fix_except.py
   sandbox/trunk/2to3/fixes/fix_intern.py
   sandbox/trunk/2to3/fixes/fix_raise.py
   sandbox/trunk/2to3/fixes/fix_throw.py
   sandbox/trunk/2to3/fixes/util.py
   sandbox/trunk/2to3/pytree.py
   sandbox/trunk/2to3/tests/test_pytree.py
   sandbox/trunk/2to3/tests/test_util.py
Log:
Convert node.children from a tuple to a list. Having it be a tuple meant jumping through casting hoops in order to modify the children; a list will enable more direct, more readable child manipulation.


Modified: sandbox/trunk/2to3/fixes/fix_except.py
==============================================================================
--- sandbox/trunk/2to3/fixes/fix_except.py	(original)
+++ sandbox/trunk/2to3/fixes/fix_except.py	Mon Apr  9 03:49:30 2007
@@ -67,7 +67,7 @@
                     # Insert "old_N = new_N" as the first statement in
                     #  the except body. This loop skips leading whitespace
                     #  and indents
-                    suite_stmts = list(e_suite.children)
+                    suite_stmts = e_suite.children
                     for i, stmt in enumerate(suite_stmts):
                         if isinstance(stmt, pytree.Node):
                             break
@@ -81,7 +81,7 @@
 
                     assign.parent = e_suite
                     suite_stmts = suite_stmts[:i] + [assign] + suite_stmts
-                    e_suite.children = tuple(suite_stmts)
+                    e_suite.children = suite_stmts
 
         children = [c.clone() for c in node.children[:3]] + try_cleanup
         return pytree.Node(node.type, children)

Modified: sandbox/trunk/2to3/fixes/fix_intern.py
==============================================================================
--- sandbox/trunk/2to3/fixes/fix_intern.py	(original)
+++ sandbox/trunk/2to3/fixes/fix_intern.py	Mon Apr  9 03:49:30 2007
@@ -35,13 +35,12 @@
             newarglist = pytree.Node(syms.arglist, [obj.clone()])
         after = results["after"]
         if after:
-            after = tuple([n.clone() for n in after])
+            after = [n.clone() for n in after]
         new = pytree.Node(syms.power,
                           Attr(Name("sys"), Name("intern")) +
-                          (pytree.Node(syms.trailer,
+                          [pytree.Node(syms.trailer,
                                        [results["lpar"].clone(),
                                         newarglist,
-                                        results["rpar"].clone()]),)
-                          + after)
+                                        results["rpar"].clone()])] + after)
         new.set_prefix(node.get_prefix())
         return new

Modified: sandbox/trunk/2to3/fixes/fix_raise.py
==============================================================================
--- sandbox/trunk/2to3/fixes/fix_raise.py	(original)
+++ sandbox/trunk/2to3/fixes/fix_raise.py	Mon Apr  9 03:49:30 2007
@@ -73,10 +73,8 @@
             tb.set_prefix("")
             
             e = Call(exc, args)
-            with_tb = Attr(e, Name('with_traceback'))
-            call_wtb = list(with_tb + (ArgList([tb]),))            
-
-            new = pytree.Node(syms.simple_stmt, [Name("raise")] + call_wtb)
+            with_tb = Attr(e, Name('with_traceback')) + [ArgList([tb])]
+            new = pytree.Node(syms.simple_stmt, [Name("raise")] + with_tb)
             new.set_prefix(node.get_prefix())
             return new
         else:

Modified: sandbox/trunk/2to3/fixes/fix_throw.py
==============================================================================
--- sandbox/trunk/2to3/fixes/fix_throw.py	(original)
+++ sandbox/trunk/2to3/fixes/fix_throw.py	Mon Apr  9 03:49:30 2007
@@ -52,9 +52,7 @@
             tb.set_prefix("")
             
             e = Call(exc, args)
-            with_tb = Attr(e, Name('with_traceback'))
-            call_wtb = list(with_tb + (ArgList([tb]),))
-            
-            throw_args.replace(pytree.Node(syms.power, call_wtb))
+            with_tb = Attr(e, Name('with_traceback')) + [ArgList([tb])]
+            throw_args.replace(pytree.Node(syms.power, with_tb))
         else:
             throw_args.replace(Call(exc, args))

Modified: sandbox/trunk/2to3/fixes/util.py
==============================================================================
--- sandbox/trunk/2to3/fixes/util.py	(original)
+++ sandbox/trunk/2to3/fixes/util.py	Mon Apr  9 03:49:30 2007
@@ -23,13 +23,13 @@
 
 def Assign(target, source):
     """Build an assignment statement"""
-    if not isinstance(target, tuple):
-        target = (target,)
-    if not isinstance(source, tuple):
+    if not isinstance(target, list):
+        target = [target]
+    if not isinstance(source, list):
         source.set_prefix(" ")
-        source = (source,)
+        source = [source]
 
-    return Node(syms.atom, target + (ass_leaf.clone(),) + source)
+    return Node(syms.atom, target + [ass_leaf.clone()] + source)
 
 def Name(name, prefix=None):
     """Return a NAME leaf"""
@@ -37,9 +37,8 @@
 
 def Attr(obj, attr):
     """A node tuple for obj.attr"""
-    return (obj,
-            Node(syms.trailer, [Leaf(token.DOT, '.'),
-                                attr]))
+    return [obj,
+            Node(syms.trailer, [Leaf(token.DOT, '.'), attr])]
 
 def Comma():
     """A comma leaf"""
@@ -157,9 +156,8 @@
             return child
         elif child.type == syms.simple_stmt:
             if child.children[0].type == syms.expr_stmt:
-                n = _find(name, child.children[0].children[0])
-                if n:
-                    return n
+                if _find(name, child.children[0].children[0]):
+                    return child.children[0]
 
 _block_syms = set([syms.funcdef, syms.classdef, syms.trailer])
 def _find(name, node):

Modified: sandbox/trunk/2to3/pytree.py
==============================================================================
--- sandbox/trunk/2to3/pytree.py	(original)
+++ sandbox/trunk/2to3/pytree.py	Mon Apr  9 03:49:30 2007
@@ -159,7 +159,7 @@
         """
         assert type >= 256, type
         self.type = type
-        self.children = tuple(children)
+        self.children = list(children)
         for ch in self.children:
             assert ch.parent is None, repr(ch)
             ch.parent = self
@@ -435,7 +435,7 @@
             assert content is None, repr(content)
         if content is not None:
             assert not isinstance(content, basestring), repr(content)
-            content = tuple(content)
+            content = list(content)
             for i, item in enumerate(content):
                 assert isinstance(item, BasePattern), (i, item)
                 if isinstance(item, WildcardPattern):
@@ -548,7 +548,7 @@
                 if results is not None:
                     results.update(r)
                     if self.name:
-                        results[self.name] = tuple(nodes)
+                        results[self.name] = list(nodes)
                 return True
         return False
 

Modified: sandbox/trunk/2to3/tests/test_pytree.py
==============================================================================
--- sandbox/trunk/2to3/tests/test_pytree.py	(original)
+++ sandbox/trunk/2to3/tests/test_pytree.py	Mon Apr  9 03:49:30 2007
@@ -75,14 +75,14 @@
         l2 = pytree.Leaf(200, "bar")
         n1 = pytree.Node(1000, [l1, l2])
         self.assertEqual(n1.type, 1000)
-        self.assertEqual(n1.children, (l1, l2))
+        self.assertEqual(n1.children, [l1, l2])
 
     def testNodeRepr(self):
         l1 = pytree.Leaf(100, "foo")
         l2 = pytree.Leaf(100, "bar", context=(" ", (1, 0)))
         n1 = pytree.Node(1000, [l1, l2])
         self.assertEqual(repr(n1),
-                         "Node(1000, (%s, %s))" % (repr(l1), repr(l2)))
+                         "Node(1000, [%s, %s])" % (repr(l1), repr(l2)))
 
     def testNodeStr(self):
         l1 = pytree.Leaf(100, "foo")
@@ -121,7 +121,7 @@
         l2 = pytree.Leaf(100, "+")
         l3 = pytree.Leaf(100, "bar")
         n1 = pytree.Node(1000, [l1, l2, l3])
-        self.assertEqual(n1.children, (l1, l2, l3))
+        self.assertEqual(n1.children, [l1, l2, l3])
         self.failIf(n1.was_changed)
         l2new = pytree.Leaf(100, "-")
         l2.replace(l2new)
@@ -271,12 +271,12 @@
         self.assertEqual(sorted(r.keys()), ["pl", "pn", "pw"])
         self.assertEqual(r["pl"], l1)
         self.assertEqual(r["pn"], n2)
-        self.assertEqual(r["pw"], (n2,))
+        self.assertEqual(r["pw"], [n2])
         # But this is equivalent
-        self.assertEqual(r, {"pl": l1, "pn": n2, "pw": (n2,)})
+        self.assertEqual(r, {"pl": l1, "pn": n2, "pw": [n2]})
         r = {}
         self.assertEqual(pw.match_seq([l1, l3], r), True)
-        self.assertEqual(r, {"pl": l3, "pw": (l1, l3)})
+        self.assertEqual(r, {"pl": l3, "pw": [l1, l3]})
         self.assert_(r["pl"] is l3)
         r = {}
 
@@ -306,7 +306,7 @@
         c, r = matches[0]
         self.assertEqual(c, 1)
         self.assertEqual(str(r["pr"]), "abcdef")
-        self.assertEqual(r["pw"], (la, lb, lc, ld, le, lf))
+        self.assertEqual(r["pw"], [la, lb, lc, ld, le, lf])
         for c in "abcdef":
             self.assertEqual(r["p" + c], pytree.Leaf(1, c))
 
@@ -318,10 +318,10 @@
         l1 = pytree.Leaf(7, "(")
         l2 = pytree.Leaf(3, "x")
         l3 = pytree.Leaf(8, ")")
-        node = pytree.Node(331, (l1, l2, l3))
+        node = pytree.Node(331, [l1, l2, l3])
         r = {}
         self.assert_(pattern.match(node, r))
-        self.assertEqual(r["args"], (l2,))
+        self.assertEqual(r["args"], [l2])
 
 
 if __name__ == "__main__":

Modified: sandbox/trunk/2to3/tests/test_util.py
==============================================================================
--- sandbox/trunk/2to3/tests/test_util.py	(original)
+++ sandbox/trunk/2to3/tests/test_util.py	Mon Apr  9 03:49:30 2007
@@ -71,7 +71,7 @@
         from fixes.util import Attr, Name
         
         attr = Attr(Name("a"), Name("b"))
-        self.assertEqual(type(attr), tuple)
+        self.assertEqual(type(attr), list)
 
         
 class Test_Name(MacroTestCase):


More information about the Python-checkins mailing list