Skip to content

Commit 4d53e8b

Browse files
committed
Merge pull request #3747 from cgohlke/py_hash_t
Fix issue #3743: Hash changed signature in Python 3.3
2 parents f88a225 + 997fe5b commit 4d53e8b

4 files changed

Lines changed: 47 additions & 34 deletions

File tree

‎numpy/core/include/numpy/npy_3kcompat.h‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,19 @@ simple_capsule_dtor(void *ptr)
397397

398398
#endif
399399

400+
/*
401+
* Hash value compatibility.
402+
* As of Python 3.2 hash values are of type Py_hash_t.
403+
* Previous versions use C long.
404+
*/
405+
#if PY_VERSION_HEX < 0x03020000
406+
typedef long npy_hash_t;
407+
#define NPY_SIZEOF_HASH_T NPY_SIZEOF_LONG
408+
#else
409+
typedef Py_hash_t npy_hash_t;
410+
#define NPY_SIZEOF_HASH_T NPY_SIZEOF_INTP
411+
#endif
412+
400413
#ifdef __cplusplus
401414
}
402415
#endif

‎numpy/core/src/multiarray/hashdescr.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ static int _array_descr_walk(PyArray_Descr* descr, PyObject *l)
243243
/*
244244
* Return 0 if successfull
245245
*/
246-
static int _PyArray_DescrHashImp(PyArray_Descr *descr, long *hash)
246+
static int _PyArray_DescrHashImp(PyArray_Descr *descr, npy_hash_t *hash)
247247
{
248248
PyObject *l, *tl, *item;
249249
Py_ssize_t i;
@@ -296,12 +296,12 @@ static int _PyArray_DescrHashImp(PyArray_Descr *descr, long *hash)
296296
return -1;
297297
}
298298

