Interview Questions

How can I execute arbitrary Python statements from C?

Python Questions and Answers


(Continued from previous question...)

How can I execute arbitrary Python statements from C?

The highest-level function to do this is PyRun_SimpleString() which takes a single string argument to be executed in the context of the module __main__ and returns 0 for success and -1 when an exception occurred (including SyntaxError). If you want more control, use PyRun_String(); see the source for PyRun_SimpleString() in Python/pythonrun.c.

(Continued on next question...)

Other Interview Questions