1
0

module.cpp 656 B

12345678910111213141516171819202122232425262728293031
  1. #include "test.h"
  2. PYBIND11_EMBEDDED_MODULE(example, m) {
  3. m.def("add", [](int a, int b) {
  4. return a + b;
  5. });
  6. auto math = m.def_submodule("math");
  7. math.def("sub", [](int a, int b) {
  8. return a - b;
  9. });
  10. }
  11. namespace {
  12. TEST_F(PYBIND11_TEST, module) {
  13. py::exec("import example");
  14. EXPECT_EVAL_EQ("example.add(1, 2)", 3);
  15. py::exec("from example import math");
  16. EXPECT_EVAL_EQ("math.sub(1, 2)", -1);
  17. py::exec("from example.math import sub");
  18. EXPECT_EVAL_EQ("sub(1, 2)", -1);
  19. auto math = py::module_::import("example.math");
  20. EXPECT_EQ(math.attr("sub")(4, 3).cast<int>(), 1);
  21. }
  22. } // namespace