7

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?

Community
  • 1
  • 1
TheJJ
  • 931
  • 12
  • 21

2 Answers2

0
// Convert a sequence of strings to an array of WCHAR pointers
PYWINTYPES_EXPORT void PyWinObject_FreeWCHARArray(LPWSTR *wchars, DWORD str_cnt);

can you use this...?

Trilok M
  • 681
  • 7
  • 21
  • this is not a function available in `/usr/include/python3.3`, I think it's Windows® specific. – TheJJ Feb 06 '14 at 12:50
0

In Python 3.5, Py_DecodeLocale can be used to do the conversion.

https://docs.python.org/3/c-api/sys.html#c.Py_DecodeLocale

TheJJ
  • 931
  • 12
  • 21