[issue9778] Make hash values the same width as a pointer (or Py_ssize_t)

Case Van Horsen report at bugs.python.org
Mon Oct 18 19:25:30 CEST 2010


Case Van Horsen <casevh at gmail.com> added the comment:

>Sorry, I could have been clearer indeed.   Consider the following code:
>
>static Py_hash_t
>long_hash(PyLongObject *v)
>{
>    unsigned long x;
>...
>    x = x * sign;
>    if (x == (unsigned long)-1)
>        x = (unsigned long)-2;
>    return (Py_hash_t)x;
>}

>Wouldn't it be cleaner if x was the same type as hash?  Note that
>unsigned long is now wrong.  What is needed is "unsigned integer type
>of the same size as Py_hash_t."  If Py_hash_t has to stay signed, I
>think we should at least not rely of sizeof(Py_hash_t) to always
>remain the same as sizeof(size_t).

The calculation of long_hash assumes an unsigned temporary type to get correct results for the bit shifting and masking. The calculation is done on the absolute value of the long and then the sign is applied. We either needed to (1) add an unsigned Py_hash_t type or (2) just use size_t and Py_ssize_t.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue9778>
_______________________________________


More information about the Python-bugs-list mailing list