pickling sublcass of gobject.GObject?

Skip Montanaro skip at pobox.com
Tue Jul 27 09:31:43 EDT 2004


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




More information about the Python-list mailing list