Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e7a2430

Browse files
committed
Fix __hash__ in functools.cmp_to_key() to work with collections.Hashable.
2 parents 4e787ae + 003be52 commit e7a2430

3 files changed

Lines changed: 6 additions & 3 deletions

File tree

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
try:

Lib/test/test_functools.py

Lines changed: 3 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
@@ -510,6 +511,7 @@ def mycmp(x, y):
510511
key = functools.cmp_to_key(mycmp)
511512
k = key(10)
512513
self.assertRaises(TypeError, hash, k)
514+
self.assertNotIsInstance(k, collections.Hashable)
513515

514516
class TestTotalOrdering(unittest.TestCase):
515517

@@ -718,12 +720,12 @@ def fib(n):
718720

719721
def test_main(verbose=None):
720722
test_classes = (
721-
TestCmpToKey,
722723
TestPartial,
723724
TestPartialSubclass,
724725
TestPythonPartial,
725726
TestUpdateWrapper,
726727
TestTotalOrdering,
728+
TestCmpToKey,
727729
TestWraps,
728730
TestReduce,
729731
TestLRU,

Misc/NEWS

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

142142
- logging: don't define QueueListener if Python has no thread support.
143143

144+
- functools.cmp_to_key() now works with collections.Hashable().
145+
144146
- Issue #11277: mmap.mmap() calls fcntl(fd, F_FULLFSYNC) on Mac OS X to get
145147
around a mmap bug with sparse files. Patch written by Steffen Daode Nurpmeso.
146148

0 commit comments

Comments
 (0)