I am working on a project where I have to convert a user input C++ string variable to python executable code, I'm using the Python C API, so I have to go through the intermediary of converting the C++ string to a C string (or character array). Once I get there, I can't get the value to print once the code has been run. I'm searching for what the proper function in the Python C API is. I've tried PyString_AsString(),
PyBytes_AsString(),
and PyObject_AsUTF8String
so far. Each of these functions has caused a memory issue when I try and run it with the code below. I've had to hard reboot my system with these three.
Py_Initialize();
//the expected output, the player's output, and the compare functions
//PyObject* expectAnswer; //the expected answer for the level
PyObject* main = PyImport_AddModule("_main_");
PyObject* globalDictionary = PyModule_GetDict(main);
PyObject* localDictionary = PyDict_New();
PyObject* result = PyRun_String(cPlayerString, Py_file_input, globalDictionary, localDictionary);
Py_Finalize();
I am wondering whether I'm just not using the right call. const char* PyUnicode_AsData()
looks promising, but I still have no idea whether I'd need to convert what I have into Unicode first.
Thanks for all the help!