[C++-sig] Pickle python subclass of C++ interface

Hans Meine hans_meine at gmx.net
Tue Jul 20 09:37:16 CEST 2010


On Sunday 18 July 2010 11:16:29 John Reid wrote:
> I'm not sure what you mean by mix-in, but my first attempt involved
> defining pickle suite getstate() and setstate() methods. I did not
> define a getinitargs() method. Unfortunately when the derived object was
> unpickled, __init__ was called with no arguments. As far as I can see
> there's no way to use the boost.python pickle suite that does not
> involve a call to __init__() on the unpickled object.

How should it work?  A proper object of a BPL extension type needs a wrapped 
C++ object "behind it".  Thus, if you unpickle such an object, the 
corresponding C++ object needs to be created.

There's the difference between pure python and BPL classes: with pure Python, 
you (or the pickle module) may just assign __class__, restore all attributes 
and be done.  For C++ objects OTOH, pickle cannot list or restore the 
attributes, or make sure that the object is in any defined state.  So you 
*need* to call a constructor, which may need arguments, hence the 
getinitargs().

In the __reduce__ hack within your last mail, you explicitly call __init__ 
with a single self arg, relying on a default constructible class, and relying 
on the fact that the C++ object will be in the same state as before.  (That's 
probably fine, since you originally wrote that you're dealing with interfaces 
w/o state, but I wanted to point it out again for the unwary readers.)

Have a nice day,
  Hans


More information about the Cplusplus-sig mailing list