mardi 5 mai 2015

Converting Strings in Linux using SWIG for Python

I have a C++ class that is able to output strings in normal ASCII or wide format. I want to get the output in Python as a string. I am using SWIG (version 3.0.4) and have read the SWIG documentation. I'm using the following typemap to convert from a standard c string to my C++ class:

%typemap(out) myNamespace::MyString &
{
    $result = PyString_FromString(const char *v);
}

This works fine in Windows with the VS2010 compiler, but it is not working completely in Linux. When I compile the wrap file under Linux, I get the following error:

error: cannot convert ‘std::string*’ to ‘myNamespace::MyString*’ in assignment

So I tried adding an extra typemap to the Linux interface file as so:

%typemap(in) myNamespace::MyString*
{
    $result = PyString_FromString(std::string*);
}

But I still get the same error. If I manually go into the wrap code and fix the assignment like so:

arg2 = (myNamespace::MyString*) ptr;

then the code compiles just fine. I don't see why my additional typemap isn't working. Any ideas or solutions would be greatly appreciated. Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire