diff options
Diffstat (limited to 'numpy')
-rw-r--r-- | numpy/core/src/multiarray/hashdescr.c | 20 | ||||
-rw-r--r-- | numpy/core/tests/test_dtype.py | 13 |
2 files changed, 32 insertions, 1 deletions
diff --git a/numpy/core/src/multiarray/hashdescr.c b/numpy/core/src/multiarray/hashdescr.c index 4d4a7868e..60a9a7361 100644 --- a/numpy/core/src/multiarray/hashdescr.c +++ b/numpy/core/src/multiarray/hashdescr.c @@ -31,6 +31,23 @@ static int _array_descr_walk_fields(PyObject* fields, PyObject* l); static int _array_descr_builtin(PyArray_Descr* descr, PyObject *l); /* + * normalize endian character: always return 'I', '<' or '>' + */ + static char _normalize_byteorder(char byteorder) + { + switch(byteorder) { + case '=': + if (PyArray_GetEndianness() == NPY_CPU_BIG) { + return '>'; + } else { + return '<'; + } + default: + return byteorder; + } + } + +/* * Return true if descr is a builtin type */ static int _is_array_descr_builtin(PyArray_Descr* descr) @@ -51,12 +68,13 @@ static int _array_descr_builtin(PyArray_Descr* descr, PyObject *l) { Py_ssize_t i; PyObject *t, *item; + char nbyteorder = _normalize_byteorder(descr->byteorder); /* * For builtin type, hash relies on : kind + byteorder + flags + * type_num + elsize + alignment */ - t = Py_BuildValue("(cciiii)", descr->kind, descr->byteorder, + t = Py_BuildValue("(cciiii)", descr->kind, nbyteorder, descr->flags, descr->type_num, descr->elsize, descr->alignment); diff --git a/numpy/core/tests/test_dtype.py b/numpy/core/tests/test_dtype.py index e817ce5d4..8d450b39d 100644 --- a/numpy/core/tests/test_dtype.py +++ b/numpy/core/tests/test_dtype.py @@ -9,6 +9,19 @@ class TestBuiltin(TestCase): dt = np.dtype(t) hash(dt) + def test_dtype(self): + # Make sure equivalent byte order char hash the same (e.g. < and = on + # little endian) + for t in [np.int, np.float]: + dt = np.dtype(t) + dt2 = dt.newbyteorder("<") + dt3 = dt.newbyteorder(">") + if dt == dt2: + self.assertTrue(dt.byteorder != dt2.byteorder, "bogus test") + self.assertTrue(hash(dt) == hash(dt2), "equivalent bytorders do not hash the same") + else: + self.assertTrue(dt.byteorder != dt3.byteorder, "bogus test") + self.assertTrue(hash(dt) == hash(dt3), "equivalent bytorders do not hash the same") class TestRecord(TestCase): def test_equivalent_record(self): """Test whether equivalent record dtypes hash the same.""" |