[Python-checkins] r51160 - in python/trunk: Doc/ref/ref3.tex Lib/test/test_builtin.py Misc/NEWS Objects/classobject.c Objects/typeobject.c

martin.v.loewis python-checkins at python.org
Wed Aug 9 09:57:42 CEST 2006


Author: martin.v.loewis
Date: Wed Aug  9 09:57:39 2006
New Revision: 51160

Modified:
   python/trunk/Doc/ref/ref3.tex
   python/trunk/Lib/test/test_builtin.py
   python/trunk/Misc/NEWS
   python/trunk/Objects/classobject.c
   python/trunk/Objects/typeobject.c
Log:
__hash__ may now return long int; the final hash
  value is obtained by invoking hash on the long int.
Fixes #1536021.


Modified: python/trunk/Doc/ref/ref3.tex
==============================================================================
--- python/trunk/Doc/ref/ref3.tex	(original)
+++ python/trunk/Doc/ref/ref3.tex	Wed Aug  9 09:57:39 2006
@@ -1307,6 +1307,11 @@
 since the dictionary implementation requires that a key's hash value
 is immutable (if the object's hash value changes, it will be in the
 wrong hash bucket).
+
+\versionchanged[\method{__hash__()} may now also return a long
+integer object; the 32-bit integer is then derived from the hash
+of that object]{2.5}
+
 \withsubitem{(object method)}{\ttindex{__cmp__()}}
 \end{methoddesc}
 

Modified: python/trunk/Lib/test/test_builtin.py
==============================================================================
--- python/trunk/Lib/test/test_builtin.py	(original)
+++ python/trunk/Lib/test/test_builtin.py	Wed Aug  9 09:57:39 2006
@@ -640,6 +640,15 @@
         def f(): pass
         self.assertRaises(TypeError, hash, [])
         self.assertRaises(TypeError, hash, {})
+        # Bug 1536021: Allow hash to return long objects
+        class X:
+            def __hash__(self):
+                return 2**100
+        self.assertEquals(type(hash(X())), int)
+        class Y(object):
+            def __hash__(self):
+                return 2**100
+        self.assertEquals(type(hash(Y())), int)
 
     def test_hex(self):
         self.assertEqual(hex(16), '0x10')

Modified: python/trunk/Misc/NEWS
==============================================================================
--- python/trunk/Misc/NEWS	(original)
+++ python/trunk/Misc/NEWS	Wed Aug  9 09:57:39 2006
@@ -12,6 +12,9 @@
 Core and builtins
 -----------------
 
+- Bug #1536021: __hash__ may now return long int; the final hash
+  value is obtained by invoking hash on the long int.
+
 - Bug #1536786: buffer comparison could emit a RuntimeWarning.
 
 - Bug #1535165: fixed a segfault in input() and raw_input() when

Modified: python/trunk/Objects/classobject.c
==============================================================================
--- python/trunk/Objects/classobject.c	(original)
+++ python/trunk/Objects/classobject.c	Wed Aug  9 09:57:39 2006
@@ -934,11 +934,9 @@
 	Py_DECREF(func);
 	if (res == NULL)
 		return -1;
-	if (PyInt_Check(res)) {
-		outcome = PyInt_AsLong(res);
-		if (outcome == -1)
-			outcome = -2;
-	}
+	if (PyInt_Check(res) || PyLong_Check(res))
+		/* This already converts a -1 result to -2. */
+		outcome = res->ob_type->tp_hash(res);
 	else {
 		PyErr_SetString(PyExc_TypeError,
 				"__hash__() should return an int");

Modified: python/trunk/Objects/typeobject.c
==============================================================================
--- python/trunk/Objects/typeobject.c	(original)
+++ python/trunk/Objects/typeobject.c	Wed Aug  9 09:57:39 2006
@@ -4559,7 +4559,10 @@
 		Py_DECREF(func);
 		if (res == NULL)
 			return -1;
-		h = PyInt_AsLong(res);
+		if (PyLong_Check(res))
+			h = res->ob_type->tp_hash(res);
+		else
+			h = PyInt_AsLong(res);
 		Py_DECREF(res);
 	}
 	else {


More information about the Python-checkins mailing list