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? |
|
— codex on behalf of Ralf — Preserve ownership for existing Ralf has yet to review these findings. |
|
🤖 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 |
Assisted-by: ClaudeCode:claude-fable-5-2
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
Assisted-by: ClaudeCode:claude-opus-5-5
Assisted-by: ClaudeCode:claude-fable-5-2
|
codex GPT-6.2-Sol ultra (I'll add a commit with the suggested fix): I’d hold approval. The revision fixes the earlier vector regression and lets a directly cast The new trait uses |
Match the policy-forwarding trait through cv/ref qualifiers only. A raw pointer to a callable wrapper still needs automatic policy normalization to take ownership of that wrapper.
py::cast() now forwards the automatic policies for a std::function in a container. A pointer cast with take_ownership deletes the std::function and gives the callable the automatic policy. Tests now check ownership, and the docs describe the policy rules. Assisted-by: ClaudeCode:claude-fable-5-2
|
🤖 AI text below 🤖 A review of this branch found the problems below. The last commit fixes them. Behavior change (documented, not changed). Pointer casts gave The fix did not apply in containers. The trait matched only a top-level Tests asserted nothing. The tests now check the result type and count destructor calls to check ownership. New cases cover an rvalue cast, a capturing lambda, an explicit Comment in Known limits:
|
py::cpp_function has the default policy automatic, not automatic_reference. Assisted-by: ClaudeCode:claude-fable-5-2
|
🤖 AI text below 🤖 Correction. The docs note and the PR description said that the new behavior agrees with One inconsistency stays on this branch: Comparison with masterThis comes from an inspection of the casters. The cases were not all run on master. Same on both
Different
Assessment
|
|
As you might gather from the AI comparison, I still think this is a nice improvement and is better than master, it also can make a difference in some cases where someone was relying on a copy (not ideal if you returned a pointer, but still could happen). So I'd be okay to wait till the next minor release or even 4.0 if you'd prefer. Since it didn't change a single test of ours, I'd also be okay with it now. |
|
I'd say let's hold off with this change for now. From a codex conversation, where I also asked codex to analyze what we did in pybind11clif: I’d hold #1043. Henry’s follow-up improves the tests and fixes the case where taking ownership of a
Yes, pybind11clif explicitly handled raw pointer returns. For bound C++ classes, The useful precedent is per-element policy selection. I verified that a CLIF policy pack could copy the ordinary pointer in a pair and borrow the callable’s result in the same conversion. I’d drop the blanket container trait from #1043, keep the direct callable fix and repair the owned-wrapper path, then handle nested callables separately. Henry’s release-timing question still matters for the intended change from copy to borrow. Also, the new owned-wrapper overload passes |
|
Okay, I'll put it no draft until we are ready for 3.3 or 4.0. |
🤖 AI text below 🤖
Fixes #1041. Fixes #1072.
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. The trait is also true for a class template that has astd::functiontemplate argument (std::pair,std::vector,std::reference_wrapper, ...) and forstd::array.In a cast of a pointer to a
std::function, the policy applies to thestd::functionobject. Withtake_ownership, the caster deletes that object and now gives the wrapped functionautomatic. Before, the wrapped function also gottake_ownership, so Python deleted each object that it returned.Behavior change:
py::cast(fn)of an lvaluestd::function<T *()>gave Python a copy of the returned object. Python now gets a reference that it does not own, as withpy::cast(ptr). This differs frompy::cpp_function(fn), which has the default policyautomaticand gives Python the ownership. The docs have a note about this.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 tests in
test_callbackscheck ownership. The top-level cast test fails on master, and the container and pointer tests fail without the last commit.Suggested changelog entry:
py::cast()of astd::function(also in a container) now forwards the automatic return value policies to the wrapped function, so astd::functionthat returns a pointer to a non-copyable type can be cast. Astd::functionthat returns a raw pointer now gives Python a non-owning reference where it gave a copy before.py::cast()of a pointer to astd::functionwithtake_ownershipno longer givestake_ownershipto the wrapped function.📚 Documentation preview 📚: https://pybind11--1043.org.readthedocs.build/