From 90e8914e02b4ff011d81775a9433712628987012 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Thu, 2 Nov 2023 17:03:20 -0700 Subject: [PATCH] Add CppDrvd2, test_PPPCCC (needs more work). --- tests/test_python_multiple_inheritance.cpp | 19 +++++++++++++++++++ tests/test_python_multiple_inheritance.py | 14 ++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/tests/test_python_multiple_inheritance.cpp b/tests/test_python_multiple_inheritance.cpp index 68991715851..664a2c23a0f 100644 --- a/tests/test_python_multiple_inheritance.cpp +++ b/tests/test_python_multiple_inheritance.cpp @@ -26,6 +26,18 @@ struct CppDrvd : CppBase { int drvd_value; }; +struct CppDrvd2 : CppBase { + explicit CppDrvd2(int value) : CppBase(value), drvd2_value(value * 5) {} + int get_drvd2_value() const { return drvd2_value; } + void reset_drvd2_value(int new_value) { drvd2_value = new_value; } + + int get_base_value_from_drvd2() const { return get_base_value(); } + void reset_base_value_from_drvd2(int new_value) { reset_base_value(new_value); } + +private: + int drvd2_value; +}; + } // namespace test_python_multiple_inheritance TEST_SUBMODULE(python_multiple_inheritance, m) { @@ -42,4 +54,11 @@ 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); + + py::class_(m, "CppDrvd2") + .def(py::init()) + .def("get_drvd2_value", &CppDrvd2::get_drvd2_value) + .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); } diff --git a/tests/test_python_multiple_inheritance.py b/tests/test_python_multiple_inheritance.py index 3bddd67dfb8..920daf0be41 100644 --- a/tests/test_python_multiple_inheritance.py +++ b/tests/test_python_multiple_inheritance.py @@ -1,6 +1,8 @@ # Adapted from: # https://github.com/google/clif/blob/5718e4d0807fd3b6a8187dde140069120b81ecef/clif/testing/python/python_multiple_inheritance_test.py +import pytest + from pybind11_tests import python_multiple_inheritance as m @@ -12,6 +14,10 @@ class PPCC(PC, m.CppDrvd): pass +class PPPCCC(PPCC, m.CppDrvd2): + pass + + def test_PC(): d = PC(11) assert d.get_base_value() == 11 @@ -33,3 +39,11 @@ 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_PPPCCC(): + with pytest.raises( + TypeError, + match=r"CppDrvd2\.__init__\(\) must be called when overriding __init__$", + ): + PPPCCC(11)