Skip to content

Fix py::cast(std::shared_ptr<T>) for shared_ptr-compatible custom holders#6066

Draft
virtuald wants to merge 8 commits into
pybind:masterfrom
virtuald:custom-holder-shared-ptr-abi-bump
Draft

Fix py::cast(std::shared_ptr<T>) for shared_ptr-compatible custom holders#6066
virtuald wants to merge 8 commits into
pybind:masterfrom
virtuald:custom-holder-shared-ptr-abi-bump

Conversation

@virtuald
Copy link
Copy Markdown
Contributor

Description

Also fixes #6064. This one is more reasonable, requires an ABI bump (but that was already done on master so it should be fine?).

... looking through this, I'm pretty sure it can be simplified, but that's a problem for another time.

Suggested changelog entry:

  • Placeholder.

virtuald added 2 commits May 17, 2026 03:51
Add internal callback plumbing so class_ registrations can expose when a
custom holder is constructible from std::shared_ptr<T>.

When py::cast() sees a returned std::shared_ptr for such a bound type,
it now creates the Python instance by reconstructing the bound holder
from an erased aliasing shared_ptr instead of rejecting the
conversion.

This preserves the pybind#6008 safety check for incompatible holders while
restoring support for private-destructor/shared_ptr patterns that use a
custom holder wrapper.
@virtuald virtuald changed the title Custom holder shared ptr abi bump Fix py::cast(std::shared_ptr<T>) for shared_ptr-compatible custom holders May 17, 2026
@rwgk
Copy link
Copy Markdown
Collaborator

rwgk commented May 18, 2026

Hi @virtuald, I have a few more commits under my 6067, from a full Cursor-assisted review. I'm just now running a final Cursor self-review before I push it out. Should be only a few minutes I hope. I'll push to 6067, so you can see everything.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG]: py::cast() from std::shared_ptr throws with custom holder

2 participants