Skip to content

Commit 37d0bf4

Browse files
authored
[smart_holder] Enable properties for non-owning holders (#4586)
* Add test_class_sh_property_non_owning.cpp,py Failing: ``` __________________________________________________________ test_persistent_holder __________________________________________________________ def test_persistent_holder(): h = m.DataFieldsHolder(2) > c = h.vec_at(0).core_fld E RuntimeError: Non-owning holder (loaded_as_shared_ptr). h = <pybind11_tests.class_sh_property_non_owning.DataFieldsHolder object at 0x7fabab516470> test_class_sh_property_non_owning.py:6: RuntimeError __________________________________________________________ test_temporary_holder ___________________________________________________________ def test_temporary_holder(): d = m.DataFieldsHolder(2).vec_at(1) > c = d.core_fld E RuntimeError: Non-owning holder (loaded_as_shared_ptr). d = <pybind11_tests.class_sh_property_non_owning.DataField object at 0x7fabab548770> test_class_sh_property_non_owning.py:13: RuntimeError ``` * Introduce `shared_ptr_from_python(responsible_parent)` and use in all `property_cpp_function`s with `const shared_ptr<T> &` arguments. Tests are incomplete. * Complete tests. * Add comment for `smart_holder_type_caster_load<T>::shared_ptr_from_python`
1 parent 945be5b commit 37d0bf4

File tree

4 files changed

+144
-6
lines changed

4 files changed

+144
-6
lines changed

include/pybind11/detail/smart_holder_type_casters.h

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,18 @@ struct smart_holder_type_caster_class_hooks : smart_holder_type_caster_base_tag
408408
}
409409
};
410410

