Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions include/pybind11/cast.h
Original file line number Diff line number Diff line change
Expand Up @@ -1739,20 +1739,29 @@ T cast(Object &&obj) {
return obj.release().ptr();
}

PYBIND11_NAMESPACE_BEGIN(detail)
// Specialize to true for a type whose caster must get the automatic policies unresolved from
// pybind11::cast() (std::function passes the policy on to the return value of the callable).
template <typename T>
struct cast_forwards_automatic_policy : std::false_type {};
PYBIND11_NAMESPACE_END(detail)

// C++ type -> py::object
template <typename T, detail::enable_if_t<!detail::is_pyobject<T>::value, int> = 0>
object cast(T &&value,
return_value_policy policy = return_value_policy::automatic_reference,
handle parent = handle()) {
using no_ref_T = typename std::remove_reference<T>::type;
if (policy == return_value_policy::automatic) {
policy = std::is_pointer<no_ref_T>::value ? return_value_policy::take_ownership
: std::is_lvalue_reference<T>::value ? return_value_policy::copy
: return_value_policy::move;
} else if (policy == return_value_policy::automatic_reference) {
policy = std::is_pointer<no_ref_T>::value ? return_value_policy::reference
: std::is_lvalue_reference<T>::value ? return_value_policy::copy
: return_value_policy::move;
if (!detail::cast_forwards_automatic_policy<detail::intrinsic_t<T>>::value) {
if (policy == return_value_policy::automatic) {
policy = std::is_pointer<no_ref_T>::value ? return_value_policy::take_ownership
: std::is_lvalue_reference<T>::value ? return_value_policy::copy
: return_value_policy::move;
} else if (policy == return_value_policy::automatic_reference) {
policy = std::is_pointer<no_ref_T>::value ? return_value_policy::reference
: std::is_lvalue_reference<T>::value ? return_value_policy::copy
: return_value_policy::move;
}
}
return reinterpret_steal<object>(
detail::make_caster<T>::cast(std::forward<T>(value), policy, parent));
Expand Down
4 changes: 4 additions & 0 deletions include/pybind11/functional.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ struct func_wrapper : func_wrapper_base {

PYBIND11_NAMESPACE_END(type_caster_std_function_specializations)

// The policy applies to the return value of the callable, not to the std::function.
template <typename Return, typename... Args>
struct cast_forwards_automatic_policy<std::function<Return(Args...)>> : std::true_type {};

template <typename Return, typename... Args>
struct type_caster<std::function<Return(Args...)>> {
using type = std::function<Return(Args...)>;
Expand Down
20 changes: 20 additions & 0 deletions tests/test_callbacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,20 @@ double apply_custom_transform(const py::object &src, double value) {
} // namespace boost_histogram
} // namespace test_callbacks

struct TestNotCopyable {
TestNotCopyable() = default;
TestNotCopyable(const TestNotCopyable &obj) = delete;
};

// Python takes ownership (automatic policy on a cpp_function)
TestNotCopyable *full_func_return_ptr() { return new TestNotCopyable(); }

// Static: a cast std::function gets the reference policy, so Python must not own it
TestNotCopyable *static_func_return_ptr() {
static TestNotCopyable obj;
return &obj;
}

int dummy_function(int i) { return i + 1; }

TEST_SUBMODULE(callbacks, m) {
Expand Down Expand Up @@ -299,4 +313,10 @@ TEST_SUBMODULE(callbacks, m) {
test_callbacks::boost_histogram::custom_transform_int);
m.def("boost_histogram_apply_custom_transform",
test_callbacks::boost_histogram::apply_custom_transform);

// Testing returning pointers to non-copyable objects
m.attr("full_func_return_ptr") = py::cpp_function(full_func_return_ptr);
std::function<TestNotCopyable *()> func_return_ptr = &static_func_return_ptr;
m.attr("func_return_ptr") = func_return_ptr;
py::class_<TestNotCopyable>(m, "TestNotCopyable");
}
8 changes: 8 additions & 0 deletions tests/test_callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,3 +246,11 @@ def test_boost_histogram_apply_custom_transform():
assert apply(lambda value: value, 9) == -200
assert apply({}, 0) == -100
assert apply("", 0) == -100


def test_full_func_return_ptr():
m.full_func_return_ptr()


def test_full_func_return_ptr_inherit():
m.func_return_ptr()
17 changes: 17 additions & 0 deletions tests/test_stl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,23 @@ TEST_SUBMODULE(stl, m) {
},
py::return_value_policy::take_ownership);

// py::cast() of an lvalue container copies the pointees, also for the automatic policies
auto cast_lvalue_ptr_vector_copies = [](py::return_value_policy policy) {
std::unique_ptr<NoAssign> owner(new NoAssign(7));
std::vector<NoAssign *> vec{owner.get()};
auto lst = py::cast(vec, policy).cast<py::list>();
bool copied = lst[0].cast<NoAssign *>() != owner.get();
if (!copied && policy == py::return_value_policy::automatic) {
owner.release(); // Python took ownership; avoid a double free
}
return copied;
};
m.def("cast_lvalue_ptr_vector_copies_automatic",
[=]() { return cast_lvalue_ptr_vector_copies(py::return_value_policy::automatic); });
m.def("cast_lvalue_ptr_vector_copies_automatic_reference", [=]() {
return cast_lvalue_ptr_vector_copies(py::return_value_policy::automatic_reference);
});

m.def("array_cast_sequence", [](std::array<int, 3> x) { return x; });

/// test_issue_1561
Expand Down
5 changes: 5 additions & 0 deletions tests/test_stl.py
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,11 @@ def test_stl_ownership():
assert cstats.alive() == 0


def test_cast_lvalue_ptr_vector_copies():
assert m.cast_lvalue_ptr_vector_copies_automatic()
assert m.cast_lvalue_ptr_vector_copies_automatic_reference()


def test_array_cast_sequence():
assert m.array_cast_sequence((1, 2, 3)) == [1, 2, 3]

Expand Down