Debian Codegraph @

pypy/module/micronumpy/hashdescr.py

raw symbols SCIP
1 """
2 How does this work ? The hash is computed from a list which contains all the
3 information specific to a type. The hard work is to build the list
4 (_array_descr_walk). The list is built as follows:
5 * If the dtype is builtin (no fields, no subarray), then the list
6 contains 6 items which uniquely define one dtype (_array_descr_builtin)
7 * If the dtype is a compound array, one walk on each field. For each
8 field, we append title, names, offset to the final list used for
9 hashing, and then append the list recursively built for each
10 corresponding dtype (_array_descr_walk_fields)
11 * If the dtype is a subarray, one adds the shape tuple to the list, and
12 then append the list recursively built for each corresponding dtype
13 (_array_descr_walk_subarray)
14 """
15 from pypy.interpreter.error import OperationError, oefmt
16 from pypy.module.micronumpy import types, boxes, support, constants as NPY
17
18 def _normalize_byteorder(descr):
19 return NPY.NATBYTE if descr.is_native() else NPY.OPPBYTE
20
21 def _array_descr_builtin(space, descr):
22 nbyteorder = _normalize_byteorder(descr)
23
24 # For builtin type, hash relies on : kind + byeorder + flags +
25 # type_num + elsize + alignment
26 nt = space.newtext
27 ni = space.newint
28 return [nt(descr.kind), nt(nbyteorder), ni(descr.flags),
29 ni(descr.elsize), ni(descr.alignment)]
30
31 def _array_descr_walk_fields(space, names, fields):
32 from pypy.module.micronumpy.descriptor import W_Dtype
33 res = []
34 for key, title in names:
35 # For each field, add the key, descr, offset
36 try:
37 value = fields[key]
38 except KeyError:
39 raise oefmt(space.w_SystemError,
40 "(Hash) names and fields inconsistent")
41 if not isinstance(key, str):
42 raise oefmt(space.w_SystemError,
43 "(Hash) key of dtype dict not a string ???")
44 if not isinstance(value, tuple):
45 raise oefmt(space.w_SystemError,
46 "(Hash) value of dtype dict not a dtype ???")
47 if len(value) < 2:
48 raise oefmt(space.w_SystemError,
49 "(Hash) Less than 2 items in dtype dict ???")
50 res.append(space.newtext(key))
51 if not isinstance(value[1], W_Dtype):
52 raise oefmt(space.w_SystemError,
53 "(Hash) item in compound dtype tuple not a descr ???")
54
55 sub_res = _array_descr_walk(space, value[1])
56 for v in sub_res:
57 res.append(v)
58 if not isinstance(value[0], int):
59 raise oefmt(space.w_SystemError,
60 "(Hash) item in compound dtype tuple not an int ???")
61 res.append(space.newint(value[0]))
62 if title:
63 res.append(space.newtext(title))
64 return res
65
66 def _array_descr_walk_subarray(space, descr):
67 # Add shape and descr itself to the list of object to hash
68 # descr.shape will always be a sequence in micronumpy, in
69 # numpy it can be an int
70 res = []
71 for item in descr.shape:
72 res.append(space.newint(item))
73 sub_res = _array_descr_walk(space, descr.subdtype)
74 for v in sub_res:
75 res.append(v)
76
77 def _array_descr_walk(space, descr):
78 """Root function to walk into a dtype. May be called recursively
79 """
80 if descr.fields:
81 return _array_descr_walk_fields(space, descr.names, descr.fields)
82 if descr.subdtype:
83 return _array_descr_walk_subarray(space, descr.subdtype)
84 return _array_descr_builtin(space, descr)
85
86
87