]> granicus.if.org Git - python/commitdiff
SF bug #1054139: serious string hashing error in 2.4b1
authorRaymond Hettinger <python@rcn.com>
Tue, 26 Oct 2004 01:52:37 +0000 (01:52 +0000)
committerRaymond Hettinger <python@rcn.com>
Tue, 26 Oct 2004 01:52:37 +0000 (01:52 +0000)
_PyString_Resize() readied strings for mutation but did not invalidate
the cached hash value.

Lib/test/string_tests.py
Misc/NEWS
Objects/stringobject.c

index 4335965b36c80262b421d82e67524543a7ce94bd..c8ed07cf60f38e454c3831b0679639aa2f0b8c85 100644 (file)
@@ -80,6 +80,15 @@ class CommonTest(unittest.TestCase):
         args = self.fixtype(args)
         getattr(object, methodname)(*args)
 
+    def test_hash(self):
+        # SF bug 1054139:  += optimization was not invalidating cached hash value
+        a = self.type2test('DNSSEC')
+        b = self.type2test('')
+        for c in a:
+            b += c
+            hash(b)
+        self.assertEqual(hash(a), hash(b))
+
     def test_capitalize(self):
         self.checkequal(' hello ', ' hello ', 'capitalize')
         self.checkequal('Hello ', 'Hello ','capitalize')
index e5437e177ef50419b366ecef492170ea37a442b4..6c0935ea774f3ad8520bd5ba0453035b94036f29 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -32,7 +32,7 @@ License Version 2.
 Core and builtins
 -----------------
 
-...
+- Bug #1054139 _PyString_Resize() now invalidates its cached hash value.
 
 Extension Modules
 -----------------
index c87b68800941adcdb46967a657a1204f2492d175..b8e5f41342c2da19f8fed2a787bd6a97019b274d 100644 (file)
@@ -3530,6 +3530,7 @@ _PyString_Resize(PyObject **pv, int newsize)
        sv = (PyStringObject *) *pv;
        sv->ob_size = newsize;
        sv->ob_sval[newsize] = '\0';
+       sv->ob_shash = -1;      /* invalidate cached hash value */
        return 0;
 }