8000 Merge pull request #8888 from eric-wieser/fix-float64.__hash__ · numpy/numpy@dda43e5 · GitHub
[go: up one dir, main page]

Skip to content

Commit dda43e5

Browse files
authored
Merge pull request #8888 from eric-wieser/fix-float64.__hash__
BUG: Don't modify types after PyType_Ready
2 parents b097bd7 + 17909cc commit dda43e5

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

numpy/core/src/multiarray/multiarraymodule.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4367,14 +4367,14 @@ setup_scalartypes(PyObject *NPY_UNUSED(dict))
43674367
Py##child##ArrType_Type.tp_bases = \
43684368
Py_BuildValue("(OO)", &Py##parent2##ArrType_Type, \
43694369
&Py##parent1##_Type); \
4370+
Py##child##ArrType_Type.tp_hash = Py##parent1##_Type.tp_hash; \
43704371
if (PyType_Ready(&Py##child##ArrType_Type) < 0) { \
43714372
PyErr_Print(); \
43724373
PyErr_Format(PyExc_SystemError, \
43734374
"could not initialize Py%sArrType_Type", \
43744375
#child); \
43754376
return -1; \
4376-
} \
4377-
Py##child##ArrType_Type.tp_hash = Py##parent1##_Type.tp_hash;
4377+
}
43784378

43794379
/*
43804380
* In Py3K, int is no longer a fixed-width integer type, so don't

numpy/core/tests/test_regression.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2235,6 +2235,18 @@ def test_invalid_structured_dtypes(self):
22352235
a = np.ones(1, dtype=('O', [('name', 'O')]))
22362236
assert_equal(a[0], 1)
22372237

2238+
def test_correct_hash_dict(self):
2239+
# gh-8887 - __hash__ would be None despite tp_hash being set
2240+
all_types = set(np.typeDict.values()) - {np.void}
2241+
for t in all_types:
2242+
val = t()
2243+
2244+
try:
2245+
hash(val)
2246+
except TypeError as e:
2247+
assert_equal(t.__hash__, None)
2248+
else:
2249+
assert_(t.__hash__ != None)
22382250

22392251
if __name__ == "__main__":
22402252
run_module_suite()

0 commit comments

Comments
 (0)
0