stl.h: propagate return value policies to type-specific casters (#1455)
* stl.h: propagate return value policies to type-specific casters
Return value policies for containers like those handled in in 'stl.h'
are currently broken.
The problem is that detail::return_value_policy_override<C>::policy()
always returns 'move' when given a non-pointer/reference type, e.g.
'std::vector<...>'.
This is sensible behavior for custom types that are exposed via
'py::class_<>', but it does not make sense for types that are handled by
other type casters (STL containers, Eigen matrices, etc.).
This commit changes the behavior so that
detail::return_value_policy_override only becomes active when the type
caster derives from type_caster_generic.
Furthermore, the override logic is called recursively in STL type
casters to enable key/value-specific behavior.
diff --git a/tests/test_stl.py b/tests/test_stl.py
index 422b02c..2c5e995 100644
--- a/tests/test_stl.py
+++ b/tests/test_stl.py
@@ -2,6 +2,7 @@
from pybind11_tests import stl as m
from pybind11_tests import UserType
+from pybind11_tests import ConstructorStats
def test_vector(doc):
@@ -198,3 +199,12 @@
with pytest.raises(TypeError) as excinfo:
cm.missing_header_return()
assert expected_message in str(excinfo.value)
+
+
+def test_stl_ownership():
+ cstats = ConstructorStats.get(m.Placeholder)
+ assert cstats.alive() == 0
+ r = m.test_stl_ownership()
+ assert len(r) == 1
+ del r
+ assert cstats.alive() == 0