[Python-checkins] cpython (3.2): Fix __hash__ in functools.cmp_to_key() to work with collections.Hashable.

raymond.hettinger python-checkins at python.org
Tue May 3 20:16:49 CEST 2011


http://hg.python.org/cpython/rev/2675415c57c1
changeset:   69810:2675415c57c1
branch:      3.2
parent:      69808:5e661f5c301a
user:        Raymond Hettinger <python at rcn.com>
date:        Tue May 03 11:01:32 2011 -0700
summary:
  Fix __hash__ in functools.cmp_to_key() to work with collections.Hashable.

files:
  Lib/functools.py           |  3 +--
  Lib/test/test_functools.py |  5 ++++-
  Misc/NEWS                  |  2 ++
  3 files changed, 7 insertions(+), 3 deletions(-)


diff --git a/Lib/functools.py b/Lib/functools.py
--- a/Lib/functools.py
+++ b/Lib/functools.py
@@ -111,8 +111,7 @@
             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")
diff --git a/Lib/test/test_functools.py b/Lib/test/test_functools.py
--- a/Lib/test/test_functools.py
+++ b/Lib/test/test_functools.py
@@ -1,4 +1,5 @@
 import functools
+import collections
 import sys
 import unittest
 from test import support
@@ -446,7 +447,8 @@
             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 @@
         TestPythonPartial,
         TestUpdateWrapper,
         TestTotalOrdering,
+        TestCmpToKey,
         TestWraps,
         TestReduce,
         TestLRU,
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -81,6 +81,8 @@
 
 - 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.
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list