3

I am using Eclipse to run C++. In my code,I use a High Level Embedding of Python to run a function. When I try to use sys and import it. I get the error:

Fatal Python error: no mem for sys.argv

CODE:

#include <python3.4m/Python.h>
#include <iostream>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main(int argc, char **argv)
{

    Py_Initialize();
    PySys_SetArgv(argc, (wchar_t**)argv);
    PyRun_SimpleString("import sys\n");
    Py_Finalize();
    return 0;
}

When I run the .exe from Terminal I get

ValueError: character U+384d2f2e is not in range [U+0000; U+10ffff] Aborted

Any help is appreciated in resolving this problem. Thank you.

Y. Reznichenko
  • 153
  • 2
  • 13

2 Answers2

4

The error was that Python expected an **argv to point to a set of unicode values. Instead argv was pointing to chars.

To solve this:

wchar_t **wargv;
wargv = (wchar_t**)malloc(1*sizeof(wchar_t *));
*wargv = (wchar_t*)malloc(6*sizeof(wchar_t));
**wargv = L'argv1';

Py_Initialize();
PySys_SetArgv(1, (wchar_t**)wargv);
PyRun_SimpleString("import sys\n"
                   "print('test')\n");
Py_Finalize();
return 0;

Hope this helps someone else.

Y. Reznichenko
  • 153
  • 2
  • 13
1

You have to do something like this:

unsigned int mySize = 200;
wchar_t* wargv = Py_DecodeLocale(argv[0], &mySize);
PySys_SetArgv(1, &wargv);

In this way all is clean, working and without compilation warning

Francesco
  • 523
  • 4
  • 25