411+
struct shared_ptr_parent_life_support {
412+
PyObject *parent;
413+
explicit shared_ptr_parent_life_support(PyObject *parent) : parent{parent} {
414+
Py_INCREF(parent);
415+
}
416+
// NOLINTNEXTLINE(readability-make-member-function-const)
417+
void operator()(void *) {
418+
gil_scoped_acquire gil;
419+
Py_DECREF(parent);
420+
}
421+
};
422+
411423
struct shared_ptr_trampoline_self_life_support {
412424
PyObject *self;
413425
explicit shared_ptr_trampoline_self_life_support(instance *inst)
@@ -462,12 +474,23 @@ struct smart_holder_type_caster_load {
462474
return *raw_ptr;
463475
}
464476

465-
std::shared_ptr<T> loaded_as_shared_ptr() const {
477+
std::shared_ptr<T> make_shared_ptr_with_responsible_parent(handle parent) const {
478+
return std::shared_ptr<T>(loaded_as_raw_ptr_unowned(),
479+
shared_ptr_parent_life_support(parent.ptr()));
480+
}
481+
482+
std::shared_ptr<T> loaded_as_shared_ptr(handle responsible_parent = nullptr) const {
466483
if (load_impl.unowned_void_ptr_from_void_ptr_capsule) {
484+
if (responsible_parent) {
485+
return make_shared_ptr_with_responsible_parent(responsible_parent);
486+
}
467487
throw cast_error("Unowned pointer from a void pointer capsule cannot be converted to a"
468488
" std::shared_ptr.");
469489
}
470490
if (load_impl.unowned_void_ptr_from_direct_conversion != nullptr) {
491+
if (responsible_parent) {
492+
return make_shared_ptr_with_responsible_parent(responsible_parent);
493+
}
471494
throw cast_error("Unowned pointer from direct conversion cannot be converted to a"
472495
" std::shared_ptr.");
473496
}
@@ -478,6 +501,9 @@ struct smart_holder_type_caster_load {
478501
holder_type &hld = holder();
479502
hld.ensure_is_not_disowned("loaded_as_shared_ptr");
480503
if (hld.vptr_is_using_noop_deleter) {
504+
if (responsible_parent) {
505+
return make_shared_ptr_with_responsible_parent(responsible_parent);
506+
}
481507
throw std::runtime_error("Non-owning holder (loaded_as_shared_ptr).");
482508
}
483509
auto *void_raw_ptr = hld.template as_raw_ptr_unowned<void>();
@@ -579,6 +605,17 @@ struct smart_holder_type_caster_load {
579605
return result;
580606
}
581607

608+
// This function will succeed even if the `responsible_parent` does not own the
609+
// wrapped C++ object directly.
610+
// It is the responsibility of the caller to ensure that the `responsible_parent`
611+
// has a `keep_alive` relationship with the owner of the wrapped C++ object, or
612+
// that the wrapped C++ object lives for the duration of the process.
613+
static std::shared_ptr<T> shared_ptr_from_python(handle responsible_parent) {
614+
smart_holder_type_caster_load<T> loader;
615+
loader.load(responsible_parent, false);
616+
return loader.loaded_as_shared_ptr(responsible_parent);
617+
}
618+
582619
private:
583620
modified_type_caster_generic_load_impl load_impl;
584621

include/pybind11/pybind11.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1586,7 +1586,8 @@ struct property_cpp_function<
15861586
template <typename PM, must_be_member_function_pointer<PM> = 0>
15871587
static cpp_function readonly(PM pm, const handle &hdl) {
15881588
return cpp_function(
1589-
[pm](const std::shared_ptr<T> &c_sp) -> std::shared_ptr<drp> {
1589+
[pm](handle c_hdl) -> std::shared_ptr<drp> {
1590+
std::shared_ptr<T> c_sp = detail::type_caster<T>::shared_ptr_from_python(c_hdl);
15901591
D ptr = (*c_sp).*pm;
15911592
return std::shared_ptr<drp>(c_sp, ptr);
15921593
},
@@ -1622,8 +1623,8 @@ struct property_cpp_function<
16221623
template <typename PM, must_be_member_function_pointer<PM> = 0>
16231624
static cpp_function readonly(PM pm, const handle &hdl) {
16241625
return cpp_function(
1625-
[pm](const std::shared_ptr<T> &c_sp)
1626-
-> std::shared_ptr<typename std::add_const<D>::type> {
1626+
[pm](handle c_hdl) -> std::shared_ptr<typename std::add_const<D>::type> {
1627+
std::shared_ptr<T> c_sp = detail::type_caster<T>::shared_ptr_from_python(c_hdl);
16271628
return std::shared_ptr<typename std::add_const<D>::type>(c_sp, &(c_sp.get()->*pm));
16281629
},
16291630
is_method(hdl));
@@ -1632,7 +1633,8 @@ struct property_cpp_function<
16321633
template <typename PM, must_be_member_function_pointer<PM> = 0>
16331634
static cpp_function read(PM pm, const handle &hdl) {
16341635
return cpp_function(
1635-
[pm](const std::shared_ptr<T> &c_sp) -> std::shared_ptr<D> {
1636+
[pm](handle c_hdl) -> std::shared_ptr<D> {
1637+
std::shared_ptr<T> c_sp = detail::type_caster<T>::shared_ptr_from_python(c_hdl);
16361638
return std::shared_ptr<D>(c_sp, &(c_sp.get()->*pm));
16371639
},
16381640
is_method(hdl));
@@ -1669,7 +1671,10 @@ struct property_cpp_function<
16691671
template <typename PM, must_be_member_function_pointer<PM> = 0>
16701672
static cpp_function read(PM pm, const handle &hdl) {
16711673
return cpp_function(
1672-
[pm](const std::shared_ptr<T> &c_sp) -> D { return D{std::move(c_sp.get()->*pm)}; },
1674+
[pm](handle c_hdl) -> D {
1675+
std::shared_ptr<T> c_sp = detail::type_caster<T>::shared_ptr_from_python(c_hdl);
1676+
return D{std::move(c_sp.get()->*pm)};
1677+
},
16731678
is_method(hdl));
16741679
}
16751680

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#include "pybind11/smart_holder.h"
2+
#include "pybind11_tests.h"
3+
4+
#include <cstddef>
5+
#include <vector>
6+
7+
namespace test_class_sh_property_non_owning {
8+
9+
struct CoreField {
10+
explicit CoreField(int int_value = -99) : int_value{int_value} {}
11+
int int_value;
12+
};
13+
14+
struct DataField {
15+
DataField(int i_value, int i_shared, int i_unique)
16+
: core_fld_value{i_value}, core_fld_shared_ptr{new CoreField{i_shared}},
17+
core_fld_raw_ptr{core_fld_shared_ptr.get()}, core_fld_unique_ptr{
18+
new CoreField{i_unique}} {}
19+
CoreField core_fld_value;
20+
std::shared_ptr<CoreField> core_fld_shared_ptr;
21+
CoreField *core_fld_raw_ptr;
22+
std::unique_ptr<CoreField> core_fld_unique_ptr;
23+
};
24+
25+
struct DataFieldsHolder {
26+
private:
27+
std::vector<DataField> vec;
28+
29+
public:
30+
DataFieldsHolder(std::size_t vec_size) {
31+
for (std::size_t i = 0; i < vec_size; i++) {
32+
int i11 = static_cast<int>(i) * 11;
33+
vec.push_back(DataField(13 + i11, 14 + i11, 15 + i11));
34+
}
35+
}
36+
37+
DataField *vec_at(std::size_t index) {
38+
if (index >= vec.size()) {
39+
return nullptr;
40+
}
41+
return &vec[index];
42+
}
43+
};
44+
45+
} // namespace test_class_sh_property_non_owning
46+
47+
using namespace test_class_sh_property_non_owning;
48+
49+
PYBIND11_SMART_HOLDER_TYPE_CASTERS(CoreField)
50+
PYBIND11_SMART_HOLDER_TYPE_CASTERS(DataField)
51+
PYBIND11_SMART_HOLDER_TYPE_CASTERS(DataFieldsHolder)
52+
53+
TEST_SUBMODULE(class_sh_property_non_owning, m) {
54+
py::classh<CoreField>(m, "CoreField").def_readwrite("int_value", &CoreField::int_value);
55+
56+
py::classh<DataField>(m, "DataField")
57+
.def_readonly("core_fld_value_ro", &DataField::core_fld_value)
58+
.def_readwrite("core_fld_value_rw", &DataField::core_fld_value)
59+
.def_readonly("core_fld_shared_ptr_ro", &DataField::core_fld_shared_ptr)
60+
.def_readwrite("core_fld_shared_ptr_rw", &DataField::core_fld_shared_ptr)
61+
.def_readonly("core_fld_raw_ptr_ro", &DataField::core_fld_raw_ptr)
62+
.def_readwrite("core_fld_raw_ptr_rw", &DataField::core_fld_raw_ptr)
63+
.def_readwrite("core_fld_unique_ptr_rw", &DataField::core_fld_unique_ptr);
64+
65+
py::classh<DataFieldsHolder>(m, "DataFieldsHolder")
66+
.def(py::init<std::size_t>())
67+
.def("vec_at", &DataFieldsHolder::vec_at, py::return_value_policy::reference_internal);
68+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import pytest
2+
3+
from pybind11_tests import class_sh_property_non_owning as m
4+
5+
6+
@pytest.mark.parametrize("persistent_holder", [True, False])
7+
@pytest.mark.parametrize(
8+
("core_fld", "expected"),
9+
[
10+
("core_fld_value_ro", (13, 24)),
11+
("core_fld_value_rw", (13, 24)),
12+
("core_fld_shared_ptr_ro", (14, 25)),
13+
("core_fld_shared_ptr_rw", (14, 25)),
14+
("core_fld_raw_ptr_ro", (14, 25)),
15+
("core_fld_raw_ptr_rw", (14, 25)),
16+
("core_fld_unique_ptr_rw", (15, 26)),
17+
],
18+
)
19+
def test_core_fld_common(core_fld, expected, persistent_holder):
20+
if persistent_holder:
21+
h = m.DataFieldsHolder(2)
22+
for i, exp in enumerate(expected):
23+
c = getattr(h.vec_at(i), core_fld)
24+
assert c.int_value == exp
25+
else:
26+
for i, exp in enumerate(expected):
27+
c = getattr(m.DataFieldsHolder(2).vec_at(i), core_fld)
28+
assert c.int_value == exp

0 commit comments

Comments
 (0)