Fix crash in def_readwrite for non-smart-holder properties of smart-holder classes (v2)#6008
Fix crash in def_readwrite for non-smart-holder properties of smart-holder classes (v2)#6008rwgk merged 10 commits intopybind:masterfrom
Conversation
- Occurs with non-smart-holder property of smart-holder class
|
@virtuald @oremanj I think this PR is definitely the way to go (suggestion to close PR #6003 in favor of this PR). Below is a Opus 4.6 1M Thinking analysis of the fix. I think we should merge this fix as-is, because it's a strict improvement, and maybe do other things in follow-on PRs. For this PR: I still need to look carefully at the tests. The bugWhen a smart-holder class ( The fixIn static handle
cast(const std::shared_ptr<type> &src, return_value_policy policy, handle parent) {
const auto *ptr = src.get();
typename type_caster_base<type>::cast_sources srcs{ptr};
if (srcs.creates_smart_holder()) {
return smart_holder_type_caster_support::smart_holder_from_shared_ptr(
src, policy, parent, srcs.result);
}
auto *tinfo = srcs.result.tinfo;
if (tinfo != nullptr && tinfo->holder_enum_v == holder_enum_t::std_shared_ptr) {
return type_caster_base<type>::cast_holder(srcs, &src);
}
if (parent) {
return type_caster_base<type>::cast(
srcs, return_value_policy::reference_internal, parent);
}
throw cast_error("Unable to convert std::shared_ptr<T> to Python when the bound type "
"does not use std::shared_ptr or py::smart_holder as its holder type");
}Three cases:
Why the
|
This actually is checked at compile time: m.def("getSharedEnumAB", []() -> std::shared_ptr<EnumAB> {
return std::make_shared<EnumAB>();
});I did add a test for the "returning shared ptr when holder is unique_ptr" case though. |
This reverts commit b299f32.
…ST_SMART_HOLDER=ON
|
Sorry, I was going to get back to it this evening but got caught up with other things. I didn't add the counterexample as a test yet, but I think it should go in here -- I'll go ahead and add it since CI failed already. |
No worries. I just thought I give it a little nudge after merging three other PRs.
Absolutely, thanks! |
rwgk
left a comment
There was a problem hiding this comment.
Looks great to me, thanks for the fix!
I'm not sure if you consider this the final state of the PR: please let me know and I'll merge as soon as I see your response.
|
I am satisfied that this fixes my issue. |
Description
Alternative fix for #6003, primarily made it because I'm not sure this is better and to check that all tests pass. See discussion over on that PR for background and analysis.
Suggested changelog entry:
Fix crash in def_readwrite for non-smart-holder properties of smart-holder classes