Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <boost/python.hpp>
- #include <boost/python/class.hpp>
- #include <boost/python/module.hpp>
- #include <boost/python/def.hpp>
- #include <boost/enable_shared_from_this.hpp>
- using namespace boost::python;
- object pyMainModule;
- object pyMainNamespace;
- #define EXAMPLE_PY_FUNCTION \
- "from scene import Scene\n" \
- "class Foo(object):\n" \
- " @staticmethod\n" \
- " def processFile(scene, filename):\n" \
- " print('here')\n"
- std::string parse_python_exception();
- class Scene : public boost::enable_shared_from_this<Scene>
- {
- public:
- void sendYourselfToPython()
- {
- try {
- object ignored = exec(EXAMPLE_PY_FUNCTION, pyMainNamespace);
- object processFileFunc = pyMainModule.attr("Foo").attr("processFile");
- processFileFunc(shared_from_this(), "test.txt");
- } catch (boost::python::error_already_set const &) {
- std::string perror = parse_python_exception();
- std::cerr << "Error in Python: " << perror << std::endl;
- }
- }
- };
- typedef boost::shared_ptr<Scene> SceneP;
- BOOST_PYTHON_MODULE(scene)
- {
- class_<Scene, boost::noncopyable>("Scene");
- }
- main(int argc, char**argv)
- {
- std::cout << "starting program..." << std::endl;
- Py_Initialize();
- pyMainModule = import("__main__");
- pyMainNamespace = pyMainModule.attr("__dict__");
- boost::python::register_ptr_to_python< boost::shared_ptr<Scene> >();
- PyImport_AppendInittab("scene", &initscene);
- SceneP scene(new Scene());
- scene->sendYourselfToPython(); // call from inside Scene
- try {
- object ignored = exec(EXAMPLE_PY_FUNCTION, pyMainNamespace);
- object processFileFunc = pyMainModule.attr("Foo").attr("processFile");
- processFileFunc(scene, "test.txt"); // call using the smart pointer
- } catch (boost::python::error_already_set const &) {
- std::string perror = parse_python_exception();
- std::cerr << "Error in Python: " << perror << std::endl;
- }
- }
- // taken from http://thejosephturner.com/blog/2011/06/15/embedding-python-in-c-applications-with-boostpython-part-2/
- namespace py = boost::python;
- std::string parse_python_exception() {
- PyObject *type_ptr = NULL, *value_ptr = NULL, *traceback_ptr = NULL;
- PyErr_Fetch(&type_ptr, &value_ptr, &traceback_ptr);
- std::string ret("Unfetchable Python error");
- if (type_ptr != NULL) {
- py::handle<> h_type(type_ptr);
- py::str type_pstr(h_type);
- py::extract<std::string> e_type_pstr(type_pstr);
- if(e_type_pstr.check())
- ret = e_type_pstr();
- else
- ret = "Unknown exception type";
- }
- if (value_ptr != NULL) {
- py::handle<> h_val(value_ptr);
- py::str a(h_val);
- py::extract<std::string> returned(a);
- if(returned.check())
- ret += ": " + returned();
- else
- ret += std::string(": Unparseable Python error: ");
- }
- if (traceback_ptr != NULL) {
- py::handle<> h_tb(traceback_ptr);
- py::object tb(py::import("traceback"));
- py::object fmt_tb(tb.attr("format_tb"));
- py::object tb_list(fmt_tb(h_tb));
- py::object tb_str(py::str("\n").join(tb_list));
- py::extract<std::string> returned(tb_str);
- if(returned.check())
- ret += ": " + returned();
- else
- ret += std::string(": Unparseable Python traceback");
- }
- return ret;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement