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 files for testing MI issue. #4374

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@ set(PYBIND11_TEST_FILES
test_tagbased_polymorphic
test_thread
test_union
test_virtual_functions)
test_virtual_functions
test_mi_debug)

# Invoking cmake with something like:
# cmake -DPYBIND11_TEST_OVERRIDE="test_callbacks.cpp;test_pickling.cpp" ..
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)