How to pickle a 'PySwigObject' ?

Hi,

I have a wxPython application that is creating a subprocess that performs a lengthy log analysis.

Currently, the subprocess is displaying its results using its own MainLoop().

I want to display the subprocess’ results using the parent process’ MainLoop().

I tried to pickle the class instance that is displaying the subprocess results (so the subclass could be sent to the parent process for display), but I get the following error:

cPickle.PicklingError: Can’t pickle <type ‘PySwigObject’>: attribute lookup builtin.PySwigObject failed

A code that demonstrate the problem is:

$ cat pickle_test.py
#!/usr/bin/env python

#import pickle
import cPickle as pickle
import wx

class ListControl(wx.Frame):

def __init__(self, parent, id):
    wx.Frame.__init__(self,parent,id)

if name == “main”:

app = wx.App(redirect=False)
output = pickle.dumps(ListControl(None, -1),2)

Any ideas what could be changed to let me pickle the class ?

Thanks,

Ron.

$ cat -n pickle_test.py
1 #!/usr/bin/env python
2
3 #import pickle
4 import cPickle as pickle
5 import wx
6
7 class ListControl(wx.Frame):
8
9 def init(self, parent, id):
10 wx.Frame.init(self,parent,id)
11
12 if name == “main”:
13
14 app = wx.App(redirect=False)
15 output = pickle.dumps(ListControl(None, -1),2)

$ python -u pickle_test.py
Traceback (most recent call last):
File “pickle_test.py”, line 15, in
output = pickle.dumps(ListControl(None, -1),2)
cPickle.PicklingError: Can’t pickle <type ‘PySwigObject’>: attribute lookup builtin.PySwigObject failed

$

I think it's mainly down to wxPython being a wrapper around the C++ library, which utilises 'smart' pointers that cannot easily be Pythonised or something. I can't really think how you could go around giving the subprocess' main loop a reference to your GUI if they're run as separate processes.

Any pickling I do with wx specific classes I work around by setting any wx references to None before saving, and restoring them on load, but I doubt this is applicable here as you don't know *what* to restore the pickled data to after loading

Sorry I can't be much help,
Steven Sproat

Barak, Ron wrote:

···

Hi,
I have a wxPython application that is creating a subprocess that performs a lengthy log analysis.
Currently, the subprocess is displaying its results using its own MainLoop().
I want to display the subprocess' results using the parent process' MainLoop().
I tried to pickle the class instance that is displaying the subprocess results (so the subclass could be sent to the parent process for display), but I get the following error:
cPickle.PicklingError: Can't pickle <type 'PySwigObject'>: attribute lookup __builtin__.PySwigObject failed
A code that demonstrate the problem is:
$ cat pickle_test.py
#!/usr/bin/env python
#import pickle
import cPickle as pickle
import wx
class ListControl(wx.Frame):
     def __init__(self, parent, id):
        wx.Frame.__init__(self,parent,id)
if __name__ == "__main__":
     app = wx.App(redirect=False)
    output = pickle.dumps(ListControl(None, -1),2)
Any ideas what could be changed to let me pickle the class ?
Thanks,
Ron.

Barak, Ron wrote:

Hi,
I have a wxPython application that is creating a subprocess that performs a lengthy log analysis.
Currently, the subprocess is displaying its results using its own MainLoop().
I want to display the subprocess' results using the parent process' MainLoop().
I tried to pickle the class instance that is displaying the subprocess results (so the subclass could be sent to the parent process for display), but I get the following error:
cPickle.PicklingError: Can't pickle <type 'PySwigObject'>: attribute lookup __builtin__.PySwigObject failed
A code that demonstrate the problem is:
$ cat pickle_test.py
#!/usr/bin/env python
#import pickle
import cPickle as pickle
import wx
class ListControl(wx.Frame):
     def __init__(self, parent, id):
        wx.Frame.__init__(self,parent,id)
if __name__ == "__main__":
     app = wx.App(redirect=False)
    output = pickle.dumps(ListControl(None, -1),2)
Any ideas what could be changed to let me pickle the class ?

Even if PySWigObjects could be pickled (they can't) if you think about this problem a little and about what pickle is doing I think you'll see that you don't really want to do that. Pickle is often used as if it was a way to save and restore the same instances of objects. It is not. It flattens the data in an object in such a way that the data can be used to construct a new object of the same type and with equivalent attributes. Although it would be possible to do a lot of that with UI objects, it would never be able to be complete because there is just too much of the native parts of the object that belong to the process where it is created and it would either be unsafe or impossible to totally recreate that from a pickled object in another process, or even later in the same process.

So like I mentioned in my other message, separate the data from the view, and pass just the data to other processes or to a database or whatever, and let the view belong to the process that is displaying the data.

···

--
Robin Dunn
Software Craftsman