Skip to content

Commit

Permalink
Experiment: specify indirect bases
Browse files Browse the repository at this point in the history
  • Loading branch information
Ralf W. Grosse-Kunstleve committed Jul 31, 2023
1 parent a2f95e1 commit 4f90d85
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
16 changes: 16 additions & 0 deletions tests/test_python_multiple_inheritance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ struct CppDrvd : CppBase {
int drvd_value;
};

struct CppDrv2 : CppDrvd {
explicit CppDrv2(int value) : CppDrvd(value), drv2_value(value * 5) {}
int get_drv2_value() const { return drv2_value; }

private:
int drv2_value;
};

} // namespace test_python_multiple_inheritance

TEST_SUBMODULE(python_multiple_inheritance, m) {
Expand All @@ -42,4 +50,12 @@ TEST_SUBMODULE(python_multiple_inheritance, m) {
.def("reset_drvd_value", &CppDrvd::reset_drvd_value)
.def("get_base_value_from_drvd", &CppDrvd::get_base_value_from_drvd)
.def("reset_base_value_from_drvd", &CppDrvd::reset_base_value_from_drvd);

#if 1 // This works.
py::class_<CppDrv2, CppDrvd>(m, "CppDrv2")
#else // Apparent undefined behavior.
py::class_<CppDrv2, CppDrvd, CppBase>(m, "CppDrv2")
#endif
.def(py::init<int>())
.def("get_drv2_value", &CppDrv2::get_drv2_value);
}
11 changes: 11 additions & 0 deletions tests/test_python_multiple_inheritance.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ class PPCC(PC, m.CppDrvd):
pass


class PPCC2(PC, m.CppDrv2):
pass


def test_PC():
d = PC(11)
assert d.get_base_value() == 11
Expand All @@ -33,3 +37,10 @@ def test_PPCC():
d.reset_base_value_from_drvd(30)
assert d.get_base_value() == 30
assert d.get_base_value_from_drvd() == 30


def test_PPCC2():
d = PPCC2(13)
assert d.get_drv2_value() == 65
assert d.get_drvd_value() == 39
assert d.get_base_value() == 13

0 comments on commit 4f90d85

Please sign in to comment.