473,796 Members | 2,648 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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___se tstate__', argument 1 of type
'pyramid::MidiB ytes *'

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(FI LE)
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.MidiBy tes_len(*args)
TypeError: in method 'MidiBytes_len' , argument 1 of
type 'pyramid::MidiB ytes *'

whereas the original object behaves well
>>n = pyramid.MidiByt es()
n.len()
0

Am I missing something obvious ?



Sep 11 '07 #1
1 1986
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___se tstate__', argument 1 of type
'pyramid::MidiB ytes *'

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(FI LE)
>>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.MidiBy tes_len(*args)
TypeError: in method 'MidiBytes_len' , argument 1 of
type 'pyramid::MidiB ytes *'
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
2016
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 Python windows FAQ I shouldn't have to make a seperate dll, see link below). I create a MyModule.i file for swig which contains a simple test c++ class. I run swig on it with the following options: swig -python -c++ -includeall -shadow MyModule.i,...
0
1643
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 good ideas. i have also sent i to the swig users list a couple of times but have not got any response so i am casting my net a little wider... i have been struggling with the following problem for a couple of days now. +specifically, i have a...
1
3788
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 = PyFloat_FromDouble($1);" %include mvf.h
3
2820
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. Trying to build using mingw out of the cygwin environment but the final call to link the extension calls directly to 'cc' which I think should really be g++ at that point.
3
2100
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> double My_variable = 3.0; int fact(int n) {
1
2162
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
4455
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 */ #include <time.h>
10
3209
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 spends quiet some time in functions like _swig_setattr_nondinamic, _swig_setattr, _swig_getattr.
0
9684
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9530
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10236
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10017
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9055
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7552
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4120
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3734
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2928
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.