Fix __hash__ in functools.cmp_to_key() to work with collections.Hashable.

This commit is contained in:
Raymond Hettinger 2011-05-03 11:16:36 -07:00
commit e7a2430dde
3 changed files with 6 additions and 3 deletions

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

View File

@ -1,4 +1,5 @@
import functools
import collections
import sys
import unittest
from test import support
@ -510,6 +511,7 @@ class TestCmpToKey(unittest.TestCase):
key = functools.cmp_to_key(mycmp)
k = key(10)
self.assertRaises(TypeError, hash, k)
self.assertNotIsInstance(k, collections.Hashable)
class TestTotalOrdering(unittest.TestCase):
@ -718,12 +720,12 @@ class TestLRU(unittest.TestCase):
def test_main(verbose=None):
test_classes = (
TestCmpToKey,
TestPartial,
TestPartialSubclass,
TestPythonPartial,
TestUpdateWrapper,
TestTotalOrdering,
TestCmpToKey,
TestWraps,
TestReduce,
TestLRU,

View File

@ -141,6 +141,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.