7 #include <boost/version.hpp>
8 #include <boost/algorithm/string/predicate.hpp>
10 #include <boost/python.hpp>
13 #if BOOST_VERSION / 100 % 1000 == 58
27 bp::object main_module = bp::import(
"__main__");
29 bp::dict globals = bp::extract<bp::dict>(main_module.attr(
"__dict__"));
36 #if BOOST_VERSION / 100 % 1000 == 58
40 std::stringstream buffer;
42 bp::exec(buffer.str().c_str(), globals);
43 #else // default implementation
44 bp::exec_file((bp::str)
filename, globals);
47 catch (bp::error_already_set & e)
55 bp::object obj_model = globals[model_name];
56 model = bp::extract<Model>(obj_model);
58 catch (bp::error_already_set & e)
65 PyObject * poMainModule = PyImport_AddModule(
"__main__");
66 PyObject * poAttrList = PyObject_Dir(poMainModule);
67 PyObject * poAttrIter = PyObject_GetIter(poAttrList);
68 PyObject * poAttrName;
70 while ((poAttrName = PyIter_Next(poAttrIter)) != NULL)
72 std::string oAttrName((bp::extract<char const *>(poAttrName)));
75 if (!boost::starts_with(oAttrName,
"__") || !boost::ends_with(oAttrName,
"__"))
77 PyObject * poAttr = PyObject_GetAttr(poMainModule, poAttrName);
80 if (poAttr && poAttr->ob_type != poMainModule->ob_type)
81 PyObject_SetAttr(poMainModule, poAttrName, NULL);
84 Py_DecRef(poAttrName);
86 Py_DecRef(poAttrIter);
87 Py_DecRef(poAttrList);