user2624915
user2624915

Reputation: 65

What do these C/C++ lines using the Python C API do?

const char * __attribute__((weak)) m5MainCommands[] = {
    "import m5",
    "m5.main()",
    0 // sentinel is required
};

PyObject *result;
const char **command = m5MainCommands;

result = PyRun_String(*command, Py_file_input, dict, dict);

Please explain to me that last line in the context of C/C++ only. Assume I don't know python at all.

Upvotes: 0

Views: 89

Answers (2)

Andrei Boyanov
Andrei Boyanov

Reputation: 2393

This has to execute the following Python code:

import m5 # m5 is some Python module
m5.main() # no secrete there - runs the main() method if the m5 module :)

Upvotes: 2

QuestionC
QuestionC

Reputation: 10064

It runs the Python command "import m5", the first command in m5MainCommands[].

If you were to run all the strings in m5MainCommands, the net effect of those Python commands would be to run the file "m5.py"'s main method in python.

Upvotes: 3

Related Questions