Skip to content

Commit

Permalink
Call all_type_info_check_for_divergence() also from `type_caster_ge…
Browse files Browse the repository at this point in the history
…neric::load_impl<>`
  • Loading branch information
Ralf W. Grosse-Kunstleve committed Nov 7, 2023
1 parent 9ae6cba commit 5f5fd6a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
14 changes: 11 additions & 3 deletions include/pybind11/detail/type_caster_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -785,14 +785,22 @@ class type_caster_generic {
// if 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.
if (bases.size() > 1) {
std::vector<type_info *> matching_bases;
for (auto *base : bases) {
if (no_cpp_mi ? PyType_IsSubtype(base->type, typeinfo->type)
: base->type == typeinfo->type) {
this_.load_value(
reinterpret_cast<instance *>(src.ptr())->get_value_and_holder(base));
return true;
matching_bases.push_back(base);
}
}
if (matching_bases.size() != 0) {
if (matching_bases.size() > 1) {
matching_bases.push_back(const_cast<type_info *>(typeinfo));
all_type_info_check_for_divergence(matching_bases);
}
this_.load_value(reinterpret_cast<instance *>(src.ptr())->get_value_and_holder(
matching_bases[0]));
return true;
}
}

// Case 2c: C++ multiple inheritance is involved and we couldn't find an exact type
Expand Down
2 changes: 2 additions & 0 deletions tests/test_python_multiple_inheritance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,6 @@ TEST_SUBMODULE(python_multiple_inheritance, m) {
.def("reset_drvd2_value", &CppDrvd2::reset_drvd2_value)
.def("get_base_value_from_drvd2", &CppDrvd2::get_base_value_from_drvd2)
.def("reset_base_value_from_drvd2", &CppDrvd2::reset_base_value_from_drvd2);

m.def("pass_CppBase", [](const CppBase *) {});
}
12 changes: 12 additions & 0 deletions tests/test_python_multiple_inheritance.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ class PCD(PC1, PC2):
pass


class PCDI(PC1, PC2):
def __init__(self):
PC1.__init__(self, 11)
PC2.__init__(self, 12)


def test_PC():
d = PC(11)
assert d.get_base_value() == 11
Expand Down Expand Up @@ -69,3 +75,9 @@ def test_PCD():
match=r"CppDrvd2\.__init__\(\) must be called when overriding __init__$",
):
PCD(11)


def test_PCDI():
obj = PCDI()
with pytest.raises(TypeError, match="^bases include diverging derived types: "):
m.pass_CppBase(obj)

0 comments on commit 5f5fd6a

Please sign in to comment.