File tree 3 files changed +14
-11
lines changed
3 files changed +14
-11
lines changed Original file line number Diff line number Diff line change @@ -983,7 +983,7 @@ struct copyable_holder_caster<
983
983
return shared_ptr_storage;
984
984
}
985
985
986
- std::shared_ptr <type> & potentially_slicing_shared_ptr () {
986
+ std::weak_ptr <type> potentially_slicing_weak_ptr () {
987
987
if (typeinfo->holder_enum_v == detail::holder_enum_t ::smart_holder) {
988
988
shared_ptr_storage
989
989
= sh_load_helper.load_as_shared_ptr (value,
@@ -1122,15 +1122,15 @@ PYBIND11_NAMESPACE_END(detail)
1122
1122
// / - the std::shared_ptr would own a reference to the derived Python object,
1123
1123
// / completing the cycle
1124
1124
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) {
1126
1126
detail::make_caster<std::shared_ptr<T>> caster;
1127
1127
if (caster.load (obj, /* convert=*/ true )) {
1128
- return caster.potentially_slicing_shared_ptr ();
1128
+ return caster.potentially_slicing_weak_ptr ();
1129
1129
}
1130
1130
const char *obj_type_name = detail::obj_class_name (obj.ptr ());
1131
1131
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
+ + " )" );
1134
1134
}
1135
1135
1136
1136
PYBIND11_NAMESPACE_BEGIN (detail)
Original file line number Diff line number Diff line change @@ -71,7 +71,7 @@ std::shared_ptr<VB> rtrn_obj_cast_shared_ptr(py::handle obj) {
71
71
72
72
template <typename VB>
73
73
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 ( );
75
75
}
76
76
77
77
template <typename VB>
@@ -93,7 +93,7 @@ struct SpOwner {
93
93
94
94
template <typename VB>
95
95
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_ ; }
97
97
98
98
int get_code () const {
99
99
auto sp = wp.lock ();
@@ -126,10 +126,13 @@ void wrap(py::module_ &m,
126
126
127
127
py::classh<WpOwner<VB>>(m, wpo_pyname)
128
128
.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
+ })
130
133
.def (" set_wp_potentially_slicing" ,
131
134
[](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));
133
136
})
134
137
.def (" get_code" , &WpOwner<VB>::get_code)
135
138
.def (" get_trampoline_state" , &WpOwner<VB>::get_trampoline_state);
Original file line number Diff line number Diff line change @@ -163,12 +163,12 @@ def test_potentially_slicing_shared_ptr_not_convertible_error():
163
163
with pytest .raises (Exception ) as excinfo :
164
164
m .SH_rtrn_potentially_slicing_shared_ptr ("" )
165
165
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>'
167
167
" (with T = pybind11_tests::potentially_slicing_shared_ptr::VirtBase<0>)"
168
168
)
169
169
with pytest .raises (Exception ) as excinfo :
170
170
m .SP_rtrn_potentially_slicing_shared_ptr ([])
171
171
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>'
173
173
" (with T = pybind11_tests::potentially_slicing_shared_ptr::VirtBase<1>)"
174
174
)
You can’t perform that action at this time.
0 commit comments