473,320 Members | 1,920 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Lost in __setstate__() in C++ and swig

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 ?



Sep 11 '07 #1
1 1971
Hello,

Martin Drautzburg a écrit :
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.
Unpickling an object does not use the normal path for creating the
object. More precisely, the __init__ method is not called! See
http://docs.python.org/dev/library/p...lass-instances

I suggest that you try to play with __getinitargs__ or __getnewargs__.
By calling __init__, it will give a chance to Swig to allocate the C++
object.
>
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 *'
This message is typical of a swig object whose __init__ method has not
been called. (No later than today, I forgot to call the base.__init__ in
a derived class. Maybe we should ask Swig for a better message)
See above for a hint. Hope this helps...
--
Amaury
Sep 13 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Jon Moldover | last post by:
Hi, I'm using Python in my win32 app by linking to the python23.dll. I'm trying to expose some c++ code in my app to Python so I can make application calls from Python scripts (according to the...
0
by: Andrew Collier | last post by:
hello, i am not sure whether this problem relates to swig, python or my c++ compiler. +please forgive me if this is inappropriately posted but i am sure that someone +on this list must have some...
1
by: Ernie | last post by:
Hi, I am learning swig to use C codes from Python. Here is the swig file mvf.i: %module mvf %include "carrays.i" %array_class(double, doubleArray); %typemap(out) double, float "$result =...
3
by: Kevin Dahlhausen | last post by:
Could anyone post a simple setup.py script that uses a SWIG interface to C++ code and the mingw compiler? I followed some online samples, and am using the setup.cfg file to specify swig-c++=1....
3
by: It's me | last post by:
I am playing around with SWING building a Python module using the no brainer example in http://www.swig.org/tutorial.html. With that first example, /* File : example.c */ #include <time.h>...
1
by: Java and Swing | last post by:
I am trying to wrap some C code I have. Currently I have something like... defs.h ----------- typedef unsigned long MY_DIGIT; myapp.c ------------- void MakeDigits(MY_DIGIT digits) {
2
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c...
10
by: Bart Ogryczak | last post by:
Hi, I´m looking for some benchmarks comparing SWIG generated modules with modules made directly with C/Python API. Just how much overhead does SWIG give? Doing profile of my code I see, that it...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.