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.
This commit is contained in:
Wenzel Jakob
2018-07-17 16:56:26 +02:00
committed by GitHub
parent b4719a60d3
commit cbd16a8247
6 changed files with 43 additions and 13 deletions

View File

@@ -2,6 +2,7 @@ import pytest
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 @@ def test_missing_header_message():
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