From: William S F. <ws...@fu...> - 2012-03-27 21:01:53
|
On 21/03/12 00:36, Bob Rossi wrote: > Hi, > > In general, I love that std::string converts to a unicode > string in python. However, sometimes this is WRONG. > > I've got an std::string that has arbitrary data in it. > It is converted in swig through this call chain: > - resultobj = SWIG_From_std_string(static_cast<std::string>(*result)); > - return SWIG_FromCharPtrAndSize(s.data(), s.size()); > - #if PY_VERSION_HEX>= 0x03000000 > return PyUnicode_FromStringAndSize(carray, static_cast< int>(size)); > #else > return PyString_FromStringAndSize(carray, static_cast< int>(size)); > #endif > > The problem is, in my case, with arbitrary data, this is a hard error > in python. Since the utf8 encoding/decoding fails. > > Is there a nice way to disable this and simply convert to a python > string for a particular std::string member? Yes, provide an 'out' typemap. Here is the default one for std::string being returned by value, which I got by running swig -E: %typemap(out,noblock=1,fragment="SWIG_" "From" "_" {std::string}) std::string, const std::string { $result = SWIG_From_std_string(static_cast< std::string >($1)); } Tweak it and read the typemap matching rules in http://swig.org/Doc2.0/Typemaps.html to find out how to target a specific method. William |