2

int PyRun_SimpleString(const char *command) is the function which can run a command of Python.

I call this function in my program, but how can I specify the encoding of the command argument. I found that Python 3 seems to use UTF-8 as the default encoding when I run a command. Can I change this encoding in the C level API? ...or with an extra parameter when call PyRun_SimpleString-like function?

1 Answer 1

3

You can prepend one of the "magic comments" described in PEP 263 to your command:

char buf[1024];
snprintf(buf, sizeof(buf), "-*- coding: %s -*-\n", yourEncoding);
strncat(buf, yourCommand, sizeof(buf) - strlen(buf) - 1);
PyRun_SimpleString(buf);
Sign up to request clarification or add additional context in comments.

1 Comment

But i think this is a little inconvenient , right? And i just want a way to specific the encoding at one time and i can use it for ever, is there this kind of function?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.