コード例 #1
0
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
コード例 #2
0
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
コード例 #3
0
    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)
コード例 #4
0
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
コード例 #5
0
ファイル: greendb.py プロジェクト: ruvcoindev/greendb
    @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):
コード例 #6
0
def deserialize(value):
    return msgpack_unpackb(value, encoding='utf-8')