Skip to content

fix: add support for shared_ptr<const T> in py::init() with smart_holder #5731

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions include/pybind11/detail/init.h
Original file line number Diff line number Diff line change
Expand Up @@ -246,20 +246,38 @@ void construct(value_and_holder &v_h,
v_h.type->init_instance(v_h.inst, &smhldr);
}

template <typename Class, detail::enable_if_t<is_smart_holder<Holder<Class>>::value, int> = 0>
void construct(value_and_holder &v_h, std::shared_ptr<Cpp<Class>> &&shd_ptr, bool need_alias) {
PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(need_alias);
template <typename PtrType, typename Class>
void construct_from_shared_ptr(value_and_holder &v_h,
std::shared_ptr<PtrType> &&shd_ptr,
bool need_alias) {
static_assert(std::is_same<PtrType, Cpp<Class>>::value
|| std::is_same<PtrType, const Cpp<Class>>::value,
"Expected (const) Cpp<Class> as shared_ptr pointee");
auto *ptr = shd_ptr.get();
no_nullptr(ptr);
if (Class::has_alias && need_alias && !is_alias<Class>(ptr)) {
throw type_error("pybind11::init(): construction failed: returned std::shared_ptr pointee "
"is not an alias instance");
}
auto smhldr = smart_holder::from_shared_ptr(shd_ptr);
v_h.value_ptr() = ptr;
// Cast to non-const if needed, consistent with internal design
auto smhldr
= smart_holder::from_shared_ptr(std::const_pointer_cast<Cpp<Class>>(std::move(shd_ptr)));
v_h.value_ptr() = const_cast<Cpp<Class> *>(ptr);
v_h.type->init_instance(v_h.inst, &smhldr);
}

template <typename Class, detail::enable_if_t<is_smart_holder<Holder<Class>>::value, int> = 0>
void construct(value_and_holder &v_h, std::shared_ptr<Cpp<Class>> &&shd_ptr, bool need_alias) {
construct_from_shared_ptr<Cpp<Class>, Class>(v_h, std::move(shd_ptr), need_alias);
}

template <typename Class, detail::enable_if_t<is_smart_holder<Holder<Class>>::value, int> = 0>
void construct(value_and_holder &v_h,
std::shared_ptr<const Cpp<Class>> &&shd_ptr,
bool need_alias) {
construct_from_shared_ptr<const Cpp<Class>, Class>(v_h, std::move(shd_ptr), need_alias);
}

template <typename Class, detail::enable_if_t<is_smart_holder<Holder<Class>>::value, int> = 0>
void construct(value_and_holder &v_h,
std::shared_ptr<Alias<Class>> &&shd_ptr,
Expand Down
14 changes: 3 additions & 11 deletions tests/test_class_sh_factory_constructors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,31 +108,23 @@ TEST_SUBMODULE(class_sh_factory_constructors, m) {
.def("get_mtxt", get_mtxt<atyp_shmp>);

py::classh<atyp_shcp>(m, "atyp_shcp")
// py::class_<atyp_shcp, std::shared_ptr<atyp_shcp>>(m, "atyp_shcp")
// class_: ... must return a compatible ...
// classh: ... cannot pass object of non-trivial type ...
// .def(py::init(&rtrn_shcp))
.def(py::init(&rtrn_shcp))
.def("get_mtxt", get_mtxt<atyp_shcp>);

py::classh<atyp_uqmp>(m, "atyp_uqmp")
.def(py::init(&rtrn_uqmp))
.def("get_mtxt", get_mtxt<atyp_uqmp>);

py::classh<atyp_uqcp>(m, "atyp_uqcp")
// class_: ... cannot pass object of non-trivial type ...
// classh: ... cannot pass object of non-trivial type ...
// .def(py::init(&rtrn_uqcp))
.def(py::init(&rtrn_uqcp))
.def("get_mtxt", get_mtxt<atyp_uqcp>);

py::classh<atyp_udmp>(m, "atyp_udmp")
.def(py::init(&rtrn_udmp))
.def("get_mtxt", get_mtxt<atyp_udmp>);

py::classh<atyp_udcp>(m, "atyp_udcp")
// py::class_<atyp_udcp, std::unique_ptr<atyp_udcp, sddc>>(m, "atyp_udcp")
// class_: ... must return a compatible ...
// classh: ... cannot pass object of non-trivial type ...
// .def(py::init(&rtrn_udcp))
.def(py::init(&rtrn_udcp))
.def("get_mtxt", get_mtxt<atyp_udcp>);

py::classh<with_alias, with_alias_alias>(m, "with_alias")
Expand Down
6 changes: 3 additions & 3 deletions tests/test_class_sh_factory_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ def test_atyp_factories():
# sert m.atyp_cptr().get_mtxt() == "Cptr"
Copy link
Preview

Copilot AI Jun 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a typo in the comment: change "sert" to "assert" to match the style of the other lines.

Copilot uses AI. Check for mistakes.

assert m.atyp_mptr().get_mtxt() == "Mptr"
assert m.atyp_shmp().get_mtxt() == "Shmp"
# sert m.atyp_shcp().get_mtxt() == "Shcp"
assert m.atyp_shcp().get_mtxt() == "Shcp"
assert m.atyp_uqmp().get_mtxt() == "Uqmp"
# sert m.atyp_uqcp().get_mtxt() == "Uqcp"
assert m.atyp_uqcp().get_mtxt() == "Uqcp"
assert m.atyp_udmp().get_mtxt() == "Udmp"
# sert m.atyp_udcp().get_mtxt() == "Udcp"
assert m.atyp_udcp().get_mtxt() == "Udcp"


@pytest.mark.parametrize(
Expand Down
Loading