Advertisement
dan-masek

Untitled

Aug 29th, 2019
395
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.79 KB | None | 0 0
  1. #define BOOST_ALL_NO_LIB
  2.  
  3. #include <boost/python.hpp>
  4.  
  5. #include <iostream>
  6. #include <string>
  7.  
  8. namespace bp = boost::python;
  9.  
  10. void module_function()
  11. {
  12.     bp::object module = bp::import("my_module");
  13.     std::string value = bp::extract<std::string>(module.attr("my_attribute"));
  14.     std::cout << value << "\n";
  15. }
  16.  
  17. BOOST_PYTHON_MODULE(my_module)
  18. {
  19.     bp::def("module_function", &module_function);
  20.  
  21.     bp::scope().attr("my_attribute") = "foo";
  22. }
  23.  
  24. int main()
  25. {
  26.     try {
  27.         Py_Initialize();
  28.         initmy_module();
  29.        
  30.         // Call the function...
  31.         bp::object module = bp::import("my_module");
  32.         bp::object fn = module.attr("module_function");
  33.         fn();
  34.  
  35.     } catch (bp::error_already_set) {
  36.         PyErr_Print();
  37.     }
  38.  
  39.     Py_Finalize();
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement