Well I cannot believe that this works in 2 but not in 3. So, I just tried this again with Python 3.7 and it works perfectly. What I did notice, which was different from my tests yesterday, was the addition of the line
#define PY_SSIZE_T_CLEAN
before the include of Python.h, as described at
Introduction — Python 3.7.3 documentation[
^].
So my source code now reads:
#define PY_SSIZE_T_CLEAN
#include <Python.h>
int main()
{
Py_Initialize();
PyRun_SimpleString("txt = u\"flag_for_Côte_d’Ivoire\"\nx = txt.encode()\nprint(x)");
PyRun_SimpleString("txt = u\"flag_for_Côte_d’Ivoire\"\nprint(txt)");
Py_Finalize();
printf("\nPress any key to exit...\n");
}
And here is the output:
C:\Users\rjmac\Documents\Code\C++>pytest
b'flag_for_C\xc3\xb4te_d\xe2\x80\x99Ivoire'
flag_for_Côte_d’Ivoire
Press any key to exit...