summaryrefslogtreecommitdiffstats
path: root/Lib/test/list_tests.py
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2008-01-30 20:15:17 (GMT)
committerRaymond Hettinger <python@rcn.com>2008-01-30 20:15:17 (GMT)
commit70b64fce96e894dfa8af5afd1f8b3fe863ba16e0 (patch)
tree15d549753edea1f11c0a3990ea0419426220c445 /Lib/test/list_tests.py
parent4f066126d616d35aa8c0911a915ad64a09aaf16e (diff)
downloadcpython-70b64fce96e894dfa8af5afd1f8b3fe863ba16e0.zip
cpython-70b64fce96e894dfa8af5afd1f8b3fe863ba16e0.tar.gz
cpython-70b64fce96e894dfa8af5afd1f8b3fe863ba16e0.tar.bz2
Issue #1771: Remove cmp parameter from list.sort() and builtin.sorted().
Diffstat (limited to 'Lib/test/list_tests.py')
-rw-r--r--Lib/test/list_tests.py17
1 files changed, 12 insertions, 5 deletions
diff --git a/Lib/test/list_tests.py b/Lib/test/list_tests.py
index f03cdfe..49283e4 100644
--- a/Lib/test/list_tests.py
+++ b/Lib/test/list_tests.py
@@ -8,6 +8,15 @@ import os
import unittest
from test import test_support, seq_tests
+def CmpToKey(mycmp):
+ 'Convert a cmp= function into a key= function'
+ class K(object):
+ def __init__(self, obj):
+ self.obj = obj
+ def __lt__(self, other):
+ return mycmp(self.obj, other.obj) == -1
+ return K
+
class CommonTest(seq_tests.CommonTest):
def test_init(self):
@@ -430,23 +439,21 @@ class CommonTest(seq_tests.CommonTest):
def revcmp(a, b):
return cmp(b, a)
- u.sort(revcmp)
+ u.sort(key=CmpToKey(revcmp))
self.assertEqual(u, self.type2test([2,1,0,-1,-2]))
# The following dumps core in unpatched Python 1.5:
def myComparison(x,y):
return cmp(x%3, y%7)
z = self.type2test(range(12))
- z.sort(myComparison)
+ z.sort(key=CmpToKey(myComparison))
self.assertRaises(TypeError, z.sort, 2)
def selfmodifyingComparison(x,y):
z.append(1)
return cmp(x, y)
- self.assertRaises(ValueError, z.sort, selfmodifyingComparison)
-
- self.assertRaises(TypeError, z.sort, lambda x, y: 's')
+ self.assertRaises(ValueError, z.sort, key=CmpToKey(selfmodifyingComparison))
self.assertRaises(TypeError, z.sort, 42, 42, 42, 42)