Skip to content

Commit

Permalink
Content of PR pybind#4374 applied on top of smart_holder branch.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ralf W. Grosse-Kunstleve committed Dec 2, 2022
1 parent 8720cf9 commit 62dc3b3
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ set(PYBIND11_TEST_FILES
test_kwargs_and_defaults
test_local_bindings
test_methods_and_attributes
test_mi_debug
test_modules
test_multiple_inheritance
test_numpy_array
Expand Down
53 changes: 53 additions & 0 deletions tests/test_mi_debug.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include <pybind11/pybind11.h>

#include "pybind11_tests.h"

#include <iostream>
#include <memory>
#include <vector>

// The first base class.
struct Base0 {
virtual ~Base0() = default;
};

using Base0Ptr = std::shared_ptr<Base0>;

// The second base class.
struct Base1 {
virtual ~Base1() = default;
std::vector<int> vec = {1, 2, 3, 4, 5};
};

using Base1Ptr = std::shared_ptr<Base1>;

// The derived class.
struct Derived : Base1, Base0 {
~Derived() override = default;
};

using DerivedPtr = std::shared_ptr<Derived>;

TEST_SUBMODULE(mi_debug, m) {
// Expose the bases.
pybind11::class_<Base0, Base0Ptr> bs0(m, "Base0");
pybind11::class_<Base1, Base1Ptr> bs1(m, "Base1");
// Expose the derived class.
pybind11::class_<Derived, DerivedPtr, Base0, Base1>(m, "Derived").def(pybind11::init<>());

// A helper that returns a pointer to base.
m.def("make_object", []() -> Base0Ptr {
auto ret_der = std::make_shared<Derived>();
std::cout << "ret der ptr: " << ret_der.get() << std::endl;
auto ret = Base0Ptr(ret_der);
std::cout << "ret base ptr: " << ret.get() << std::endl;
return ret;
});

// A helper that accepts in input a pointer to derived.
m.def("get_object_vec_size", [](const DerivedPtr &object) {
std::cout << "der ptr: " << object.get() << std::endl;
std::cout << object->vec.size() << std::endl;
return object->vec.size();
});
}
8 changes: 8 additions & 0 deletions tests/test_mi_debug.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import pytest

m = pytest.importorskip("pybind11_tests.mi_debug")


def test_vec():
o = m.make_object()
assert 5 == m.get_object_vec_size(o)

0 comments on commit 62dc3b3

Please sign in to comment.