299-
NPY_NO_EXPORT long
299+
NPY_NO_EXPORT npy_hash_t
300300
PyArray_DescrHash(PyObject* odescr)
301301
{
302302
PyArray_Descr *descr;
303303
int st;
304-
long hash;
304+
npy_hash_t hash;
305305

306306
if (!PyArray_DescrCheck(odescr)) {
307307
PyErr_SetString(PyExc_ValueError,
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#ifndef _NPY_HASHDESCR_H_
22
#define _NPY_HASHDESCR_H_
33

4-
NPY_NO_EXPORT long
4+
NPY_NO_EXPORT npy_hash_t
55
PyArray_DescrHash(PyObject* odescr);
66

77
#endif

‎numpy/core/src/multiarray/scalartypes.c.src‎

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2864,21 +2864,21 @@ void_arrtype_new(PyTypeObject *type, PyObject *args, PyObject *NPY_UNUSED(kwds))
28642864
* #lname = bool, ubyte, ushort#
28652865
* #name = Bool,UByte, UShort#
28662866
*/
2867-
static long
2867+
static npy_hash_t
28682868
@lname@_arrtype_hash(PyObject *obj)
28692869
{
2870-
return (long)(((Py@name@ScalarObject *)obj)->obval);
2870+
return (npy_hash_t)(((Py@name@ScalarObject *)obj)->obval);
28712871
}
28722872
/**end repeat**/
28732873

28742874
/**begin repeat
28752875
* #lname = byte, short, uint, ulong#
28762876
* #name = Byte, Short, UInt, ULong#
28772877
*/
2878-
static long
2878+
static npy_hash_t
28792879
@lname@_arrtype_hash(PyObject *obj)
28802880
{
2881-
long x = (long)(((Py@name@ScalarObject *)obj)->obval);
2881+
npy_hash_t x = (npy_hash_t)(((Py@name@ScalarObject *)obj)->obval);
28822882
if (x == -1) {
28832883
x = -2;
28842884
}
@@ -2887,10 +2887,10 @@ static long
28872887
/**end repeat**/
28882888

28892889
#if (NPY_SIZEOF_INT != NPY_SIZEOF_LONG) || defined(NPY_PY3K)
2890-
static long
2890+
static npy_hash_t
28912891
int_arrtype_hash(PyObject *obj)
28922892
{
2893-
long x = (long)(((PyIntScalarObject *)obj)->obval);
2893+
npy_hash_t x = (npy_hash_t)(((PyIntScalarObject *)obj)->obval);
28942894
if (x == -1) {
28952895
x = -2;
28962896
}
@@ -2903,16 +2903,16 @@ int_arrtype_hash(PyObject *obj)
29032903
* #Char = ,U#
29042904
* #ext = && (x >= LONG_MIN),#
29052905
*/
2906-
#if NPY_SIZEOF_LONG != NPY_SIZEOF_LONGLONG
2906+
#if NPY_SIZEOF_HASH_T != NPY_SIZEOF_LONGLONG
29072907
/* we assume NPY_SIZEOF_LONGLONG=2*NPY_SIZEOF_LONG */
2908-
static long
2908+
static npy_hash_t
29092909
@char@longlong_arrtype_hash(PyObject *obj)
29102910
{
2911-
long y;
2911+
npy_hash_t y;
29122912
npy_@char@longlong x = (((Py@Char@LongLongScalarObject *)obj)->obval);
29132913

29142914
if ((x <= LONG_MAX)@ext@) {
2915-
y = (long) x;
2915+
y = (npy_hash_t) x;
29162916
}
29172917
else {
29182918
union Mask {
@@ -2930,10 +2930,10 @@ static long
29302930
}
29312931
#else
29322932

2933-
static long
2933+
static npy_hash_t
29342934
@char@longlong_arrtype_hash(PyObject *obj)
29352935
{
2936-
long x = (long)(((Py@Char@LongLongScalarObject *)obj)->obval);
2936+
npy_hash_t x = (npy_hash_t)(((Py@Char@LongLongScalarObject *)obj)->obval);
29372937
if (x == -1) {
29382938
x = -2;
29392939
}
@@ -2948,25 +2948,25 @@ static long
29482948
* #lname = datetime, timedelta#
29492949
* #name = Datetime, Timedelta#
29502950
*/
2951-
#if NPY_SIZEOF_LONG==NPY_SIZEOF_DATETIME
2952-
static long
2951+
#if NPY_SIZEOF_HASH_T==NPY_SIZEOF_DATETIME
2952+
static npy_hash_t
29532953
@lname@_arrtype_hash(PyObject *obj)
29542954
{
2955-
long x = (long)(((Py@name@ScalarObject *)obj)->obval);
2955+
npy_hash_t x = (npy_hash_t)(((Py@name@ScalarObject *)obj)->obval);
29562956
if (x == -1) {
29572957
x = -2;
29582958
}
29592959
return x;
29602960
}
29612961
#elif NPY_SIZEOF_LONGLONG==NPY_SIZEOF_DATETIME
2962-
static long
2962+
static npy_hash_t
29632963
@lname@_arrtype_hash(PyObject *obj)
29642964
{
2965-
long y;
2965+
npy_hash_t y;
29662966
npy_longlong x = (((Py@name@ScalarObject *)obj)->obval);
29672967

29682968
if ((x <= LONG_MAX)) {
2969-
y = (long) x;
2969+
y = (npy_hash_t) x;
29702970
}
29712971
else {
29722972
union Mask {
@@ -2993,17 +2993,17 @@ static long
29932993
* #lname = float, longdouble#
29942994
* #name = Float, LongDouble#
29952995
*/
2996-
static long
2996+
static npy_hash_t
29972997
@lname@_arrtype_hash(PyObject *obj)
29982998
{
29992999
return _Py_HashDouble((double) ((Py@name@ScalarObject *)obj)->obval);
30003000
}
30013001

30023002
/* borrowed from complex_hash */
3003-
static long
3003+
static npy_hash_t
30043004
c@lname@_arrtype_hash(PyObject *obj)
30053005
{
3006-
long hashreal, hashimag, combined;
3006+
npy_hash_t hashreal, hashimag, combined;
30073007
hashreal = _Py_HashDouble((double)
30083008
(((PyC@name@ScalarObject *)obj)->obval).real);
30093009

@@ -3023,13 +3023,13 @@ c@lname@_arrtype_hash(PyObject *obj)
30233023
}
30243024
/**end repeat**/
30253025

3026-
static long
3026+
static npy_hash_t
30273027
half_arrtype_hash(PyObject *obj)
30283028
{
30293029
return _Py_HashDouble(npy_half_to_double(((PyHalfScalarObject *)obj)->obval));
30303030
}
30313031

3032-
static long
3032+
static npy_hash_t
30333033
object_arrtype_hash(PyObject *obj)
30343034
{
30353035
return PyObject_Hash(((PyObjectScalarObject *)obj)->obval);
@@ -3038,14 +3038,14 @@ object_arrtype_hash(PyObject *obj)
30383038
/* we used to just hash the pointer */
30393039
/* now use tuplehash algorithm using voidtype_item to get the object
30403040
*/
3041-
static long
3041+
static npy_hash_t
30423042
void_arrtype_hash(PyObject *obj)
30433043
{
3044-
register long x, y;
3045-
register Py_ssize_t len, n;
3046-
register PyVoidScalarObject *p;
3047-
register PyObject *element;
3048-
long mult = 1000003L;
3044+
npy_hash_t x, y;
3045+
Py_ssize_t len, n;
3046+
PyVoidScalarObject *p;
3047+
PyObject *element;
3048+
npy_hash_t mult = 1000003L;
30493049
x = 0x345678L;
30503050
p = (PyVoidScalarObject *)obj;
30513051
/* Cannot hash mutable void scalars */
@@ -3061,7 +3061,7 @@ void_arrtype_hash(PyObject *obj)
30613061
if (y == -1)
30623062
return -1;
30633063
x = (x ^ y) * mult;
3064-
mult += (long)(82520L + len + len);
3064+
mult += (npy_hash_t)(82520L + len + len);
30653065
}
30663066
x += 97531L;
30673067
if (x == -1)

0 commit comments

Comments
 (0)