mirror of
https://github.com/pybind/pybind11.git
synced 2024-11-22 13:15:12 +00:00
Adding test_pure_cpp_sft_raw_ptr (with 3 TODO: Fix), test_pure_cpp_sft_shd_ptr (works as desired).
This commit is contained in:
parent
4a15ed742c
commit
ccd16a1a14
@ -12,7 +12,7 @@ namespace {
|
||||
|
||||
struct Sft : std::enable_shared_from_this<Sft> {
|
||||
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<Sft> &obj) {
|
||||
|
||||
void pass_unique_ptr(const std::unique_ptr<Sft> &) {}
|
||||
|
||||
Sft *make_pure_cpp_sft_raw_ptr(const std::string &history_seed) { return new Sft{history_seed}; }
|
||||
|
||||
std::shared_ptr<Sft> make_pure_cpp_sft_shd_ptr(const std::string &history_seed) {
|
||||
return std::make_shared<Sft>(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);
|
||||
}
|
||||
|
@ -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"
|
||||
|
Loading…
Reference in New Issue
Block a user