I'm trying to embed Python 3.3 in our C++ project. Python 3.3 seems to have introduced UTF-8 as preferred storage, PEP 393: "the specification chooses UTF-8 as the recommended way of exposing strings to C code."
I wrote this initialization code, which seems to be simple and intuitive:
#include <Python.h>
#include "log.h"
void python_init(const char *program_name) {
if (not Py_IsInitialized()) {
Py_SetProgramName(program_name);
Py_Initialize();
const char *py_version = Py_GetVersion();
log::msg("initialized python %s", py_version);
}
}
but compiling it fails:
/home/jj/devel/openage/src/engine/python.cpp:13:3: error: no matching function for call to 'Py_SetProgramName'
Py_SetProgramName(program_name);
^~~~~~~~~~~~~~~~~
/usr/include/python3.3/pythonrun.h:25:18: note: candidate function not viable: no known conversion from 'const char *' to 'wchar_t *' for 1st argument
PyAPI_FUNC(void) Py_SetProgramName(wchar_t *);
^
So yeah, obviously I need a wchar_t *
here, but I don't see any reason why char *
would not do the job here.
What is the best practice here?
Convert char *
to wchar *
and deal with locales (mbstowcs), which would also introduce unnecessary dynamic memory allocs?
Also, if Python decided to go for wchar
entirely, why does Py_GetVersion()
return a char *
as I expected it?
I found a similar question for Python <3.3 , but I hope Python 3.3 is different (PEP 393?).
The code has to be cross-platform capable.
=> What's a fast and efficient solution to pass C strings (char *
) to Python 3.3?