Does ctypes uses MultiByteToWideChar to convert Python strings?

Hi all,
I see that ctypes docs says c_wchar_p is equal to string in python. I know that python strings are utf-8 by default. And wchar is utf-16. Does ctypes uses MultiByteToWideChar to convert Python strings to wchar ?

NOTE: MultiByteToWideChar is a windows api function. Question is asked under windows os context.

Python strings are unicode. utf-8 is one possible convertion of unicode to bytes. utf-16 is another possible convertion.

In the case of ctypes I expect my unicode string to be converted to utf-16 for passing to Windows function.

I think MultiByteToWideChar is for non-unicode byte streams.

I do not see any calls to MultiByteToWideChar in the ctypes code.

1 Like

Python strings aren’t UTF-8. Depending on the contents they can be ASCII, UTF-16 or UTF-32. That’s more of an implementation detail and could change, externally they’re just abstractly Unicode. Ctypes doesn’t call Windows APIs to do the wchar_t, instead it uses PyUnicode_AsWideCharString()unicode_copy_as_widechar() which directly does the conversion.

1 Like

Thank you @ Barry Scott & @ Spencer Brown for correcting me.