diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 3a7fbffad..a4df196f9 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -106,6 +106,7 @@ set(PYBIND11_TEST_FILES test_class_sh_disowning_mi.cpp test_class_sh_factory_constructors.cpp test_class_sh_inheritance.cpp + test_class_sh_shared_ptr_copy.cpp test_class_sh_trampoline_basic.cpp test_class_sh_trampoline_self_life_support.cpp test_class_sh_trampoline_shared_ptr_cpp_arg.cpp diff --git a/tests/test_class_sh_shared_ptr_copy.cpp b/tests/test_class_sh_shared_ptr_copy.cpp new file mode 100644 index 000000000..ce9a2a504 --- /dev/null +++ b/tests/test_class_sh_shared_ptr_copy.cpp @@ -0,0 +1,51 @@ +#include "pybind11_tests.h" + +#include + +#include +#include +#include + +namespace pybind11_tests { +namespace class_sh_shared_ptr_copy { + +template +struct Foo { + std::string mtxt; + Foo() : mtxt("DefaultConstructor") {} + Foo(const std::string &mtxt_) : mtxt(mtxt_) {} + Foo(const Foo &other) { mtxt = other.mtxt + "_CpCtor"; } + Foo(Foo &&other) { mtxt = other.mtxt + "_MvCtor"; } +}; + +using FooAVL = Foo<0>; +using FooDEF = Foo<1>; + +} // namespace class_sh_shared_ptr_copy +} // namespace pybind11_tests + +PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_shared_ptr_copy::FooAVL) + +namespace pybind11_tests { +namespace class_sh_shared_ptr_copy { + +TEST_SUBMODULE(class_sh_shared_ptr_copy, m) { + namespace py = pybind11; + + py::class_(m, "FooAVL"); + py::class_>(m, "FooDEF"); + + m.def("test_avl", []() { + auto o = std::make_shared("AVL"); + auto l = py::list(); + l.append(o); + }); + m.def("test_def", []() { + auto o = std::make_shared("DEF"); + auto l = py::list(); + l.append(o); + }); +} + +} // namespace class_sh_shared_ptr_copy +} // namespace pybind11_tests diff --git a/tests/test_class_sh_shared_ptr_copy.py b/tests/test_class_sh_shared_ptr_copy.py new file mode 100644 index 000000000..6e724c27b --- /dev/null +++ b/tests/test_class_sh_shared_ptr_copy.py @@ -0,0 +1,11 @@ +# -*- coding: utf-8 -*- + +from pybind11_tests import class_sh_shared_ptr_copy as m + + +def test_avl(): + m.test_avl() + + +def test_def(): + m.test_def() diff --git a/tests/test_smart_ptr.cpp b/tests/test_smart_ptr.cpp index a97682cec..c378d1fe0 100644 --- a/tests/test_smart_ptr.cpp +++ b/tests/test_smart_ptr.cpp @@ -98,16 +98,6 @@ private: int value; }; -// an uncopyable object managed by a std::shared_ptr<> -class MyObject3a { -public: - MyObject3a(int value) : value(value) { print_created(this, toString()); } - std::string toString() const { return "MyObject3a[" + std::to_string(value) + "]"; } - virtual ~MyObject3a() { print_destroyed(this); } -private: - int value; -}; - // test_unique_nodelete // Object with a private destructor class MyObject4; @@ -367,15 +357,6 @@ TEST_SUBMODULE(smart_ptr, m) { m.def("print_myobject3_3", [](const std::shared_ptr &obj) { py::print(obj->toString()); }); m.def("print_myobject3_4", [](const std::shared_ptr *obj) { py::print((*obj)->toString()); }); - py::class_(m, "MyObject3a"); - m.def("make_myobject3_1", []() { return new MyObject3a(8); }); - m.def("make_myobject3_2", []() { return std::make_shared(9); }); - m.def("print_myobject3a_1", [](const MyObject3a *obj) { py::print(obj->toString()); }); - m.def("print_myobject3a_2", [](std::shared_ptr obj) { py::print(obj->toString()); }); - m.def("print_myobject3a_3", [](const std::shared_ptr &obj) { py::print(obj->toString()); }); - // this doesn't compile, should it? - //m.def("print_myobject3a_4", [](const std::shared_ptr *obj) { py::print((*obj)->toString()); }); - // test_smart_ptr_refcounting m.def("test_object1_refcounting", []() { ref o = new MyObject1(0); @@ -499,10 +480,4 @@ TEST_SUBMODULE(smart_ptr, m) { list.append(py::cast(e)); return list; }); - - m.def("test_3011_shared_ptr", []() { - auto o = std::make_shared(42); - auto l = py::list(); - l.append(o); - }); } diff --git a/tests/test_smart_ptr.py b/tests/test_smart_ptr.py index b25ed5aa6..d4e8e2e06 100644 --- a/tests/test_smart_ptr.py +++ b/tests/test_smart_ptr.py @@ -316,7 +316,3 @@ def test_shared_ptr_gc(): pytest.gc_collect() for i, v in enumerate(el.get()): assert i == v.value() - - -def test_3011_shared_ptr(): - m.test_3011_shared_ptr() \ No newline at end of file