Skip to content

Commit 22dbfa6

Browse files
committed
Add test_home_planet_wrap_very_lonely_traveler(), test_exo_planet_pybind11_wrap_very_lonely_traveler()
1 parent 69890bb commit 22dbfa6

7 files changed

+72
-2
lines changed

tests/CMakeLists.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,8 @@ tests_extra_targets("test_exceptions.py;test_local_bindings.py;test_stl.py;test_
219219
# And add additional targets for other tests.
220220
tests_extra_targets("test_exceptions.py" "cross_module_interleaved_error_already_set")
221221
tests_extra_targets("test_gil_scoped.py" "cross_module_gil_utils")
222-
tests_extra_targets("test_cpp_conduit.py" "exo_planet_pybind11;exo_planet_c_api")
222+
tests_extra_targets("test_cpp_conduit.py"
223+
"exo_planet_pybind11;exo_planet_c_api;home_planet_very_lonely_traveler")
223224

224225
set(PYBIND11_EIGEN_REPO
225226
"https://gitlab.com/libeigen/eigen.git"

tests/exo_planet_pybind11.cpp

+10-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,13 @@
77

88
#include "test_cpp_conduit_traveler_bindings.h"
99

10-
PYBIND11_MODULE(exo_planet_pybind11, m) { pybind11_tests::test_cpp_conduit::wrap_traveler(m); }
10+
namespace pybind11_tests {
11+
namespace test_cpp_conduit {
12+
13+
PYBIND11_MODULE(exo_planet_pybind11, m) {
14+
wrap_traveler(m);
15+
m.def("wrap_very_lonely_traveler", [m]() { wrap_very_lonely_traveler(m); });
16+
}
17+
18+
} // namespace test_cpp_conduit
19+
} // namespace pybind11_tests
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright (c) 2024 The pybind Community.
2+
3+
#include "test_cpp_conduit_traveler_bindings.h"
4+
5+
namespace pybind11_tests {
6+
namespace test_cpp_conduit {
7+
8+
PYBIND11_MODULE(home_planet_very_lonely_traveler, m) {
9+
m.def("wrap_very_lonely_traveler", [m]() { wrap_very_lonely_traveler(m); });
10+
}
11+
12+
} // namespace test_cpp_conduit
13+
} // namespace pybind11_tests

tests/test_cpp_conduit.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ TEST_SUBMODULE(cpp_conduit, m) {
1515
m.attr("cpp_type_info_capsule_int") = py::capsule(&typeid(int), typeid(std::type_info).name());
1616

1717
wrap_traveler(m);
18+
wrap_lonely_traveler(m);
1819
}
1920

2021
} // namespace test_cpp_conduit

tests/test_cpp_conduit.py

+35
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import exo_planet_c_api
66
import exo_planet_pybind11
7+
import home_planet_very_lonely_traveler
78
import pytest
89

910
from pybind11_tests import cpp_conduit as home_planet
@@ -125,3 +126,37 @@ def test_exo_planet_c_api_premium_traveler(premium_traveler_type):
125126
pt = premium_traveler_type("gucci", 5)
126127
assert exo_planet_c_api.GetLuggage(pt) == "gucci"
127128
assert exo_planet_c_api.GetPoints(pt) == 5
129+
130+
131+
def test_home_planet_wrap_very_lonely_traveler():
132+
# This does not exercise the cpp_conduit feature, but is here to
133+
# demonstrate that the cpp_conduit feature does not solve all
134+
# cross-extension interoperability issues.
135+
# Here is the proof that the following works for extensions with
136+
# matching `PYBIND11_INTERNALS_ID`s:
137+
# test_cpp_conduit.cpp:
138+
# py::class_<LonelyTraveler>
139+
# home_planet_very_lonely_traveler.cpp:
140+
# py::class_<VeryLonelyTraveler, LonelyTraveler>
141+
# See test_exo_planet_pybind11_wrap_very_lonely_traveler() for the negative
142+
# test.
143+
assert home_planet.LonelyTraveler is not None # Verify that the base class exists.
144+
home_planet_very_lonely_traveler.wrap_very_lonely_traveler()
145+
# Ensure that the derived class exists.
146+
assert home_planet_very_lonely_traveler.VeryLonelyTraveler is not None
147+
148+
149+
def test_exo_planet_pybind11_wrap_very_lonely_traveler():
150+
# See comment under test_home_planet_wrap_very_lonely_traveler() first.
151+
# Here the `PYBIND11_INTERNALS_ID`s don't match between:
152+
# test_cpp_conduit.cpp:
153+
# py::class_<LonelyTraveler>
154+
# exo_planet_pybind11.cpp:
155+
# py::class_<VeryLonelyTraveler, LonelyTraveler>
156+
assert home_planet.LonelyTraveler is not None # Verify that the base class exists.
157+
with pytest.raises(
158+
RuntimeError,
159+
match='^generic_type: type "VeryLonelyTraveler" referenced unknown base type '
160+
'"pybind11_tests::test_cpp_conduit::LonelyTraveler"$',
161+
):
162+
exo_planet_pybind11.wrap_very_lonely_traveler()

tests/test_cpp_conduit_traveler_bindings.h

+8
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,13 @@ inline void wrap_traveler(py::module_ m) {
3535
m.def("get_points", [](const PremiumTraveler &person) { return person.points; });
3636
}
3737

38+
inline void wrap_lonely_traveler(py::module_ m) {
39+
py::class_<LonelyTraveler>(m, "LonelyTraveler");
40+
}
41+
42+
inline void wrap_very_lonely_traveler(py::module_ m) {
43+
py::class_<VeryLonelyTraveler, LonelyTraveler>(m, "VeryLonelyTraveler");
44+
}
45+
3846
} // namespace test_cpp_conduit
3947
} // namespace pybind11_tests

tests/test_cpp_conduit_traveler_types.h

+3
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,8 @@ struct PremiumTraveler : Traveler {
1818
int points;
1919
};
2020

21+
struct LonelyTraveler {};
22+
struct VeryLonelyTraveler : LonelyTraveler {};
23+
2124
} // namespace test_cpp_conduit
2225
} // namespace pybind11_tests

0 commit comments

Comments
 (0)