Python cPickle.PicklingError() Examples

The following are 22 code examples of cPickle.PicklingError(). 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 also want to check out all available functions/classes of the module cPickle , or try the search function .
Example #1
Source File: pickletester.py    From ironpython2 with 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 in Python implementation is less strict and also accepts
        # iterables.
        for proto in protocols:
            try:
                self.dumps(C(), proto)
            except (AttributeError, pickle.PicklingError, cPickle.PicklingError):
                pass
            try:
                self.dumps(D(), proto)
            except (AttributeError, pickle.PicklingError, cPickle.PicklingError):
                pass 
Example #2
Source File: rpc.py    From BinderFilter with MIT License 6 votes vote down vote up
def putmessage(self, message):
        self.debug("putmessage:%d:" % message[0])
        try:
            s = pickle.dumps(message)
        except pickle.PicklingError:
            print >>sys.__stderr__, "Cannot pickle:", repr(message)
            raise
        s = struct.pack("<i", len(s)) + s
        while len(s) > 0:
            try:
                r, w, x = select.select([], [self.sock], [])
                n = self.sock.send(s[:BUFSIZE])
            except (AttributeError, TypeError):
                raise IOError, "socket no longer exists"
            except socket.error:
                raise
            else:
                s = s[n:] 
Example #3
Source File: rpc.py    From oss-ftp with MIT License 6 votes vote down vote up
def putmessage(self, message):
        self.debug("putmessage:%d:" % message[0])
        try:
            s = pickle.dumps(message)
        except pickle.PicklingError:
            print >>sys.__stderr__, "Cannot pickle:", repr(message)
            raise
        s = struct.pack("<i", len(s)) + s
        while len(s) > 0:
            try:
                r, w, x = select.select([], [self.sock], [])
                n = self.sock.send(s[:BUFSIZE])
            except (AttributeError, TypeError):
                raise IOError, "socket no longer exists"
            except socket.error:
                raise
            else:
                s = s[n:] 
Example #4
Source File: gipc.py    From gipc with MIT License 6 votes vote down vote up
def put(self, o):
        """Encode object ``o`` and write it to the pipe.
        Block gevent-cooperatively until all data is written. The default
        encoder is ``pickle.dumps``.

        :arg o: a Python object that is encodable with the encoder of choice.

        Raises:
            - :exc:`GIPCError`
            - :exc:`GIPCClosed`
            - :exc:`pickle.PicklingError`

        """
        self._validate()
        with self._lock:
            bindata = self._encoder(o)
            self._write(struct.pack("!i", len(bindata)) + bindata) 
Example #5
Source File: scoopzmq.py    From scoop with GNU Lesser General Public License v3.0 6 votes vote down vote up
def sendFuture(self, future):
        """Send a Future to be executed remotely."""
        future = copy.copy(future)
        future.greenlet = None
        future.children = {}

        try:
            if shared.getConst(hash(future.callable), timeout=0):
                # Enforce name reference passing if already shared
                future.callable = SharedElementEncapsulation(hash(future.callable))
            self.socket.send_multipart([
                TASK,
                pickle.dumps(future.id, pickle.HIGHEST_PROTOCOL),
                pickle.dumps(future, pickle.HIGHEST_PROTOCOL),
            ])
        except (pickle.PicklingError, TypeError) as e:
            # If element not picklable, pickle its name
            # TODO: use its fully qualified name
            scoop.logger.warn("Pickling Error: {0}".format(e))
            future.callable = hash(future.callable)
            self.socket.send_multipart([
                TASK,
                pickle.dumps(future.id, pickle.HIGHEST_PROTOCOL),
                pickle.dumps(future, pickle.HIGHEST_PROTOCOL),
            ]) 
Example #6
Source File: scooptcp.py    From scoop with GNU Lesser General Public License v3.0 6 votes vote down vote up
def sendFuture(self, future):
        """Send a Future to be executed remotely."""
        try:
            if shared.getConst(hash(future.callable),
                               timeout=0):
                # Enforce name reference passing if already shared
                future.callable = SharedElementEncapsulation(hash(future.callable))
            self.socket.send_multipart([b"TASK",
                                        pickle.dumps(future,
                                                     pickle.HIGHEST_PROTOCOL)])
        except pickle.PicklingError as e:
            # If element not picklable, pickle its name
            # TODO: use its fully qualified name
            scoop.logger.warn("Pickling Error: {0}".format(e))
            previousCallable = future.callable
            future.callable = hash(future.callable)
            self.socket.send_multipart([b"TASK",
                                        pickle.dumps(future,
                                                     pickle.HIGHEST_PROTOCOL)])
            future.callable = previousCallable 
