Skip to content

Commit

Permalink
Add test_home_planet_wrap_very_lonely_traveler(), test_exo_planet_pyb…
Browse files Browse the repository at this point in the history
…ind11_wrap_very_lonely_traveler()
  • Loading branch information
rwgk committed Sep 13, 2024
1 parent 69890bb commit 22dbfa6
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 2 deletions.
3 changes: 2 additions & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,8 @@ tests_extra_targets("test_exceptions.py;test_local_bindings.py;test_stl.py;test_
# And add additional targets for other tests.
tests_extra_targets("test_exceptions.py" "cross_module_interleaved_error_already_set")
tests_extra_targets("test_gil_scoped.py" "cross_module_gil_utils")
tests_extra_targets("test_cpp_conduit.py" "exo_planet_pybind11;exo_planet_c_api")
tests_extra_targets("test_cpp_conduit.py"
"exo_planet_pybind11;exo_planet_c_api;home_planet_very_lonely_traveler")

set(PYBIND11_EIGEN_REPO
"https://gitlab.com/libeigen/eigen.git"
Expand Down
11 changes: 10 additions & 1 deletion tests/exo_planet_pybind11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,13 @@

#include "test_cpp_conduit_traveler_bindings.h"

PYBIND11_MODULE(exo_planet_pybind11, m) { pybind11_tests::test_cpp_conduit::wrap_traveler(m); }
namespace pybind11_tests {
namespace test_cpp_conduit {

PYBIND11_MODULE(exo_planet_pybind11, m) {
wrap_traveler(m);
m.def("wrap_very_lonely_traveler", [m]() { wrap_very_lonely_traveler(m); });
}

} // namespace test_cpp_conduit
} // namespace pybind11_tests
13 changes: 13 additions & 0 deletions tests/home_planet_very_lonely_traveler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) 2024 The pybind Community.

#include "test_cpp_conduit_traveler_bindings.h"

namespace pybind11_tests {
namespace test_cpp_conduit {

PYBIND11_MODULE(home_planet_very_lonely_traveler, m) {
m.def("wrap_very_lonely_traveler", [m]() { wrap_very_lonely_traveler(m); });
}

} // namespace test_cpp_conduit
} // namespace pybind11_tests
1 change: 1 addition & 0 deletions tests/test_cpp_conduit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ TEST_SUBMODULE(cpp_conduit, m) {
m.attr("cpp_type_info_capsule_int") = py::capsule(&typeid(int), typeid(std::type_info).name());

wrap_traveler(m);
wrap_lonely_traveler(m);
}

} // namespace test_cpp_conduit
Expand Down
35 changes: 35 additions & 0 deletions tests/test_cpp_conduit.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import exo_planet_c_api
import exo_planet_pybind11
import home_planet_very_lonely_traveler
import pytest

from pybind11_tests import cpp_conduit as home_planet
Expand Down Expand Up @@ -125,3 +126,37 @@ def test_exo_planet_c_api_premium_traveler(premium_traveler_type):
pt = premium_traveler_type("gucci", 5)
assert exo_planet_c_api.GetLuggage(pt) == "gucci"
assert exo_planet_c_api.GetPoints(pt) == 5


def test_home_planet_wrap_very_lonely_traveler():
# This does not exercise the cpp_conduit feature, but is here to
# demonstrate that the cpp_conduit feature does not solve all
# cross-extension interoperability issues.
# Here is the proof that the following works for extensions with
# matching `PYBIND11_INTERNALS_ID`s:
# test_cpp_conduit.cpp:
# py::class_<LonelyTraveler>
# home_planet_very_lonely_traveler.cpp:
# py::class_<VeryLonelyTraveler, LonelyTraveler>
# See test_exo_planet_pybind11_wrap_very_lonely_traveler() for the negative
# test.
assert home_planet.LonelyTraveler is not None # Verify that the base class exists.
home_planet_very_lonely_traveler.wrap_very_lonely_traveler()
# Ensure that the derived class exists.
assert home_planet_very_lonely_traveler.VeryLonelyTraveler is not None


def test_exo_planet_pybind11_wrap_very_lonely_traveler():
# See comment under test_home_planet_wrap_very_lonely_traveler() first.
# Here the `PYBIND11_INTERNALS_ID`s don't match between:
# test_cpp_conduit.cpp:
# py::class_<LonelyTraveler>
# exo_planet_pybind11.cpp:
# py::class_<VeryLonelyTraveler, LonelyTraveler>
assert home_planet.LonelyTraveler is not None # Verify that the base class exists.
with pytest.raises(
RuntimeError,
match='^generic_type: type "VeryLonelyTraveler" referenced unknown base type '
'"pybind11_tests::test_cpp_conduit::LonelyTraveler"$',
):
exo_planet_pybind11.wrap_very_lonely_traveler()
8 changes: 8 additions & 0 deletions tests/test_cpp_conduit_traveler_bindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,13 @@ inline void wrap_traveler(py::module_ m) {
m.def("get_points", [](const PremiumTraveler &person) { return person.points; });
}

inline void wrap_lonely_traveler(py::module_ m) {
py::class_<LonelyTraveler>(m, "LonelyTraveler");
}

inline void wrap_very_lonely_traveler(py::module_ m) {
py::class_<VeryLonelyTraveler, LonelyTraveler>(m, "VeryLonelyTraveler");
}

} // namespace test_cpp_conduit
} // namespace pybind11_tests
3 changes: 3 additions & 0 deletions tests/test_cpp_conduit_traveler_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,8 @@ struct PremiumTraveler : Traveler {
int points;
};

struct LonelyTraveler {};
struct VeryLonelyTraveler : LonelyTraveler {};

} // namespace test_cpp_conduit
} // namespace pybind11_tests

0 comments on commit 22dbfa6

Please sign in to comment.