2017-03-29 22:20:42 +00:00
|
|
|
#include <pybind11/embed.h>
|
2017-03-23 12:32:54 +00:00
|
|
|
namespace py = pybind11;
|
|
|
|
|
2017-03-29 22:20:42 +00:00
|
|
|
PYBIND11_EMBEDDED_MODULE(test_cmake_build, m) {
|
2017-03-23 12:32:54 +00:00
|
|
|
m.def("add", [](int i, int j) { return i + j; });
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
if (argc != 2)
|
|
|
|
throw std::runtime_error("Expected test.py file as the first argument");
|
|
|
|
auto test_py_file = argv[1];
|
|
|
|
|
2017-03-29 22:20:42 +00:00
|
|
|
py::scoped_interpreter guard{};
|
|
|
|
|
2020-10-03 17:38:03 +00:00
|
|
|
auto m = py::module_::import("test_cmake_build");
|
2017-03-29 22:20:42 +00:00
|
|
|
if (m.attr("add")(1, 2).cast<int>() != 3)
|
|
|
|
throw std::runtime_error("embed.cpp failed");
|
2017-03-23 12:32:54 +00:00
|
|
|
|
2020-10-03 17:38:03 +00:00
|
|
|
py::module_::import("sys").attr("argv") = py::make_tuple("test.py", "embed.cpp");
|
2017-03-29 22:20:42 +00:00
|
|
|
py::eval_file(test_py_file, py::globals());
|
2017-03-23 12:32:54 +00:00
|
|
|
}
|