dict subclass and pickle bug (?)

Terry Reedy tjreedy at udel.edu
Fri Dec 5 12:55:53 EST 2008


James Stroud wrote:
> James Stroud wrote:
>> Hello All,
>>
>> I subclassed dict and overrode __setitem__. When instances are 
>> unpickled, the __setstate__ is not called before the keys are assigned 
>> via __setitem__ in the unpickling protocol.
>>
>> I googled a bit and found that this a bug filed in 2003:

It is an 'issue' reporting a possibly unexpected side-effect of protocol 
working as designed and documented.  Possibly a design flaw, but not a 
bug in the narrow sense (in spite of the url of the issue tracker).

>> http://bugs.python.org/issue826897
>>
>> It is still "open" with "normal" priority.
> 
> Here is the ugly "fix" I'm basically going to have to live with, it seems:
> 
> class DictPlus(dict):
>   def __init__(self, *args, **kwargs):
>     self.extra_thing = ExtraThingClass()
>     dict.__init__(self, *args, **kwargs)
>   def __setitem__(self, k, v):
>     try:
>       do_something_with(self.extra_thing, k, v)
>     except AttributeError:
>       self.extra_thing = ExtraThingClass()
>       do_something_with(self.extra_thing, k, v)
>     dict.__setitem__(self, k, v)
>   def __setstate__(self, adict):
>     pass

I took the liberty of adding this to the issue.

> I can't imagine this bug has survived

because there is no bug to fix.  I have suggesting closing.

Terry Jan Reedy




More information about the Python-list mailing list