Skip to content

Commit 7679cd5

Browse files
committed
Content of PR pybind#4374 applied on top of smart_holder branch.
1 parent 33fb7a6 commit 7679cd5

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ set(PYBIND11_TEST_FILES
158158
test_kwargs_and_defaults
159159
test_local_bindings
160160
test_methods_and_attributes
161+
test_mi_debug
161162
test_modules
162163
test_multiple_inheritance
163164
test_numpy_array

tests/test_mi_debug.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include <pybind11/pybind11.h>
2+
3+
#include "pybind11_tests.h"
4+
5+
#include <iostream>
6+
#include <memory>
7+
#include <vector>
8+
9+
// The first base class.
10+
struct Base0 {
11+
virtual ~Base0() = default;
12+
};
13+
14+
using Base0Ptr = std::shared_ptr<Base0>;
15+
16+
// The second base class.
17+
struct Base1 {
18+
virtual ~Base1() = default;
19+
std::vector<int> vec = {1, 2, 3, 4, 5};
20+
};
21+
22+
using Base1Ptr = std::shared_ptr<Base1>;
23+
24+
// The derived class.
25+
struct Derived : Base1, Base0 {
26+
~Derived() override = default;
27+
};
28+
29+
using DerivedPtr = std::shared_ptr<Derived>;
30+
31+
TEST_SUBMODULE(mi_debug, m) {
32+
// Expose the bases.
33+
pybind11::class_<Base0, Base0Ptr> bs0(m, "Base0");
34+
pybind11::class_<Base1, Base1Ptr> bs1(m, "Base1");
35+
// Expose the derived class.
36+
pybind11::class_<Derived, DerivedPtr, Base0, Base1>(m, "Derived").def(pybind11::init<>());
37+
38+
// A helper that returns a pointer to base.
39+
m.def("make_object", []() -> Base0Ptr {
40+
auto ret_der = std::make_shared<Derived>();
41+
std::cout << "ret der ptr: " << ret_der.get() << std::endl;
42+
auto ret = Base0Ptr(ret_der);
43+
std::cout << "ret base ptr: " << ret.get() << std::endl;
44+
return ret;
45+
});
46+
47+
// A helper that accepts in input a pointer to derived.
48+
m.def("get_object_vec_size", [](const DerivedPtr &object) {
49+
std::cout << "der ptr: " << object.get() << std::endl;
50+
std::cout << object->vec.size() << std::endl;
51+
return object->vec.size();
52+
});
53+
}

tests/test_mi_debug.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import pytest
2+
3+
m = pytest.importorskip("pybind11_tests.mi_debug")
4+
5+
6+
def test_vec():
7+
o = m.make_object()
8+
assert 5 == m.get_object_vec_size(o)

0 commit comments

Comments
 (0)