[C++-sig] How about class_<...>.enable_copy() ?

Hans Meine meine at informatik.uni-hamburg.de
Fri Jun 8 17:17:08 CEST 2007


Hi again!

Am Freitag, 08. Juni 2007 17:06:36 schrieb Hans Meine:
> I often use copy.[deep]copy() to copy python objects / BPL-exported types.
> This works if pickling support has been explicitly coded, by pickling and
> unpickling.

Oh, and BTW, there is a related thing that bugs me: When I copy.copy() a BPL 
exported type with pickling support and __getstate_manages_dict__ set, I get 
two objects with *the same* __dict__:

In [15]: cm.__dict__
Out[15]: {}

In [16]: cm2 = copy.copy(cm)

In [17]: cm.foo = 42

In [18]: cm2.foo
Out[18]: 42

That looks like a bug to me, and pickle3.cpp also does not copy the __dict__.  
Oh, but it uses update().. maybe that should be put into the docs.  From 
pickle3.cpp:

>         // restore the object's __dict__
>         dict d = extract<dict>(w_obj.attr("__dict__"))();
>         d.update(state[0]);

I suggest to add a sentence in the "Practical Advice" section:
> If __getstate__ is required, include the instance's __dict__ in the Python
> object that is returned**BEGIN**, and use the dict to update() your
> __dict__ in __setstate__**END**.

Also, above it says:
> To resolve this problem, it should first be established that the
> __getstate__ and __setstate__ methods manage the instances's __dict__
> correctly.
without defining "correctly". ;-)

-- 
Ciao, /  /
     /--/
    /  / ANS



More information about the Cplusplus-sig mailing list