pickling sublcass of gobject.GObject?

S

Skip Montanaro

I have a class that inherits from PyGTK's gobject.GObject. I thought to
pickle it I'd be able to just define __[gs]etstate__ methods. When I
attempt to dump an instance of this class I get a TypeError from
copy_reg._reduce_ex telling me that GObject.__init__() takes no args but
that it was called with one arg. Here's a "hello world" example:

#!/usr/bin/env python

import pickle

import gobject

class C(gobject.GObject):
def __init__(self):
self.__gobject_init__()
self.x = 5

def __getstate__(self):
return (self.x,)

def __setstate__(self, state):
self.x = state[0]

c = C()
print pickle.dumps(c)

and the output I get when I run it:

% python pickletst.py
Traceback (most recent call last):
File "pickletst.py", line 19, in ?
print pickle.dumps(c)
File "/opt/lang/python-2.3.4/lib/python2.3/pickle.py", line 1386, in dumps
Pickler(file, protocol, bin).dump(obj)
File "/opt/lang/python-2.3.4/lib/python2.3/pickle.py", line 231, in dump
self.save(obj)
File "/opt/lang/python-2.3.4/lib/python2.3/pickle.py", line 313, in save
rv = reduce(self.proto)
File "/opt/lang/python-2.3.4/lib/python2.3/copy_reg.py", line 70, in _reduce_ex
state = base(self)
TypeError: GObject.__init__() takes exactly 0 arguments (1 given)

Thinking that since my class is a subclass of an extension class I might
have to provide a __reduce__ method, I read about it in the Pickle docs
("Pickling and unpickling extension types"), but I found the description of
what it's supposed to return completely incomprehensible. If someone can
provide me with a simple example, I will be happy to update that section of
the docs.

Thx,

Skip
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,052
Latest member
LucyCarper

Latest Threads

Top