Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions docs/classes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,12 @@ Alternatively, we can also assign a name to the previously bound ``Pet``
.def(py::init<const std::string &>())
.def("bark", &Dog::bark);

Functionality-wise, both approaches are equivalent. Afterwards, instances will
expose fields and methods of both types:
Functionality-wise, both approaches are equivalent, with one exception. Method 2
gives pybind11 no C++ type for the parent, so pybind11 uses the same pointer for
both classes. Use method 1 when the parent subobject is not at the start of the
child, for example when the child has virtual functions and the parent has none.

Afterwards, instances will expose fields and methods of both types:

.. code-block:: pycon

Expand Down
10 changes: 10 additions & 0 deletions include/pybind11/pybind11-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -1354,6 +1354,16 @@ PYBIND11_INLINE void generic_type::initialize(const type_record &rec) {
tinfo->simple_ancestors = parent_simple_ancestors;
// The parent can no longer be a simple type if it has MI and has a child
parent_tinfo->simple_type = parent_tinfo->simple_type && parent_simple_ancestors;
// A base given as a Python handle has no registered C++ cast, and the load of
// this type as a non-simple parent needs one. The handle form already requires
// the two pointers to be equal.
bool has_cast = false;
for (const auto &cast : parent_tinfo->implicit_casts) {
has_cast = has_cast || same_type(*cast.first, *rec.type);
}
if (!has_cast) {
parent_tinfo->implicit_casts.emplace_back(rec.type, [](void *src) { return src; });
}
}

with_internals([&](internals &internals) {
Expand Down
5 changes: 4 additions & 1 deletion include/pybind11/pybind11.h
Original file line number Diff line number Diff line change
Expand Up @@ -1406,7 +1406,10 @@ class class_ : public detail::generic_type {
// so the reinterpret_cast shortcut in load_impl Case 2a is invalid.
// Force the MI path (implicit_casts) for correct pointer adjustment.
// Detection: static_cast<Derived*>(Base*) is ill-formed for virtual bases.
if PYBIND11_MAYBE_CONSTEXPR (!detail::is_static_downcastable<Base, type>::value) {
// The vptr of a polymorphic class also offsets a non-polymorphic base.
if PYBIND11_MAYBE_CONSTEXPR (!detail::is_static_downcastable<Base, type>::value
|| (std::is_polymorphic<type>::value
&& !std::is_polymorphic<Base>::value)) {
rec.multiple_inheritance = true;
}
}
Expand Down
33 changes: 33 additions & 0 deletions tests/test_multiple_inheritance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,20 @@ struct I801B2 {
struct I801C : I801B1, I801B2 {};
struct I801D : I801C {}; // Indirect MI

// Issue #1080: the vptr of a polymorphic derived class offsets a non-polymorphic base
struct I1080Base {
unsigned int a = 1;
unsigned int d = 4;
};
struct I1080Plain : I1080Base {};
struct I1080Derived : I1080Base {
I1080Derived() = default;
I1080Derived(const I1080Derived &) = default;
virtual ~I1080Derived() = default;
};
struct I1080Child : I1080Derived {};
struct I1080HandleChild : I1080Derived {};

} // namespace

TEST_SUBMODULE(multiple_inheritance, m) {
Expand Down Expand Up @@ -350,4 +364,23 @@ TEST_SUBMODULE(multiple_inheritance, m) {
.def("get_f_e", &MVF::get_f_e)
.def("get_f_f", &MVF::get_f_f)
.def_readwrite("f", &MVF::f);

py::class_<I1080Base, std::shared_ptr<I1080Base>> base(m, "I1080Base");
base.def(py::init<>()).def_readwrite("a", &I1080Base::a).def_readwrite("d", &I1080Base::d);
py::class_<I1080Plain, std::shared_ptr<I1080Plain>>(m, "I1080Plain", py::handle(base))
.def(py::init<>());
py::class_<I1080Derived, I1080Base, std::shared_ptr<I1080Derived>> derived(m, "I1080Derived");
derived.def(py::init<>());
py::class_<I1080Child, I1080Derived, std::shared_ptr<I1080Child>>(m, "I1080Child")
.def(py::init<>());
py::class_<I1080HandleChild, std::shared_ptr<I1080HandleChild>>(
m, "I1080HandleChild", py::handle(derived))
.def(py::init<>());
m.def("i1080_get_d", [](const I1080Base &b) { return b.d; });
m.def("i1080_get_d_shared", [](const std::shared_ptr<I1080Base> &b) { return b->d; });
m.def("i1080_derived_get_d", [](const I1080Derived &b) { return b.d; });
m.def(
"i1080_as_base",
[](I1080Derived &d) -> I1080Base * { return &d; },
py::return_value_policy::reference);
}
36 changes: 36 additions & 0 deletions tests/test_multiple_inheritance.py
Original file line number Diff line number Diff line change
Expand Up @@ -515,3 +515,39 @@ def get_g_g(self):
assert o.g == 7

assert o.get_g_g() == 7


def test_polymorphic_derived_nonpolymorphic_base():
"""Issue #1080: the base subobject is not at offset 0"""

class PyDerived(m.I1080Derived):
pass

for cls in (m.I1080Derived, PyDerived, m.I1080Child, m.I1080HandleChild):
o = cls()
assert o.a == 1
assert o.d == 4
assert m.i1080_get_d(o) == 4
o.d = 7
assert m.i1080_get_d(o) == 7
assert m.i1080_get_d_shared(o) == 7
assert m.i1080_derived_get_d(o) == 7
assert m.i1080_as_base(o).d == 7


def test_handle_base_with_polymorphic_sibling():
o = m.I1080Plain()
assert o.d == 4
assert m.i1080_get_d(o) == 4


# PyPy: "instance layout conflicts" when a second base has a bound C++ parent
@pytest.mark.xfail("env.PYPY", raises=TypeError)
@pytest.mark.parametrize("cpp_base", [m.I1080Plain, m.I1080Derived])
def test_polymorphic_siblings_with_python_mi(cpp_base):
class PyMulti(m.I801B1, cpp_base):
def __init__(self):
m.I801B1.__init__(self)
cpp_base.__init__(self)

assert m.i1080_get_d(PyMulti()) == 4
Loading