Debian Codegraph @

pypy/module/struct/formatiterator.py

raw symbols SCIP
1 from rpython.rlib.rarithmetic import (r_uint, r_ulonglong, r_longlong,
2 maxint, intmask)
3 from rpython.rlib import jit, rutf8
4 from rpython.rlib.objectmodel import specialize
5 from rpython.rlib.rstruct.error import StructError
6 from rpython.rlib.rstruct.formatiterator import FormatIterator
7
8 from pypy.interpreter.error import OperationError
9
10
11 class PackFormatIterator(FormatIterator):
12 def __init__(self, space, wbuf, args_w):
13 self.space = space
14 self.args_w = args_w
15 self.args_index = 0
16 self.pos = 0
17 self.wbuf = wbuf
18
19 def advance(self, count):
20 self.pos += count
21
22 # This *should* be always unroll safe, the only way to get here is by
23 # unroll the interpret function, which means the fmt is const, and thus
24 # this should be const (in theory ;)
25 @jit.unroll_safe
26 @specialize.arg(1)
27 def operate(self, fmtdesc, repetitions):
28 if fmtdesc.needcount:
29 fmtdesc.pack(self, repetitions)
30 else:
31 for i in range(repetitions):
32 fmtdesc.pack(self)
33 _operate_is_specialized_ = True
34
35 @jit.unroll_safe
36 def align(self, mask):
37 pad = (-self.pos) & mask
38 for i in range(self.pos, self.pos+pad):
39 self.wbuf.setitem(i, '\x00')
40 self.advance(pad)
41
42 def finished(self):
43 if self.args_index != len(self.args_w):
44 raise StructError("too many arguments for struct format")
45
46 def accept_obj_arg(self):
47 try:
48 w_obj = self.args_w[self.args_index]
49 except IndexError:
50 raise StructError("struct format requires more arguments")
51 self.args_index += 1
52 return w_obj
53
54 def accept_int_arg(self):
55 return self._accept_integral("int_w")
56
57 def accept_uint_arg(self):
58 return self._accept_integral("uint_w")
59
60 def accept_longlong_arg(self):
61 return self._accept_integral("r_longlong_w")
62
63 def accept_ulonglong_arg(self):
64 return self._accept_integral("r_ulonglong_w")
65
66 @specialize.arg(1)
67 def _accept_integral(self, meth):
68 space = self.space
69 w_obj = self.accept_obj_arg()
70 if space.isinstance_w(w_obj, space.w_int):
71 w_index = w_obj
72 else:
73 w_index = None
74 if space.lookup(w_obj, '__index__'):
75 try:
76 w_index = space.index(w_obj)
77 except OperationError as e:
78 if not e.match(space, space.w_TypeError):
79 raise
80 pass
81 if w_index is None:
82 raise StructError("required argument is not an integer")
83 method = getattr(space, meth)
84 try:
85 return method(w_index)
86 except OperationError as e:
87 if e.match(self.space, self.space.w_OverflowError):
88 raise StructError("argument out of range")
89 raise
90
91 def accept_bool_arg(self):
92 w_obj = self.accept_obj_arg()
93 return self.space.is_true(w_obj)
94
95 def accept_str_arg(self):
96 w_obj = self.accept_obj_arg()
97 return self.space.bytes_w(w_obj)
98
99 def accept_unicode_arg(self):
100 w_obj = self.accept_obj_arg()
101 return self.space.utf8_len_w(w_obj)
102
103 def accept_float_arg(self):
104 w_obj = self.accept_obj_arg()
105 try:
106 return self.space.float_w(w_obj)
107 except OperationError as e:
108 if e.match(self.space, self.space.w_TypeError):
109 raise StructError("required argument is not a float")
110 raise
111
112
113 class UnpackFormatIterator(FormatIterator):
114 def __init__(self, space, buf):
115 self.space = space
116 self.buf = buf
117 self.length = buf.getlength()
118 self.pos = 0
119 self.strides = None
120 self.result_w = [] # list of wrapped objects
121
122 # See above comment on operate.
123 @jit.unroll_safe
124 @specialize.arg(1)
125 def operate(self, fmtdesc, repetitions):
126 if fmtdesc.needcount:
127 fmtdesc.unpack(self, repetitions)
128 else:
129 for i in range(repetitions):
130 fmtdesc.unpack(self)
131 _operate_is_specialized_ = True
132
133 def align(self, mask):
134 self.pos = (self.pos + mask) & ~mask
135
136 def finished(self):
137 value = self.pos
138 if self.strides and self.strides[0] < 0:
139 value = -self.pos
140 if value != self.length:
141 raise StructError("unpack str size too long for format")
142
143 def can_advance(self, count):
144 if self.strides:
145 count = self.strides[0]
146 end = self.pos + count
147 return end <= self.length
148
149 def advance(self, count):
150 if not self.can_advance(count):
151 raise StructError("unpack str size too short for format")
152 self.pos += count
153
154 def read(self, count):
155 curpos = self.pos
156 self.advance(count) # raise if we are out of bound
157 return self.buf.getslice(curpos, 1, count)
158
159 @specialize.argtype(1)
160 def appendobj(self, value):
161 if isinstance(value, float):
162 w_value = self.space.newfloat(value)
163 elif isinstance(value, str):
164 w_value = self.space.newbytes(value)
165 elif isinstance(value, unicode):
166 assert not isinstance(value, unicode)
167 w_value = self.space.newutf8(value.decode('utf-8'), len(value))
168 elif isinstance(value, bool):
169 w_value = self.space.newbool(value)
170 else:
171 w_value = self.space.newint(value)
172 self.result_w.append(w_value)
173
174 def append_utf8(self, value):
175 w_ch = self.space.newutf8(rutf8.unichr_as_utf8(r_uint(value)), 1)
176 self.result_w.append(w_ch)
177
178 def get_pos(self):
179 return self.pos
180
181 def get_buffer_and_pos(self):
182 return self.buf, self.pos
183
184 def skip(self, count):
185 # assumption: UnpackFormatIterator only iterates over
186 # flat structures (continous memory) either: forward (index
187 # grows) or reverse
188 if self.strides:
189 assert len(self.strides) == 1
190 count = self.strides[0]
191 end = self.pos + count
192 if end > self.length:
193 raise StructError("unpack str size too short for format")
194 self.pos = end