Python pickle.PickleError() Examples

The following are 30 code examples for showing how to use pickle.PickleError(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module pickle , or try the search function .

Example 1
Project: recruit   Author: Frank-qlu   File: cache.py    License: Apache License 2.0 6 votes vote down vote up
def load_object(self, value):
        """The reversal of :meth:`dump_object`.  This might be called with
        None.
        """
        if value is None:
            return None
        if value.startswith(b"!"):
            try:
                return pickle.loads(value[1:])
            except pickle.PickleError:
                return None
        try:
            return int(value)
        except ValueError:
            # before 0.8 we did not have serialization.  Still support that.
            return value 
Example 2
Project: jbox   Author: jpush   File: cache.py    License: MIT License 6 votes vote down vote up
def load_object(self, value):
        """The reversal of :meth:`dump_object`.  This might be called with
        None.
        """
        if value is None:
            return None
        if value.startswith(b'!'):
            try:
                return pickle.loads(value[1:])
            except pickle.PickleError:
                return None
        try:
            return int(value)
        except ValueError:
            # before 0.8 we did not have serialization.  Still support that.
            return value 
Example 3
Project: mars   Author: mars-project   File: test_gevent_pool.py    License: Apache License 2.0 6 votes vote down vote up
def testProcessUnpickled(self):
        with create_actor_pool(n_process=2, distributor=DummyDistributor(2),
                               backend='gevent') as pool:
            ref1 = pool.create_actor(DummyActor, 1)
            with self.assertRaises(pickle.PickleError):
                ref1.send(lambda x: x)

            ref2 = pool.create_actor(DummyActor, 1, uid='admin-1')
            with self.assertRaises(pickle.PickleError):
                ref1.send(('send_unpickled', ref2))

            with self.assertRaises(pickle.PickleError):
                ref1.send(('send', ref2, 'send_unpickled', ref1))

            with self.assertRaises(pickle.PickleError):
                pool.create_actor(DummyActor, lambda x: x)

            with self.assertRaises(pickle.PickleError):
                ref1.send(('create_unpickled',)) 
Example 4
Project: mars   Author: mars-project   File: test_gevent_pool.py    License: Apache License 2.0 6 votes vote down vote up
def testRemoteProcessPoolUnpickled(self):
        with create_actor_pool(address=True, n_process=2, distributor=DummyDistributor(2),
                               backend='gevent') as pool:
            addr = pool.cluster_info.address

            client = new_client(backend='gevent')

            ref1 = client.create_actor(DummyActor, 1, address=addr)
            with self.assertRaises(pickle.PickleError):
                ref1.send(lambda x: x)

            ref2 = client.create_actor(DummyActor, 1, address=addr, uid='admin-1')
            with self.assertRaises(pickle.PickleError):
                ref1.send(('send_unpickled', ref2))

            with self.assertRaises(pickle.PickleError):
                ref1.send(('send', ref2, 'send_unpickled', ref1))

            with self.assertRaises(pickle.PickleError):
                client.create_actor(DummyActor, lambda x: x, address=addr)

            with self.assertRaises(pickle.PickleError):
                ref1.send(('create_unpickled',)) 
Example 5
Project: GTDWeb   Author: lanbing510   File: locmem.py    License: GNU General Public License v2.0 6 votes vote down vote up
def get(self, key, default=None, version=None, acquire_lock=True):
        key = self.make_key(key, version=version)
        self.validate_key(key)
        pickled = None
        with (self._lock.reader() if acquire_lock else dummy()):
            if not self._has_expired(key):
                pickled = self._cache[key]
        if pickled is not None:
            try:
                return pickle.loads(pickled)
            except pickle.PickleError:
                return default

        with (self._lock.writer() if acquire_lock else dummy()):
            try:
                del self._cache[key]
                del self._expire_info[key]
            except KeyError:
                pass
            return default 
Example 6
Project: cachelib   Author: pallets   File: redis.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def load_object(self, value):
        """The reversal of :meth:`dump_object`.  This might be called with
        None.
        """
        if value is None:
            return None
        if value.startswith(b'!'):
            try:
                return pickle.loads(value[1:])
            except pickle.PickleError:
                return None
        try:
            return int(value)
        except ValueError:
            # before 0.8 we did not have serialization.  Still support that.
            return value 
Example 7
Project: lambda-packs   Author: ryfeus   File: cache.py    License: MIT License 6 votes vote down vote up
def load_object(self, value):
        """The reversal of :meth:`dump_object`.  This might be called with
        None.
        """
        if value is None:
            return None
        if value.startswith(b'!'):
            try:
                return pickle.loads(value[1:])
            except pickle.PickleError:
                return None
        try:
            return int(value)
        except ValueError:
            # before 0.8 we did not have serialization.  Still support that.
            return value 
Example 8
Project: aioamqp_consumer   Author: aio-libs   File: exceptions.py    License: MIT License 6 votes vote down vote up
def dumps(self):
        try:
            return self._dumps(self.err)
        except settings.PICKLE_ERRORS as exc:
            logger.warning(exc, exc_info=exc)

            return self._dumps(exc)
        except BaseException as exc:
            logger.critical(exc, exc_info=exc)

            try:
                return self._dumps(exc)
            except BaseException as exc:
                logger.critical(exc, exc_info=exc)

                exc_tb = traceback.format_exception(
                    etype=type(exc),
                    value=exc,
                    tb=exc.__traceback__,
                )

                return self._dumps(pickle.PickleError(''.join(exc_tb))) 
Example 9
Project: exopy   Author: Exopy   File: qt_clipboard.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def __init__(self, data=None, pickle=False):
        QtCore.QMimeData.__init__(self)

        # Keep a local reference to be returned if possible.
        self._local_instance = data

        if pickle:
            if data is not None:
                # We may not be able to pickle the data.
                try:
                    pdata = dumps(data, -1)
                    # This format (as opposed to using a single sequence)
                    # allows the type to be extracted without unpickling
                    # the data.
                    self.setData(self.MIME_TYPE, dumps(data.__class__) + pdata)
                except (PickleError, TypeError):
                    # if pickle fails, still try to create a draggable
                    warnings.warn(("Could not pickle dragged object %s, " +
                                   "using %s mimetype instead") % (repr(data),
                                  self.NOPICKLE_MIME_TYPE), RuntimeWarning)
                    self.setData(self.NOPICKLE_MIME_TYPE,
                                 str(id(data)).encode('utf8'))

        else:
            self.setData(self.NOPICKLE_MIME_TYPE, str(id(data)).encode('utf8')) 
Example 10
Project: exopy   Author: Exopy   File: qt_clipboard.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def instance(self):
        """ Return the instance.

        """
        if self._local_instance is not None:
            return self._local_instance

        if not self.hasFormat(self.MIME_TYPE):
            # We have no pickled python data defined.
            return None

        io = StringIO(bytes(self.data(self.MIME_TYPE)))

        try:
            # Skip the type.
            load(io)

            # Recreate the instance.
            return load(io)
        except PickleError:
            pass

        return None 
Example 11
Project: BinderFilter   Author: dxwu   File: pickletester.py    License: MIT License 6 votes vote down vote up
def test_reduce_bad_iterator(self):
        # Issue4176: crash when 4th and 5th items of __reduce__()
        # are not iterators
        class C(object):
            def __reduce__(self):
                # 4th item is not an iterator
                return list, (), None, [], None
        class D(object):
            def __reduce__(self):
                # 5th item is not an iterator
                return dict, (), None, None, []

        # Protocol 0 is less strict and also accept iterables.
        for proto in protocols:
            try:
                self.dumps(C(), proto)
            except (AttributeError, pickle.PickleError, cPickle.PickleError):
                pass
            try:
                self.dumps(D(), proto)
            except (AttributeError, pickle.PickleError, cPickle.PickleError):
                pass 
Example 12
Project: RSSNewsGAE   Author: liantian-cn   File: cache.py    License: Apache License 2.0 6 votes vote down vote up
def load_object(self, value):
        """The reversal of :meth:`dump_object`.  This might be called with
        None.
        """
        if value is None:
            return None
        if value.startswith(b'!'):
            try:
                return pickle.loads(value[1:])
            except pickle.PickleError:
                return None
        try:
            return int(value)
        except ValueError:
            # before 0.8 we did not have serialization.  Still support that.
            return value 
Example 13
Project: oss-ftp   Author: aliyun   File: pickletester.py    License: MIT License 6 votes vote down vote up
def test_reduce_bad_iterator(self):
        # Issue4176: crash when 4th and 5th items of __reduce__()
        # are not iterators
        class C(object):
            def __reduce__(self):
                # 4th item is not an iterator
                return list, (), None, [], None
        class D(object):
            def __reduce__(self):
                # 5th item is not an iterator
                return dict, (), None, None, []

        # Protocol 0 is less strict and also accept iterables.
        for proto in protocols:
            try:
                self.dumps(C(), proto)
            except (AttributeError, pickle.PickleError, cPickle.PickleError):
                pass
            try:
                self.dumps(D(), proto)
            except (AttributeError, pickle.PickleError, cPickle.PickleError):
                pass 
Example 14
Project: Fluid-Designer   Author: Microvellum   File: pickletester.py    License: GNU General Public License v3.0 6 votes vote down vote up
def test_reduce_bad_iterator(self):
        # Issue4176: crash when 4th and 5th items of __reduce__()
        # are not iterators
        class C(object):
            def __reduce__(self):
                # 4th item is not an iterator
                return list, (), None, [], None
        class D(object):
            def __reduce__(self):
                # 5th item is not an iterator
                return dict, (), None, None, []

        # Protocol 0 is less strict and also accept iterables.
        for proto in protocols:
            try:
                self.dumps(C(), proto)
            except (pickle.PickleError):
                pass
            try:
                self.dumps(D(), proto)
            except (pickle.PickleError):
                pass 
Example 15
Project: Building-Recommendation-Systems-with-Python   Author: PacktPublishing   File: cache.py    License: MIT License 6 votes vote down vote up
def load_object(self, value):
        """The reversal of :meth:`dump_object`.  This might be called with
        None.
        """
        if value is None:
            return None
        if value.startswith(b"!"):
            try:
                return pickle.loads(value[1:])
            except pickle.PickleError:
                return None
        try:
            return int(value)
        except ValueError:
            # before 0.8 we did not have serialization.  Still support that.
            return value 
Example 16
Project: Building-Recommendation-Systems-with-Python   Author: PacktPublishing   File: cache.py    License: MIT License 6 votes vote down vote up
def load_object(self, value):
        """The reversal of :meth:`dump_object`.  This might be called with
        None.
        """
        if value is None:
            return None
        if value.startswith(b"!"):
            try:
                return pickle.loads(value[1:])
            except pickle.PickleError:
                return None
        try:
            return int(value)
        except ValueError:
            # before 0.8 we did not have serialization.  Still support that.
            return value 
Example 17
Project: scylla   Author: acaceres2176   File: cache.py    License: Apache License 2.0 6 votes vote down vote up
def load_object(self, value):
        """The reversal of :meth:`dump_object`.  This might be called with
        None.
        """
        if value is None:
            return None
        if value.startswith(b"!"):
            try:
                return pickle.loads(value[1:])
            except pickle.PickleError:
                return None
        try:
            return int(value)
        except ValueError:
            # before 0.8 we did not have serialization.  Still support that.
            return value 
Example 18
Project: ironpython3   Author: IronLanguages   File: pickletester.py    License: Apache License 2.0 6 votes vote down vote up
def test_reduce_bad_iterator(self):
        # Issue4176: crash when 4th and 5th items of __reduce__()
        # are not iterators
        class C(object):
            def __reduce__(self):
                # 4th item is not an iterator
                return list, (), None, [], None
        class D(object):
            def __reduce__(self):
                # 5th item is not an iterator
                return dict, (), None, None, []

        # Protocol 0 is less strict and also accept iterables.
        for proto in protocols:
            try:
                self.dumps(C(), proto)
            except (pickle.PickleError):
                pass
            try:
                self.dumps(D(), proto)
            except (pickle.PickleError):
                pass 
Example 19
Project: Financial-Portfolio-Flask   Author: Ryan-Gordon   File: cache.py    License: MIT License 6 votes vote down vote up
def load_object(self, value):
        """The reversal of :meth:`dump_object`.  This might be called with
        None.
        """
        if value is None:
            return None
        if value.startswith(b'!'):
            try:
                return pickle.loads(value[1:])
            except pickle.PickleError:
                return None
        try:
            return int(value)
        except ValueError:
            # before 0.8 we did not have serialization.  Still support that.
            return value 
Example 20
Project: Flask-P2P   Author: chalasr   File: cache.py    License: MIT License 6 votes vote down vote up
def load_object(self, value):
        """The reversal of :meth:`dump_object`.  This might be callde with
        None.
        """
        if value is None:
            return None
        if value.startswith(b'!'):
            try:
                return pickle.loads(value[1:])
            except pickle.PickleError:
                return None
        try:
            return int(value)
        except ValueError:
            # before 0.8 we did not have serialization.  Still support that.
            return value 
Example 21
Project: planespotter   Author: yfauser   File: cache.py    License: MIT License 6 votes vote down vote up
def load_object(self, value):
        """The reversal of :meth:`dump_object`.  This might be called with
        None.
        """
        if value is None:
            return None
        if value.startswith(b'!'):
            try:
                return pickle.loads(value[1:])
            except pickle.PickleError:
                return None
        try:
            return int(value)
        except ValueError:
            # before 0.8 we did not have serialization.  Still support that.
            return value 
Example 22
Project: malwareHunter   Author: abdesslem   File: sqla.py    License: GNU General Public License v2.0 6 votes vote down vote up
def do_open(self, flags, replace):
        if self.loaded:
            self.flags = flags
            return
        select = sa.select([self.table.c.data],
                           (self.table.c.namespace == self.namespace))
        result = self.bind.execute(select).fetchone()
        if not result:
            self._is_new = True
            self.hash = {}
        else:
            self._is_new = False
            try:
                self.hash = result['data']
            except (IOError, OSError, EOFError, cPickle.PickleError,
                    pickle.PickleError):
                log.debug("Couln't load pickle data, creating new storage")
                self.hash = {}
                self._is_new = True
        self.flags = flags
        self.loaded = True 
Example 23
Project: malwareHunter   Author: abdesslem   File: database.py    License: GNU General Public License v2.0 6 votes vote down vote up
def do_open(self, flags, replace):
        # If we already loaded the data, don't bother loading it again
        if self.loaded:
            self.flags = flags
            return

        cache = self.cache
        result = sa.select([cache.c.data],
                           cache.c.namespace == self.namespace
                          ).execute().fetchone()
        if not result:
            self._is_new = True
            self.hash = {}
        else:
            self._is_new = False
            try:
                self.hash = result['data']
            except (IOError, OSError, EOFError, cPickle.PickleError,
                    pickle.PickleError):
                log.debug("Couln't load pickle data, creating new storage")
                self.hash = {}
                self._is_new = True
        self.flags = flags
        self.loaded = True 
Example 24
Project: opentracing-python   Author: opentracing   File: binary_propagator.py    License: Apache License 2.0 5 votes vote down vote up
def extract(self, carrier):
        if type(carrier) is not bytearray:
            raise InvalidCarrierException()

        try:
            span_context = pickle.loads(carrier)
        except (EOFError, pickle.PickleError):
            raise SpanContextCorruptedException()

        return span_context 
Example 25
Project: recruit   Author: Frank-qlu   File: cache.py    License: Apache License 2.0 5 votes vote down vote up
def set(self, key, value, timeout=None):
        """Add a new key/value to the cache (overwrites value, if key already
        exists in the cache).

        :param key: the key to set
        :param value: the value for the key
        :param timeout: the cache timeout for the key in seconds (if not
                        specified, it uses the default timeout). A timeout of
                        0 idicates that the cache never expires.
        :returns: ``True`` if key has been updated, ``False`` for backend
                  errors. Pickling errors, however, will raise a subclass of
                  ``pickle.PickleError``.
        :rtype: boolean
        """
        return True 
Example 26
Project: recruit   Author: Frank-qlu   File: cache.py    License: Apache License 2.0 5 votes vote down vote up
def get(self, key):
        try:
            expires, value = self._cache[key]
            if expires == 0 or expires > time():
                return pickle.loads(value)
        except (KeyError, pickle.PickleError):
            return None 
Example 27
Project: recruit   Author: Frank-qlu   File: cache.py    License: Apache License 2.0 5 votes vote down vote up
def get(self, key):
        filename = self._get_filename(key)
        try:
            with open(filename, "rb") as f:
                pickle_time = pickle.load(f)
                if pickle_time == 0 or pickle_time >= time():
                    return pickle.load(f)
                else:
                    os.remove(filename)
                    return None
        except (IOError, OSError, pickle.PickleError):
            return None 
Example 28
Project: recruit   Author: Frank-qlu   File: cache.py    License: Apache License 2.0 5 votes vote down vote up
def has(self, key):
        filename = self._get_filename(key)
        try:
            with open(filename, "rb") as f:
                pickle_time = pickle.load(f)
                if pickle_time == 0 or pickle_time >= time():
                    return True
                else:
                    os.remove(filename)
                    return False
        except (IOError, OSError, pickle.PickleError):
            return False 
Example 29
Project: jbox   Author: jpush   File: cache.py    License: MIT License 5 votes vote down vote up
def set(self, key, value, timeout=None):
        """Add a new key/value to the cache (overwrites value, if key already
        exists in the cache).

        :param key: the key to set
        :param value: the value for the key
        :param timeout: the cache timeout for the key (if not specified,
                        it uses the default timeout). A timeout of 0 idicates
                        that the cache never expires.
        :returns: ``True`` if key has been updated, ``False`` for backend
                  errors. Pickling errors, however, will raise a subclass of
                  ``pickle.PickleError``.
        :rtype: boolean
        """
        return True 
Example 30
Project: jbox   Author: jpush   File: cache.py    License: MIT License 5 votes vote down vote up
def get(self, key):
        try:
            expires, value = self._cache[key]
            if expires == 0 or expires > time():
                return pickle.loads(value)
        except (KeyError, pickle.PickleError):
            return None