def build(self, obj): """ Build an object in memory. """ stream = BytesIO() self.build_stream(obj, stream) return stream.getvalue()
def _build(self, obj, stream, context): size = self._sizeof(context) stream2 = BytesIO() self.subcon._build(obj, stream2, context) data = self.encoder(stream2.getvalue()) assert len(data) == size _write_stream(stream, self._sizeof(context), data)
def parse(self, data): """ Parse an in-memory buffer. Strings, buffers, memoryviews, and other complete buffers can be parsed with this method. """ return self.parse_stream(BytesIO(data))
def _build(self, obj, stream, context): if self.include_name: name, obj = obj for sc in self.subcons: if sc.name == name: sc._build(obj, stream, context) return else: for sc in self.subcons: stream2 = BytesIO() context2 = context.__copy__() try: sc._build(obj, stream2, context2) except Exception: pass else: context.__update__(context2) stream.write(stream2.getvalue()) return raise SelectError("no subconstruct matched", obj)
def _parse(self, stream, context): data = _read_stream(stream, self._sizeof(context)) stream2 = BytesIO(self.decoder(data)) return self.subcon._parse(stream2, context)
def _build(self, obj, stream, context): inner_stream = BytesIO() self.subcon._build(obj, inner_stream, context) data = inner_stream.getvalue() self.length_field._build(len(data), stream, context) stream.write(data)