[Chicago] problem with __getstate__

Atul Varma varmaa at gmail.com
Sun May 18 21:06:21 CEST 2008


I could be wrong, but I don't think the issue is inheritance--I think it's
actually because you're subclassing a built-in type, which requires doing
some special stuff with pickle (namely, registering with copy_reg as you've
done in your last email).

If you use pickle instead of cPickle, you get a more informative traceback;
take a look at pickle.Pickler and notice how the dispatch table, which
appears to map types to serialization functions, maps the dictionary type to
Pickler.save_dict().  I think that has something to do with it.

Hope that helps...  I have to run so I can't look into this more right now,
but let me know if you'd like me to.

- Atul

On Sun, May 18, 2008 at 11:58 AM, Massimo Di Pierro <mdipierro at cs.depaul.edu>
wrote:

> Odd... this works instead...
>
> from cPickle import *
> import copy_reg
>
> class S(dict):
>    def __getattr__(self,key): return self[key]
>    def __setattr__(self, key, value): self[key]=value
>
> class A(S):
>    def __init__(self): self.i=lambda u: u
>    #def __getstate__(self): return {}
>
> def unserialize_A(d):
>    return A()
>
> def serialize_A(o):
>    return unserialize_A, ({},)
>
> copy_reg.pickle(A, serialize_A)
>
> a=A()
> print a.i(3)
> x=dumps(a)
> b=loads(x)
> print b.i(4)
>
> I guess the rule is use copy_reg and not __getstate__ if you have
> inheritance.
>
>
>
> On May 18, 2008, at 1:44 PM, Massimo Di Pierro wrote:
>
>  does anybody know why the following code does not work?
>>
>>     from cPickle import *
>>
>>     class S(dict):
>>         def __setattr__(self, key, value): self[key]=value
>>     class A(S):
>>        def __init__(self): self.i=lambda u: u
>>        def __getstate__(self): return {}
>>
>>     a=A()
>>     x=dumps(a)
>>
>> I know that self.i is a lambda and thus not pickable but __getstate__
>> is called (I checked) and thus it should not matter. I guess I do not
>> undretansd how pickling under inheritance.
>>
>> Massimo
>> _______________________________________________
>> Chicago mailing list
>> Chicago at python.org
>> http://mail.python.org/mailman/listinfo/chicago
>>
>
> _______________________________________________
> Chicago mailing list
> Chicago at python.org
> http://mail.python.org/mailman/listinfo/chicago
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/chicago/attachments/20080518/dc964024/attachment.htm>


More information about the Chicago mailing list