Exception classes don't follow pickle protocol, problems unpickling

Irmen de Jong irmen at -nospam-xs4all.nl
Tue Dec 8 15:25:54 EST 2009


On 7-12-2009 10:12, Peter Otten wrote:

>> So there are 2 problems: the pickle protocol isn't used when exception
>> objects (or instances of classes derived from Exception) are pickled, and
>> during unpickling, it then
>> crashes because it calls __init__  with the wrong amount of parameters.
>> (why is it bothering with __init__ anyway? Aren't exceptions new style
>> classes?)
>
> The __reduce__() method is called when you pickle an object. It returns an
> argument tuple and a factory. For exceptions that factory is the class which
> is why __init__() is called when the object is unpickled.

I didn't realize exceptions are treated as "objects pickle know nothing 
about" (or 'extension types'). They weren't in Python 2.4 at least :)

So I was expecting them to follow the pickle protocol for normal Python 
types, and didn't look past __getnewargs__ and __getinitargs__...


>> This started happening in Python 2.5, Python 2.4 works without error.
>>
>> What is causing this?
>
> I think Exceptions need special treatment because they have state that is
> not stored in the instance __dict__.
>
>> How can I best solve this error?
>
> You could override __reduce__() to call __getstate__(). I don't see the need
> for __getnewargs__() because exceptions aren't immutable.
>
> Peter

Thanks, that was enlightening.
I never had to deal with __reduce__ before :)

--irmen





More information about the Python-list mailing list