I've a question regarding boost-python. Basically I want to execute a specific python function which is stored in a std::string with boost-python. There is an example how to achieve this in the documentation: Boost python doc.
So what I'm doing is (C++ Code):
using namespace boost::python;
// Retrieve the main module.
object main = import("__main__");
// Retrieve the main module's namespace
// Define greet function in Python.
object result = exec(string_with_python_code.c_str(), global, global);
object greet = global["greet"];
//calling greet() function
string_with_python_code = "print 'Hello global world!' \n"
"def greet(): \n"
" print 'Hello local world!' \n"
" return \n";
A Python function definition is an execution of Python code. So is a Python module import: importing a module can cause arbitrary Python statements to run.
The way to not run code outside your function definition is to not pass code outside your function definition to
exec will do exactly what you tell it to do, no less.