Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I was trying Basics embedding Python in C++ and when i run this code i get error
"SyntaxError: (unicode error) 'utf-8' codec can't decode byte 0xf4 in position 10
: invalid continuation byte". I am not able to figure out what's wrong in this. I encoded this txt on Pycharm it worked perfectly fine but not when i try to embed this with C++. Thanking in advance for the help

PyObject* pInt;

Py_Initialize();

PyRun_SimpleString("txt = u\"flag_for_Côte_d’Ivoire\"\nx = txt.encode()\nprint(x)");

Py_Finalize();

printf("\nPress any key to exit...\n");
if(!_getch()) _getch();
return 0;

if i only do

PyRun_SimpleString("txt = u\"flag_for_Côte_d’Ivoire\"")
it fails for that too. Python Version which i am using is Python 3.7.3

What I have tried:

I have different Encoding techniques. Couldn't find anything in Docs related to PyRun_simplestring.Python version i am using is Python3.7.3
Posted
Updated 9-May-19 23:09pm
Comments
Richard MacCutchan 9-May-19 7:16am    
Where is the string with the 0xf4 character?
Member 13703287 9-May-19 8:48am    
"ô" this character is 0xf4. you can check https://www.rapidtables.com/convert/number/hex-to-ascii.html here
Richard MacCutchan 9-May-19 9:56am    
Try changing the u prefix to u8.
Member 13703287 9-May-19 9:58am    
I tried it is not supported in Windows i guess coz it is giving Invalid Syntax
error. I am working on VS 2008.
Richard MacCutchan 9-May-19 10:23am    
VS2008 is very old now and does not support the latest C++ standard. You need to upgrade to 2017.

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:
C++
#define PY_SSIZE_T_CLEAN
#include <Python.h>

int main()
{
//    PyObject* pInt;  -- not used

    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)"); // and text

    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...
 
Share this answer
 
Comments
Member 13703287 10-May-19 5:17am    
which version of Visual Studio you are using?
Richard MacCutchan 10-May-19 5:40am    
I am on VS2017, but it should be possible with VS2008, as long as you can get past the issue with special characters in your text.
Member 13703287 10-May-19 5:46am    
I tried Richard on my VS 2008 but it didn't work for me. Thank you Richard
Richard MacCutchan 10-May-19 6:21am    
I gave up using VS2008 some years ago, so I cannot test it myself. I guess you need to upgrade if you want to use the more modern features.
Member 13703287 13-May-19 5:50am    
Finally working converted string to UTF-8 before sending to Pyrun_simplestring()
Convert it before the call. There's no point in doing it in the call because it buys you nothing and you're not controlling "that" environment (regarding assumptions about that encoding).

Quote:
When I discovered that the popular web development tool PHP has almost complete ignorance of character encoding issues, blithely using 8 bits for characters, making it darn near impossible to develop good international web applications, I thought, enough is enough.


The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!) – Joel on Software[^]
 
Share this answer
 
Comments
Member 13703287 10-May-19 5:24am    
Thank you Gerry Schmitz for ur reply but converting before won't help. Pyrun_simplestring accepts only "simpleString"

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900