[pypy-commit] pypy py3k: kill the cmp argument to list.sort() and sorted()

antocuni noreply at buildbot.pypy.org
Fri Mar 2 11:25:08 CET 2012


Author: Antonio Cuni <anto.cuni at gmail.com>
Branch: py3k
Changeset: r53097:084d689a182e
Date: 2012-03-02 11:18 +0100
http://bitbucket.org/pypy/pypy/changeset/084d689a182e/

Log:	kill the cmp argument to list.sort() and sorted()

diff --git a/pypy/module/__builtin__/app_functional.py b/pypy/module/__builtin__/app_functional.py
--- a/pypy/module/__builtin__/app_functional.py
+++ b/pypy/module/__builtin__/app_functional.py
@@ -11,10 +11,10 @@
 
 # ____________________________________________________________
 
-def sorted(lst, cmp=None, key=None, reverse=None):
-    "sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list"
+def sorted(lst, key=None, reverse=None):
+    "sorted(iterable, key=None, reverse=False) --> new sorted list"
     sorted_lst = list(lst)
-    sorted_lst.sort(cmp, key, reverse)
+    sorted_lst.sort(key=key, reverse=reverse)
     return sorted_lst
 
 def any(seq):
diff --git a/pypy/objspace/std/listobject.py b/pypy/objspace/std/listobject.py
--- a/pypy/objspace/std/listobject.py
+++ b/pypy/objspace/std/listobject.py
@@ -1364,20 +1364,6 @@
     def lt(self, a, b):
         return a < b
 
-class CustomCompareSort(SimpleSort):
-    def lt(self, a, b):
-        space = self.space
-        w_cmp = self.w_cmp
-        w_result = space.call_function(w_cmp, a, b)
-        try:
-            result = space.int_w(w_result)
-        except OperationError, e:
-            if e.match(space, space.w_TypeError):
-                raise OperationError(space.w_TypeError,
-                    space.wrap("comparison function must return int"))
-            raise
-        return result < 0
-
 class CustomKeySort(SimpleSort):
     def lt(self, a, b):
         assert isinstance(a, KeyContainer)
@@ -1385,24 +1371,16 @@
         space = self.space
         return space.is_true(space.lt(a.w_key, b.w_key))
 
-class CustomKeyCompareSort(CustomCompareSort):
-    def lt(self, a, b):
-        assert isinstance(a, KeyContainer)
-        assert isinstance(b, KeyContainer)
-        return CustomCompareSort.lt(self, a.w_key, b.w_key)
+def list_sort__List_ANY_ANY(space, w_list, w_keyfunc, w_reverse):
 
-def list_sort__List_ANY_ANY_ANY(space, w_list, w_cmp, w_keyfunc, w_reverse):
-
-    has_cmp = not space.is_w(w_cmp, space.w_None)
     has_key = not space.is_w(w_keyfunc, space.w_None)
     has_reverse = space.is_true(w_reverse)
 
     # create and setup a TimSort instance
-    if has_cmp:
-        if has_key:
-            sorterclass = CustomKeyCompareSort
-        else:
-            sorterclass = CustomCompareSort
+    if 0:
+        # this was the old "if has_cmp" path. We didn't remove the if not to
+        # diverge too much from default, to avoid spurious conflicts
+        pass
     else:
         if has_key:
             sorterclass = CustomKeySort
@@ -1415,7 +1393,6 @@
 
     sorter = sorterclass(w_list.getitems(), w_list.length())
     sorter.space = space
-    sorter.w_cmp = w_cmp
 
     try:
         # The list is temporarily made empty, so that mutations performed
diff --git a/pypy/objspace/std/listtype.py b/pypy/objspace/std/listtype.py
--- a/pypy/objspace/std/listtype.py
+++ b/pypy/objspace/std/listtype.py
@@ -24,10 +24,10 @@
                         ' occurrences of value')
 list_reverse  = SMM('reverse',1,
                     doc='L.reverse() -- reverse *IN PLACE*')
-list_sort     = SMM('sort',   4, defaults=(None, None, False),
-                    argnames=['cmp', 'key', 'reverse'],
-                    doc='L.sort(cmp=None, key=None, reverse=False) -- stable'
-                        ' sort *IN PLACE*;\ncmp(x, y) -> -1, 0, 1')
+list_sort     = SMM('sort',   3, defaults=(None, None, False),
+                    argnames=['key', 'reverse'],
+                    doc='L.sort(key=None, reverse=False) -- stable'
+                        ' sort *IN PLACE*')
 list_reversed = SMM('__reversed__', 1,
                     doc='L.__reversed__() -- return a reverse iterator over'
                         ' the list')
diff --git a/pypy/objspace/std/test/test_listobject.py b/pypy/objspace/std/test/test_listobject.py
--- a/pypy/objspace/std/test/test_listobject.py
+++ b/pypy/objspace/std/test/test_listobject.py
@@ -543,18 +543,6 @@
         l.sort()
         assert l == [1.1, 2.2, 3.1, 3.3, 4.4, 5.5]
 
-    def test_sort_cmp(self):
-        def lencmp(a,b): return cmp(len(a), len(b))
-        l = [ 'a', 'fiver', 'tre', '' ]
-        l.sort(lencmp)
-        assert l == ['', 'a', 'tre', 'fiver']
-        l = []
-        l.sort(lencmp)
-        assert l == []
-        l = [ 'a' ]
-        l.sort(lencmp)
-        assert l == [ 'a' ]
-
     def test_sort_key(self):
         def lower(x): return x.lower()
         l = ['a', 'C', 'b']


More information about the pypy-commit mailing list