def _decodeMsgpack ( value ) : """Decodes unpacked Msgpack data. :param value: The raw data from the database or the admin interface. \ In the former case it is `bytes`, in the latter - `str`. :returns: Decoded native Python structure or `None`. :raises: None Does not raise exceptions. """ result = None # KLUDGE: value should not have type `str` here. But I couldn't make # msgpackfield work without this kludge - it seems that the value from # the widget is always `str`, while the value from the database is `bytes`. if type( value ) == str : try : result = json_loads( value ) except ( TypeError, JSONDecodeError ) : getLogger( 'msgpackfield' ).warning( "Failed to decode data as JSON." ) # If type of value is 'bytes' (expected behaviour) else : try: result = msgpack_unpackb( value, raw=False ) except : try : result = msgpack_unpackb( value, raw=True ) except : getLogger( 'msgpackfield' ).warning( "Failed to decode data as MsgPack." ) return result
def unpackb(raw_data: bytes, exc_cls=SerdePackingError) -> dict: """ Raises: SerdePackingError """ try: return msgpack_unpackb( raw_data, ext_hook=_unpackb_ext_hook, raw=False, max_bin_len=MAX_BIN_LEN ) except (ExtraData, ValueError, FormatError, StackError) as exc: raise exc_cls(f"Invalid msgpack data: {exc}") from exc
def _receive(self): length = int(self.server.recv(256)) sleep(0.01) flag = True if length != 0: data = '' amount_received = len(data) while len(data) < length: data += self.server.recv(length - len(data)) sleep(0.01) debug("Received response: pickled data arrived") flag = False self.response_arg = msgpack_unpackb(data, object_hook=m.decode)
def unpackb(raw_data: bytes, exc_cls=SerdePackingError) -> dict: """ Raises: SerdePackingError """ try: ret = msgpack_unpackb( raw_data, ext_hook=_unpackb_ext_hook, raw=False, max_bin_len=MAX_BIN_LEN, strict_map_key=False, ) except (ExtraData, ValueError, FormatError, StackError) as exc: raise exc_cls(f"Invalid msgpack data: {exc}") from exc # MessagePack can return any type (int, string, list etc.) as root value if not isinstance(ret, dict): raise exc_cls(f"Invalid msgpack data: root must be a map") return ret
@contextmanager def cursor(self, write=False): with self.ctx(write) as txn: cursor = txn.cursor() try: yield cursor finally: cursor.close() def close(self): self.sock.close() mpackb = lambda o: msgpack_packb(o, use_bin_type=True) munpackb = lambda b: msgpack_unpackb(b, raw=False) def mpackdict(d): for key, value in d.items(): yield (encode(key), mpackb(value)) def encodedict(d): for key, value in d.items(): yield (encode(key), encode(value)) def requires_dupsort(meth): @wraps(meth) def verify_dupsort(self, client, *args):
def deserialize(value): return msgpack_unpackb(value, encoding='utf-8')