Skip to content

Commit

Permalink
BUG: Don't modify types after PyType_Ready
Browse files Browse the repository at this point in the history
This would previously cause hashing to work even though `__hash__` is None.

Fixes numpy#8887
  • Loading branch information
eric-wieser committed Apr 3, 2017
1 parent 6e17c72 commit 17909cc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
4 changes: 2 additions & 2 deletions numpy/core/src/multiarray/multiarraymodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -4367,14 +4367,14 @@ setup_scalartypes(PyObject *NPY_UNUSED(dict))
Py##child##ArrType_Type.tp_bases = \
Py_BuildValue("(OO)", &Py##parent2##ArrType_Type, \
&Py##parent1##_Type); \
Py##child##ArrType_Type.tp_hash = Py##parent1##_Type.tp_hash; \
if (PyType_Ready(&Py##child##ArrType_Type) < 0) { \
PyErr_Print(); \
PyErr_Format(PyExc_SystemError, \
"could not initialize Py%sArrType_Type", \
#child); \
return -1; \
} \
Py##child##ArrType_Type.tp_hash = Py##parent1##_Type.tp_hash;
}

/*
* In Py3K, int is no longer a fixed-width integer type, so don't
Expand Down
12 changes: 12 additions & 0 deletions numpy/core/tests/test_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -2235,6 +2235,18 @@ def test_invalid_structured_dtypes(self):
a = np.ones(1, dtype=('O', [('name', 'O')]))
assert_equal(a[0], 1)

def test_correct_hash_dict(self):
# gh-8887 - __hash__ would be None despite tp_hash being set
all_types = set(np.typeDict.values()) - {np.void}
for t in all_types:
val = t()

try:
hash(val)
except TypeError as e:
assert_equal(t.__hash__, None)
else:
assert_(t.__hash__ != None)

if __name__ == "__main__":
run_module_suite()

0 comments on commit 17909cc

Please sign in to comment.