]> granicus.if.org Git - python/commitdiff
Fix __hash__ in functools.cmp_to_key() to work with collections.Hashable.
authorRaymond Hettinger <python@rcn.com>
Tue, 3 May 2011 18:16:36 +0000 (11:16 -0700)
committerRaymond Hettinger <python@rcn.com>
Tue, 3 May 2011 18:16:36 +0000 (11:16 -0700)
1  2 
Lib/functools.py
Lib/test/test_functools.py
Misc/NEWS

index 098f6b6fac2b4d054b5b21f0726cacce964f4edf,90642a58b2dbe680a5c367694d7a74f22aa58437..b2bcc21f48e605d4a1ae7c0fc10f99dad633d7d6
@@@ -111,15 -111,9 +111,14 @@@ 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:
 +    from _functools import cmp_to_key
 +except ImportError:
 +    pass
 +
  _CacheInfo = namedtuple("CacheInfo", "hits misses maxsize currsize")
  
  def lru_cache(maxsize=100):
index c50336e3e0deb037f95f2b2a2ccc7193b5eef5be,7d11b53a71c3c7c3329067f7c53b20ece02d29f2..97d7524f597d8b88c17e83b475e83fc4f4ffccfe
@@@ -510,6 -448,7 +511,7 @@@ class TestCmpToKey(unittest.TestCase)
          key = functools.cmp_to_key(mycmp)
          k = key(10)
          self.assertRaises(TypeError, hash, k)
 -        self.assertFalse(isinstance(k, collections.Hashable))
++        self.assertNotIsInstance(k, collections.Hashable)
  
  class TestTotalOrdering(unittest.TestCase):
  
diff --cc Misc/NEWS
index f8f90d02dc07058fcfb97610fab92798a3e951b0,f77befd332e746584685b4a89ef3a2ac33171a5a..13b42e2b1a175af7ff0783ca94468ec4fec7e3f6
+++ b/Misc/NEWS
@@@ -136,11 -79,10 +136,13 @@@ Core and Builtin
  Library
  -------
  
 +- Issue #11930: Removed deprecated time.accept2dyear variable.
 +  Removed year >= 1000 restriction from datetime.strftime.
 +
  - 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.