Skip to content

Commit 5f5fd6a

Browse files
committed
Call all_type_info_check_for_divergence() also from type_caster_generic::load_impl<>
1 parent 9ae6cba commit 5f5fd6a

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

include/pybind11/detail/type_caster_base.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -785,14 +785,22 @@ class type_caster_generic {
785785
// if we can find an exact match (or, for a simple C++ type, an inherited match); if
786786
// so, we can safely reinterpret_cast to the relevant pointer.
787787
if (bases.size() > 1) {
788+
std::vector<type_info *> matching_bases;
788789
for (auto *base : bases) {
789790
if (no_cpp_mi ? PyType_IsSubtype(base->type, typeinfo->type)
790791
: base->type == typeinfo->type) {
791-
this_.load_value(
792-
reinterpret_cast<instance *>(src.ptr())->get_value_and_holder(base));
793-
return true;
792+
matching_bases.push_back(base);
794793
}
795794
}
795+
if (matching_bases.size() != 0) {
796+
if (matching_bases.size() > 1) {
797+
matching_bases.push_back(const_cast<type_info *>(typeinfo));
798+
all_type_info_check_for_divergence(matching_bases);
799+
}
800+
this_.load_value(reinterpret_cast<instance *>(src.ptr())->get_value_and_holder(
801+
matching_bases[0]));
802+
return true;
803+
}
796804
}
797805

798806
// Case 2c: C++ multiple inheritance is involved and we couldn't find an exact type

tests/test_python_multiple_inheritance.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,6 @@ TEST_SUBMODULE(python_multiple_inheritance, m) {
6161
.def("reset_drvd2_value", &CppDrvd2::reset_drvd2_value)
6262
.def("get_base_value_from_drvd2", &CppDrvd2::get_base_value_from_drvd2)
6363
.def("reset_base_value_from_drvd2", &CppDrvd2::reset_base_value_from_drvd2);
64+
65+
m.def("pass_CppBase", [](const CppBase *) {});
6466
}

tests/test_python_multiple_inheritance.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ class PCD(PC1, PC2):
3030
pass
3131

3232

33+
class PCDI(PC1, PC2):
34+
def __init__(self):
35+
PC1.__init__(self, 11)
36+
PC2.__init__(self, 12)
37+
38+
3339
def test_PC():
3440
d = PC(11)
3541
assert d.get_base_value() == 11
@@ -69,3 +75,9 @@ def test_PCD():
6975
match=r"CppDrvd2\.__init__\(\) must be called when overriding __init__$",
7076
):
7177
PCD(11)
78+
79+
80+
def test_PCDI():
81+
obj = PCDI()
82+
with pytest.raises(TypeError, match="^bases include diverging derived types: "):
83+
m.pass_CppBase(obj)

0 commit comments

Comments
 (0)