Advertisement
Guest User

Untitled

a guest
Sep 3rd, 2011
402
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.37 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. #include <boost/python.hpp>
  4. #include <boost/python/class.hpp>
  5. #include <boost/python/module.hpp>
  6. #include <boost/python/def.hpp>
  7. #include <boost/enable_shared_from_this.hpp>
  8. using namespace boost::python;
  9.  
  10. object pyMainModule;
  11. object pyMainNamespace;
  12.  
  13. #define EXAMPLE_PY_FUNCTION \
  14.   "from scene import Scene\n" \
  15.   "class Foo(object):\n" \
  16.   "  @staticmethod\n" \
  17.   "  def processFile(scene, filename):\n" \
  18.   "    print('here')\n"
  19.  
  20. std::string parse_python_exception();
  21.  
  22. class Scene : public boost::enable_shared_from_this<Scene>
  23. {
  24. public:
  25.   void sendYourselfToPython()
  26.   {
  27.     try {
  28.       object ignored = exec(EXAMPLE_PY_FUNCTION, pyMainNamespace);
  29.       object processFileFunc = pyMainModule.attr("Foo").attr("processFile");
  30.       processFileFunc(shared_from_this(), "test.txt");
  31.     } catch (boost::python::error_already_set const &) {
  32.       std::string perror = parse_python_exception();
  33.       std::cerr << "Error in Python: " << perror << std::endl;
  34.     }
  35.   }
  36. };
  37. typedef boost::shared_ptr<Scene> SceneP;
  38.  
  39. BOOST_PYTHON_MODULE(scene)
  40. {
  41.   class_<Scene, boost::noncopyable>("Scene");
  42. }
  43.  
  44. main(int argc, char**argv)
  45. {
  46.   std::cout << "starting program..." << std::endl;
  47.  
  48.   Py_Initialize();
  49.   pyMainModule = import("__main__");
  50.   pyMainNamespace = pyMainModule.attr("__dict__");
  51.  
  52.   boost::python::register_ptr_to_python< boost::shared_ptr<Scene> >();
  53.   PyImport_AppendInittab("scene", &initscene);
  54.  
  55.   SceneP scene(new Scene());
  56.   scene->sendYourselfToPython(); // call from inside Scene
  57.  
  58.   try {
  59.     object ignored = exec(EXAMPLE_PY_FUNCTION, pyMainNamespace);
  60.     object processFileFunc = pyMainModule.attr("Foo").attr("processFile");
  61.     processFileFunc(scene, "test.txt"); // call using the smart pointer
  62.   } catch (boost::python::error_already_set const &) {
  63.     std::string perror = parse_python_exception();
  64.     std::cerr << "Error in Python: " << perror << std::endl;
  65.   }
  66. }
  67.  
  68.  
  69. // taken from http://thejosephturner.com/blog/2011/06/15/embedding-python-in-c-applications-with-boostpython-part-2/
  70. namespace py = boost::python;
  71. std::string parse_python_exception() {
  72.     PyObject *type_ptr = NULL, *value_ptr = NULL, *traceback_ptr = NULL;
  73.     PyErr_Fetch(&type_ptr, &value_ptr, &traceback_ptr);
  74.     std::string ret("Unfetchable Python error");
  75.     if (type_ptr != NULL) {
  76.         py::handle<> h_type(type_ptr);
  77.         py::str type_pstr(h_type);
  78.         py::extract<std::string> e_type_pstr(type_pstr);
  79.         if(e_type_pstr.check())
  80.             ret = e_type_pstr();
  81.         else
  82.             ret = "Unknown exception type";
  83.     }
  84.  
  85.     if (value_ptr != NULL) {
  86.         py::handle<> h_val(value_ptr);
  87.         py::str a(h_val);
  88.         py::extract<std::string> returned(a);
  89.         if(returned.check())
  90.             ret +=  ": " + returned();
  91.         else
  92.             ret += std::string(": Unparseable Python error: ");
  93.     }
  94.  
  95.     if (traceback_ptr != NULL) {
  96.         py::handle<> h_tb(traceback_ptr);
  97.         py::object tb(py::import("traceback"));
  98.         py::object fmt_tb(tb.attr("format_tb"));
  99.         py::object tb_list(fmt_tb(h_tb));
  100.         py::object tb_str(py::str("\n").join(tb_list));
  101.         py::extract<std::string> returned(tb_str);
  102.         if(returned.check())
  103.             ret += ": " + returned();
  104.         else
  105.             ret += std::string(": Unparseable Python traceback");
  106.     }
  107.     return ret;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement