Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Passing unicode string to C++ module

I'm working with an existing module at the moment that provides a C++ interface and does a few operations with strings.

I needed to use Unicode strings and the module unfortunately didn't have any support for a Unicode interface, so I wrote an extra function to add to the interface:

void SomeUnicodeFunction(const wchar_t* string)

However, when I attempt 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 have been changed).

I've tried changing wchar_t in the C++ module to Py_UNICODE with no success. How do I solve this problem?

like image 745
Matthew Iselin Avatar asked Sep 19 '10 01:09

Matthew Iselin


1 Answers

For Linux you don't have to change your API, just do:

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

On Windows all Unicode APIs are using UTF-16 LE (Little Endian) so you have to encode it this way:

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.

like image 73
sorin Avatar answered Oct 10 '22 08:10

sorin