Repository navigation
Conversation
|
Feel free to close this (or I will) if the decision is that forwarding on function creation is correct (it very well may be). |
|
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) |
|
Sounds good. The tests in the PR are possibly still useful as a basis to see if the forwarding behavior is fixed. |
715d03c to
7aa38af
Compare
|
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: pybind11/include/pybind11/cast.h Lines 1607 to 1610 in 6898679 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: pybind11/include/pybind11/cast.h Lines 786 to 787 in 6898679 Is there a reason for those initial lines (1607-1610)? |
|
Removing those policy translations from The only questions is if there are any casters or situations that do require the policy translations made in (Although, I wouldn't recommend to make this change for v2.2.1. Better for v2.3 so it can sit in |
|
Does this need to be merged to master so it can sit in master for a while? I've rebased on current master. |
|
Could this be useful in 2.3.0? |
Assisted-by: ClaudeCode:claude-fable-5-2
|
— codex on behalf of Ralf — Preserve ownership for existing 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
|
🤖 AI text below 🤖 @rwgk The finding is correct. I reproduced it: on 9501ae7, Fixed in 48a4635:
The PR description is updated. Other casters have no behavior change now, so the |
🤖 AI text below 🤖
Fixes #1041.
py::cast()resolvesautomaticandautomatic_referencetocopy/move/take_ownership/referencebefore it calls the caster. For an lvaluestd::function, that gavecopyto the functional caster, which passed it to the wrapped function as the policy for its return value. Astd::function<T *()>with a non-copyableTthen failed withreturn_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.hsets it forstd::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 lvaluestd::vector<T *>still copies each element.The new test in
test_callbacksfails on master without the change.Suggested changelog entry:
py::cast()of astd::functionnow forwards the automatic return value policies to the wrapped function, so astd::functionthat returns a pointer to a non-copyable type can be cast.