def check_pack_unpack(packet, numeric_type): packed = dictpack.pack(packet, numeric_type) assert dictpack.pack(packet, numeric_type) == packed unpack_packet, unpack_numeric = dictpack.unpack(packed) assert unpack_packet == packet assert unpack_numeric == numeric_type
def test_pack_errors(): # test unknown packet class PacketUnknown(packets.Packet): pass packet = PacketUnknown() packed = dictpack.pack(PacketUnknown(), True) unpack_packet, unpack_numeric = dictpack.unpack(packed) assert isinstance(unpack_packet, packets.PacketError), 'unpack should return PacketError' assert unpack_numeric == True, 'unpack should return numeric type False'
def sendPacket(self, packet, reset_keepalive=True): p_dict = pack(packet, self._numeric_type) p_type = p_dict.pop('type') self.dataWrite(self._packer.pack([p_type, p_dict]), reset_keepalive)
def _pack_packets(self, packets): for packet in packets: p_dict = pack(packet, self._numeric_type) p_type = p_dict.pop('type') yield self._packer.pack([p_type, p_dict])