Minimal test covering classh_type_casters load_impl Case 2b.

This commit is contained in:
Ralf W. Grosse-Kunstleve 2021-01-22 13:17:27 -08:00
parent 82f619bdb2
commit c362b91a80
3 changed files with 15 additions and 3 deletions

View File

@ -167,7 +167,6 @@ public:
// we can find an exact match (or, for a simple C++ type, an inherited match); if so, we
// can safely reinterpret_cast to the relevant pointer.
else if (bases.size() > 1) {
pybind11_fail("classh_type_casters: Case 2b UNTESTED.");
for (auto base : bases) {
if (no_cpp_mi ? PyType_IsSubtype(base->type, typeinfo->type) : base->type == typeinfo->type) {
this_.load_value_and_holder(reinterpret_cast<instance *>(src.ptr())->get_value_and_holder(base));

View File

@ -67,8 +67,8 @@ TEST_SUBMODULE(classh_inheritance, m) {
m.def("pass_base", pass_base);
m.def("pass_drvd", pass_drvd);
py::classh<base1>(m, "base1");
py::classh<base2>(m, "base2");
py::classh<base1>(m, "base1").def(py::init<>()); // __init__ needed for Python inheritance.
py::classh<base2>(m, "base2").def(py::init<>());
py::classh<drvd2, base1, base2>(m, "drvd2");
m.def("make_drvd2", make_drvd2, py::return_value_policy::take_ownership);

View File

@ -34,3 +34,16 @@ def test_make_drvd2_up_casts_pass_drvd2():
assert b2.__class__.__name__ == "drvd2"
i2 = m.pass_drvd2(b2)
assert i2 == 3 * 110 + 4 * 120 + 23
def test_python_drvd2():
class Drvd2(m.base1, m.base2):
def __init__(self):
m.base1.__init__(self)
m.base2.__init__(self)
d = Drvd2()
i1 = m.pass_base1(d) # load_impl Case 2b
assert i1 == 110 + 21
i2 = m.pass_base2(d)
assert i2 == 120 + 22