#include #include #include #include #include #include #include #include #include class Foo { public: Foo() : mValue(0) {} void SetValue(int i) { mValue = i;} int GetValue() const { return mValue;} private: int mValue; }; namespace python = boost::python; BOOST_PYTHON_MODULE(Sandbox) { python::class_ foo("Foo"); foo.add_property("value", &Foo::GetValue, &Foo::SetValue); } int main(int, char **) { PyImport_AppendInittab("Sandbox", initSandbox); Py_Initialize(); python::handle<> main_module(python::borrowed(PyImport_AddModule("__main__"))); python::dict main_namespace = python::dict(python::handle<>(python::borrowed(PyModule_GetDict(main_module.get())))); try { python::handle<> FooType(Py_CompileString("from Sandbox import * \n" "Foo \n", "null", Py_file_input)); python::object foo(FooType); main_namespace["object"] = foo; FILE *fp = fopen("script.py", "r"); python::handle<> result(PyRun_File(fp, "script.py", Py_file_input, main_namespace.ptr(), main_namespace.ptr())); Foo &foo1 = python::extract(main_namespace["object"]); std::cout << foo1.GetValue() << std::endl; } catch(const python::error_already_set &) { std::cout << "python exception" << std::endl; PyErr_Print(); } Py_Finalize(); }