1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
|
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#define NPY_NO_DEPRECATED_API NPY_API_VERSION
#define _MULTIARRAYMODULE
#include <numpy/arrayobject.h>
#include "npy_config.h"
#include "npy_pycompat.h"
#include "hashdescr.h"
/*
* How does this work ? The hash is computed from a list which contains all the
* information specific to a type. The hard work is to build the list
* (_array_descr_walk). The list is built as follows:
* * If the dtype is builtin (no fields, no subarray), then the list
* contains 6 items which uniquely define one dtype (_array_descr_builtin)
* * If the dtype is a compound array, one walk on each field. For each
* field, we append title, names, offset to the final list used for
* hashing, and then append the list recursively built for each
* corresponding dtype (_array_descr_walk_fields)
* * If the dtype is a subarray, one adds the shape tuple to the list, and
* then append the list recursively built for each corresponding dtype
* (_array_descr_walk_subarray)
*
*/
static int _is_array_descr_builtin(PyArray_Descr* descr);
static int _array_descr_walk(PyArray_Descr* descr, PyObject *l);
static int _array_descr_walk_fields(PyObject *names, 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)
{
if (descr->fields != NULL && descr->fields != Py_None) {
return 0;
}
if (PyDataType_HASSUBARRAY(descr)) {
return 0;
}
return 1;
}
/*
* Add to l all the items which uniquely define a builtin type
*/
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("(cccii)", descr->kind, nbyteorder,
descr->flags, descr->elsize, descr->alignment);
for(i = 0; i < PyTuple_Size(t); ++i) {
item = PyTuple_GetItem(t, i);
if (item == NULL) {
PyErr_SetString(PyExc_SystemError,
"(Hash) Error while computing builting hash");
goto clean_t;
}
PyList_Append(l, item);
}
Py_DECREF(t);
return 0;
clean_t:
Py_DECREF(t);
return -1;
}
/*
* Walk inside the fields and add every item which will be used for hashing
* into the list l
*
* Return 0 on success
*/
static int _array_descr_walk_fields(PyObject *names, PyObject* fields, PyObject* l)
{
PyObject *key, *value, *foffset, *fdescr, *ftitle;
Py_ssize_t pos = 0;
int st;
if (!PyTuple_Check(names)) {
PyErr_SetString(PyExc_SystemError,
"(Hash) names is not a tuple ???");
return -1;
}
if (!PyDict_Check(fields)) {
PyErr_SetString(PyExc_SystemError,
"(Hash) fields is not a dict ???");
return -1;
}
for (pos = 0; pos < PyTuple_GET_SIZE(names); pos++) {
/*
* For each field, add the key + descr + offset to l
*/
key = PyTuple_GET_ITEM(names, pos);
value = PyDict_GetItem(fields, key);
/* XXX: are those checks necessary ? */
if (value == NULL) {
PyErr_SetString(PyExc_SystemError,
"(Hash) names and fields inconsistent ???");
return -1;
}
if (!PyUString_Check(key)) {
PyErr_SetString(PyExc_SystemError,
"(Hash) key of dtype dict not a string ???");
return -1;
}
if (!PyTuple_Check(value)) {
PyErr_SetString(PyExc_SystemError,
"(Hash) value of dtype dict not a dtype ???");
return -1;
}
if (PyTuple_GET_SIZE(value) < 2) {
PyErr_SetString(PyExc_SystemError,
"(Hash) Less than 2 items in dtype dict ???");
return -1;
}
PyList_Append(l, key);
fdescr = PyTuple_GET_ITEM(value, 0);
if (!PyArray_DescrCheck(fdescr)) {
PyErr_SetString(PyExc_SystemError,
"(Hash) First item in compound dtype tuple not a descr ???");
return -1;
}
else {
Py_INCREF(fdescr);
st = _array_descr_walk((PyArray_Descr*)fdescr, l);
Py_DECREF(fdescr);
if (st) {
return -1;
}
}
foffset = PyTuple_GET_ITEM(value, 1);
if (!PyInt_Check(foffset)) {
PyErr_SetString(PyExc_SystemError,
"(Hash) Second item in compound dtype tuple not an int ???");
return -1;
}
else {
PyList_Append(l, foffset);
}
if (PyTuple_GET_SIZE(value) > 2) {
ftitle = PyTuple_GET_ITEM(value, 2);
PyList_Append(l, ftitle);
}
}
return 0;
}
/*
* Walk into subarray, and add items for hashing in l
*
* Return 0 on success
*/
static int _array_descr_walk_subarray(PyArray_ArrayDescr* adescr, PyObject *l)
{
PyObject *item;
Py_ssize_t i;
int st;
/*
* Add shape and descr itself to the list of object to hash
*/
if (PyTuple_Check(adescr->shape)) {
for(i = 0; i < PyTuple_Size(adescr->shape); ++i) {
item = PyTuple_GetItem(adescr->shape, i);
if (item == NULL) {
PyErr_SetString(PyExc_SystemError,
"(Hash) Error while getting shape item of subarray dtype ???");
return -1;
}
PyList_Append(l, item);
}
}
else if (PyInt_Check(adescr->shape)) {
PyList_Append(l, adescr->shape);
}
else {
PyErr_SetString(PyExc_SystemError,
"(Hash) Shape of subarray dtype neither a tuple or int ???");
return -1;
}
Py_INCREF(adescr->base);
st = _array_descr_walk(adescr->base, l);
Py_DECREF(adescr->base);
return st;
}
/*
* 'Root' function to walk into a dtype. May be called recursively
*/
static int _array_descr_walk(PyArray_Descr* descr, PyObject *l)
{
int st;
if (_is_array_descr_builtin(descr)) {
return _array_descr_builtin(descr, l);
}
else {
if(descr->fields != NULL && descr->fields != Py_None) {
st = _array_descr_walk_fields(descr->names, descr->fields, l);
if (st) {
return -1;
}
}
if(PyDataType_HASSUBARRAY(descr)) {
st = _array_descr_walk_subarray(descr->subarray, l);
if (st) {
return -1;
}
}
}
return 0;
}
/*
* Return 0 if successful
*/
static int _PyArray_DescrHashImp(PyArray_Descr *descr, npy_hash_t *hash)
{
PyObject *l, *tl;
int st;
l = PyList_New(0);
if (l == NULL) {
return -1;
}
st = _array_descr_walk(descr, l);
if (st) {
Py_DECREF(l);
return -1;
}
/*
* Convert the list to tuple and compute the tuple hash using python
* builtin function
*/
tl = PyList_AsTuple(l);
Py_DECREF(l);
if (tl == NULL)
return -1;
*hash = PyObject_Hash(tl);
Py_DECREF(tl);
if (*hash == -1) {
/* XXX: does PyObject_Hash set an exception on failure ? */
#if 0
PyErr_SetString(PyExc_SystemError,
"(Hash) Error while hashing final tuple");
#endif
return -1;
}
return 0;
}
NPY_NO_EXPORT npy_hash_t
PyArray_DescrHash(PyObject* odescr)
{
PyArray_Descr *descr;
int st;
if (!PyArray_DescrCheck(odescr)) {
PyErr_SetString(PyExc_ValueError,
"PyArray_DescrHash argument must be a type descriptor");
return -1;
}
descr = (PyArray_Descr*)odescr;
if (descr->hash == -1) {
st = _PyArray_DescrHashImp(descr, &descr->hash);
if (st) {
return -1;
}
}
return descr->hash;
}
|