[Python-checkins] cpython: Forgot the "empty string -> hash == 0" special case for strings.

georg.brandl python-checkins at python.org
Tue Feb 21 00:50:47 CET 2012


http://hg.python.org/cpython/rev/4a77887b1ac2
changeset:   75099:4a77887b1ac2
user:        Georg Brandl <georg at python.org>
date:        Tue Feb 21 00:50:13 2012 +0100
summary:
  Forgot the "empty string -> hash == 0" special case for strings.

files:
  Objects/unicodeobject.c |  8 ++++++++
  1 files changed, 8 insertions(+), 0 deletions(-)


diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -11219,6 +11219,14 @@
     if (PyUnicode_READY(self) == -1)
         return -1;
     len = PyUnicode_GET_LENGTH(self);
+    /*
+      We make the hash of the empty string be 0, rather than using
+      (prefix ^ suffix), since this slightly obfuscates the hash secret
+    */
+    if (len == 0) {
+        _PyUnicode_HASH(self) = 0;
+        return 0;
+    }
 
     /* The hash function as a macro, gets expanded three times below. */
 #define HASH(P)                                            \

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list