From effa01cff29531c3cddcad143a88ac9d82a2c53d Mon Sep 17 00:00:00 2001 From: Jakob Lykke Andersen Date: Wed, 16 Jun 2021 14:30:48 +0200 Subject: [PATCH 01/17] Initial test --- tests/test_smart_ptr.cpp | 25 +++++++++++++++++++++++++ tests/test_smart_ptr.py | 4 ++++ 2 files changed, 29 insertions(+) diff --git a/tests/test_smart_ptr.cpp b/tests/test_smart_ptr.cpp index c378d1fe0d..a97682cecd 100644 --- a/tests/test_smart_ptr.cpp +++ b/tests/test_smart_ptr.cpp @@ -98,6 +98,16 @@ class MyObject3 : public std::enable_shared_from_this { 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; @@ -357,6 +367,15 @@ 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); @@ -480,4 +499,10 @@ 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 d4e8e2e063..b25ed5aa65 100644 --- a/tests/test_smart_ptr.py +++ b/tests/test_smart_ptr.py @@ -316,3 +316,7 @@ 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 From 76610aea46c758fe2fb2ec1a48118f89c7cb5591 Mon Sep 17 00:00:00 2001 From: Jakob Lykke Andersen Date: Fri, 18 Jun 2021 21:44:20 +0200 Subject: [PATCH 02/17] SH, update shared_ptr copy tests --- tests/CMakeLists.txt | 1 + tests/test_class_sh_shared_ptr_copy.cpp | 51 +++++++++++++++++++++++++ tests/test_class_sh_shared_ptr_copy.py | 11 ++++++ tests/test_smart_ptr.cpp | 25 ------------ tests/test_smart_ptr.py | 4 -- 5 files changed, 63 insertions(+), 29 deletions(-) create mode 100644 tests/test_class_sh_shared_ptr_copy.cpp create mode 100644 tests/test_class_sh_shared_ptr_copy.py diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 3a7fbffad2..a4df196f9e 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 0000000000..ce9a2a504b --- /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 0000000000..6e724c27be --- /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 a97682cecd..c378d1fe0d 100644 --- a/tests/test_smart_ptr.cpp +++ b/tests/test_smart_ptr.cpp @@ -98,16 +98,6 @@ class MyObject3 : public std::enable_shared_from_this { 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 b25ed5aa65..d4e8e2e063 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 From 20a9495e967e34fc124e05bd7b9c1cead2475860 Mon Sep 17 00:00:00 2001 From: Jakob Lykke Andersen Date: Sat, 19 Jun 2021 10:45:32 +0200 Subject: [PATCH 03/17] SH, test update --- tests/test_class_sh_shared_ptr_copy.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_class_sh_shared_ptr_copy.cpp b/tests/test_class_sh_shared_ptr_copy.cpp index ce9a2a504b..094b123128 100644 --- a/tests/test_class_sh_shared_ptr_copy.cpp +++ b/tests/test_class_sh_shared_ptr_copy.cpp @@ -33,7 +33,7 @@ TEST_SUBMODULE(class_sh_shared_ptr_copy, m) { namespace py = pybind11; py::class_(m, "FooAVL"); - py::class_>(m, "FooDEF"); + py::class_(m, "FooDEF"); m.def("test_avl", []() { auto o = std::make_shared("AVL"); From 4fc34e2eca852f24d98890adf0add3671c9704e6 Mon Sep 17 00:00:00 2001 From: Jakob Lykke Andersen Date: Wed, 16 Jun 2021 15:42:51 +0200 Subject: [PATCH 04/17] Allow copy policy in smart holder caster for shared_ptr --- include/pybind11/detail/smart_holder_type_casters.h | 1 + 1 file changed, 1 insertion(+) diff --git a/include/pybind11/detail/smart_holder_type_casters.h b/include/pybind11/detail/smart_holder_type_casters.h index 653332bf83..d8a97334a4 100644 --- a/include/pybind11/detail/smart_holder_type_casters.h +++ b/include/pybind11/detail/smart_holder_type_casters.h @@ -625,6 +625,7 @@ struct smart_holder_type_caster> : smart_holder_type_caster_l static handle cast(const std::shared_ptr &src, return_value_policy policy, handle parent) { if (policy != return_value_policy::automatic && policy != return_value_policy::reference_internal + && policy != return_value_policy::copy && policy != return_value_policy::automatic_reference) { // SMART_HOLDER_WIP: IMPROVABLE: Error message. throw cast_error("Invalid return_value_policy for shared_ptr."); From a977560e7223afce7ff52ec24669840bb8ce96c7 Mon Sep 17 00:00:00 2001 From: Jakob Lykke Andersen Date: Sat, 19 Jun 2021 16:35:20 +0200 Subject: [PATCH 05/17] SH, test for shared_ptr move --- tests/CMakeLists.txt | 2 +- tests/test_class_sh_shared_ptr_copy.py | 11 -------- ...=> test_class_sh_shared_ptr_copy_move.cpp} | 27 +++++++++++++------ tests/test_class_sh_shared_ptr_copy_move.py | 19 +++++++++++++ 4 files changed, 39 insertions(+), 20 deletions(-) delete mode 100644 tests/test_class_sh_shared_ptr_copy.py rename tests/{test_class_sh_shared_ptr_copy.cpp => test_class_sh_shared_ptr_copy_move.cpp} (61%) create mode 100644 tests/test_class_sh_shared_ptr_copy_move.py diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index a4df196f9e..0c850ac9ee 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -106,7 +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_shared_ptr_copy_move.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.py b/tests/test_class_sh_shared_ptr_copy.py deleted file mode 100644 index 6e724c27be..0000000000 --- a/tests/test_class_sh_shared_ptr_copy.py +++ /dev/null @@ -1,11 +0,0 @@ -# -*- 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_class_sh_shared_ptr_copy.cpp b/tests/test_class_sh_shared_ptr_copy_move.cpp similarity index 61% rename from tests/test_class_sh_shared_ptr_copy.cpp rename to tests/test_class_sh_shared_ptr_copy_move.cpp index 094b123128..1cecc6a094 100644 --- a/tests/test_class_sh_shared_ptr_copy.cpp +++ b/tests/test_class_sh_shared_ptr_copy_move.cpp @@ -7,7 +7,7 @@ #include namespace pybind11_tests { -namespace class_sh_shared_ptr_copy { +namespace class_sh_shared_ptr_copy_move { template struct Foo { @@ -21,31 +21,42 @@ struct Foo { using FooAVL = Foo<0>; using FooDEF = Foo<1>; -} // namespace class_sh_shared_ptr_copy +} // namespace class_sh_shared_ptr_copy_move } // namespace pybind11_tests -PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_shared_ptr_copy::FooAVL) +PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_shared_ptr_copy_move::FooAVL) namespace pybind11_tests { -namespace class_sh_shared_ptr_copy { +namespace class_sh_shared_ptr_copy_move { -TEST_SUBMODULE(class_sh_shared_ptr_copy, m) { +TEST_SUBMODULE(class_sh_shared_ptr_copy_move, m) { namespace py = pybind11; py::class_(m, "FooAVL"); py::class_(m, "FooDEF"); - m.def("test_avl", []() { + m.def("test_avl_copy", []() { auto o = std::make_shared("AVL"); auto l = py::list(); l.append(o); }); - m.def("test_def", []() { + m.def("test_def_copy", []() { auto o = std::make_shared("DEF"); auto l = py::list(); l.append(o); }); + + m.def("test_avl_move", []() { + auto o = std::make_shared("AVL"); + auto l = py::list(); + l.append(std::move(o)); + }); + m.def("test_def_move", []() { + auto o = std::make_shared("DEF"); + auto l = py::list(); + l.append(std::move(o)); + }); } -} // namespace class_sh_shared_ptr_copy +} // namespace class_sh_shared_ptr_copy_move } // namespace pybind11_tests diff --git a/tests/test_class_sh_shared_ptr_copy_move.py b/tests/test_class_sh_shared_ptr_copy_move.py new file mode 100644 index 0000000000..4263013e81 --- /dev/null +++ b/tests/test_class_sh_shared_ptr_copy_move.py @@ -0,0 +1,19 @@ +# -*- coding: utf-8 -*- + +from pybind11_tests import class_sh_shared_ptr_copy_move as m + + +def test_avl_copy(): + m.test_avl_copy() + + +def test_def_copy(): + m.test_def_copy() + + +def test_avl_move(): + m.test_avl_move() + + +def test_def_move(): + m.test_def_move() From 2a6c9b5b6fa615cc2e370977c5b2e38a037946ff Mon Sep 17 00:00:00 2001 From: Jakob Lykke Andersen Date: Sat, 19 Jun 2021 16:40:51 +0200 Subject: [PATCH 06/17] Allow move policy in smart holder caster for shared_ptr --- include/pybind11/detail/smart_holder_type_casters.h | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/include/pybind11/detail/smart_holder_type_casters.h b/include/pybind11/detail/smart_holder_type_casters.h index d8a97334a4..fd6c25ed27 100644 --- a/include/pybind11/detail/smart_holder_type_casters.h +++ b/include/pybind11/detail/smart_holder_type_casters.h @@ -624,9 +624,12 @@ struct smart_holder_type_caster> : smart_holder_type_caster_l static handle cast(const std::shared_ptr &src, return_value_policy policy, handle parent) { if (policy != return_value_policy::automatic - && policy != return_value_policy::reference_internal + && policy != return_value_policy::automatic_reference + // (but not take_ownership) && policy != return_value_policy::copy - && policy != return_value_policy::automatic_reference) { + && policy != return_value_policy::move + // (but not reference) + && policy != return_value_policy::reference_internal) { // SMART_HOLDER_WIP: IMPROVABLE: Error message. throw cast_error("Invalid return_value_policy for shared_ptr."); } From d9691f0130efb805307cd03507f42e43dcccc7bf Mon Sep 17 00:00:00 2001 From: Jakob Lykke Andersen Date: Sat, 19 Jun 2021 16:43:12 +0200 Subject: [PATCH 07/17] SH, improve error message from shared_ptr cast policy check --- .../detail/smart_holder_type_casters.h | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/include/pybind11/detail/smart_holder_type_casters.h b/include/pybind11/detail/smart_holder_type_casters.h index fd6c25ed27..fccb945e00 100644 --- a/include/pybind11/detail/smart_holder_type_casters.h +++ b/include/pybind11/detail/smart_holder_type_casters.h @@ -623,15 +623,16 @@ struct smart_holder_type_caster> : smart_holder_type_caster_l static constexpr auto name = _>(); static handle cast(const std::shared_ptr &src, return_value_policy policy, handle parent) { - if (policy != return_value_policy::automatic - && policy != return_value_policy::automatic_reference - // (but not take_ownership) - && policy != return_value_policy::copy - && policy != return_value_policy::move - // (but not reference) - && policy != return_value_policy::reference_internal) { - // SMART_HOLDER_WIP: IMPROVABLE: Error message. - throw cast_error("Invalid return_value_policy for shared_ptr."); + switch (policy) { + case return_value_policy::automatic: break; + case return_value_policy::automatic_reference: break; + case return_value_policy::take_ownership: + throw cast_error("Invalid return_value_policy for shared_ptr (take_ownership)."); + case return_value_policy::copy: break; + case return_value_policy::move: break; + case return_value_policy::reference: + throw cast_error("Invalid return_value_policy for shared_ptr (reference)."); + case return_value_policy::reference_internal: break; } if (!src) return none().release(); From 49878d20b64954fbc1a4797800ae79e60cc73bcc Mon Sep 17 00:00:00 2001 From: Jakob Lykke Andersen Date: Sun, 20 Jun 2021 09:44:25 +0200 Subject: [PATCH 08/17] SH, shared_ptr copy/move, update after review --- tests/test_class_sh_shared_ptr_copy_move.cpp | 68 ++++++++++++-------- tests/test_class_sh_shared_ptr_copy_move.py | 20 +++--- 2 files changed, 53 insertions(+), 35 deletions(-) diff --git a/tests/test_class_sh_shared_ptr_copy_move.cpp b/tests/test_class_sh_shared_ptr_copy_move.cpp index 1cecc6a094..3c6872cecc 100644 --- a/tests/test_class_sh_shared_ptr_copy_move.cpp +++ b/tests/test_class_sh_shared_ptr_copy_move.cpp @@ -7,56 +7,70 @@ #include namespace pybind11_tests { -namespace class_sh_shared_ptr_copy_move { +namespace { -template +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"; } + Foo(const Foo &other) = delete; + Foo(Foo &&other) = delete; + std::string get_text() const { + std::string res = "Foo"; + if (SerNo == 0) + res += "ShPtr_"; + else if (SerNo == 1) + res += "SmHld_"; + return res + mtxt; + } }; -using FooAVL = Foo<0>; -using FooDEF = Foo<1>; +using FooShPtr = Foo<0>; +using FooSmHld = Foo<1>; -} // namespace class_sh_shared_ptr_copy_move +} // namespace } // namespace pybind11_tests -PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_shared_ptr_copy_move::FooAVL) +PYBIND11_TYPE_CASTER_BASE_HOLDER(pybind11_tests::FooShPtr) +PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::FooSmHld) namespace pybind11_tests { -namespace class_sh_shared_ptr_copy_move { +namespace { TEST_SUBMODULE(class_sh_shared_ptr_copy_move, m) { namespace py = pybind11; - py::class_(m, "FooAVL"); - py::class_(m, "FooDEF"); + py::class_>(m, "FooShPtr") + .def("get_text", &FooShPtr::get_text); + py::classh(m, "FooSmHld") + .def("get_text", &FooSmHld::get_text); - m.def("test_avl_copy", []() { - auto o = std::make_shared("AVL"); + m.def("test_ShPtr_copy", []() { + auto o = std::make_shared("copy"); auto l = py::list(); l.append(o); + return l; }); - m.def("test_def_copy", []() { - auto o = std::make_shared("DEF"); - auto l = py::list(); - l.append(o); + m.def("test_SmHld_copy", []() { + auto o = std::make_shared("copy"); + auto l = py::list(); + l.append(o); + return l; }); - m.def("test_avl_move", []() { - auto o = std::make_shared("AVL"); - auto l = py::list(); - l.append(std::move(o)); + m.def("test_ShPtr_move", []() { + auto o = std::make_shared("move"); + auto l = py::list(); + l.append(std::move(o)); + return l; }); - m.def("test_def_move", []() { - auto o = std::make_shared("DEF"); - auto l = py::list(); - l.append(std::move(o)); + m.def("test_SmHld_move", []() { + auto o = std::make_shared("move"); + auto l = py::list(); + l.append(std::move(o)); + return l; }); } -} // namespace class_sh_shared_ptr_copy_move +} // namespace } // namespace pybind11_tests diff --git a/tests/test_class_sh_shared_ptr_copy_move.py b/tests/test_class_sh_shared_ptr_copy_move.py index 4263013e81..9f38134344 100644 --- a/tests/test_class_sh_shared_ptr_copy_move.py +++ b/tests/test_class_sh_shared_ptr_copy_move.py @@ -3,17 +3,21 @@ from pybind11_tests import class_sh_shared_ptr_copy_move as m -def test_avl_copy(): - m.test_avl_copy() +def test_shptr_copy(): + txt = m.test_ShPtr_copy()[0].get_text() + assert txt == "FooShPtr_copy" -def test_def_copy(): - m.test_def_copy() +def test_smhld_copy(): + txt = m.test_SmHld_copy()[0].get_text() + assert txt == "FooSmHld_copy" -def test_avl_move(): - m.test_avl_move() +def test_shptr_move(): + txt = m.test_ShPtr_move()[0].get_text() + assert txt == "FooShPtr_move" -def test_def_move(): - m.test_def_move() +def test_smhld_move(): + txt = m.test_SmHld_move()[0].get_text() + assert txt == "FooSmHld_move" From 17033613705f89dfb56f8bb5a41143bce71f7349 Mon Sep 17 00:00:00 2001 From: Jakob Lykke Andersen Date: Thu, 24 Jun 2021 11:50:31 +0200 Subject: [PATCH 09/17] SH, shared_ptr copy/move, make MSVC happy --- tests/test_class_sh_shared_ptr_copy_move.cpp | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/tests/test_class_sh_shared_ptr_copy_move.cpp b/tests/test_class_sh_shared_ptr_copy_move.cpp index 3c6872cecc..a0b9fde5be 100644 --- a/tests/test_class_sh_shared_ptr_copy_move.cpp +++ b/tests/test_class_sh_shared_ptr_copy_move.cpp @@ -9,20 +9,15 @@ namespace pybind11_tests { namespace { +const std::string fooNames[] = {"ShPtr_", "SmHld_"}; + template struct Foo { std::string mtxt; Foo(const std::string &mtxt_) : mtxt(mtxt_) {} Foo(const Foo &other) = delete; - Foo(Foo &&other) = delete; - std::string get_text() const { - std::string res = "Foo"; - if (SerNo == 0) - res += "ShPtr_"; - else if (SerNo == 1) - res += "SmHld_"; - return res + mtxt; - } + Foo(Foo &&other) = delete; + std::string get_text() const { return "Foo" + fooNames[SerNo] + mtxt; } }; using FooShPtr = Foo<0>; From 1a5865408821b4e6c5f56d2bba251ba7c83037c9 Mon Sep 17 00:00:00 2001 From: Jakob Lykke Andersen Date: Thu, 24 Jun 2021 11:54:20 +0200 Subject: [PATCH 10/17] SH, shared_ptr copy/move, rename to 'history' --- tests/test_class_sh_shared_ptr_copy_move.cpp | 10 +++++----- tests/test_class_sh_shared_ptr_copy_move.py | 8 ++++---- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/tests/test_class_sh_shared_ptr_copy_move.cpp b/tests/test_class_sh_shared_ptr_copy_move.cpp index a0b9fde5be..ded0a40d78 100644 --- a/tests/test_class_sh_shared_ptr_copy_move.cpp +++ b/tests/test_class_sh_shared_ptr_copy_move.cpp @@ -13,11 +13,11 @@ const std::string fooNames[] = {"ShPtr_", "SmHld_"}; template struct Foo { - std::string mtxt; - Foo(const std::string &mtxt_) : mtxt(mtxt_) {} + std::string hisotry; + Foo(const std::string &hisotry_) : hisotry(hisotry_) {} Foo(const Foo &other) = delete; Foo(Foo &&other) = delete; - std::string get_text() const { return "Foo" + fooNames[SerNo] + mtxt; } + std::string get_history() const { return "Foo" + fooNames[SerNo] + hisotry; } }; using FooShPtr = Foo<0>; @@ -36,9 +36,9 @@ TEST_SUBMODULE(class_sh_shared_ptr_copy_move, m) { namespace py = pybind11; py::class_>(m, "FooShPtr") - .def("get_text", &FooShPtr::get_text); + .def("get_history", &FooShPtr::get_history); py::classh(m, "FooSmHld") - .def("get_text", &FooSmHld::get_text); + .def("get_history", &FooSmHld::get_history); m.def("test_ShPtr_copy", []() { auto o = std::make_shared("copy"); diff --git a/tests/test_class_sh_shared_ptr_copy_move.py b/tests/test_class_sh_shared_ptr_copy_move.py index 9f38134344..81c5b96019 100644 --- a/tests/test_class_sh_shared_ptr_copy_move.py +++ b/tests/test_class_sh_shared_ptr_copy_move.py @@ -4,20 +4,20 @@ def test_shptr_copy(): - txt = m.test_ShPtr_copy()[0].get_text() + txt = m.test_ShPtr_copy()[0].get_history() assert txt == "FooShPtr_copy" def test_smhld_copy(): - txt = m.test_SmHld_copy()[0].get_text() + txt = m.test_SmHld_copy()[0].get_history() assert txt == "FooSmHld_copy" def test_shptr_move(): - txt = m.test_ShPtr_move()[0].get_text() + txt = m.test_ShPtr_move()[0].get_history() assert txt == "FooShPtr_move" def test_smhld_move(): - txt = m.test_SmHld_move()[0].get_text() + txt = m.test_SmHld_move()[0].get_history() assert txt == "FooSmHld_move" From a6fa6d1f10caf6c23c353d4bd55c32b51642fbd4 Mon Sep 17 00:00:00 2001 From: Jakob Lykke Andersen Date: Thu, 24 Jun 2021 14:00:00 +0200 Subject: [PATCH 11/17] SH, fix typo --- tests/test_class_sh_shared_ptr_copy_move.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_class_sh_shared_ptr_copy_move.cpp b/tests/test_class_sh_shared_ptr_copy_move.cpp index ded0a40d78..5f2644fc86 100644 --- a/tests/test_class_sh_shared_ptr_copy_move.cpp +++ b/tests/test_class_sh_shared_ptr_copy_move.cpp @@ -13,11 +13,11 @@ const std::string fooNames[] = {"ShPtr_", "SmHld_"}; template struct Foo { - std::string hisotry; - Foo(const std::string &hisotry_) : hisotry(hisotry_) {} + std::string history; + Foo(const std::string &history_) : history(history_) {} Foo(const Foo &other) = delete; Foo(Foo &&other) = delete; - std::string get_history() const { return "Foo" + fooNames[SerNo] + hisotry; } + std::string get_history() const { return "Foo" + fooNames[SerNo] + history; } }; using FooShPtr = Foo<0>; From cb7cee8304d18e1a6f0a229e27e5130caeaa01f1 Mon Sep 17 00:00:00 2001 From: Jakob Lykke Andersen Date: Thu, 24 Jun 2021 14:02:29 +0200 Subject: [PATCH 12/17] SH, fix use of PYBIND11_TYPE_CASTER_BASE_HOLDER --- tests/test_class_sh_shared_ptr_copy_move.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_class_sh_shared_ptr_copy_move.cpp b/tests/test_class_sh_shared_ptr_copy_move.cpp index 5f2644fc86..7e12396513 100644 --- a/tests/test_class_sh_shared_ptr_copy_move.cpp +++ b/tests/test_class_sh_shared_ptr_copy_move.cpp @@ -26,7 +26,7 @@ using FooSmHld = Foo<1>; } // namespace } // namespace pybind11_tests -PYBIND11_TYPE_CASTER_BASE_HOLDER(pybind11_tests::FooShPtr) +PYBIND11_TYPE_CASTER_BASE_HOLDER(pybind11_tests::FooShPtr, std::shared_ptr) PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::FooSmHld) namespace pybind11_tests { From 53a04e878f8c12c284dbc77e02e04f8cfd98e829 Mon Sep 17 00:00:00 2001 From: Jakob Lykke Andersen Date: Thu, 24 Jun 2021 14:13:28 +0200 Subject: [PATCH 13/17] SH, history tracking --- tests/test_class_sh_shared_ptr_copy_move.cpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/tests/test_class_sh_shared_ptr_copy_move.cpp b/tests/test_class_sh_shared_ptr_copy_move.cpp index 7e12396513..69172efbf8 100644 --- a/tests/test_class_sh_shared_ptr_copy_move.cpp +++ b/tests/test_class_sh_shared_ptr_copy_move.cpp @@ -15,8 +15,16 @@ template struct Foo { std::string history; Foo(const std::string &history_) : history(history_) {} - Foo(const Foo &other) = delete; - Foo(Foo &&other) = delete; + Foo(const Foo &other) : history(other.history + "_CpCtor") {} + Foo(Foo &&other) : history(other.history + "_MvCtor") {} + Foo &operator=(const Foo &other) { + history = other.history + "_OpEqLv"; + return *this; + } + Foo &operator=(Foo &&other) { + history = other.history + "_OpEqRv"; + return *this; + } std::string get_history() const { return "Foo" + fooNames[SerNo] + history; } }; From 19ca146cd73ecbdd97f633286c76d1ad96254e19 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Thu, 24 Jun 2021 12:53:09 -0700 Subject: [PATCH 14/17] Automatic clang-tidy fixes. --- tests/test_class_sh_shared_ptr_copy_move.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_class_sh_shared_ptr_copy_move.cpp b/tests/test_class_sh_shared_ptr_copy_move.cpp index 69172efbf8..823cbc4bf7 100644 --- a/tests/test_class_sh_shared_ptr_copy_move.cpp +++ b/tests/test_class_sh_shared_ptr_copy_move.cpp @@ -16,12 +16,12 @@ struct Foo { std::string history; Foo(const std::string &history_) : history(history_) {} Foo(const Foo &other) : history(other.history + "_CpCtor") {} - Foo(Foo &&other) : history(other.history + "_MvCtor") {} + Foo(Foo &&other) noexcept : history(other.history + "_MvCtor") {} Foo &operator=(const Foo &other) { history = other.history + "_OpEqLv"; return *this; } - Foo &operator=(Foo &&other) { + Foo &operator=(Foo &&other) noexcept { history = other.history + "_OpEqRv"; return *this; } From 505179b80ac5aa9bd48c188f4a4423d3b71d1d9b Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Thu, 24 Jun 2021 12:53:58 -0700 Subject: [PATCH 15/17] Automatic clang-format. --- tests/test_class_sh_shared_ptr_copy_move.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_class_sh_shared_ptr_copy_move.cpp b/tests/test_class_sh_shared_ptr_copy_move.cpp index 823cbc4bf7..66770c41c3 100644 --- a/tests/test_class_sh_shared_ptr_copy_move.cpp +++ b/tests/test_class_sh_shared_ptr_copy_move.cpp @@ -34,7 +34,8 @@ using FooSmHld = Foo<1>; } // namespace } // namespace pybind11_tests -PYBIND11_TYPE_CASTER_BASE_HOLDER(pybind11_tests::FooShPtr, std::shared_ptr) +PYBIND11_TYPE_CASTER_BASE_HOLDER(pybind11_tests::FooShPtr, + std::shared_ptr) PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::FooSmHld) namespace pybind11_tests { @@ -45,8 +46,7 @@ TEST_SUBMODULE(class_sh_shared_ptr_copy_move, m) { py::class_>(m, "FooShPtr") .def("get_history", &FooShPtr::get_history); - py::classh(m, "FooSmHld") - .def("get_history", &FooSmHld::get_history); + py::classh(m, "FooSmHld").def("get_history", &FooSmHld::get_history); m.def("test_ShPtr_copy", []() { auto o = std::make_shared("copy"); From a84c18e124cb0d620a30036d3d9a32d6a48b25f2 Mon Sep 17 00:00:00 2001 From: Jakob Lykke Andersen Date: Tue, 29 Jun 2021 12:35:50 +0200 Subject: [PATCH 16/17] SH, attribute and property tests --- tests/test_class_sh_shared_ptr_copy_move.cpp | 35 ++++++++++++++++++++ tests/test_class_sh_shared_ptr_copy_move.py | 18 ++++++++++ 2 files changed, 53 insertions(+) diff --git a/tests/test_class_sh_shared_ptr_copy_move.cpp b/tests/test_class_sh_shared_ptr_copy_move.cpp index 66770c41c3..c46c2c7a9f 100644 --- a/tests/test_class_sh_shared_ptr_copy_move.cpp +++ b/tests/test_class_sh_shared_ptr_copy_move.cpp @@ -31,6 +31,15 @@ struct Foo { using FooShPtr = Foo<0>; using FooSmHld = Foo<1>; +struct Outer { + std::shared_ptr ShPtr; + std::shared_ptr SmHld; + Outer() + : ShPtr(std::make_shared("Outer")), SmHld(std::make_shared("Outer")) {} + std::shared_ptr getShPtr() const { return ShPtr; } + std::shared_ptr getSmHld() const { return SmHld; } +}; + } // namespace } // namespace pybind11_tests @@ -48,6 +57,32 @@ TEST_SUBMODULE(class_sh_shared_ptr_copy_move, m) { .def("get_history", &FooShPtr::get_history); py::classh(m, "FooSmHld").def("get_history", &FooSmHld::get_history); + auto outer = py::class_(m, "Outer").def(py::init()); +#define MAKE_PROP(PropTyp) \ + MAKE_PROP_FOO(ShPtr, PropTyp) \ + MAKE_PROP_FOO(SmHld, PropTyp) + +#define MAKE_PROP_FOO(FooTyp, PropTyp) \ + .def_##PropTyp(#FooTyp "_" #PropTyp "_default", &Outer::FooTyp) \ + .def_##PropTyp( \ + #FooTyp "_" #PropTyp "_copy", &Outer::FooTyp, py::return_value_policy::copy) \ + .def_##PropTyp( \ + #FooTyp "_" #PropTyp "_move", &Outer::FooTyp, py::return_value_policy::move) + outer MAKE_PROP(readonly) MAKE_PROP(readwrite); +#undef MAKE_PROP_FOO + +#define MAKE_PROP_FOO(FooTyp, PropTyp) \ + .def_##PropTyp(#FooTyp "_property_" #PropTyp "_default", &Outer::FooTyp) \ + .def_property_##PropTyp(#FooTyp "_property_" #PropTyp "_copy", \ + &Outer::get##FooTyp, \ + py::return_value_policy::copy) \ + .def_property_##PropTyp(#FooTyp "_property_" #PropTyp "_move", \ + &Outer::get##FooTyp, \ + py::return_value_policy::move) + outer MAKE_PROP(readonly); +#undef MAKE_PROP_FOO +#undef MAKE_PROP + m.def("test_ShPtr_copy", []() { auto o = std::make_shared("copy"); auto l = py::list(); diff --git a/tests/test_class_sh_shared_ptr_copy_move.py b/tests/test_class_sh_shared_ptr_copy_move.py index 81c5b96019..f9b4b15349 100644 --- a/tests/test_class_sh_shared_ptr_copy_move.py +++ b/tests/test_class_sh_shared_ptr_copy_move.py @@ -21,3 +21,21 @@ def test_shptr_move(): def test_smhld_move(): txt = m.test_SmHld_move()[0].get_history() assert txt == "FooSmHld_move" + + +def _check_property(foo_typ: str, prop_typ: str, policy: str): + o = m.Outer() + name = "{}_{}_{}".format(foo_typ, prop_typ, policy) + history = "Foo{}_Outer".format(foo_typ) + f = getattr(o, name) + assert f.get_history() == history + # and try again to check that o did not get changed + f = getattr(o, name) + assert f.get_history() == history + + +def test_properties(): + for prop_typ in ("readonly", "readwrite", "property_readonly"): + for foo_typ in ("ShPtr", "SmHld"): + for policy in ("default", "copy", "move"): + _check_property(foo_typ, prop_typ, policy) From 662e5157b44bd57978307481c3be2c85195d2632 Mon Sep 17 00:00:00 2001 From: Jakob Lykke Andersen Date: Tue, 29 Jun 2021 12:56:34 +0200 Subject: [PATCH 17/17] SM, remove type annotations in test --- tests/test_class_sh_shared_ptr_copy_move.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_class_sh_shared_ptr_copy_move.py b/tests/test_class_sh_shared_ptr_copy_move.py index f9b4b15349..4ea47c9a63 100644 --- a/tests/test_class_sh_shared_ptr_copy_move.py +++ b/tests/test_class_sh_shared_ptr_copy_move.py @@ -23,7 +23,7 @@ def test_smhld_move(): assert txt == "FooSmHld_move" -def _check_property(foo_typ: str, prop_typ: str, policy: str): +def _check_property(foo_typ, prop_typ, policy): o = m.Outer() name = "{}_{}_{}".format(foo_typ, prop_typ, policy) history = "Foo{}_Outer".format(foo_typ)