Skip to content

Commit 003be52

Browse files
committed
Fix __hash__ in functools.cmp_to_key() to work with collections.Hashable.
1 parent 5098d44 commit 003be52

File tree

3 files changed

+7
-3
lines changed

3 files changed

+7
-3
lines changed

Lib/functools.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,7 @@ def __ge__(self, other):
111111
return mycmp(self.obj, other.obj) >= 0
112112
def __ne__(self, other):
113113
return mycmp(self.obj, other.obj) != 0
114-
def __hash__(self):
115-
raise TypeError('hash not implemented')
114+
__hash__ = None
116115
return K
117116

118117
_CacheInfo = namedtuple("CacheInfo", "hits misses maxsize currsize")

Lib/test/test_functools.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import functools
2+
import collections
23
import sys
34
import unittest
45
from test import support
@@ -446,7 +447,8 @@ def mycmp(x, y):
446447
return y - x
447448
key = functools.cmp_to_key(mycmp)
448449
k = key(10)
449-
self.assertRaises(TypeError, hash(k))
450+
self.assertRaises(TypeError, hash, k)
451+
self.assertFalse(isinstance(k, collections.Hashable))
450452

451453
class TestTotalOrdering(unittest.TestCase):
452454

@@ -660,6 +662,7 @@ def test_main(verbose=None):
660662
TestPythonPartial,
661663
TestUpdateWrapper,
662664
TestTotalOrdering,
665+
TestCmpToKey,
663666
TestWraps,
664667
TestReduce,
665668
TestLRU,

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ Library
8181

8282
- logging: don't define QueueListener if Python has no thread support.
8383

84+
- functools.cmp_to_key() now works with collections.Hashable().
85+
8486
- Issue #11277: mmap.mmap() calls fcntl(fd, F_FULLFSYNC) on Mac OS X to get
8587
around a mmap bug with sparse files. Patch written by Steffen Daode Nurpmeso.
8688

0 commit comments

Comments
 (0)