Example #7
Source File: core.py    From mitogen with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def pickled(cls, obj, **kwargs):
        """
        Construct a pickled message, setting :attr:`data` to the serialization
        of `obj`, and setting remaining fields using `kwargs`.

        :returns:
            The new message.
        """
        self = cls(**kwargs)
        try:
            self.data = pickle__dumps(obj, protocol=2)
        except pickle.PicklingError:
            e = sys.exc_info()[1]
            self.data = pickle__dumps(CallError(e), protocol=2)
        return self 
Example #8
Source File: lstm_seqlabel_callbacks.py    From neural_wfst with MIT License 5 votes vote down vote up
def update_saved_parameters(training_stats, test_stack_config, args):
    if training_stats['worthy_epoch']:
        test_stack_config['best_epoch_id'] = training_stats['best_epoch_id']
        test_stack_config['validation_result'] = training_stats['validation_result']
        test_stack_config['training_result'] = training_stats['training_result']
        try:
            lstm_seqlabel_load_save_model.save_parameters_to_file(
                test_stack_config, args.saveto)
        except pickle.PicklingError as e:
            print "Suffering from Pickling Error in saving \n%s \nto %s"%(
                ' '.join(test_stack_config.keys()),
                args.saveto)

    return 
Example #9
Source File: rpc.py    From Splunking-Crime with GNU Affero General Public License v3.0 5 votes vote down vote up
def putmessage(self, message):
        self.debug("putmessage:%d:" % message[0])
        try:
            s = pickle.dumps(message)
        except pickle.PicklingError:
            print >>sys.__stderr__, "Cannot pickle:", repr(message)
            raise
        s = struct.pack("<i", len(s)) + s
        while len(s) > 0:
            try:
                r, w, x = select.select([], [self.sock], [])
                n = self.sock.send(s[:BUFSIZE])
            except (AttributeError, TypeError):
                raise IOError, "socket no longer exists"
            s = s[n:] 
Example #10
Source File: test_reusable_executor.py    From loky with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_queue_full_deadlock(self):
        executor = get_reusable_executor(max_workers=1)
        fs_fail = [executor.submit(do_nothing, ErrorAtPickle(True))
                   for i in range(100)]
        fs = [executor.submit(do_nothing, ErrorAtPickle(False))
              for i in range(100)]
        with pytest.raises(PicklingError):
            fs_fail[99].result()
        assert fs[99].result() 
Example #11
Source File: test_reusable_executor.py    From loky with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __reduce__(self):
        if self.fail:
            raise PicklingError("Error in pickle")
        else:
            return id, (42, ) 
Example #12
Source File: SensationalismClassifier.py    From news-audit with GNU General Public License v3.0 5 votes vote down vote up
def dump_model(self, model_file="model_%s.pkl" % strftime("%Y%m%d_%H%M")):
        """ Pickle trained model """
        if self.debug:
            logging.info("Dumping model to %s" % model_file)
        with open(model_file, "wb") as f_pkl:
            try:
                pickle.dump(self.pipeline, f_pkl, pickle.HIGHEST_PROTOCOL)
                self.model_name = model_file
            except pickle.PicklingError as e_pkl:
                print str(e_pkl) + ": continuing without dumping." 
Example #13
Source File: bias_classifier.py    From news-audit with GNU General Public License v3.0 5 votes vote down vote up
def dump_model(self, model_file="model_%s.pkl" % strftime("%Y%m%d_%H%M")):
        """ Pickle trained model """
        if self.debug:
            logging.info("Dumping model to %s" % model_file)
        with open(model_file, "wb") as f_pkl:
            try:
                pickle.dump(self.pipeline_1, f_pkl, pickle.HIGHEST_PROTOCOL)
                pickle.dump(self.pipeline_2, f_pkl, pickle.HIGHEST_PROTOCOL)
                self.model_name = model_file
            except pickle.PicklingError as e_pkl:
                print str(e_pkl) + ": continuing without dumping." 
Example #14
Source File: test_styles.py    From learn_python3_spider with MIT License 5 votes vote down vote up
def test_lambdaRaisesPicklingError(self):
        """
        Pickling a C{lambda} function ought to raise a L{pickle.PicklingError}.
        """
        self.assertRaises(pickle.PicklingError, pickle.dumps, lambdaExample)
        try:
            import cPickle
        except:
            pass
        else:
            self.assertRaises(cPickle.PicklingError, cPickle.dumps,
                              lambdaExample) 
