how to pickle unpicklable objects
Hans Georg Krauthaeuser
hgk at et.uni-magdeburg.de
Fri Sep 23 09:45:01 EDT 2005
Guy Lateur schrieb:
> Hi all,
>
> I've been writing an application containing a lot of settings which can be
> changed by the user. I'm using wx.Config to read/write these settings (to
> the windows registry). This means I can only store strings, ints and floats.
>
> However, it would be very convenient if I could also store more general
> objects. It seems to work for wx.Colour, but not for wx.Font. It raises a
> "TypeError: can't pickle PySwigObject objects".
The object is wrapped by SWIG. So, python can not know anything about it
and the object can not be pickled.
As far as I see, there are two possibilities
- define __getstate__ and __setstate__ in the c/c++-source or the .i
file (used by swig). This is only possible if the source is available
- use copy_reg (http://docs.python.org/lib/module-copyreg.html) to
register a 'reduce' function (I never used that).
I use the first option in the .i-File for a wrapped c++-class like this:
%extend UMDMResult {
%insert("python") %{
def __getstate__(self):
return (self.v,self.u,self.l,self.unit,self.Z0,self.Eta0,self.t)
def __setstate__(self,tup):
self.this = _umddevice.new_UMDMResult(tup[0],tup[1],tup[2],tup[3])
self.thisown=1
(self.Z0,self.Eta0,self.t)=[i for i in tup[4:]]
%}
}
regards
Hans Georg Krauthaeuser
More information about the Python-list
mailing list