[Python-checkins] r57907 - in sandbox/trunk/2to3: fixes/fix_sort.py tests/test_fixers.py

collin.winter python-checkins at python.org
Sun Sep 2 21:10:04 CEST 2007


Author: collin.winter
Date: Sun Sep  2 21:10:02 2007
New Revision: 57907

Modified:
   sandbox/trunk/2to3/fixes/fix_sort.py
   sandbox/trunk/2to3/tests/test_fixers.py
Log:
Expand the reach of fix_sort.

Modified: sandbox/trunk/2to3/fixes/fix_sort.py
==============================================================================
--- sandbox/trunk/2to3/fixes/fix_sort.py	(original)
+++ sandbox/trunk/2to3/fixes/fix_sort.py	Sun Sep  2 21:10:02 2007
@@ -1,21 +1,27 @@
 """Change the two-line list/sort idiom into the modern sorted() call.
 
-That is,
+That is, both
 
-    v = list(t)
-    v.sorted
+    v = list(t())
+    v.sort()
+    foo(v)
+
+and
+
+    v = t()
+    v.sort()
     foo(v)
     
 becomes
 
-    v = sorted(t)
+    v = sorted(t())
     foo(v)
 """
 # Author: Collin Winter
 
 # Local imports
 from fixes import basefix
-from fixes.util import Name
+from fixes.util import Name, Call
 
 
 class FixSort(basefix.BaseFix):
@@ -38,6 +44,19 @@
                   >
                   next=any*
               >
+              |
+              any<
+                  any*
+                  simple_stmt< expr_stmt< id1=any '=' expr=any > '\n' >
+                  sort=
+                  simple_stmt<
+                    power< id2=any
+                           trailer< '.' 'sort' > trailer< '(' ')' >
+                    >
+                    '\n'
+                  >
+                  next=any*
+              >
               """
 
     def match(self, node):
@@ -50,10 +69,19 @@
 
     def transform(self, node, results):
         sort_stmt = results["sort"]
-        list_call = results["list"]
         next_stmt = results["next"]
+        list_call = results.get("list")
+        simple_expr = results.get("expr")
 
-        list_call.replace(Name("sorted", prefix=list_call.get_prefix()))
+        if list_call:
+            list_call.replace(Name("sorted", prefix=list_call.get_prefix()))
+        elif simple_expr:
+            new = simple_expr.clone()
+            new.set_prefix("")
+            simple_expr.replace(Call(Name("sorted"), [new],
+                                     prefix=simple_expr.get_prefix()))
+        else:
+            raise RuntimeError("should not have reached here")
         sort_stmt.remove()
         if next_stmt:
             next_stmt[0].set_prefix(sort_stmt.get_prefix())

Modified: sandbox/trunk/2to3/tests/test_fixers.py
==============================================================================
--- sandbox/trunk/2to3/tests/test_fixers.py	(original)
+++ sandbox/trunk/2to3/tests/test_fixers.py	Sun Sep  2 21:10:02 2007
@@ -2381,7 +2381,7 @@
 class Test_sort(FixerTestCase):
     fixer = "sort"
 
-    def test(self):
+    def test_list_call(self):
         b = """
             v = list(t)
             v.sort()
@@ -2441,6 +2441,88 @@
             """
         self.check(b, a)
 
+    def test_simple_expr(self):
+        b = """
+            v = t
+            v.sort()
+            foo(v)
+            """
+        a = """
+            v = sorted(t)
+            foo(v)
+            """
+        self.check(b, a)
+
+        b = """
+            v = foo(b)
+            v.sort()
+            foo(v)
+            """
+        a = """
+            v = sorted(foo(b))
+            foo(v)
+            """
+        self.check(b, a)
+
+        b = """
+            v = b.keys()
+            v.sort()
+            foo(v)
+            """
+        a = """
+            v = sorted(b.keys())
+            foo(v)
+            """
+        self.check(b, a)
+
+        b = """
+            v = foo(b) + d
+            v.sort()
+            foo(v)
+            """
+        a = """
+            v = sorted(foo(b) + d)
+            foo(v)
+            """
+        self.check(b, a)
+
+        b = """
+            while x:
+                v = t
+                v.sort()
+                foo(v)
+            """
+        a = """
+            while x:
+                v = sorted(t)
+                foo(v)
+            """
+        self.check(b, a)
+
+        b = """
+            v = t
+            # foo
+            v.sort()
+            foo(v)
+            """
+        a = """
+            v = sorted(t)
+            # foo
+            foo(v)
+            """
+        self.check(b, a)
+
+        b = r"""
+            v =   t
+            v.sort()
+            foo(v)
+            """
+        a = r"""
+            v =   sorted(t)
+            foo(v)
+            """
+        self.check(b, a)
+
     def test_unchanged(self):
         s = """
             v = list(t)
@@ -2450,13 +2532,6 @@
         self.unchanged(s)
 
         s = """
-            v = list(t, u)
-            v.sort()
-            foo(v)
-            """
-        self.unchanged(s)
-
-        s = """
             v = list(t)
             v.sort(u)
             foo(v)


More information about the Python-checkins mailing list