Advertisement
dan-masek

MCVE for https://stackoverflow.com/q/78378063/3962537

Apr 24th, 2024 (edited)
832
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.67 KB | None | 0 0
  1. #include <pybind11/pybind11.h>
  2. #include <pybind11/embed.h>
  3. #include <iostream>
  4.  
  5. namespace py = pybind11;
  6.  
  7. PYBIND11_EMBEDDED_MODULE(testmodule, m)
  8. {
  9.     m.def("start", [](py::module& foo) {
  10.         try {
  11.             py::module bar = foo.attr("bar");
  12.             std::cout << "ok\n";
  13.         } catch (py::error_already_set const& e) {
  14.             std::cerr << "error" << e.what() << '\n';
  15.         }
  16.     });
  17. }
  18.  
  19. int main()
  20. {
  21.     py::scoped_interpreter guard{};
  22.  
  23.     try {
  24.         py::exec(R"(\
  25. import testmodule
  26. import foo
  27.  
  28. testmodule.start(foo)
  29. )");
  30.     } catch (py::error_already_set& e) {
  31.         std::cerr << e.what() << '\n';
  32.     }
  33.  
  34.     return 0;
  35. }
  36.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement