Lost in __setstate__() in C++ and swig

Martin Drautzburg Martin.Drautzburg at web.de
Tue Sep 11 15:08:07 EDT 2007


I am trying to cPickle/unpickle a C++ extension class with some private
data members. I wrote __getstate__() and __setstate__ in C++ (I have
to, due to the private data). Pickling writes the following file:

        ccopy_reg
        _reconstructor
        p1
        (cpyramid
        MidiBytes
        p2
        c__builtin__
        object
        p3
        NtRp4
        (S'some unknown MidiBytes'
        I1
        I2
        I3
        tb.

But when unpickling I get the following error

        TypeError: in method 'MidiBytes___setstate__', argument 1 of type 
        'pyramid::MidiBytes *'

I debugged the swig wrapper and indeed the wrapper is called with just
one tuple argument. No object seems to be present at the time.

All the examples I've seen use python functions for __getstate__ and
__setstate__ and it seems to me that the object itself is already there
when __setstate__ is called. 

In a moment of dispair I declared __setstate__() static and have it
create and return a MidiBytes object. 

        MidiBytes *MidiBytes:: __setstate__ (PyObject * aTuple) {
                return new MidiBytes();
        }

Then unpickling no longer complains about "argument 1", but the
unpickled object does not work

        >>> nn = cPickle.load(FILE)
        >>> nn.len()
        Traceback (most recent call last):
          File "<stdin>", line 1, in ?
          File "/usr/src/sound.d/pyramid/pyramid.py", line 618, in len
            return _pyramid.MidiBytes_len(*args)
        TypeError: in method 'MidiBytes_len', argument 1 of
        type 'pyramid::MidiBytes *'

whereas the original object behaves well

        >>> n = pyramid.MidiBytes()
        >>> n.len()
        0

Am I missing something obvious ?












More information about the Python-list mailing list