]> granicus.if.org Git - python/commitdiff
Fix __hash__ in functools.cmp_to_key() to work with collections.Hashable.
authorRaymond Hettinger <python@rcn.com>
Tue, 3 May 2011 18:01:32 +0000 (11:01 -0700)
committerRaymond Hettinger <python@rcn.com>
Tue, 3 May 2011 18:01:32 +0000 (11:01 -0700)
Lib/functools.py
Lib/test/test_functools.py
Misc/NEWS

index e92a2fcd68b97ad8305882933d79abe47313d43e..90642a58b2dbe680a5c367694d7a74f22aa58437 100644 (file)
@@ -111,8 +111,7 @@ def cmp_to_key(mycmp):
             return mycmp(self.obj, other.obj) >= 0
         def __ne__(self, other):
             return mycmp(self.obj, other.obj) != 0
-        def __hash__(self):
-            raise TypeError('hash not implemented')
+        __hash__ = None
     return K
 
 _CacheInfo = namedtuple("CacheInfo", "hits misses maxsize currsize")
index 73a77d63f27b5f1484c22a2f617349ed795a9fcb..7d11b53a71c3c7c3329067f7c53b20ece02d29f2 100644 (file)
@@ -1,4 +1,5 @@
 import functools
+import collections
 import sys
 import unittest
 from test import support
@@ -446,7 +447,8 @@ class TestCmpToKey(unittest.TestCase):
             return y - x
         key = functools.cmp_to_key(mycmp)
         k = key(10)
-        self.assertRaises(TypeError, hash(k))
+        self.assertRaises(TypeError, hash, k)
+        self.assertFalse(isinstance(k, collections.Hashable))
 
 class TestTotalOrdering(unittest.TestCase):
 
@@ -660,6 +662,7 @@ def test_main(verbose=None):
         TestPythonPartial,
         TestUpdateWrapper,
         TestTotalOrdering,
+        TestCmpToKey,
         TestWraps,
         TestReduce,
         TestLRU,
index 8ca2f02bfc04387a9a433f87172880b42553f926..f77befd332e746584685b4a89ef3a2ac33171a5a 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -81,6 +81,8 @@ Library
 
 - logging: don't define QueueListener if Python has no thread support.
 
+- functools.cmp_to_key() now works with collections.Hashable().
+
 - Issue #11277: mmap.mmap() calls fcntl(fd, F_FULLFSYNC) on Mac OS X to get
   around a mmap bug with sparse files. Patch written by Steffen Daode Nurpmeso.