From c362b91a80fa463075bb0fca94d4e418ba948c66 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Fri, 22 Jan 2021 13:17:27 -0800 Subject: [PATCH] Minimal test covering classh_type_casters load_impl Case 2b. --- include/pybind11/detail/classh_type_casters.h | 1 - tests/test_classh_inheritance.cpp | 4 ++-- tests/test_classh_inheritance.py | 13 +++++++++++++ 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/include/pybind11/detail/classh_type_casters.h b/include/pybind11/detail/classh_type_casters.h index aa2885986..d00543e5d 100644 --- a/include/pybind11/detail/classh_type_casters.h +++ b/include/pybind11/detail/classh_type_casters.h @@ -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(src.ptr())->get_value_and_holder(base)); diff --git a/tests/test_classh_inheritance.cpp b/tests/test_classh_inheritance.cpp index 0e3314ecc..ed801fc65 100644 --- a/tests/test_classh_inheritance.cpp +++ b/tests/test_classh_inheritance.cpp @@ -67,8 +67,8 @@ TEST_SUBMODULE(classh_inheritance, m) { m.def("pass_base", pass_base); m.def("pass_drvd", pass_drvd); - py::classh(m, "base1"); - py::classh(m, "base2"); + py::classh(m, "base1").def(py::init<>()); // __init__ needed for Python inheritance. + py::classh(m, "base2").def(py::init<>()); py::classh(m, "drvd2"); m.def("make_drvd2", make_drvd2, py::return_value_policy::take_ownership); diff --git a/tests/test_classh_inheritance.py b/tests/test_classh_inheritance.py index 7af68f80c..9af0bc3ed 100644 --- a/tests/test_classh_inheritance.py +++ b/tests/test_classh_inheritance.py @@ -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