mirror of
https://github.com/pybind/pybind11.git
synced 2026-03-14 20:27:47 +00:00
Make reference(_internal) the default return value policy for properties (#473)
* Make reference(_internal) the default return value policy for properties 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 #436. * Almost always use return_value_policy::move for rvalues 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)`.
This commit is contained in:
committed by
Wenzel Jakob
parent
030d10e826
commit
03f627ebb1
@@ -85,6 +85,44 @@ def test_static_properties():
|
||||
assert Type.def_property_static == 3
|
||||
|
||||
|
||||
@pytest.mark.parametrize("access", ["ro", "rw", "static_ro", "static_rw"])
|
||||
def test_property_return_value_policies(access):
|
||||
from pybind11_tests import TestPropRVP
|
||||
|
||||
if not access.startswith("static"):
|
||||
obj = TestPropRVP()
|
||||
else:
|
||||
obj = TestPropRVP
|
||||
|
||||
ref = getattr(obj, access + "_ref")
|
||||
assert ref.value == 1
|
||||
ref.value = 2
|
||||
assert getattr(obj, access + "_ref").value == 2
|
||||
ref.value = 1 # restore original value for static properties
|
||||
|
||||
copy = getattr(obj, access + "_copy")
|
||||
assert copy.value == 1
|
||||
copy.value = 2
|
||||
assert getattr(obj, access + "_copy").value == 1
|
||||
|
||||
copy = getattr(obj, access + "_func")
|
||||
assert copy.value == 1
|
||||
copy.value = 2
|
||||
assert getattr(obj, access + "_func").value == 1
|
||||
|
||||
|
||||
def test_property_rvalue_policy():
|
||||
"""When returning an rvalue, the return value policy is automatically changed from
|
||||
`reference(_internal)` to `move`. The following would not work otherwise."""
|
||||
from pybind11_tests import TestPropRVP
|
||||
|
||||
instance = TestPropRVP()
|
||||
o = instance.rvalue
|
||||
assert o.value == 1
|
||||
o = TestPropRVP.static_rvalue
|
||||
assert o.value == 1
|
||||
|
||||
|
||||
def test_dynamic_attributes():
|
||||
from pybind11_tests import DynamicClass, CppDerivedDynamicClass
|
||||
|
||||
|
||||
Reference in New Issue
Block a user