]> granicus.if.org Git - python/commitdiff
Fix SF bug #689659, 64-bit int and long hash keys incompatible
authorNeal Norwitz <nnorwitz@gmail.com>
Sun, 23 Feb 2003 23:11:41 +0000 (23:11 +0000)
committerNeal Norwitz <nnorwitz@gmail.com>
Sun, 23 Feb 2003 23:11:41 +0000 (23:11 +0000)
On a 64-bit machine, a dictionary could contain duplicate int/long keys
if the value was > 2**32.

Lib/test/test_types.py
Misc/NEWS
Objects/longobject.c

index 449d0dd14617178fb6ea1d532b52ad5e89c125f0..d571a025075bbb77629a10a5f49401ad62d8bb05 100644 (file)
@@ -639,6 +639,14 @@ try: d.pop(k)
 except KeyError: pass
 else: raise TestFailed, "{}.pop(k) doesn't raise KeyError when dictionary is empty"
 
+# verify longs/ints get same value when key > 32 bits (for 64-bit archs)
+# see SF bug #689659
+x = 4503599627370496L
+y = 4503599627370496
+h = {x: 'anything', y: 'something else'} 
+if h[x] != h[y]:
+    raise TestFailed, "long/int key should match"
+
 d[1] = 1
 try:
     for i in d:
index 8d5a66959b95e64d0bcb29bfa9669cf3daa0dbea..c1236894efe4e8373d7947f71b8fb0bf32e197c0 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,7 +12,8 @@ What's New in Python 2.3 beta 1?
 Core and builtins
 -----------------
 
-TBD
+- On 64-bit systems, a dictionary could contain duplicate long/int keys
+  if the key value was larger than 2**32.  See SF bug #689659.
 
 Extension modules
 -----------------
index c2d6ea74a72a594063ef158498f289fe93aecf64..446affb54d53b18012a983f9a95d70807d2a16d0 100644 (file)
@@ -1490,11 +1490,13 @@ long_hash(PyLongObject *v)
                sign = -1;
                i = -(i);
        }
+#define LONG_BIT_SHIFT (8*sizeof(long) - SHIFT)
        while (--i >= 0) {
-               /* Force a 32-bit circular shift */
-               x = ((x << SHIFT) & ~MASK) | ((x >> (32-SHIFT)) & MASK);
+               /* Force a native long #-bits (32 or 64) circular shift */
+               x = ((x << SHIFT) & ~MASK) | ((x >> LONG_BIT_SHIFT) & MASK);
                x += v->ob_digit[i];
        }
+#undef LONG_BIT_SHIFT
        x = x * sign;
        if (x == -1)
                x = -2;