Example #15
Source File: test_styles.py    From learn_python3_spider with MIT License 5 votes vote down vote up
def test_handledBycPickleModule(self):
        """
        Handling L{cPickle.PicklingError} handles
        L{_UniversalPicklingError}.
        """
        try:
            import cPickle
        except ImportError:
            raise unittest.SkipTest("cPickle not available.")
        else:
            self.assertRaises(cPickle.PicklingError,
                              self.raise_UniversalPicklingError) 
Example #16
Source File: test_styles.py    From learn_python3_spider with MIT License 5 votes vote down vote up
def test_handledByPickleModule(self):
        """
        Handling L{pickle.PicklingError} handles
        L{_UniversalPicklingError}.
        """
        self.assertRaises(pickle.PicklingError,
                          self.raise_UniversalPicklingError) 
Example #17
Source File: broadcast.py    From LearningApacheSpark with MIT License 5 votes vote down vote up
def dump(self, value, f):
        try:
            pickle.dump(value, f, 2)
        except pickle.PickleError:
            raise
        except Exception as e:
            msg = "Could not serialize broadcast: %s: %s" \
                  % (e.__class__.__name__, _exception_message(e))
            print_exec(sys.stderr)
            raise pickle.PicklingError(msg)
        f.close() 
Example #18
Source File: serializers.py    From LearningApacheSpark with MIT License 5 votes vote down vote up
def dumps(self, obj):
        try:
            return cloudpickle.dumps(obj, 2)
        except pickle.PickleError:
            raise
        except Exception as e:
            emsg = _exception_message(e)
            if "'i' format requires" in emsg:
                msg = "Object too large to serialize: %s" % emsg
            else:
                msg = "Could not serialize object: %s: %s" % (e.__class__.__name__, emsg)
            cloudpickle.print_exec(sys.stderr)
            raise pickle.PicklingError(msg) 
Example #19
Source File: test_styles.py    From Safejumper-for-Desktop with GNU General Public License v2.0 5 votes vote down vote up
def test_lambdaRaisesPicklingError(self):
        """
        Pickling a C{lambda} function ought to raise a L{pickle.PicklingError}.
        """
        self.assertRaises(pickle.PicklingError, pickle.dumps, lambdaExample)
        try:
            import cPickle
        except:
            pass
        else:
            self.assertRaises(cPickle.PicklingError, cPickle.dumps,
                              lambdaExample) 
Example #20
Source File: test_styles.py    From Safejumper-for-Desktop with GNU General Public License v2.0 5 votes vote down vote up
def test_handledBycPickleModule(self):
        """
        Handling L{cPickle.PicklingError} handles
        L{_UniversalPicklingError}.
        """
        try:
            import cPickle
        except ImportError:
            raise unittest.SkipTest("cPickle not available.")
        else:
            self.assertRaises(cPickle.PicklingError,
                              self.raise_UniversalPicklingError) 
Example #21
Source File: test_styles.py    From Safejumper-for-Desktop with GNU General Public License v2.0 5 votes vote down vote up
def test_handledByPickleModule(self):
        """
        Handling L{pickle.PicklingError} handles
        L{_UniversalPicklingError}.
        """
        self.assertRaises(pickle.PicklingError,
                          self.raise_UniversalPicklingError) 
Example #22
Source File: Action.py    From pivy with ISC License 5 votes vote down vote up
def _object_contents(obj):
    """Return the signature contents of any Python object.

    We have to handle the case where object contains a code object
    since it can be pickled directly.
    """
    try:
        # Test if obj is a method.
        return _function_contents(obj.__func__)

    except AttributeError:
        try:
            # Test if obj is a callable object.
            return _function_contents(obj.__call__.__func__)

        except AttributeError:
            try:
                # Test if obj is a code object.
                return _code_contents(obj)

            except AttributeError:
                try:
                    # Test if obj is a function object.
                    return _function_contents(obj)

                except AttributeError:
                    # Should be a pickable Python object.
                    try:
                        return cPickle.dumps(obj)
                    except (cPickle.PicklingError, TypeError):
                        # This is weird, but it seems that nested classes
                        # are unpickable. The Python docs say it should
                        # always be a PicklingError, but some Python
                        # versions seem to return TypeError.  Just do
                        # the best we can.
                        return str(obj)