[Python-checkins] peps: Simplify code for PyUnicode (thx Serhiy)

christian.heimes python-checkins at python.org
Thu Oct 3 22:29:30 CEST 2013


http://hg.python.org/peps/rev/f7da0866e6dc
changeset:   5167:f7da0866e6dc
user:        Christian Heimes <christian at cheimes.de>
date:        Thu Oct 03 22:26:22 2013 +0200
summary:
  Simplify code for PyUnicode (thx Serhiy)

files:
  pep-0456.txt |  27 +++++----------------------
  1 files changed, 5 insertions(+), 22 deletions(-)


diff --git a/pep-0456.txt b/pep-0456.txt
--- a/pep-0456.txt
+++ b/pep-0456.txt
@@ -430,34 +430,17 @@
 unicode_hash (Objects/unicodeobject.c)
 --------------------------------------
 
-``bytes_hash`` provides the tp_hash slot function for unicode. Right now it
+``unicode_hash`` provides the tp_hash slot function for unicode. Right now it
 implements the FNV algorithm three times for ``unsigned char*``, ``Py_UCS2``
 and ``Py_UCS4``. A reimplementation of the function must take care to use the
 correct length. Since the macro ``PyUnicode_GET_LENGTH`` returns the length
 of the unicode string and not its size in octets, the length must be
 multiplied with the size of the internal unicode kind::
 
-    Py_ssize_t len;
-    Py_uhash_t x;
-
-    len = PyUnicode_GET_LENGTH(self);
-    switch (PyUnicode_KIND(self)) {
-    case PyUnicode_1BYTE_KIND: {
-        const Py_UCS1 *c = PyUnicode_1BYTE_DATA(self);
-        x = _PyHash_Func->hashfunc(c, len * sizeof(Py_UCS1));
-        break;
-    }
-    case PyUnicode_2BYTE_KIND: {
-        const Py_UCS2 *s = PyUnicode_2BYTE_DATA(self);
-        x = _PyHash_Func->hashfunc(s, len * sizeof(Py_UCS2));
-        break;
-    }
-    case PyUnicode_4BYTE_KIND: {
-        const Py_UCS4 *l = PyUnicode_4BYTE_DATA(self);
-        x = _PyHash_Func->hashfunc(l, len * sizeof(Py_UCS4));
-        break;
-    }
-    }
+    if (PyUnicode_READY(u) == -1)
+        return -1;
+    x = _PyHash_Func->hashfunc(PyUnicode_DATA(u),
+                               PyUnicode_GET_LENGTH(u) * PyUnicode_KIND(u));
 
 
 generic_hash (Modules/_datetimemodule.c)

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


More information about the Python-checkins mailing list