Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #28454
| From | Dieter Maurer <dieter@handshake.de> |
|---|---|
| Subject | Re: Why derivated exception can not be pickled ? |
| Date | 2012-09-05 08:02 +0200 |
| References | <7bb72fb5-5ab5-4748-acbf-c8e7666ee834@googlegroups.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.211.1346824947.27098.python-list@python.org> (permalink) |
Mathieu Courtois <mathieu.courtois@gmail.com> writes:
> Here is my example :
>
>
> import cPickle
>
> ParentClass = object # works
> ParentClass = Exception # does not
>
> class MyError(ParentClass):
> def __init__(self, arg):
> self.arg = arg
>
> def __getstate__(self):
> print '#DBG pass in getstate'
> odict = self.__dict__.copy()
> return odict
>
> def __setstate__(self, state):
> print '#DBG pass in setstate'
> self.__dict__.update(state)
>
> exc = MyError('IDMESS')
>
> fo = open('pick.1', 'w')
> cPickle.dump(exc, fo)
> fo.close()
>
> fo = open('pick.1', 'r')
> obj = cPickle.load(fo)
> fo.close()
>
>
> 1. With ParentClass=object, it works as expected.
>
> 2. With ParentClass=Exception, __getstate__/__setstate__ are not called.
The pickle interface is actually more complex and there are several
ways an object can ensure picklability. For example, there is
also a "__reduce__" method. I suppose, that "Exception" defines methods
which trigger the use of an alternative picklability approach (different
from "__getstate__/__setstate__").
I would approach your case the following way: Use "pickle" instead
of "cPickle" and debug picking/unpickling to find out what
happens in detail.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Why derivated exception can not be pickled ? Mathieu Courtois <mathieu.courtois@gmail.com> - 2012-09-04 08:57 -0700
Re: Why derivated exception can not be pickled ? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-09-05 00:34 +0000
Re: Why derivated exception can not be pickled ? Dieter Maurer <dieter@handshake.de> - 2012-09-05 08:02 +0200
Re: Why derivated exception can not be pickled ? Mathieu Courtois <mathieu.courtois@gmail.com> - 2012-09-05 01:54 -0700
Re: Why derivated exception can not be pickled ? Mathieu Courtois <mathieu.courtois@gmail.com> - 2012-09-05 01:54 -0700
Re: Why derivated exception can not be pickled ? Mathieu Courtois <mathieu.courtois@gmail.com> - 2012-09-05 03:18 -0700
csiph-web