0

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 and on windows VS 2008

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
Devious
  • 9
  • 8
  • 1
    This probably isn't helpful but it works for me (Linux, g++ 8.3.1, Python 3.7.2). It might be worth specifiying the string with a [`u8` prefix in C++](https://en.cppreference.com/w/cpp/language/string_literal) since I don't think the non-ascii characters are valid otherwise – DavidW May 09 '19 at 11:34
  • I am working on windows VS 2008 it is giving syntax error – Devious May 09 '19 at 13:59
  • @Devious What’s your source character set (i.e. what file character encoding are you using)? Does it help to change that encoding to UTF-8 (with or without BOM)? MSVC might not like this … – Konrad Rudolph May 09 '19 at 14:11
  • The `u8` prefix I suggested is a C++17 feature so probably won't work in VS 2008. If I were you I'd look at `PyRun_SimpleFile` instead. – DavidW May 09 '19 at 14:51
  • Pyrun_simplestring works with Python 2.7 on VS 2008 can't find any changes mentioned in docs of Python 3.7.3 related to this function.I will try with Python 3.4 and see whether it works or not. Thank you for your help DavidW – Devious May 10 '19 at 05:44
  • Tried with Python 3.4.3 and it failed with that too whereas worked with Python 2.7. I think PyRun_SimpleString () error was introduced with Python 3.x – Devious May 10 '19 at 06:25
  • Before Sending the string converted string to UTF8 and passed to Pyrun_simplestring() and it worked. So if you are working on older version of VS do as said and if you are working on new VS you need to add "#define PY_SSIZE_T_CLEAN" before Python.h – Devious May 13 '19 at 10:37

1 Answers1

0

Before Sending the string converted string to UTF8 and passed to Pyrun_simplestring() and it worked. So if you are working on older version of VS do as said and if you are working on new VS you need to add "#define PY_SSIZE_T_CLEAN" before Python.h

NOTE:-This solution is for those people who are working on Python 3

Devious
  • 9
  • 8