|
| 1 | +#include <mwrap/nanobind/macros.hpp> |
| 2 | +#include <python_sdk.h> |
| 3 | + |
| 4 | +import std; |
| 5 | +import mcpplibs.mtest; |
| 6 | +import mcpplibs.mwrap.nanobind; |
| 7 | +import mcpplibs.mwrap.nanobind.stl; |
| 8 | +import mcpplibs.mwrap.nanobind.ndarray; |
| 9 | + |
| 10 | +using namespace mcpplibs::mtest; |
| 11 | +namespace nb = nanobind; |
| 12 | + |
| 13 | +namespace { |
| 14 | + |
| 15 | +struct Point { |
| 16 | + double x = 0, y = 0; |
| 17 | + double norm() const { return std::sqrt(x * x + y * y); } |
| 18 | +}; |
| 19 | + |
| 20 | +std::string join_words(const std::vector<std::string>& words) { |
| 21 | + std::string result; |
| 22 | + for (const std::string& word : words) { |
| 23 | + if (!result.empty()) result += ' '; |
| 24 | + result += word; |
| 25 | + } |
| 26 | + return result; |
| 27 | +} |
| 28 | + |
| 29 | +void fail_negative(int value) { |
| 30 | + if (value < 0) throw nb::value_error("negative value"); |
| 31 | +} |
| 32 | + |
| 33 | +} // namespace |
| 34 | + |
| 35 | +NB_MODULE(nanobind_embed, m) { |
| 36 | + m.doc() = "nanobind-embed: the immutable nanobind wrapper"; |
| 37 | + m.def("add", [](int a, int b) { return a + b; }, nb::arg("a"), nb::arg("b")); |
| 38 | + m.def("join_words", &join_words, nb::arg("words")); |
| 39 | + m.def("fail_negative", &fail_negative, nb::arg("value")); |
| 40 | + m.def("array_sum", [](nb::ndarray<const float, nb::ndim<1>> values) { |
| 41 | + double total = 0; |
| 42 | + for (std::size_t i = 0; i < values.shape(0); ++i) total += values(i); |
| 43 | + return total; |
| 44 | + }, nb::arg("values")); |
| 45 | + nb::class_<Point>(m, "Point") |
| 46 | + .def(nb::init<double, double>(), nb::arg("x") = 0, nb::arg("y") = 0) |
| 47 | + .def_rw("x", &Point::x) |
| 48 | + .def_rw("y", &Point::y) |
| 49 | + .def("norm", &Point::norm); |
| 50 | +} |
| 51 | + |
| 52 | +UTest behavior{"behavior", [] { |
| 53 | + expect(fatal(PyImport_AppendInittab("nanobind_embed", PyInit_nanobind_embed) == 0)) |
| 54 | + << "PyImport_AppendInittab failed"; |
| 55 | + |
| 56 | + PyConfig config; |
| 57 | + PyConfig_InitPythonConfig(&config); |
| 58 | + PyStatus status = PyConfig_SetBytesString(&config, &config.home, MCPP_PYTHON_HOME); |
| 59 | + if (!PyStatus_Exception(status)) { |
| 60 | + config.parse_argv = 0; |
| 61 | + config.install_signal_handlers = 0; |
| 62 | + status = Py_InitializeFromConfig(&config); |
| 63 | + } |
| 64 | + PyConfig_Clear(&config); |
| 65 | + expect(fatal(!PyStatus_Exception(status))) << (status.err_msg ? status.err_msg : "unknown PyStatus"); |
| 66 | + |
| 67 | + struct Guard { |
| 68 | + ~Guard() { |
| 69 | + if (Py_IsInitialized()) Py_Finalize(); |
| 70 | + } |
| 71 | + } guard; |
| 72 | + |
| 73 | + "module, functions and stl casters"_test = [] { |
| 74 | + expect(eq(PyRun_SimpleString( |
| 75 | + "import nanobind_embed as b\n" |
| 76 | + "assert b.add(2, 3) == 5\n" |
| 77 | + "assert b.add(a=10, b=32) == 42\n" |
| 78 | + "assert b.join_words(['hello', 'nanobind', 'world']) == 'hello nanobind world'\n"), |
| 79 | + 0)); |
| 80 | + }; |
| 81 | + |
| 82 | + "classes and exceptions"_test = [] { |
| 83 | + expect(eq(PyRun_SimpleString( |
| 84 | + "import nanobind_embed as b\n" |
| 85 | + "p = b.Point(3, 4)\n" |
| 86 | + "assert p.x == 3 and p.y == 4\n" |
| 87 | + "assert abs(p.norm() - 5.0) < 1e-12\n" |
| 88 | + "p.x = 6\n" |
| 89 | + "assert p.x == 6\n" |
| 90 | + "try:\n" |
| 91 | + " b.fail_negative(-1)\n" |
| 92 | + "except ValueError as error:\n" |
| 93 | + " assert str(error) == 'negative value'\n" |
| 94 | + "else:\n" |
| 95 | + " raise AssertionError('expected ValueError')\n" |
| 96 | + "b.fail_negative(1)\n"), |
| 97 | + 0)); |
| 98 | + }; |
| 99 | + |
| 100 | + "ndarray through the buffer protocol"_test = [] { |
| 101 | + expect(eq(PyRun_SimpleString( |
| 102 | + "import array\n" |
| 103 | + "import nanobind_embed as b\n" |
| 104 | + "values = array.array('f', [1.0, 2.0, 3.0])\n" |
| 105 | + "assert abs(b.array_sum(values) - 6.0) < 1e-12\n"), |
| 106 | + 0)); |
| 107 | + }; |
| 108 | +}}; |
0 commit comments