[Python-Dev] returning longs from __hash__()

"Martin v. Löwis" martin at v.loewis.de
Tue Aug 8 23:14:56 CEST 2006


Tim Peters schrieb:
> It sounds fine to me, except I'm not immediately clear on which code
> needs to be changed.

My change would essentially be the code below, in instance_hash and
slot_tp_hash; I have yet to add test cases and check for documentation
changes.

Regards,
Martin

Index: Objects/typeobject.c
===================================================================
--- Objects/typeobject.c        (Revision 51155)
+++ Objects/typeobject.c        (Arbeitskopie)
@@ -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 {
Index: Objects/classobject.c
===================================================================
--- Objects/classobject.c       (Revision 51155)
+++ Objects/classobject.c       (Arbeitskopie)
@@ -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");


More information about the Python-Dev mailing list