| 1 |
from rpython.rlib import jit |
| 2 |
from rpython.rlib.buffer import SubBuffer |
| 3 |
from rpython.rlib.mutbuffer import MutableStringBuffer |
| 4 |
from rpython.rlib.rarithmetic import r_uint, widen |
| 5 |
from rpython.rlib.rstruct.error import StructError, StructOverflowError |
| 6 |
from rpython.rlib.rstruct.formatiterator import CalcSizeFormatIterator |
| 7 |
|
| 8 |
from pypy.interpreter.baseobjspace import W_Root |
| 9 |
from pypy.interpreter.gateway import interp2app, unwrap_spec |
| 10 |
from pypy.interpreter.error import OperationError, oefmt |
| 11 |
from pypy.interpreter.typedef import TypeDef, interp_attrproperty |
| 12 |
from pypy.interpreter.typedef import make_weakref_descr |
| 13 |
from pypy.module.struct.formatiterator import ( |
| 14 |
PackFormatIterator, UnpackFormatIterator |
| 15 |
) |
| 16 |
|
| 17 |
|
| 18 |
class Cache: |
| 19 |
def __init__(self, space): |
| 20 |
self.error = space.new_exception_class("struct.error", space.w_Exception) |
| 21 |
|
| 22 |
|
| 23 |
def get_error(space): |
| 24 |
return space.fromcache(Cache).error |
| 25 |
|
| 26 |
|
| 27 |
def _calcsize(space, format): |
| 28 |
fmtiter = CalcSizeFormatIterator() |
| 29 |
try: |
| 30 |
fmtiter.interpret(format) |
| 31 |
except StructOverflowError as e: |
| 32 |
raise OperationError(space.w_OverflowError, space.newtext(e.msg)) |
| 33 |
except StructError as e: |
| 34 |
raise OperationError(get_error(space), space.newtext(e.msg)) |
| 35 |
return fmtiter.totalsize |
| 36 |
|
| 37 |
|
| 38 |
def text_or_bytes_w(space, w_input): |
| 39 |
|
| 40 |
if space.isinstance_w(w_input, space.w_bytes): |
| 41 |
return space.bytes_w(w_input) |
| 42 |
else: |
| 43 |
return space.text_w(w_input) |
| 44 |
|
| 45 |
def calcsize(space, w_format): |
| 46 |
"""Return size of C struct described by format string fmt.""" |
| 47 |
format = text_or_bytes_w(space, w_format) |
| 48 |
return space.newint(_calcsize(space, format)) |
| 49 |
|
| 50 |
|
| 51 |
def _pack(space, format, args_w): |
| 52 |
"""Return string containing values v1, v2, ... packed according to fmt.""" |
| 53 |
size = _calcsize(space, format) |
| 54 |
wbuf = MutableStringBuffer(size) |
| 55 |
fmtiter = PackFormatIterator(space, wbuf, args_w) |
| 56 |
try: |
| 57 |
fmtiter.interpret(format) |
| 58 |
except StructOverflowError as e: |
| 59 |
raise OperationError(space.w_OverflowError, space.newtext(e.msg)) |
| 60 |
except StructError as e: |
| 61 |
raise OperationError(get_error(space), space.newtext(e.msg)) |
| 62 |
assert fmtiter.pos == wbuf.getlength(), 'missing .advance() or wrong calcsize()' |
| 63 |
return wbuf.finish() |
| 64 |
|
| 65 |
|
| 66 |
def pack(space, w_format, args_w): |
| 67 |
"""Return string containing values v1, v2, ... packed according to fmt.""" |
| 68 |
format = text_or_bytes_w(space, w_format) |
| 69 |
return do_pack(space, format, args_w) |
| 70 |
|
| 71 |
def do_pack(space, format, args_w): |
| 72 |
return space.newbytes(_pack(space, format, args_w)) |
| 73 |
|
| 74 |
|
| 75 |
@unwrap_spec(offset=int) |
| 76 |
def pack_into(space, w_format, w_buffer, offset, args_w): |
| 77 |
""" Pack the values v1, v2, ... according to fmt. |
| 78 |
Write the packed bytes into the writable buffer buf starting at offset |
| 79 |
""" |
| 80 |
format = text_or_bytes_w(space, w_format) |
| 81 |
return do_pack_into(space, format, w_buffer, offset, args_w) |
| 82 |
|
| 83 |
def do_pack_into(space, format, w_buffer, offset, args_w): |
| 84 |
""" Pack the values v1, v2, ... according to fmt. |
| 85 |
Write the packed bytes into the writable buffer buf starting at offset |
| 86 |
""" |
| 87 |
size = _calcsize(space, format) |
| 88 |
buf = space.writebuf_w(w_buffer) |
| 89 |
buflen = buf.getlength() |
| 90 |
if offset < 0: |
| 91 |
|
| 92 |
if offset + size > 0: |
| 93 |
raise oefmt(get_error(space), |
| 94 |
"no space to pack %d bytes at offset %d", |
| 95 |
size, |
| 96 |
offset) |
| 97 |
|
| 98 |
if offset + buflen < 0: |
| 99 |
raise oefmt(get_error(space), |
| 100 |
"offset %d out of range for %d-byte buffer", |
| 101 |
offset, |
| 102 |
buflen) |
| 103 |
offset += buflen |
| 104 |
if (buflen - offset) < size: |
| 105 |
raise oefmt(get_error(space), |
| 106 |
"pack_into requires a buffer of at least %d bytes for " |
| 107 |
"packing %d bytes at offset %d " |
| 108 |
"(actual buffer size is %d)", |
| 109 |
r_uint(size + offset), |
| 110 |
size, |
| 111 |
offset, |
| 112 |
buflen) |
| 113 |
|
| 114 |
wbuf = SubBuffer(buf, offset, size) |
| 115 |
fmtiter = PackFormatIterator(space, wbuf, args_w) |
| 116 |
try: |
| 117 |
fmtiter.interpret(format) |
| 118 |
except StructOverflowError as e: |
| 119 |
raise OperationError(space.w_OverflowError, space.newtext(e.msg)) |
| 120 |
except StructError as e: |
| 121 |
raise OperationError(get_error(space), space.newtext(e.msg)) |
| 122 |
|
| 123 |
|
| 124 |
def _unpack(space, format, buf): |
| 125 |
fmtiter = UnpackFormatIterator(space, buf) |
| 126 |
try: |
| 127 |
fmtiter.interpret(format) |
| 128 |
except StructOverflowError as e: |
| 129 |
raise OperationError(space.w_OverflowError, space.newtext(e.msg)) |
| 130 |
except StructError as e: |
| 131 |
raise OperationError(get_error(space), space.newtext(e.msg)) |
| 132 |
return space.newtuple(fmtiter.result_w[:]) |
| 133 |
|
| 134 |
|
| 135 |
def unpack(space, w_format, w_str): |
| 136 |
format = text_or_bytes_w(space, w_format) |
| 137 |
return do_unpack(space, format, w_str) |
| 138 |
|
| 139 |
def do_unpack(space, format, w_str): |
| 140 |
buf = space.readbuf_w(w_str) |
| 141 |
return _unpack(space, format, buf) |
| 142 |
|
| 143 |
|
| 144 |
@unwrap_spec(offset=int) |
| 145 |
def unpack_from(space, w_format, w_buffer, offset=0): |
| 146 |
"""Unpack the buffer, containing packed C structure data, according to |
| 147 |
fmt, starting at offset. Requires len(buffer[offset:]) >= calcsize(fmt).""" |
| 148 |
format = text_or_bytes_w(space, w_format) |
| 149 |
return do_unpack_from(space, format, w_buffer, offset) |
| 150 |
|
| 151 |
def do_unpack_from(space, format, w_buffer, offset=0): |
| 152 |
"""Unpack the buffer, containing packed C structure data, according to |
| 153 |
fmt, starting at offset. Requires len(buffer[offset:]) >= calcsize(fmt).""" |
| 154 |
s_size = _calcsize(space, format) |
| 155 |
buf = space.readbuf_w(w_buffer) |
| 156 |
buf_length = buf.getlength() |
| 157 |
if offset < 0: |
| 158 |
if offset + s_size > 0: |
| 159 |
raise oefmt(get_error(space), |
| 160 |
"not enough data to unpack %d bytes at offset %d", |
| 161 |
s_size, offset) |
| 162 |
if offset + buf_length < 0: |
| 163 |
raise oefmt(get_error(space), |
| 164 |
"offset %d out of range for %d-byte buffer", |
| 165 |
offset, buf_length) |
| 166 |
offset += buf_length |
| 167 |
if buf_length - offset < s_size: |
| 168 |
raise oefmt(get_error(space), |
| 169 |
"unpack_from requires a buffer of at least %d bytes for " |
| 170 |
"unpacking %d bytes at offset %d " |
| 171 |
"(actual buffer size is %d)", |
| 172 |
r_uint(s_size + offset), s_size, offset, buf_length) |
| 173 |
buf = SubBuffer(buf, offset, s_size) |
| 174 |
return _unpack(space, format, buf) |
| 175 |
|
| 176 |
|
| 177 |
class W_UnpackIter(W_Root): |
| 178 |
def __init__(self, space, w_struct, w_buffer): |
| 179 |
buf = space.readbuf_w(w_buffer) |
| 180 |
if w_struct.size <= 0: |
| 181 |
raise oefmt(get_error(space), |
| 182 |
"cannot iteratively unpack with a struct of length %d", |
| 183 |
w_struct.size) |
| 184 |
if buf.getlength() % w_struct.size != 0: |
| 185 |
raise oefmt(get_error(space), |
| 186 |
"iterative unpacking requires a bytes length multiple of %d", |
| 187 |
w_struct.size) |
| 188 |
self.w_struct = w_struct |
| 189 |
self.buf = buf |
| 190 |
self.index = 0 |
| 191 |
|
| 192 |
def descr_iter(self, space): |
| 193 |
return self |
| 194 |
|
| 195 |
def descr_next(self, space): |
| 196 |
if self.w_struct is None: |
| 197 |
raise OperationError(space.w_StopIteration, space.w_None) |
| 198 |
if self.index >= self.buf.getlength(): |
| 199 |
raise OperationError(space.w_StopIteration, space.w_None) |
| 200 |
size = self.w_struct.size |
| 201 |
buf = SubBuffer(self.buf, self.index, size) |
| 202 |
w_res = _unpack(space, self.w_struct.format, buf) |
| 203 |
self.index += size |
| 204 |
return w_res |
| 205 |
|
| 206 |
def descr_length_hint(self, space): |
| 207 |
if self.w_struct is None: |
| 208 |
return space.newint(0) |
| 209 |
length = (self.buf.getlength() - self.index) // self.w_struct.size |
| 210 |
return space.newint(length) |
| 211 |
|
| 212 |
|
| 213 |
class W_Struct(W_Root): |
| 214 |
_immutable_fields_ = ["format", "size"] |
| 215 |
|
| 216 |
format = "" |
| 217 |
size = -1 |
| 218 |
|
| 219 |
def descr__new__(space, w_subtype, __args__): |
| 220 |
return space.allocate_instance(W_Struct, w_subtype) |
| 221 |
|
| 222 |
def descr__init__(self, space, w_format): |
| 223 |
format = text_or_bytes_w(space, w_format) |
| 224 |
self.format = format |
| 225 |
self.size = _calcsize(space, format) |
| 226 |
|
| 227 |
def descr_pack(self, space, args_w): |
| 228 |
return do_pack(space, jit.promote_string(self.format), args_w) |
| 229 |
|
| 230 |
@unwrap_spec(offset=int) |
| 231 |
def descr_pack_into(self, space, w_buffer, offset, args_w): |
| 232 |
return do_pack_into(space, jit.promote_string(self.format), w_buffer, offset, args_w) |
| 233 |
|
| 234 |
def descr_unpack(self, space, w_str): |
| 235 |
return do_unpack(space, jit.promote_string(self.format), w_str) |
| 236 |
|
| 237 |
@unwrap_spec(offset=int) |
| 238 |
def descr_unpack_from(self, space, w_buffer, offset=0): |
| 239 |
return do_unpack_from(space, jit.promote_string(self.format), w_buffer, offset) |
| 240 |
|
| 241 |
def descr_iter_unpack(self, space, w_buffer): |
| 242 |
return W_UnpackIter(space, self, w_buffer) |
| 243 |
|
| 244 |
W_Struct.typedef = TypeDef("Struct", |
| 245 |
__new__=interp2app(W_Struct.descr__new__.im_func), |
| 246 |
__init__=interp2app(W_Struct.descr__init__), |
| 247 |
format=interp_attrproperty("format", cls=W_Struct, wrapfn="newtext"), |
| 248 |
size=interp_attrproperty("size", cls=W_Struct, wrapfn="newint"), |
| 249 |
|
| 250 |
pack=interp2app(W_Struct.descr_pack), |
| 251 |
unpack=interp2app(W_Struct.descr_unpack), |
| 252 |
pack_into=interp2app(W_Struct.descr_pack_into), |
| 253 |
unpack_from=interp2app(W_Struct.descr_unpack_from), |
| 254 |
iter_unpack=interp2app(W_Struct.descr_iter_unpack), |
| 255 |
__weakref__=make_weakref_descr(W_Struct), |
| 256 |
) |
| 257 |
|
| 258 |
W_UnpackIter.typedef = TypeDef("unpack_iterator", |
| 259 |
__iter__=interp2app(W_UnpackIter.descr_iter), |
| 260 |
__next__=interp2app(W_UnpackIter.descr_next), |
| 261 |
__length_hint__=interp2app(W_UnpackIter.descr_length_hint) |
| 262 |
) |
| 263 |
|
| 264 |
def iter_unpack(space, w_format, w_buffer): |
| 265 |
w_struct = W_Struct() |
| 266 |
w_struct.descr__init__(space, w_format) |
| 267 |
return W_UnpackIter(space, w_struct, w_buffer) |
| 268 |
|
| 269 |
def clearcache(space): |
| 270 |
"""No-op on PyPy""" |