1

I'm writing a C++ program that requires Python (3.11) code to be embedded into it and am using Python.h to try and accomplish this. The general idea is that my a python script, which will be stored by the C++ program as a string, as I'll be performing operations on the source at runtime, will contain a "main()" function which returns an array of known size.

I'm aware I can do it via:

...
PyObject *pName = PyString_FromString("main");
PyObject *pModule = PyImport_Import(pName)
...

However, in order to actually execute the script, I would need to write it to a file just so that python could read it again. This adds extra time to execution that I'd prefer to avoid. Isn't there some way in which I can pass python the source code directly as a string and work from there? Or am I just screwed?

EDIT: BTW, PyRun_SimpleString does not do what I want, as it doesn't return anything from the executed code.

3

1 Answer 1

2

Found the answer thanks to nick in the comments. An example of usage of PyRun_String: https://schneide.blog/2011/10/10/embedding-python-into-cpp/, and extracting list variables from python script https://docs.python.org/3/c-api/list.html The final frankenstein:

PyObject *main = PyImport_AddModule("__main__");
PyObject *globalDictionary = PyModule_GetDict(main);
PyObject *localDictionary = PyDict_New();
PyRun_String("a=[0, 1, 2, 3, 4, 5]", Py_file_input, globalDictionary, localDictionary);
    
PyObject *result = PyDict_GetItemString(localDictionary, "a");

double a[6];
for (int i = 0; i < PyList_Size(result); i++) {
    a[i] = PyFloat_AsDouble(PyList_GetItem(result, i));
}
Sign up to request clarification or add additional context in comments.

3 Comments

Happy to know it works, but wow, what a pain to implement... >_<
Indeed. Only 2 goats sacrificed this time, an all time personal low.
This might be important for my current project in a few weeks, hence my peaked interest. Thanks for taking one for the team ! ;D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.