Unpickling data with classes from dynamic modules

Fredrik Tolf fredrik at dolda2000.com
Wed Aug 21 23:05:01 EDT 2013


On Wed, 21 Aug 2013, Prasad, Ramit wrote:
> Fredrik Tolf wrote:
>> [...]
>> I considered trying to create subclasses of the pickler and unpickler that
>> pickle a reference to a module loader and data for the particular module
>> along with a class that comes from such a module, by overriding
>> Pickler.save_global and Unpickler.load_global. Unfortunately, however,
>> those functions aren't part of the public interface and can't be
>> overridden when the C-pickle module is used instead (which, obviously, is
>> what normally happens).
>> [...]
>
> I believe rather than subclassing the pickler, you are expected to
> change the behavior from within the class via __getstate__ and __setstate__.

Well, that clearly wouldn't work, since that still assumes the module can 
be found.

> Maybe all you need to add is implementation for obj.__reduce__

That would certainly work, and I guess I could perhaps use it as a 
work-around, but that would mean I'd have to mark every single such class 
as such in some way or another. What I'm looking for is a solution that 
could make them picklable transparently, just like normal.

--

Fredrik Tolf



More information about the Python-list mailing list