Commit 003be529 authored by Raymond Hettinger's avatar Raymond Hettinger

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

parent 5098d443
...@@ -111,8 +111,7 @@ def cmp_to_key(mycmp): ...@@ -111,8 +111,7 @@ def cmp_to_key(mycmp):
return mycmp(self.obj, other.obj) >= 0 return mycmp(self.obj, other.obj) >= 0
def __ne__(self, other): def __ne__(self, other):
return mycmp(self.obj, other.obj) != 0 return mycmp(self.obj, other.obj) != 0
def __hash__(self): __hash__ = None
raise TypeError('hash not implemented')
return K return K
_CacheInfo = namedtuple("CacheInfo", "hits misses maxsize currsize") _CacheInfo = namedtuple("CacheInfo", "hits misses maxsize currsize")
......
import functools import functools
import collections
import sys import sys
import unittest import unittest
from test import support from test import support
...@@ -446,7 +447,8 @@ class TestCmpToKey(unittest.TestCase): ...@@ -446,7 +447,8 @@ class TestCmpToKey(unittest.TestCase):
return y - x return y - x
key = functools.cmp_to_key(mycmp) key = functools.cmp_to_key(mycmp)
k = key(10) k = key(10)
self.assertRaises(TypeError, hash(k)) self.assertRaises(TypeError, hash, k)
self.assertFalse(isinstance(k, collections.Hashable))
class TestTotalOrdering(unittest.TestCase): class TestTotalOrdering(unittest.TestCase):
...@@ -660,6 +662,7 @@ def test_main(verbose=None): ...@@ -660,6 +662,7 @@ def test_main(verbose=None):
TestPythonPartial, TestPythonPartial,
TestUpdateWrapper, TestUpdateWrapper,
TestTotalOrdering, TestTotalOrdering,
TestCmpToKey,
TestWraps, TestWraps,
TestReduce, TestReduce,
TestLRU, TestLRU,
......
...@@ -81,6 +81,8 @@ Library ...@@ -81,6 +81,8 @@ Library
- logging: don't define QueueListener if Python has no thread support. - 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 - 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. around a mmap bug with sparse files. Patch written by Steffen Daode Nurpmeso.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment