[Python-checkins] r85803 - in python/branches/py3k: Include/pyport.h Objects/complexobject.c Objects/longobject.c Objects/object.c Objects/tupleobject.c Objects/typeobject.c Python/sysmodule.c

benjamin.peterson python-checkins at python.org
Sat Oct 23 18:20:50 CEST 2010


Author: benjamin.peterson
Date: Sat Oct 23 18:20:50 2010
New Revision: 85803

Log:
follow up to #9778: define and use an unsigned hash type

Modified:
   python/branches/py3k/Include/pyport.h
   python/branches/py3k/Objects/complexobject.c
   python/branches/py3k/Objects/longobject.c
   python/branches/py3k/Objects/object.c
   python/branches/py3k/Objects/tupleobject.c
   python/branches/py3k/Objects/typeobject.c
   python/branches/py3k/Python/sysmodule.c

Modified: python/branches/py3k/Include/pyport.h
==============================================================================
--- python/branches/py3k/Include/pyport.h	(original)
+++ python/branches/py3k/Include/pyport.h	Sat Oct 23 18:20:50 2010
@@ -135,7 +135,7 @@
 #else
 #define _PyHASH_BITS 31
 #endif
-#define _PyHASH_MODULUS ((1UL << _PyHASH_BITS) - 1)
+#define _PyHASH_MODULUS (((size_t)1 << _PyHASH_BITS) - 1)
 #define _PyHASH_INF 314159
 #define _PyHASH_NAN 0
 #define _PyHASH_IMAG 1000003UL
@@ -179,6 +179,8 @@
 
 /* Py_hash_t is the same size as a pointer. */
 typedef Py_ssize_t Py_hash_t;
+/* Py_uhash_t is the unsigned equivalent needed to calculate numeric hash. */
+typedef size_t Py_uhash_t;
 
 /* Largest possible value of size_t.
    SIZE_MAX is part of C99, so it might be defined on some

Modified: python/branches/py3k/Objects/complexobject.c
==============================================================================
--- python/branches/py3k/Objects/complexobject.c	(original)
+++ python/branches/py3k/Objects/complexobject.c	Sat Oct 23 18:20:50 2010
@@ -397,12 +397,12 @@
 static Py_hash_t
 complex_hash(PyComplexObject *v)
 {
-    unsigned long hashreal, hashimag, combined;
-    hashreal = (unsigned long)_Py_HashDouble(v->cval.real);
-    if (hashreal == (unsigned long)-1)
+    Py_uhash_t hashreal, hashimag, combined;
+    hashreal = (Py_uhash_t)_Py_HashDouble(v->cval.real);
+    if (hashreal == (Py_uhash_t)-1)
         return -1;
-    hashimag = (unsigned long)_Py_HashDouble(v->cval.imag);
-    if (hashimag == (unsigned long)-1)
+    hashimag = (Py_uhash_t)_Py_HashDouble(v->cval.imag);
+    if (hashimag == (Py_uhash_t)-1)
         return -1;
     /* Note:  if the imaginary part is 0, hashimag is 0 now,
      * so the following returns hashreal unchanged.  This is
@@ -411,8 +411,8 @@
      * hash(x + 0*j) must equal hash(x).
      */
     combined = hashreal + _PyHASH_IMAG * hashimag;
-    if (combined == (unsigned long)-1)
-        combined = (unsigned long)-2;
+    if (combined == (Py_uhash_t)-1)
+        combined = (Py_uhash_t)-2;
     return (Py_hash_t)combined;
 }
 

Modified: python/branches/py3k/Objects/longobject.c
==============================================================================
--- python/branches/py3k/Objects/longobject.c	(original)
+++ python/branches/py3k/Objects/longobject.c	Sat Oct 23 18:20:50 2010
@@ -2555,7 +2555,7 @@
 static Py_hash_t
 long_hash(PyLongObject *v)
 {
-    unsigned long x;
+    Py_uhash_t x;
     Py_ssize_t i;
     int sign;
 
@@ -2604,8 +2604,8 @@
             x -= _PyHASH_MODULUS;
     }
     x = x * sign;
-    if (x == (unsigned long)-1)
-        x = (unsigned long)-2;
+    if (x == (Py_uhash_t)-1)
+        x = (Py_uhash_t)-2;
     return (Py_hash_t)x;
 }
 

Modified: python/branches/py3k/Objects/object.c
==============================================================================
--- python/branches/py3k/Objects/object.c	(original)
+++ python/branches/py3k/Objects/object.c	Sat Oct 23 18:20:50 2010
@@ -692,7 +692,7 @@
 {
     int e, sign;
     double m;
-    unsigned long x, y;
+    Py_uhash_t x, y;
 
     if (!Py_IS_FINITE(v)) {
         if (Py_IS_INFINITY(v))
@@ -716,7 +716,7 @@
         x = ((x << 28) & _PyHASH_MODULUS) | x >> (_PyHASH_BITS - 28);
         m *= 268435456.0;  /* 2**28 */
         e -= 28;
-        y = (unsigned long)m;  /* pull out integer part */
+        y = (Py_uhash_t)m;  /* pull out integer part */
         m -= y;
         x += y;
         if (x >= _PyHASH_MODULUS)
@@ -728,8 +728,8 @@
     x = ((x << e) & _PyHASH_MODULUS) | x >> (_PyHASH_BITS - e);
 
     x = x * sign;
-    if (x == (unsigned long)-1)
-        x = (unsigned long)-2;
+    if (x == (Py_uhash_t)-1)
+        x = (Py_uhash_t)-2;
     return (Py_hash_t)x;
 }
 

Modified: python/branches/py3k/Objects/tupleobject.c
==============================================================================
--- python/branches/py3k/Objects/tupleobject.c	(original)
+++ python/branches/py3k/Objects/tupleobject.c	Sat Oct 23 18:20:50 2010
@@ -318,7 +318,7 @@
     register Py_hash_t x, y;
     register Py_ssize_t len = Py_SIZE(v);
     register PyObject **p;
-    long mult = 1000003L;
+    Py_hash_t mult = 1000003L;
     x = 0x345678L;
     p = v->ob_item;
     while (--len >= 0) {
@@ -327,7 +327,7 @@
             return -1;
         x = (x ^ y) * mult;
         /* the cast might truncate len; that doesn't change hash stability */
-        mult += (long)(82520L + len + len);
+        mult += (Py_hash_t)(82520L + len + len);
     }
     x += 97531L;
     if (x == -1)

Modified: python/branches/py3k/Objects/typeobject.c
==============================================================================
--- python/branches/py3k/Objects/typeobject.c	(original)
+++ python/branches/py3k/Objects/typeobject.c	Sat Oct 23 18:20:50 2010
@@ -4314,14 +4314,14 @@
 wrap_hashfunc(PyObject *self, PyObject *args, void *wrapped)
 {
     hashfunc func = (hashfunc)wrapped;
-    long res;
+    Py_hash_t res;
 
     if (!check_num_args(args, 0))
         return NULL;
     res = (*func)(self);
     if (res == -1 && PyErr_Occurred())
         return NULL;
-    return PyLong_FromLong(res);
+    return PyLong_FromSsize_t(res);
 }
 
 static PyObject *

Modified: python/branches/py3k/Python/sysmodule.c
==============================================================================
--- python/branches/py3k/Python/sysmodule.c	(original)
+++ python/branches/py3k/Python/sysmodule.c	Sat Oct 23 18:20:50 2010
@@ -569,7 +569,7 @@
     PyStructSequence_SET_ITEM(hash_info, field++,
                               PyLong_FromLong(8*sizeof(Py_hash_t)));
     PyStructSequence_SET_ITEM(hash_info, field++,
-                              PyLong_FromLong(_PyHASH_MODULUS));
+                              PyLong_FromSsize_t(_PyHASH_MODULUS));
     PyStructSequence_SET_ITEM(hash_info, field++,
                               PyLong_FromLong(_PyHASH_INF));
     PyStructSequence_SET_ITEM(hash_info, field++,


More information about the Python-checkins mailing list