7

I am trying to bind python3 in C++.

When using this:

Py_SetProgramName(argv[0]);

it gives this error:

error C2664: 'Py_SetProgramName' : cannot convert parameter 1 from 'char *' to 'wchar_t *'

Even though that's how the documentation example shows to do it.

I also tried this:

Py_SetProgramName((wchar_t*)argv[0]);

But apparently that's the wrong way to do it.

So how do I fix this, and is there any other good resources on binding Python 3 in C++?

Community
  • 1
  • 1
Neros
  • 1,139
  • 3
  • 14
  • 22

3 Answers3

4

The official way of converting from char to wchar_t is now :

wchar_t *program = Py_DecodeLocale(argv[0], NULL);
Py_SetProgramName(program);

on a side note mbstowcs is not reliable on some platforms.

A quite good example of using python2/3 with c++ would be Panda3D. a c++ game engine scripted with python, that also provides a c++ module builder.

Pmp P.
  • 407
  • 3
  • 9
  • sorry but i don't see any answer for "is there any other good resources on binding Python 3 in C++", correcting wrongs like mbstowcs use is 4 months old ( python3.7+ ) and imho important for newcomers. also embedding python in C++ is not very well supported so question has no age. – Pmp P. Apr 05 '18 at 13:12
  • not saying you cant add to it, its just funny to me that its so old – Neros Apr 06 '18 at 12:49
3

Try following:

wchar_t progname[FILENAME_MAX + 1];
mbstowcs(progname, argv[0], strlen(argv[0]) + 1);
Py_SetProgramName(progname);

http://www.cplusplus.com/reference/cstdlib/mbstowcs/

falsetru
  • 357,413
  • 63
  • 732
  • 636
  • MAXPATHLEN is undefined, I hard-coded it to 255 which allows me to compile. It appears to work though just like my 'wrong' solution I have no way to test it just yet. +1 – Neros Aug 15 '13 at 08:25
  • Insert `#include ` if you are using Unix. – falsetru Aug 15 '13 at 08:36
  • @Brae, I can't find MAXPATHLEN equivalent in Windows. Use `#define MAXPATHLEN 1024`. – falsetru Aug 15 '13 at 08:44
  • Ok, though I will probably leave it at 255 to be safe - http://stackoverflow.com/questions/265769/maximum-filename-length-in-ntfs-windows-xp-and-windows-vista – Neros Aug 15 '13 at 08:47
  • 1
    @Brae, I replaced `MAXPATHLEN` with `FILENAME_MAX`. It does not require header. (not tested in Windows). – falsetru Aug 15 '13 at 08:47
  • @Brae, According to [MSDN](http://msdn.microsoft.com/en-us/library/y075fy39.aspx), it seems to be supported in Windows. – falsetru Aug 15 '13 at 08:48
  • `FILENAME_MAX` is ISO C and C++. The Microsoft specific equivalent is `MAX_PATH`. See http://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx – cdarke Aug 15 '13 at 12:35
  • 1
    be warned mbstowcs is a real source of trouble of some platforms. One random example would be android api 19. Python3.7+ has now integrated official workarounds and unicode coercion see PEP 538/540. – Pmp P. Apr 05 '18 at 09:04
  • @PmpP., Thank you for the info. – falsetru Apr 05 '18 at 14:40
1

I suggest you look at this question

The example documentation for the Python 3 API appears to have not been upgraded from Python 2 - the example you show is one of them (I have reported some of the others).

I have found no good documentation in this area. Even the new (Python 3) editions of well-known Python books either cover this subject sparsely or have code errors (usually because the code comes from Py2).

Community
  • 1
  • 1
cdarke
  • 42,728
  • 8
  • 80
  • 84