Skip to content

fix: functional returning a pointer policy - #1043

Open
henryiii wants to merge 6 commits into
pybind:masterfrom
henryiii:functional
Open

henryiii wants to merge 6 commits into
pybind:masterfrom
henryiii:functional

Conversation

@henryiii

@henryiii henryiii commented Aug 30, 2017 •

Copy link
Copy Markdown
Collaborator

🤖 AI text below 🤖

Fixes #1041.

py::cast() resolves automatic and automatic_reference to copy/move/take_ownership/reference before it calls the caster. For an lvalue std::function, that gave copy to the functional caster, which passed it to the wrapped function as the policy for its return value. A std::function<T *()> with a non-copyable T then failed with return_value_policy = copy, but type T is non-copyable!.

This PR adds the trait detail::cast_forwards_automatic_policy<T>. py::cast() does not resolve the automatic policies when the trait is true. functional.h sets it for std::function, so the wrapped function resolves the policy for its return value, as it does on the function-return path.

All other types keep the current py::cast() behavior. For example, py::cast(vec) on an lvalue std::vector<T *> still copies each element.

The new test in test_callbacks fails on master without the change.

Suggested changelog entry:

  • py::cast() of a std::function now forwards the automatic return value policies to the wrapped function, so a std::function that returns a pointer to a non-copyable type can be cast.

@henryiii

Copy link
Copy Markdown
Collaborator Author

Feel free to close this (or I will) if the decision is that forwarding on function creation is correct (it very well may be).

@wjakob

wjakob commented Aug 30, 2017

Copy link
Copy Markdown
Member

I don't think this is the best way to address the underlying issue, since it imposes a fixed policy -- instead, we should fix the logic that forwards the return value policy to the std::function caster (see my comment on #1041)

@henryiii

Copy link
Copy Markdown
Collaborator Author

Sounds good. The tests in the PR are possibly still useful as a basis to see if the forwarding behavior is fixed.

@henryiii henryiii changed the title Functional returning a pointer fix WIP: Functional returning a pointer policy Aug 30, 2017
@henryiii
henryiii force-pushed the functional branch 2 times, most recently from 715d03c to 7aa38af Compare August 31, 2017 01:36
@henryiii

henryiii commented Aug 31, 2017 •

Copy link
Copy Markdown
Collaborator Author

I tracked down the policy calls and it seems to be making a decision to copy at the end if you keep automatic, as well. So if you simply comment out these four lines:

if (policy == return_value_policy::automatic)
policy = std::is_pointer<T>::value ? return_value_policy::take_ownership : return_value_policy::copy;
else if (policy == return_value_policy::automatic_reference)
policy = std::is_pointer<T>::value ? return_value_policy::reference : return_value_policy::copy;

Automatic gets passed down the chain and the tests I've added pass along with all the others.

The place where automatic goes to copy is here:

if (policy == return_value_policy::automatic || policy == return_value_policy::automatic_reference)
policy = return_value_policy::copy;

Is there a reason for those initial lines (1607-1610)?

@dean0x7d

dean0x7d commented Sep 9, 2017

Copy link
Copy Markdown
Member

Removing those policy translations from py::cast() does seem reasonable to me and it looks like it doesn't affect the tests in any way. AFAIK all the casters which need policies already know how to translate policies internally because other to-Python conversions (which don't do through py::cast()) don't have these extra policy checks.

The only questions is if there are any casters or situations that do require the policy translations made in py::cast(), but that are not currently covered by the test suite. @wjakob Is there a special reason for those 4 lines in py::cast()? If they can be removed, it would propagate the automatic policy to the casters which seems desirable.

(Although, I wouldn't recommend to make this change for v2.2.1. Better for v2.3 so it can sit in master for a while and we can see if there are any issues with it.)

@dean0x7d dean0x7d mentioned this pull request Sep 10, 2017
@henryiii henryiii changed the title WIP: Functional returning a pointer policy Functional returning a pointer policy Sep 10, 2017
@henryiii

henryiii commented Apr 4, 2018

Copy link
Copy Markdown
Collaborator Author

Does this need to be merged to master so it can sit in master for a while? I've rebased on current master.

@henryiii

Copy link
Copy Markdown
Collaborator Author

Could this be useful in 2.3.0?

Comment thread include/pybind11/cast.h Resolved
Comment thread tests/test_callbacks.cpp Resolved
Comment thread tests/test_callbacks.py Resolved
@henryiii henryiii closed this Sep 23, 2021
@henryiii henryiii reopened this Sep 23, 2021
@henryiii henryiii changed the title Functional returning a pointer policy fix!: functional returning a pointer policy Nov 12, 2026
@rwgk

rwgk commented Nov 21, 2026

Copy link
Copy Markdown
Collaborator

— codex on behalf of Ralf —

Preserve ownership for existing py::cast(..., automatic) calls. Deferring normalization changes what happens when an lvalue std::vector<Item *> is cast with explicit return_value_policy::automatic. Before this change, the lvalue policy copies the pointees. The forwarded automatic now reaches each raw-pointer caster and transfers ownership. With a vector containing unique_owner.get(), destroying the resulting Python list destroyed the source object on this head, leaving the C++ owner dangling. The base preserved the C++-owned object. I reproduced the difference on with-GIL and free-threaded CPython 3.15.2. Can the deferred policy be scoped to the callable case so existing py::cast ownership semantics are preserved? Policy forwarding · Container element forwarding · Earlier policy discussion

Ralf has yet to review these findings.

py::cast() resolves the automatic policies again, so nested casters
(for example an lvalue std::vector<T *>) keep the copy semantics.
Only the std::function caster gets the unresolved policy.

Assisted-by: ClaudeCode:claude-fable-5-2
@henryiii

Copy link
Copy Markdown
Collaborator Author

🤖 AI text below 🤖

@rwgk The finding is correct. I reproduced it: on 9501ae7, py::cast(vec, return_value_policy::automatic) on an lvalue std::vector<T *> gave Python ownership of the pointees, and automatic_reference gave references. On master, both copy.

Fixed in 48a4635:

  • py::cast() resolves the automatic policies again, as on master.
  • New trait detail::cast_forwards_automatic_policy<T> (default false). functional.h sets it to true for std::function, so only that caster gets the unresolved policy.
  • New regression test test_cast_lvalue_ptr_vector_copies in test_stl. It failed before the fix and passes now.

The PR description is updated. Other casters have no behavior change now, so the ! in the title is possibly not necessary.

@henryiii henryiii changed the title fix!: functional returning a pointer policy fix: functional returning a pointer policy Nov 21, 2026

This branch has not been deployed

No deployments
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.

function<Type*(void)> will attempt to copy Type on return

4 participants