Debian Codegraph @

pypy/module/micronumpy/flagsobj.py

raw symbols SCIP
1 from rpython.rlib import jit
2 from rpython.rlib.rstring import StringBuilder
3
4 from pypy.interpreter.baseobjspace import W_Root
5 from pypy.interpreter.error import oefmt
6 from pypy.interpreter.gateway import interp2app
7 from pypy.interpreter.typedef import TypeDef, GetSetProperty
8 from pypy.module.micronumpy import constants as NPY
9 from pypy.module.micronumpy.strides import is_c_contiguous, is_f_contiguous
10
11 def enable_flags(arr, flags):
12 arr.flags |= flags
13
14 def clear_flags(arr, flags):
15 arr.flags &= ~flags
16
17 def get_tf_str(flags, key):
18 if flags & key:
19 return 'True'
20 return 'False'
21
22 class W_FlagsObject(W_Root):
23 def __init__(self, arr):
24 if arr:
25 self.flags = arr.get_flags()
26 else:
27 self.flags = (NPY.ARRAY_C_CONTIGUOUS | NPY.ARRAY_F_CONTIGUOUS |
28 NPY.ARRAY_OWNDATA | NPY.ARRAY_ALIGNED)
29
30 def descr__new__(space, w_subtype):
31 self = space.allocate_instance(W_FlagsObject, w_subtype)
32 W_FlagsObject.__init__(self, None)
33 return self
34
35 def descr_c_contiguous(self, space):
36 return space.newbool(bool(self.flags & NPY.ARRAY_C_CONTIGUOUS))
37
38 def descr_f_contiguous(self, space):
39 return space.newbool(bool(self.flags & NPY.ARRAY_F_CONTIGUOUS))
40
41 def descr_get_writeable(self, space):
42 return space.newbool(bool(self.flags & NPY.ARRAY_WRITEABLE))
43
44 def descr_get_owndata(self, space):
45 return space.newbool(bool(self.flags & NPY.ARRAY_OWNDATA))
46
47 def descr_get_aligned(self, space):
48 return space.newbool(bool(self.flags & NPY.ARRAY_ALIGNED))
49
50 def descr_get_fnc(self, space):
51 return space.newbool(bool(
52 self.flags & NPY.ARRAY_F_CONTIGUOUS and not
53 self.flags & NPY.ARRAY_C_CONTIGUOUS ))
54
55 def descr_get_forc(self, space):
56 return space.newbool(bool(
57 self.flags & NPY.ARRAY_F_CONTIGUOUS or
58 self.flags & NPY.ARRAY_C_CONTIGUOUS ))
59
60 def descr_get_num(self, space):
61 return space.newint(self.flags)
62
63 def descr_getitem(self, space, w_item):
64 key = space.text_w(w_item)
65 if key == "C" or key == "CONTIGUOUS" or key == "C_CONTIGUOUS":
66 return self.descr_c_contiguous(space)
67 if key == "F" or key == "FORTRAN" or key == "F_CONTIGUOUS":
68 return self.descr_f_contiguous(space)
69 if key == "W" or key == "WRITEABLE":
70 return self.descr_get_writeable(space)
71 if key == "FNC":
72 return self.descr_get_fnc(space)
73 if key == "FORC":
74 return self.descr_get_forc(space)
75 raise oefmt(space.w_KeyError, "Unknown flag")
76
77 def descr_setitem(self, space, w_item, w_value):
78 raise oefmt(space.w_KeyError, "Unknown flag")
79
80 def eq(self, space, w_other):
81 if not isinstance(w_other, W_FlagsObject):
82 return False
83 return self.flags == w_other.flags
84
85 def descr_eq(self, space, w_other):
86 return space.newbool(self.eq(space, w_other))
87
88 def descr_ne(self, space, w_other):
89 return space.newbool(not self.eq(space, w_other))
90
91 def descr___str__(self, space):
92 s = StringBuilder()
93 s.append(' C_CONTIGUOUS : ')
94 s.append(get_tf_str(self.flags, NPY.ARRAY_C_CONTIGUOUS))
95 s.append('\n F_CONTIGUOUS : ')
96 s.append(get_tf_str(self.flags, NPY.ARRAY_F_CONTIGUOUS))
97 s.append('\n OWNDATA : ')
98 s.append(get_tf_str(self.flags, NPY.ARRAY_OWNDATA))
99 s.append('\n WRITEABLE : ')
100 s.append(get_tf_str(self.flags, NPY.ARRAY_WRITEABLE))
101 s.append('\n ALIGNED : ')
102 s.append(get_tf_str(self.flags, NPY.ARRAY_ALIGNED))
103 s.append('\n UPDATEIFCOPY : ')
104 s.append(get_tf_str(self.flags, NPY.ARRAY_UPDATEIFCOPY))
105 return space.newtext(s.build())
106
107 W_FlagsObject.typedef = TypeDef("numpy.flagsobj",
108 __new__ = interp2app(W_FlagsObject.descr__new__.im_func),
109
110 __getitem__ = interp2app(W_FlagsObject.descr_getitem),
111 __setitem__ = interp2app(W_FlagsObject.descr_setitem),
112 __eq__ = interp2app(W_FlagsObject.descr_eq),
113 __ne__ = interp2app(W_FlagsObject.descr_ne),
114 __str__ = interp2app(W_FlagsObject.descr___str__),
115 __repr__ = interp2app(W_FlagsObject.descr___str__),
116
117 contiguous = GetSetProperty(W_FlagsObject.descr_c_contiguous),
118 c_contiguous = GetSetProperty(W_FlagsObject.descr_c_contiguous),
119 f_contiguous = GetSetProperty(W_FlagsObject.descr_f_contiguous),
120 fortran = GetSetProperty(W_FlagsObject.descr_f_contiguous),
121 writeable = GetSetProperty(W_FlagsObject.descr_get_writeable),
122 owndata = GetSetProperty(W_FlagsObject.descr_get_owndata),
123 aligned = GetSetProperty(W_FlagsObject.descr_get_aligned),
124 fnc = GetSetProperty(W_FlagsObject.descr_get_fnc),
125 forc = GetSetProperty(W_FlagsObject.descr_get_forc),
126 num = GetSetProperty(W_FlagsObject.descr_get_num),
127 )