Why derivated exception can not be pickled ?

Steven D'Aprano steve+comp.lang.python at pearwood.info
Tue Sep 4 20:34:40 EDT 2012


On Tue, 04 Sep 2012 08:57:00 -0700, Mathieu Courtois wrote:

> Here is my example :
> 
> 
> import cPickle
> 
> ParentClass = object     # works
> ParentClass = Exception  # does not
[...]
> 1. With ParentClass=object, it works as expected.
> 
> 2. With ParentClass=Exception, __getstate__/__setstate__ are not called.
> 
> Does anyone explain me why ?


I think it is a bug. According to the documentation, if your class is 
unpickleable, an exception should be raised. If it is pickleable, 
__getstate__ should be called. I can't see anything to explain that what 
you are seeing is expected behaviour.


Exceptions should definitely be pickleable:

http://bugs.python.org/issue1692335

so __getstate__ should be called. I think you should report this as a bug.


-- 
Steven



More information about the Python-list mailing list