Commits

Antonio Cuni committed 084d689

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

Comments (0)

Files changed (4)

pypy/module/__builtin__/app_functional.py

 
 # ____________________________________________________________
 
-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):

pypy/objspace/std/listobject.py

     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)
         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
 
     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

pypy/objspace/std/listtype.py

                         ' 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')

pypy/objspace/std/test/test_listobject.py

         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']