fix(regression): support embedded submodule (#5650)

* tests: add test for embedded submodule

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>

* tests: adjust

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>

* Update test_interpreter.cpp

* Update test_interpreter.cpp

* Update test_interpreter.cpp

* Update test_interpreter.cpp

* Update pybind11.h

* fix: end instead of endif

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>

* Update include/pybind11/pybind11.h

---------

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
This commit is contained in:
Henry Schreiner
2025-05-12 05:32:36 -04:00
committed by GitHub
parent 9a191c245d
commit d7d782c5e0
2 changed files with 14 additions and 3 deletions

View File

@@ -61,6 +61,9 @@ PYBIND11_EMBEDDED_MODULE(widget_module, m) {
.def_property_readonly("the_message", &Widget::the_message);
m.def("add", [](int i, int j) { return i + j; });
auto sub = m.def_submodule("sub");
sub.def("add", [](int i, int j) { return i + j; });
}
PYBIND11_EMBEDDED_MODULE(trampoline_module, m) {
@@ -316,6 +319,9 @@ TEST_CASE("Restart the interpreter") {
auto cpp_module = py::module_::import("widget_module");
REQUIRE(cpp_module.attr("add")(1, 2).cast<int>() == 3);
// Also verify submodules work
REQUIRE(cpp_module.attr("sub").attr("add")(1, 41).cast<int>() == 42);
// C++ type information is reloaded and can be used in python modules.
auto py_module = py::module_::import("test_interpreter");
auto py_widget = py_module.attr("DerivedWidget")("Hello after restart");