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

John Reid j.reid at mail.cryst.bbk.ac.uk
Tue Jul 20 13:43:40 CEST 2010


Hans Meine wrote:
> 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.)
> 

Thanks for the clarification, that all makes sense.

I was hoping that I could make all the sub-classes picklable without 
recoding a getinitargs() for each one. I think I've managed that by 
using __reduce__(). I don't think I can do that using the boost.python 
pickle suite.

I can see why you call my method a 'hack' but I think it is fine for my 
purposes.

Thanks,
John.



More information about the Cplusplus-sig mailing list