From ccd16a1a149d57237060337fd296163165331616 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Wed, 23 Jun 2021 16:35:27 -0700 Subject: [PATCH] Adding test_pure_cpp_sft_raw_ptr (with 3 TODO: Fix), test_pure_cpp_sft_shd_ptr (works as desired). --- ...t_class_sh_trampoline_shared_from_this.cpp | 10 ++++++- ...st_class_sh_trampoline_shared_from_this.py | 26 +++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/tests/test_class_sh_trampoline_shared_from_this.cpp b/tests/test_class_sh_trampoline_shared_from_this.cpp index 4977dd3f9..49dfb036c 100644 --- a/tests/test_class_sh_trampoline_shared_from_this.cpp +++ b/tests/test_class_sh_trampoline_shared_from_this.cpp @@ -12,7 +12,7 @@ namespace { struct Sft : std::enable_shared_from_this { std::string history; - explicit Sft(const std::string &history) : history{history} {} + explicit Sft(const std::string &history_seed) : history{history_seed} {} virtual ~Sft() = default; #if defined(__clang__) @@ -87,6 +87,12 @@ long pass_shared_ptr(const std::shared_ptr &obj) { void pass_unique_ptr(const std::unique_ptr &) {} +Sft *make_pure_cpp_sft_raw_ptr(const std::string &history_seed) { return new Sft{history_seed}; } + +std::shared_ptr make_pure_cpp_sft_shd_ptr(const std::string &history_seed) { + return std::make_shared(history_seed); +} + } // namespace PYBIND11_SMART_HOLDER_TYPE_CASTERS(Sft) @@ -108,5 +114,7 @@ TEST_SUBMODULE(class_sh_trampoline_shared_from_this, m) { m.def("use_count", use_count); m.def("pass_shared_ptr", pass_shared_ptr); m.def("pass_unique_ptr", pass_unique_ptr); + m.def("make_pure_cpp_sft_raw_ptr", make_pure_cpp_sft_raw_ptr); + m.def("make_pure_cpp_sft_shd_ptr", make_pure_cpp_sft_shd_ptr); m.def("to_cout", to_cout); } diff --git a/tests/test_class_sh_trampoline_shared_from_this.py b/tests/test_class_sh_trampoline_shared_from_this.py index f240a1505..7a72a7677 100644 --- a/tests/test_class_sh_trampoline_shared_from_this.py +++ b/tests/test_class_sh_trampoline_shared_from_this.py @@ -128,3 +128,29 @@ def test_pass_released_shared_ptr_as_unique_ptr(): assert str(exc_info.value) == ( "Python instance is currently owned by a std::shared_ptr." ) + + +def test_pure_cpp_sft_raw_ptr(): + obj = m.make_pure_cpp_sft_raw_ptr("PureCppSft") + with pytest.raises(RuntimeError) as exc_info: + assert m.pass_shared_ptr(obj) == 3 # TODO: FIX. + assert str(exc_info.value) == "bad_weak_ptr" + obj = m.make_pure_cpp_sft_raw_ptr("PureCppSft") + stash1 = m.SftSharedPtrStash(1) + with pytest.raises(RuntimeError) as exc_info: + stash1.AddSharedFromThis(obj) # TODO: FIX. + assert str(exc_info.value) == "bad_weak_ptr" + stash1.Add(obj) + with pytest.raises(RuntimeError) as exc_info: + stash1.AddSharedFromThis(obj) # TODO: FIX. + assert str(exc_info.value) == "bad_weak_ptr" + + +def test_pure_cpp_sft_shd_ptr(): + obj = m.make_pure_cpp_sft_shd_ptr("PureCppSft") + assert m.pass_shared_ptr(obj) == 3 + assert obj.history == "PureCppSft_PassSharedPtr" + obj = m.make_pure_cpp_sft_shd_ptr("PureCppSft") + stash1 = m.SftSharedPtrStash(1) + stash1.AddSharedFromThis(obj) + assert obj.history == "PureCppSft_Stash1AddSharedFromThis"