From ef126b64256a150412fe46fdc70ecfc3c1b85ec4 Mon Sep 17 00:00:00 2001 From: Francesco Biscani Date: Thu, 1 Dec 2022 10:49:01 +0100 Subject: [PATCH 1/5] Add files for testin MI issue. --- tests/CMakeLists.txt | 3 ++- tests/test_mi_debug.cpp | 59 +++++++++++++++++++++++++++++++++++++++++ tests/test_mi_debug.py | 0 3 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 tests/test_mi_debug.cpp create mode 100644 tests/test_mi_debug.py diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 491f215cef..b678fd7bf5 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -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" .. diff --git a/tests/test_mi_debug.cpp b/tests/test_mi_debug.cpp new file mode 100644 index 0000000000..c7aded44ba --- /dev/null +++ b/tests/test_mi_debug.cpp @@ -0,0 +1,59 @@ +#include +#include +#include + +#include + +// The first base class. +struct Base0 +{ + virtual ~Base0() { } +}; + +using Base0Ptr = std::shared_ptr< Base0 >; + +// The second base class. +struct Base1 +{ + virtual ~Base1() { } + std::vector< int > vec = { 1, 2, 3, 4, 5 }; +}; + +using Base1Ptr = std::shared_ptr< Base1 >; + +// The derived class. +struct Derived : Base1, Base0 +{ + virtual ~Derived() { } +}; + +using DerivedPtr = std::shared_ptr< Derived >; + +PYBIND11_MODULE(example, 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(); + } ); +} + diff --git a/tests/test_mi_debug.py b/tests/test_mi_debug.py new file mode 100644 index 0000000000..e69de29bb2 From 86d955196b113fe7aacc103ebdf9434a166c6ca8 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 1 Dec 2022 09:50:54 +0000 Subject: [PATCH 2/5] style: pre-commit fixes --- tests/test_mi_debug.cpp | 64 ++++++++++++++++++----------------------- 1 file changed, 28 insertions(+), 36 deletions(-) diff --git a/tests/test_mi_debug.cpp b/tests/test_mi_debug.cpp index c7aded44ba..d0eaa0d9e1 100644 --- a/tests/test_mi_debug.cpp +++ b/tests/test_mi_debug.cpp @@ -1,59 +1,51 @@ +#include + +#include #include #include -#include - -#include // The first base class. -struct Base0 -{ - virtual ~Base0() { } +struct Base0 { + virtual ~Base0() {} }; -using Base0Ptr = std::shared_ptr< Base0 >; +using Base0Ptr = std::shared_ptr; // The second base class. -struct Base1 -{ - virtual ~Base1() { } - std::vector< int > vec = { 1, 2, 3, 4, 5 }; +struct Base1 { + virtual ~Base1() {} + std::vector vec = {1, 2, 3, 4, 5}; }; -using Base1Ptr = std::shared_ptr< Base1 >; +using Base1Ptr = std::shared_ptr; // The derived class. -struct Derived : Base1, Base0 -{ - virtual ~Derived() { } +struct Derived : Base1, Base0 { + virtual ~Derived() {} }; -using DerivedPtr = std::shared_ptr< Derived >; +using DerivedPtr = std::shared_ptr; PYBIND11_MODULE(example, m) { // Expose the bases. - pybind11::class_< Base0, Base0Ptr > bs0( m, "Base0" ); - pybind11::class_< Base1, Base1Ptr > bs1( m, "Base1" ); + pybind11::class_ bs0(m, "Base0"); + pybind11::class_ bs1(m, "Base1"); // Expose the derived class. - pybind11::class_< Derived, DerivedPtr, Base0, Base1 >( m, "Derived" ).def( pybind11::init<>() ); + pybind11::class_(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; - } ); + m.def("make_object", []() -> Base0Ptr { + auto ret_der = std::make_shared(); + 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(); - } ); + 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(); + }); } - From c8741c10230164e05e3b3b930dcb4cc0eeb14f3b Mon Sep 17 00:00:00 2001 From: Francesco Biscani Date: Thu, 1 Dec 2022 11:57:16 +0100 Subject: [PATCH 3/5] clang-tidy fixes. --- tests/test_mi_debug.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_mi_debug.cpp b/tests/test_mi_debug.cpp index d0eaa0d9e1..21ad19a113 100644 --- a/tests/test_mi_debug.cpp +++ b/tests/test_mi_debug.cpp @@ -6,14 +6,14 @@ // The first base class. struct Base0 { - virtual ~Base0() {} + virtual ~Base0() = default; }; using Base0Ptr = std::shared_ptr; // The second base class. struct Base1 { - virtual ~Base1() {} + virtual ~Base1() = default; std::vector vec = {1, 2, 3, 4, 5}; }; @@ -21,7 +21,7 @@ using Base1Ptr = std::shared_ptr; // The derived class. struct Derived : Base1, Base0 { - virtual ~Derived() {} + ~Derived() override = default; }; using DerivedPtr = std::shared_ptr; From 138b3fb434455e337121993ad32bdf777fc9d1cc Mon Sep 17 00:00:00 2001 From: Francesco Biscani Date: Thu, 1 Dec 2022 12:04:43 +0100 Subject: [PATCH 4/5] Initial attempt at adding test. --- tests/test_mi_debug.cpp | 4 +++- tests/test_mi_debug.py | 9 +++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/tests/test_mi_debug.cpp b/tests/test_mi_debug.cpp index 21ad19a113..c475314ca3 100644 --- a/tests/test_mi_debug.cpp +++ b/tests/test_mi_debug.cpp @@ -1,5 +1,7 @@ #include +#include "pybind11_tests.h" + #include #include #include @@ -26,7 +28,7 @@ struct Derived : Base1, Base0 { using DerivedPtr = std::shared_ptr; -PYBIND11_MODULE(example, m) { +TEST_SUBMODULE(mi_debug, m) { // Expose the bases. pybind11::class_ bs0(m, "Base0"); pybind11::class_ bs1(m, "Base1"); diff --git a/tests/test_mi_debug.py b/tests/test_mi_debug.py index e69de29bb2..63f9d8634b 100644 --- a/tests/test_mi_debug.py +++ b/tests/test_mi_debug.py @@ -0,0 +1,9 @@ +import pytest + +m = pytest.importorskip("pybind11_tests.mi_debug") + + + +def test_vec(): + o = m.make_object() + assert 5 == m.get_object_vec_size(o) From 5dc8ce2691c7cae53f313aa51928b7539342c6d5 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 1 Dec 2022 11:07:20 +0000 Subject: [PATCH 5/5] style: pre-commit fixes --- tests/test_mi_debug.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/test_mi_debug.py b/tests/test_mi_debug.py index 63f9d8634b..d4acc87653 100644 --- a/tests/test_mi_debug.py +++ b/tests/test_mi_debug.py @@ -3,7 +3,6 @@ m = pytest.importorskip("pybind11_tests.mi_debug") - def test_vec(): o = m.make_object() assert 5 == m.get_object_vec_size(o)