Skip to content

Commit 222b4a7

Browse files
committed
Minimal (!) changes to have py::potentially_slicing_weak_ptr<T>(handle) as the public API. For CI testing, before changing the names around more widely, and the documentation.
1 parent faf1e61 commit 222b4a7

File tree

3 files changed

+14
-11
lines changed

3 files changed

+14
-11
lines changed

include/pybind11/cast.h

+5-5
Original file line numberDiff line numberDiff line change
@@ -983,7 +983,7 @@ struct copyable_holder_caster<
983983
return shared_ptr_storage;
984984
}
985985

986-
std::shared_ptr<type> &potentially_slicing_shared_ptr() {
986+
std::weak_ptr<type> potentially_slicing_weak_ptr() {
987987
if (typeinfo->holder_enum_v == detail::holder_enum_t::smart_holder) {
988988
shared_ptr_storage
989989
= sh_load_helper.load_as_shared_ptr(value,
@@ -1122,15 +1122,15 @@ PYBIND11_NAMESPACE_END(detail)
11221122
/// - the std::shared_ptr would own a reference to the derived Python object,
11231123
/// completing the cycle
11241124
template <typename T>
1125-
std::shared_ptr<T> potentially_slicing_shared_ptr(handle obj) {
1125+
std::weak_ptr<T> potentially_slicing_weak_ptr(handle obj) {
11261126
detail::make_caster<std::shared_ptr<T>> caster;
11271127
if (caster.load(obj, /*convert=*/true)) {
1128-
return caster.potentially_slicing_shared_ptr();
1128+
return caster.potentially_slicing_weak_ptr();
11291129
}
11301130
const char *obj_type_name = detail::obj_class_name(obj.ptr());
11311131
throw type_error("\"" + std::string(obj_type_name)
1132-
+ "\" object is not convertible to std::shared_ptr<T> (with T = "
1133-
+ type_id<T>() + ")");
1132+
+ "\" object is not convertible to std::weak_ptr<T> (with T = " + type_id<T>()
1133+
+ ")");
11341134
}
11351135

11361136
PYBIND11_NAMESPACE_BEGIN(detail)

tests/test_potentially_slicing_shared_ptr.cpp

+7-4
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ std::shared_ptr<VB> rtrn_obj_cast_shared_ptr(py::handle obj) {
7171

7272
template <typename VB>
7373
std::shared_ptr<VB> rtrn_potentially_slicing_shared_ptr(py::handle obj) {
74-
return py::potentially_slicing_shared_ptr<VB>(obj);
74+
return py::potentially_slicing_weak_ptr<VB>(obj).lock();
7575
}
7676

7777
template <typename VB>
@@ -93,7 +93,7 @@ struct SpOwner {
9393

9494
template <typename VB>
9595
struct WpOwner {
96-
void set_wp(const std::shared_ptr<VB> &sp) { wp = sp; }
96+
void set_wp(const std::weak_ptr<VB> &wp_) { wp = wp_; }
9797

9898
int get_code() const {
9999
auto sp = wp.lock();
@@ -126,10 +126,13 @@ void wrap(py::module_ &m,
126126

127127
py::classh<WpOwner<VB>>(m, wpo_pyname)
128128
.def(py::init<>())
129-
.def("set_wp", &WpOwner<VB>::set_wp)
129+
.def("set_wp",
130+
[](WpOwner<VB> &self, py::handle obj) {
131+
self.set_wp(obj.cast<std::shared_ptr<VB>>());
132+
})
130133
.def("set_wp_potentially_slicing",
131134
[](WpOwner<VB> &self, py::handle obj) {
132-
self.set_wp(py::potentially_slicing_shared_ptr<VB>(obj));
135+
self.set_wp(py::potentially_slicing_weak_ptr<VB>(obj));
133136
})
134137
.def("get_code", &WpOwner<VB>::get_code)
135138
.def("get_trampoline_state", &WpOwner<VB>::get_trampoline_state);

tests/test_potentially_slicing_shared_ptr.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,12 @@ def test_potentially_slicing_shared_ptr_not_convertible_error():
163163
with pytest.raises(Exception) as excinfo:
164164
m.SH_rtrn_potentially_slicing_shared_ptr("")
165165
assert str(excinfo.value) == (
166-
'"str" object is not convertible to std::shared_ptr<T>'
166+
'"str" object is not convertible to std::weak_ptr<T>'
167167
" (with T = pybind11_tests::potentially_slicing_shared_ptr::VirtBase<0>)"
168168
)
169169
with pytest.raises(Exception) as excinfo:
170170
m.SP_rtrn_potentially_slicing_shared_ptr([])
171171
assert str(excinfo.value) == (
172-
'"list" object is not convertible to std::shared_ptr<T>'
172+
'"list" object is not convertible to std::weak_ptr<T>'
173173
" (with T = pybind11_tests::potentially_slicing_shared_ptr::VirtBase<1>)"
174174
)

0 commit comments

Comments
 (0)