Skip to content

Commit 2960056

Browse files
juliantaylorcharris
authored andcommitted
BUG: fix hashing of long integers under python3
python3 long_hash is more complex than int_hash so instead of copying into numpy call it via the Python capi. Same for long long for wich the numpy hash function is not correct with python 2.7 on i386. Will be slower but doesn't need adapting each timy python changes. closes numpy#3793
1 parent e20a2bb commit 2960056

2 files changed

Lines changed: 48 additions & 37 deletions

File tree

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

Lines changed: 28 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2872,8 +2872,8 @@ static npy_hash_t
28722872
/**end repeat**/
28732873

28742874
/**begin repeat
2875-
* #lname = byte, short, uint, ulong#
2876-
* #name = Byte, Short, UInt, ULong#
2875+
* #lname = byte, short, uint#
2876+
* #name = Byte, Short, UInt#
28772877
*/
28782878
static npy_hash_t
28792879
@lname@_arrtype_hash(PyObject *obj)
@@ -2886,6 +2886,15 @@ static npy_hash_t
28862886
}
28872887
/**end repeat**/
28882888

2889+
static npy_hash_t
2890+
ulong_arrtype_hash(PyObject *obj)
2891+
{
2892+
PyObject * l = PyLong_FromUnsignedLong(((PyULongScalarObject*)obj)->obval);
2893+
npy_hash_t x = PyObject_Hash(l);
2894+
Py_DECREF(l);
2895+
return x;
2896+
}
2897+
28892898
#if (NPY_SIZEOF_INT != NPY_SIZEOF_LONG) || defined(NPY_PY3K)
28902899
static npy_hash_t
28912900
int_arrtype_hash(PyObject *obj)
@@ -2898,49 +2907,31 @@ int_arrtype_hash(PyObject *obj)
28982907
}
28992908
#endif
29002909

2901-
/**begin repeat
2902-
* #char = ,u#
2903-
* #Char = ,U#
2904-
* #ext = && (x >= LONG_MIN),#
2905-
*/
2906-
#if NPY_SIZEOF_HASH_T != NPY_SIZEOF_LONGLONG
2907-
/* we assume NPY_SIZEOF_LONGLONG=2*NPY_SIZEOF_LONG */
2910+
#if defined(NPY_PY3K)
29082911
static npy_hash_t
2909-
@char@longlong_arrtype_hash(PyObject *obj)
2912+
long_arrtype_hash(PyObject *obj)
29102913
{
2911-
npy_hash_t y;
2912-
npy_@char@longlong x = (((Py@Char@LongLongScalarObject *)obj)->obval);
2913-
2914-
if ((x <= LONG_MAX)@ext@) {
2915-
y = (npy_hash_t) x;
2916-
}
2917-
else {
2918-
union Mask {
2919-
long hashvals[2];
2920-
npy_@char@longlong v;
2921-
} both;
2922-
2923-
both.v = x;
2924-
y = both.hashvals[0] + (1000003)*both.hashvals[1];
2925-
}
2926-
if (y == -1) {
2927-
y = -2;
2928-
}
2929-
return y;
2914+
PyObject * l = PyLong_FromLong(((PyLongScalarObject*)obj)->obval);
2915+
npy_hash_t x = PyObject_Hash(l);
2916+
Py_DECREF(l);
2917+
return x;
29302918
}
2931-
#else
2919+
#endif
29322920

2921+
/**begin repeat
2922+
* #char = ,u#
2923+
* #Char = ,U#
2924+
* #Word = ,Unsigned#
2925+
*/
29332926
static npy_hash_t
29342927
@char@longlong_arrtype_hash(PyObject *obj)
29352928
{
2936-
npy_hash_t x = (npy_hash_t)(((Py@Char@LongLongScalarObject *)obj)->obval);
2937-
if (x == -1) {
2938-
x = -2;
2939-
}
2929+
PyObject * l = PyLong_From@Word@LongLong(
2930+
((Py@Char@LongLongScalarObject*)obj)->obval);
2931+
npy_hash_t x = PyObject_Hash(l);
2932+
Py_DECREF(l);
29402933
return x;
29412934
}
2942-
2943-
#endif
29442935
/**end repeat**/
29452936

29462937

@@ -4079,7 +4070,7 @@ initialize_numeric_types(void)
40794070

40804071
#if defined(NPY_PY3K)
40814072
/* We won't be inheriting from Python Int type. */
4082-
PyLongArrType_Type.tp_hash = int_arrtype_hash;
4073+
PyLongArrType_Type.tp_hash = long_arrtype_hash;
40834074
#endif
40844075

40854076
#if (NPY_SIZEOF_LONG != NPY_SIZEOF_LONGLONG) || defined(NPY_PY3K)

‎numpy/core/tests/test_multiarray.py‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,26 @@ def test_otherflags(self):
5959
assert_equal(self.a.flags.aligned, True)
6060
assert_equal(self.a.flags.updateifcopy, False)
6161

62+
class TestHash(TestCase):
63+
# see #3793
64+
def test_int(self):
65+
for st, ut, s in [(np.int8, np.uint8, 8),
66+
(np.int16, np.uint16, 16),
67+
(np.int32, np.uint32, 32),
68+
(np.int64, np.uint64, 64)]:
69+
for i in range(1, s):
70+
assert_equal(hash(st(-2**i)), hash(-2**i),
71+
err_msg="%r: -2**%d" % (st, i))
72+
assert_equal(hash(st(2**(i - 1))), hash(2**(i - 1)),
73+
err_msg="%r: 2**%d" % (st, i - 1))
74+
assert_equal(hash(st(2**i - 1)), hash(2**i - 1),
75+
err_msg="%r: 2**%d - 1" % (st, i))
76+
77+
i = max(i - 1, 1)
78+
assert_equal(hash(ut(2**(i - 1))), hash(2**(i - 1)),
79+
err_msg="%r: 2**%d" % (ut, i - 1))
80+
assert_equal(hash(ut(2**i - 1)), hash(2**i - 1),
81+
err_msg="%r: 2**%d - 1" % (ut, i))
6282

6383
class TestAttributes(TestCase):
6484
def setUp(self):

0 commit comments

Comments
 (0)