winterTTr
winterTTr

Reputation: 1768

run python command from C code with specific encoding

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?

Upvotes: 2

Views: 360

Answers (1)

Frédéric Hamidi
Frédéric Hamidi

Reputation: 262939

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);

Upvotes: 3

Related Questions