Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding tests to exercise disowning combined with multiple inheritance. #2917

Merged
merged 1 commit into from
Mar 23, 2021
Merged
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
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ set(PYBIND11_TEST_FILES
test_class.cpp
test_class_sh_basic.cpp
test_class_sh_disowning.cpp
test_class_sh_disowning_mi.cpp
test_class_sh_factory_constructors.cpp
test_class_sh_inheritance.cpp
test_class_sh_trampoline_shared_ptr_cpp_arg.cpp
Expand Down
70 changes: 70 additions & 0 deletions tests/test_class_sh_disowning_mi.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include "pybind11_tests.h"

#include <pybind11/smart_holder.h>

#include <memory>

namespace pybind11_tests {
namespace class_sh_disowning_mi {

// Diamond inheritance (copied from test_multiple_inheritance.cpp).
struct B {
int val_b = 10;
B() = default;
B(const B &) = default;
virtual ~B() = default;
};

struct C0 : public virtual B {
int val_c0 = 20;
};

struct C1 : public virtual B {
int val_c1 = 21;
};

struct D : public C0, public C1 {
int val_d = 30;
};

void disown(std::unique_ptr<B>) {}

} // namespace class_sh_disowning_mi
} // namespace pybind11_tests

PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_disowning_mi::B)
PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_disowning_mi::C0)
PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_disowning_mi::C1)
PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_disowning_mi::D)

TEST_SUBMODULE(class_sh_disowning_mi, m) {
using namespace pybind11_tests::class_sh_disowning_mi;

py::classh<B>(m, "B")
.def(py::init<>())
.def_readonly("val_b", &D::val_b)
.def("b", [](B *self) { return self; })
.def("get", [](const B &self) { return self.val_b; });

py::classh<C0, B>(m, "C0")
.def(py::init<>())
.def_readonly("val_c0", &D::val_c0)
.def("c0", [](C0 *self) { return self; })
.def("get", [](const C0 &self) { return self.val_b * 100 + self.val_c0; });

py::classh<C1, B>(m, "C1")
.def(py::init<>())
.def_readonly("val_c1", &D::val_c1)
.def("c1", [](C1 *self) { return self; })
.def("get", [](const C1 &self) { return self.val_b * 100 + self.val_c1; });

py::classh<D, C0, C1>(m, "D")
.def(py::init<>())
.def_readonly("val_d", &D::val_d)
.def("d", [](D *self) { return self; })
.def("get", [](const D &self) {
return self.val_b * 1000000 + self.val_c0 * 10000 + self.val_c1 * 100 + self.val_d;
});

m.def("disown", disown);
}
65 changes: 65 additions & 0 deletions tests/test_class_sh_disowning_mi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# -*- coding: utf-8 -*-

from pybind11_tests import class_sh_disowning_mi as m


def test_diamond_inheritance():
# Very similar to test_multiple_inheritance.py:test_diamond_inheritance.
d = m.D()
assert d is d.d()
assert d is d.c0()
assert d is d.c1()
assert d is d.b()
assert d is d.c0().b()
assert d is d.c1().b()
assert d is d.c0().c1().b().c0().b()


def was_disowned(obj):
try:
obj.get()
except ValueError as e:
assert (
str(e)
== "Missing value for wrapped C++ type: Python instance was disowned."
)
return True
return False


def test_disown_b():
b = m.B()
assert b.get() == 10
m.disown(b)
assert was_disowned(b)


def test_disown_c0():
c0 = m.C0()
assert c0.get() == 1020
b = c0.b()
m.disown(c0)
assert was_disowned(c0)
assert was_disowned(b)


def test_disown_c1():
c1 = m.C1()
assert c1.get() == 1021
b = c1.b()
m.disown(c1)
assert was_disowned(c1)
assert was_disowned(b)


def test_disown_d():
d = m.D()
assert d.get() == 10202130
b = d.b()
c0 = d.c0()
c1 = d.c1()
m.disown(d)
assert was_disowned(d)
assert was_disowned(c1)
assert was_disowned(c0)
assert was_disowned(b)