Python: passing a Unicode string to a C ++ module

I am working with an existing module at the moment that provides a C ++ interface and performs several string operations.

I needed to use Unicode strings, and, unfortunately, the module did not have Unicode interface support, so I wrote an additional function to add to the interface:

void SomeUnicodeFunction(const wchar_t* string) 

However, when I try to use the following code in Python:

 SomeModule.SomeUnicodeFunction(ctypes.c_wchar_p(unicode_string)) 

I get this error:

 ArgumentError: Python argument types in SomeModule.SomeUnicodeFunction(SomeModule, c_wchar_p) did not match C++ signature: SomeUnicodeFunction(... {lvalue}, wchar_t const*) 

(names changed).

I tried changing wchar_t in a C ++ module to Py_UNICODE without success. How to solve this problem?

+6
c ++ python module unicode
source share
2 answers

For Linux, you do not need to change your API, just do:

 SomeModule.SomeFunction(str(s.encode('utf-8'))) 

On Windows, all Unicode interfaces use UTF-16 LE (Little Endian), so you need to encode it like this:

 SomeModule.SomeFunctionW(str(s.encode('utf-16-le'))) 

Good to know : wchar_t can have different sizes on different platforms: 8, 16 or 32 bits.

+2
source share

Found to crack the problem:

 SomeModule.SomeUnicodeFunction(str(s.encode('utf-8'))) 

It seems to work just fine for my purposes.

Update. In fact, using UTF-8 means that I don't need SomeUnicodeFunction and I can use the standard SomeFunction function without specializing in Unicode. Learn something new every day, I think :).

+2
source share

All Articles