File indexing completed on 2025-01-18 10:17:51
0001 #include <pybind11/embed.h>
0002 namespace py = pybind11;
0003
0004 PYBIND11_EMBEDDED_MODULE(test_cmake_build, m) {
0005 m.def("add", [](int i, int j) { return i + j; });
0006 }
0007
0008 int main(int argc, char *argv[]) {
0009 if (argc != 2) {
0010 throw std::runtime_error("Expected test.py file as the first argument");
0011 }
0012 auto *test_py_file = argv[1];
0013
0014 py::scoped_interpreter guard{};
0015
0016 auto m = py::module_::import("test_cmake_build");
0017 if (m.attr("add")(1, 2).cast<int>() != 3) {
0018 throw std::runtime_error("embed.cpp failed");
0019 }
0020
0021 py::module_::import("sys").attr("argv") = py::make_tuple("test.py", "embed.cpp");
0022 py::eval_file(test_py_file, py::globals());
0023 }