File tree 2 files changed +32
-1
lines changed
2 files changed +32
-1
lines changed Original file line number Diff line number Diff line change @@ -30,6 +30,23 @@ static int _array_descr_walk(PyArray_Descr* descr, PyObject *l);
30
30
static int _array_descr_walk_fields (PyObject * fields , PyObject * l );
31
31
static int _array_descr_builtin (PyArray_Descr * descr , PyObject * l );
32
32
33
+ /*
34
+ * normalize endian character: always return 'I', '<' or '>'
35
+ */
36
+ static char _normalize_byteorder (char byteorder )
37
+ {
38
+ switch (byteorder ) {
39
+ case '=' :
40
+ if (PyArray_GetEndianness () == NPY_CPU_BIG ) {
41
+ return '>' ;
42
+ } else {
43
+ return '<' ;
44
+ }
45
+ default :
46
+ return byteorder ;
47
+ }
48
+ }
49
+
33
50
/*
34
51
* Return true if descr is a builtin type
35
52
*/
@@ -51,12 +68,13 @@ static int _array_descr_builtin(PyArray_Descr* descr, PyObject *l)
51
68
{
52
69
Py_ssize_t i ;
53
70
PyObject * t , * item ;
71
+ char nbyteorder = _normalize_byteorder (descr -> byteorder );
54
72
55
73
/*
56
74
* For builtin type, hash relies on : kind + byteorder + flags +
57
75
* type_num + elsize + alignment
58
76
*/
59
- t = Py_BuildValue ("(cciiii)" , descr -> kind , descr -> byteorder ,
77
+ t = Py_BuildValue ("(cciiii)" , descr -> kind , nbyteorder ,
60
78
descr -> flags , descr -> type_num , descr -> elsize ,
61
79
descr -> alignment );
62
80
Original file line number Diff line number Diff line change @@ -9,6 +9,19 @@ def test_run(self):
9
9
dt = np .dtype (t )
10
10
hash (dt )
11
11
12
+ def test_dtype (self ):
13
+ # Make sure equivalent byte order char hash the same (e.g. < and = on
14
+ # little endian)
15
+ for t in [np .int , np .float ]:
16
+ dt = np .dtype (t )
17
+ dt2 = dt .newbyteorder ("<" )
18
+ dt3 = dt .newbyteorder (">" )
19
+ if dt == dt2 :
20
+ self .assertTrue (dt .byteorder != dt2 .byteorder , "bogus test" )
21
+ self .assertTrue (hash (dt ) == hash (dt2 ), "equivalent bytorders do not hash the same" )
22
+ else :
23
+ self .assertTrue (dt .byteorder != dt3 .byteorder , "bogus test" )
24
+ self .assertTrue (hash (dt ) == hash (dt3 ), "equivalent bytorders do not hash the same" )
12
25
class TestRecord (TestCase ):
13
26
def test_equivalent_record (self ):
14
27
"""Test whether equivalent record dtypes hash the same."""
You can’t perform that action at this time.
0 commit comments