Skip to content

Commit 56d23dc

Browse files
committed
test_potentially_slicing_shared_ptr.cpp,py (for smart_holder only)
1 parent 3b4b28c commit 56d23dc

6 files changed

+103
-237
lines changed

tests/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,9 @@ set(PYBIND11_TEST_FILES
130130
test_class_sh_trampoline_shared_from_this
131131
test_class_sh_trampoline_shared_ptr_cpp_arg
132132
test_class_sh_trampoline_unique_ptr
133-
test_class_sh_trampoline_weak_ptr
134133
test_class_sh_unique_ptr_custom_deleter
135134
test_class_sh_unique_ptr_member
136135
test_class_sh_virtual_py_cpp_mix
137-
test_class_sp_trampoline_weak_ptr
138136
test_const_name
139137
test_constants_and_functions
140138
test_copy_move
@@ -163,6 +161,7 @@ set(PYBIND11_TEST_FILES
163161
test_opaque_types
164162
test_operator_overloading
165163
test_pickling
164+
test_potentially_slicing_shared_ptr
166165
test_python_multiple_inheritance
167166
test_pytypes
168167
test_sequences_and_iterators

tests/test_class_sh_trampoline_weak_ptr.cpp

Lines changed: 0 additions & 66 deletions
This file was deleted.

tests/test_class_sh_trampoline_weak_ptr.py

Lines changed: 0 additions & 53 deletions
This file was deleted.

tests/test_class_sp_trampoline_weak_ptr.py

Lines changed: 0 additions & 86 deletions
This file was deleted.

tests/test_class_sp_trampoline_weak_ptr.cpp renamed to tests/test_potentially_slicing_shared_ptr.cpp

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include <memory>
88

99
namespace pybind11_tests {
10-
namespace class_sp_trampoline_weak_ptr {
10+
namespace potentially_slicing_shared_ptr {
1111

1212
struct VirtBase {
1313
virtual ~VirtBase() = default;
@@ -17,66 +17,69 @@ struct VirtBase {
1717
struct PyVirtBase : VirtBase, py::trampoline_self_life_support {
1818
using VirtBase::VirtBase;
1919
int get_code() override { PYBIND11_OVERRIDE(int, VirtBase, get_code); }
20-
21-
~PyVirtBase() override {
22-
fflush(stderr);
23-
printf("\nLOOOK ~PyVirtBase()\n");
24-
fflush(stdout);
25-
}
2620
};
2721

28-
struct WpOwner {
29-
void set_wp(const std::shared_ptr<VirtBase> &sp) { wp = sp; }
22+
std::shared_ptr<VirtBase> rtrn_obj_cast_shared_ptr(py::handle obj) {
23+
return obj.cast<std::shared_ptr<VirtBase>>();
24+
}
25+
26+
std::shared_ptr<VirtBase> rtrn_potentially_slicing_shared_ptr(py::handle obj) {
27+
return py::potentially_slicing_shared_ptr<VirtBase>(obj);
28+
}
29+
30+
struct SpOwner {
31+
void set_sp(const std::shared_ptr<VirtBase> &sp_) { sp = sp_; }
3032

3133
int get_code() {
32-
auto sp = wp.lock();
3334
if (!sp) {
34-
return -999;
35+
return -888;
3536
}
3637
return sp->get_code();
3738
}
3839

3940
private:
40-
std::weak_ptr<VirtBase> wp;
41+
std::shared_ptr<VirtBase> sp;
4142
};
4243

43-
struct SpOwner {
44-
void set_sp(const std::shared_ptr<VirtBase> &sp_) { sp = sp_; }
44+
struct WpOwner {
45+
void set_wp(const std::shared_ptr<VirtBase> &sp) { wp = sp; }
4546

4647
int get_code() {
48+
auto sp = wp.lock();
4749
if (!sp) {
48-
return -888;
50+
return -999;
4951
}
5052
return sp->get_code();
5153
}
5254

5355
private:
54-
std::shared_ptr<VirtBase> sp;
56+
std::weak_ptr<VirtBase> wp;
5557
};
5658

57-
std::shared_ptr<VirtBase> pass_through_sp_VirtBase(const std::shared_ptr<VirtBase> &sp) {
58-
return sp;
59-
}
60-
61-
} // namespace class_sp_trampoline_weak_ptr
59+
} // namespace potentially_slicing_shared_ptr
6260
} // namespace pybind11_tests
6361

64-
using namespace pybind11_tests::class_sp_trampoline_weak_ptr;
62+
using namespace pybind11_tests::potentially_slicing_shared_ptr;
6563

66-
TEST_SUBMODULE(class_sp_trampoline_weak_ptr, m) {
67-
py::class_<VirtBase, std::shared_ptr<VirtBase>, PyVirtBase>(m, "VirtBase")
64+
TEST_SUBMODULE(potentially_slicing_shared_ptr, m) {
65+
py::classh<VirtBase, PyVirtBase>(m, "VirtBase")
6866
.def(py::init<>())
6967
.def("get_code", &VirtBase::get_code);
7068

71-
py::class_<WpOwner>(m, "WpOwner")
72-
.def(py::init<>())
73-
.def("set_wp", &WpOwner::set_wp)
74-
.def("get_code", &WpOwner::get_code);
69+
m.def("rtrn_obj_cast_shared_ptr", rtrn_obj_cast_shared_ptr);
70+
m.def("rtrn_potentially_slicing_shared_ptr", rtrn_potentially_slicing_shared_ptr);
7571

76-
py::class_<SpOwner>(m, "SpOwner")
72+
py::classh<SpOwner>(m, "SpOwner")
7773
.def(py::init<>())
7874
.def("set_sp", &SpOwner::set_sp)
7975
.def("get_code", &SpOwner::get_code);
8076

81-
m.def("pass_through_sp_VirtBase", pass_through_sp_VirtBase);
77+
py::classh<WpOwner>(m, "WpOwner")
78+
.def(py::init<>())
79+
.def("set_wp", &WpOwner::set_wp)
80+
.def("set_wp_potentially_slicing",
81+
[](WpOwner &self, py::handle obj) {
82+
self.set_wp(py::potentially_slicing_shared_ptr<VirtBase>(obj));
83+
})
84+
.def("get_code", &WpOwner::get_code);
8285
}

0 commit comments

Comments
 (0)