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

Add test_python_multiple_inheritance (Not For Merging) #248

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 2 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ nanobind_add_module(test_ndarray_ext test_ndarray.cpp ${NB_EXTRA_ARGS})
nanobind_add_module(test_intrusive_ext test_intrusive.cpp object.cpp object.h ${NB_EXTRA_ARGS})
nanobind_add_module(test_exception_ext test_exception.cpp ${NB_EXTRA_ARGS})
nanobind_add_module(test_make_iterator_ext test_make_iterator.cpp ${NB_EXTRA_ARGS})
nanobind_add_module(test_python_multiple_inheritance_ext test_python_multiple_inheritance.cpp ${NB_EXTRA_ARGS})

find_package (Eigen3 3.3.1 NO_MODULE)
if (TARGET Eigen3::Eigen)
Expand Down Expand Up @@ -67,6 +68,7 @@ set(TEST_FILES
test_inter_module.py
test_intrusive.py
test_make_iterator.py
test_python_multiple_inheritance.py
test_stl.py
test_stl_bind_map.py
test_stl_bind_vector.py
Expand Down
46 changes: 46 additions & 0 deletions tests/test_python_multiple_inheritance.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <nanobind/nanobind.h>

namespace test_python_multiple_inheritance {

// Copied from:
// https://github.com/google/clif/blob/5718e4d0807fd3b6a8187dde140069120b81ecef/clif/testing/python_multiple_inheritance.h

struct CppBase {
explicit CppBase(int value) : base_value(value) {}
int get_base_value() const { return base_value; }
void reset_base_value(int new_value) { base_value = new_value; }

private:
int base_value;
};

struct CppDrvd : CppBase {
explicit CppDrvd(int value) : CppBase(value), drvd_value(value * 3) {}
int get_drvd_value() const { return drvd_value; }
void reset_drvd_value(int new_value) { drvd_value = new_value; }

int get_base_value_from_drvd() const { return get_base_value(); }
void reset_base_value_from_drvd(int new_value) { reset_base_value(new_value); }

private:
int drvd_value;
};

} // namespace test_python_multiple_inheritance

NB_MODULE(test_python_multiple_inheritance_ext, m) {
using namespace test_python_multiple_inheritance;
namespace nb = nanobind;

nb::class_<CppBase>(m, "CppBase")
.def(nb::init<int>())
.def("get_base_value", &CppBase::get_base_value)
.def("reset_base_value", &CppBase::reset_base_value);

nb::class_<CppDrvd, CppBase>(m, "CppDrvd")
.def(nb::init<int>())
.def("get_drvd_value", &CppDrvd::get_drvd_value)
.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);
}
22 changes: 22 additions & 0 deletions tests/test_python_multiple_inheritance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Adapted from:
# https://github.com/google/clif/blob/5718e4d0807fd3b6a8187dde140069120b81ecef/clif/testing/python/python_multiple_inheritance_test.py
# See also:
# https://github.com/pybind/pybind11/pull/4762

import test_python_multiple_inheritance_ext as m


class PC(m.CppBase):
pass


# RuntimeError: nb_type_init(): invalid number of bases!
# class PPCC(PC, m.CppDrvd):
# pass


def test_PC():
d = PC(11)
assert d.get_base_value() == 11
d.reset_base_value(13)
assert d.get_base_value() == 13