Discussion:
converting a std::vector of typedef primitive values to a python list
Blaine Bell
2010-10-07 03:38:38 UTC
Permalink
I have a "typedef unsigned long long uint64" set, and I want to pass a
vector<uint64> to and from python to c++. I can create the vector, send
it to c++, but when I get it back with values and look at the list, I
get string values like this:

uids= ['_6080820900000000_p_uint64', '_6880820900000000_p_uint64',
'_7080820900000000_p_uint64', '_7880820900000000_p_uint64',
'_8080820900000000_p_uint64', '_8880820900000000_p_uint64',
'_9080820900000000_p_uint64', '_9880820900000000_p_uint64',
'_a080820900000000_p_uint64', '_a880820900000000_p_uint64',
'_b080820900000000_p_uint64', '_b880820900000000_p_uint64',
'_c080820900000000_p_uint64', '_c880820900000000_p_uint64']

These values should get automatically converted to python numerical
objects, but they don't. It turns out that the SWIG generated function
_wrap_SwigUInt64Vector___getitem__() uses this piece of code to return
the SWIG object:

resultobj = SWIG_NewPointerObj((void *) result, SWIGTYPE_p_uint64, 0);
return resultobj;

and not something like:

resultobj = PyLong_FromUnsignedLongLong(result);
return resultobj;

I have tried everything in the .i file to try and get the correct code
to get generated by SWIG but I can't. I have tried all typemaps with no
success. It seems like SWIG is treating uint64 like an object and not
a primitive object. Is there a way to change this? I am using SWIG 1.3
for historical reasons, if there is something in SWIG 2.0 that could do
this, it would be useful, so I could push to upgrade.

Thanks in advance,

Blaine Bell
Russell E. Owen
2010-10-07 19:35:16 UTC
Permalink
Post by Blaine Bell
I have a "typedef unsigned long long uint64" set, and I want to pass a
vector<uint64> to and from python to c++. I can create the vector, send
it to c++, but when I get it back with values and look at the list, I
...
Have you tried:
%import "std_vector.i"
%template(VectorUInt64) std::vector<uint64>;

-- Russell
Blaine Bell
2010-10-07 20:33:49 UTC
Permalink
Post by Russell E. Owen
Post by Blaine Bell
I have a "typedef unsigned long long uint64" set, and I want to pass a
vector<uint64> to and from python to c++. I can create the vector, send
it to c++, but when I get it back with values and look at the list, I
...
%import "std_vector.i"
%template(VectorUInt64) std::vector<uint64>;
Thanks for the reply. My apologies, this is exactly what I have
implemented in my .i file. Oddly enough, the vector::pop() function
returns a correct value for this class. However, if I use:

%template(VectorUInt64) vector<unsigned long long>;

It does the same thing... Any help would be appreciated. I will try to
use 2.0 to see if it works.

Thanks,

Blaine
Schrodinger Account
2010-10-10 12:56:21 UTC
Permalink
I fixed this using:

std {

specialize_std_vector(int64,PyLong_Check,PyLong_AsLongLong,PyLong_FromLongLong);

specialize_std_vector(uint64,PyLong_Check,PyLong_AsUnsignedLongLong,PyLong_FromUnsignedLongLong);
}

Thanks for the responses.

Blaine
Post by Russell E. Owen
Post by Blaine Bell
I have a "typedef unsigned long long uint64" set, and I want to pass a
vector<uint64> to and from python to c++. I can create the vector, send
it to c++, but when I get it back with values and look at the list, I
...
%import "std_vector.i"
%template(VectorUInt64) std::vector<uint64>;
-- Russell
------------------------------------------------------------------------------
Beautiful is writing same markup. Internet Explorer 9 supports
standards for HTML5, CSS3, SVG 1.1, ECMAScript5, and DOM L2& L3.
Spend less time writing and rewriting code and more time creating great
experiences on the web. Be a part of the beta today.
http://p.sf.net/sfu/beautyoftheweb
_______________________________________________
Swig-user mailing list
https://lists.sourceforge.net/lists/listinfo/swig-user
Loading...