Exemple #1
0
 def read_message_py2(self):
     chunks = []
     while True:
         hi, lo = self.wire.read(2)
         if hi == lo == 0:
             break
         size = hi << 8 | lo
         chunks.append(self.wire.read(size))
     message = bytearray(b"".join(map(bytes, chunks)))
     _, n = divmod(message[0], 0x10)
     unpacker = UnpackStream(message, offset=2)
     fields = [unpacker.unpack() for _ in range(n)]
     return message[1], fields
Exemple #2
0
 def read_message_py2(self):
     chunks = []
     while True:
         hi, lo = self.wire.read(2)
         if hi == lo == 0:
             break
         size = hi << 8 | lo
         chunks.append(self.wire.read(size))
     message = bytearray(b"".join(map(bytes, chunks)))
     _, n = divmod(message[0], 0x10)
     try:
         unpacker = UnpackStream(message, offset=2)
         fields = [unpacker.unpack() for _ in range(n)]
     except ValueError as error:
         raise_from(ProtocolError("Bad message content"), error)
     else:
         return message[1], fields
Exemple #3
0
 def read_message(self):
     chunks = []
     more = True
     while more:
         chunk = self._read_chunk()
         if chunk:
             chunks.append(chunk)
         elif chunks:
             more = False
     try:
         message = b"".join(chunks)
     except TypeError:
         # Python 2 compatibility
         message = bytearray(b"".join(map(bytes, chunks)))
     _, n = divmod(message[0], 0x10)
     tag = message[1]
     unpacker = UnpackStream(message[2:])
     fields = tuple(unpacker.unpack() for _ in range(n))
     return tag, fields
Exemple #4
0
 def read_message(self):
     chunks = []
     while True:
         try:
             hi, lo = self.wire.read(2)
         except WireError as error:
             raise_from(ConnectionBroken("Failed to read message"), error)
         else:
             if hi == lo == 0:
                 break
             size = hi << 8 | lo
             chunks.append(self.wire.read(size))
     message = b"".join(chunks)
     _, n = divmod(message[0], 0x10)
     try:
         unpacker = UnpackStream(message, offset=2)
         fields = [unpacker.unpack() for _ in range(n)]
     except ValueError as error:
         raise_from(ProtocolError("Bad message content"), error)
     else:
         return message[1], fields
Exemple #5
0
def pack_and_unpack(value):
    buffer = BytesIO()
    pack(buffer, value)
    b = buffer.getvalue()
    unpacked = UnpackStream(b).unpack()
    return b, unpacked
Exemple #6
0
def test_unpacking_unknown_marker():
    with raises(ValueError):
        UnpackStream(b"\xDF").unpack()
Exemple #7
0
def pack_and_unpack(value, version=()):
    buffer = BytesIO()
    pack_into(buffer, value, version=version)
    b = buffer.getvalue()
    unpacked = UnpackStream(b).unpack()
    return b, unpacked