Example #1
0
 def build(self, obj):
     """
     Build an object in memory.
     """
     stream = BytesIO()
     self.build_stream(obj, stream)
     return stream.getvalue()
Example #2
0
 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)
Example #3
0
 def build(self, obj):
     """
     Build an object in memory.
     """
     stream = BytesIO()
     self.build_stream(obj, stream)
     return stream.getvalue()
Example #4
0
 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)
Example #5
0
    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))
Example #6
0
 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)
Example #7
0
 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)
Example #8
0
 def _parse(self, stream, context):
     data = _read_stream(stream, self._sizeof(context))
     stream2 = BytesIO(self.decoder(data))
     return self.subcon._parse(stream2, context)
Example #9
0
 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)