Example #1
0
    def test_switch_class(self):
        def lookup_func(base, module, name):
            if name == "A":
                return B
            return base(module, name)

        value = pickle_loads(pickle_dumps(A()), lookup_func)
        assert isinstance(value, B)
    def test_switch_class(self):

        def lookup_func(base, module, name):
            if name == "A":
                return B
            return base(module, name)

        value = pickle_loads(pickle_dumps(A()), lookup_func)
        assert isinstance(value, B)
Example #3
0
def load_audio_files(data, process=True):
    """unpickles the item list and if some class isn't found unpickle
    as a dict and filter them out afterwards.

    In case everything gets filtered out will raise SerializationError
    (because then likely something larger went wrong)

    Args:
        data (bytes)
        process (bool): if the dict key/value types should be converted,
            either to be usable from py3 or to convert to newer types
    Returns:
        List[AudioFile]
    Raises:
        SerializationError
    """

    dummy = type("dummy", (dict, ), {})
    error_occured = []
    temp_type_cache = {}

    def lookup_func(base, module, name):
        try:
            real_type = base(module, name)
        except (ImportError, AttributeError):
            error_occured.append(True)
            return dummy

        if module.split(".")[0] not in ("quodlibet", "tests"):
            return real_type

        # return a straight dict subclass so that unpickle doesn't call
        # our __setitem__. Further down we simply change the __class__
        # to our real type.
        if not real_type in temp_type_cache:
            new_type = type(name, (dict, ), {"real_type": real_type})
            temp_type_cache[real_type] = new_type

        return temp_type_cache[real_type]

    try:
        items = pickle_loads(data, lookup_func)
    except pickle.UnpicklingError as e:
        raise SerializationError(e)

    if error_occured:
        items = [i for i in items if not isinstance(i, dummy)]

        if not items:
            raise SerializationError(
                "all class lookups failed. something is wrong")

    if process:
        items = _py2_to_py3(items)

    try:
        for i in items:
            i.__class__ = i.real_type
    except AttributeError as e:
        raise SerializationError(e)

    return items
Example #4
0
 def test_pickle_dump(self):
     f = BytesIO()
     pickle_dump(42, f)
     assert pickle_loads(f.getvalue()) == 42
Example #5
0
 def test_pickle_dumps(self):
     v = [u"foo", b"bar", 42]
     for protocol in [0, 1, 2]:
         assert pickle_loads(pickle_dumps(v)) == v
Example #6
0
    def test_invalid(self):
        with self.assertRaises(UnpicklingError):
            pickle_loads(b"")

        with self.assertRaises(UnpicklingError):
            pickle_load(BytesIO(b""))
Example #7
0
    def test_pickle_load(self):
        data = {b"foo": u"bar", u"quux": b"baz"}

        for protocol in [0, 1, 2]:
            assert pickle_loads(pickle_dumps(data)) == data
            assert pickle_load(BytesIO(pickle_dumps(data))) == data
 def test_pickle_dump(self):
     f = cBytesIO()
     pickle_dump(42, f)
     assert pickle_loads(f.getvalue()) == 42
 def test_pickle_dumps(self):
     v = [u"foo", b"bar", 42]
     for protocol in [0, 1, 2]:
         assert pickle_loads(pickle_dumps(v)) == v
    def test_invalid(self):
        with self.assertRaises(UnpicklingError):
            pickle_loads(b"")

        with self.assertRaises(UnpicklingError):
            pickle_load(cBytesIO(b""))
    def test_pickle_load(self):
        data = {b"foo": u"bar", u"quux": b"baz"}

        for protocol in [0, 1, 2]:
            assert pickle_loads(pickle_dumps(data)) == data
            assert pickle_load(cBytesIO(pickle_dumps(data))) == data
Example #12
0
def load_audio_files(data, process=True):
    """unpickles the item list and if some class isn't found unpickle
    as a dict and filter them out afterwards.

    In case everything gets filtered out will raise SerializationError
    (because then likely something larger went wrong)

    Args:
        data (bytes)
        process (bool): if the dict key/value types should be converted,
            either to be usable from py3 or to convert to newer types
    Returns:
        List[AudioFile]
    Raises:
        SerializationError
    """

    dummy = type("dummy", (dict,), {})
    error_occured = []
    temp_type_cache = {}

    def lookup_func(base, module, name):
        try:
            real_type = base(module, name)
        except (ImportError, AttributeError):
            error_occured.append(True)
            return dummy

        if module.split(".")[0] not in ("quodlibet", "tests"):
            return real_type

        # return a straight dict subclass so that unpickle doesn't call
        # our __setitem__. Further down we simply change the __class__
        # to our real type.
        if not real_type in temp_type_cache:
            new_type = type(name, (dict,), {"real_type": real_type})
            temp_type_cache[real_type] = new_type

        return temp_type_cache[real_type]

    try:
        items = pickle_loads(data, lookup_func)
    except pickle.UnpicklingError as e:
        raise SerializationError(e)

    if error_occured:
        items = [i for i in items if not isinstance(i, dummy)]

        if not items:
            raise SerializationError("all class lookups failed. something is wrong")

    if process:
        if PY3:
            items = _py2_to_py3(items)
        else:
            items = _py2_to_py2(items)

    try:
        for i in items:
            i.__class__ = i.real_type
    except AttributeError as e:
        raise SerializationError(e)

    return items