Skip to content

Make reference(_internal) the default return value policy for properties #473

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 1, 2016

Conversation

dean0x7d
Copy link
Member

See #436 for related discussion.

The tests should cover all possible combinations of def_propery* and return_value_policy and make sure the correct overload is chosen:

  1. Default (no explicitly selected policy)
  2. Policy selected via direct argument
  3. Policy selected via cppfunction

To make the new default reference(_internal) policy work correctly for rvalues, type_caster_base needed to be tweaked as well. The discussed always-move policy turned into 'almost always'. copy will still be honored if it's selected explicitly. I'm not sure that anyone would actually want to do this (move already has a fallback for copy-only classes), but I guess it's best not to forbid a valid option. However, all other policies are outright errors for rvalues (pointer to temporary) and therefore cannot be selected.

Before this, all `def_property*` functions used `automatic` as their
default return value policy. This commit makes it so that:

 * Non-static properties use `reference_interal` by default, thus
   matching `def_readonly` and `def_readwrite`.

 * Static properties use `reference` by default, thus matching
   `def_readonly_static` and `def_readwrite_static`.

In case `cpp_function` is passed to any `def_property*`, its policy will
be used instead of any defaults. User-defined arguments in `extras`
still have top priority and will override both the default policies and
the ones from `cpp_function`.

Resolves pybind#436.
For functions which return rvalues or rvalue references, the only viable
return value policies are `copy` and `move`. `reference(_internal)` and
`take_ownership` would take the address of a temporary which is always
an error.

This commit prevents possible user errors by overriding the bad rvalue
policies with `move`. Besides `move`, only `copy` is allowed, and only
if it's explicitly selected by the user.

This is also a necessary safety feature to support the new default
return value policies for properties: `reference(_internal)`.
@wjakob
Copy link
Member

wjakob commented Nov 1, 2016

looks good to me, thanks!

@wjakob wjakob merged commit 03f627e into pybind:master Nov 1, 2016
@dean0x7d dean0x7d deleted the property-rvp branch November 1, 2016 12:05
dean0x7d added a commit to dean0x7d/pybind11 that referenced this pull request Nov 19, 2016
Fixes pybind#509.

The move policy was already set for rvalues in PR pybind#473, but this only
applied to directly cast user-defined types. The problem is that STL
containers cast values indirectly and the rvalue information is lost.
Therefore the move policy was not set correctly. This commit fixes it.

This also makes an additional adjustment to remove the `copy` policy
exception: rvalues now always use the `move` policy. This is also safe
for copy-only rvalues because the `move` policy has an internal fallback
to copying.
wjakob pushed a commit that referenced this pull request Nov 20, 2016
Fixes #509.

The move policy was already set for rvalues in PR #473, but this only
applied to directly cast user-defined types. The problem is that STL
containers cast values indirectly and the rvalue information is lost.
Therefore the move policy was not set correctly. This commit fixes it.

This also makes an additional adjustment to remove the `copy` policy
exception: rvalues now always use the `move` policy. This is also safe
for copy-only rvalues because the `move` policy has an internal fallback
to copying.
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.

2 participants