Skip to content

Commit 7509064

Browse files
authored
More precise return_value_policy::automatic documentation. (#2920)
* Adding test_return_vector_bool_raw_ptr to test_stl.py. * First attempt to make the documentation more accurate, but not trying to be comprehensive, to not bloat the reference table with too many details. * Fixing minor oversights. * Applying reviewer suggestion.
1 parent ddf0efb commit 7509064

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

docs/advanced/functions.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,17 +90,18 @@ The following table provides an overview of available policies:
9090
| | return value is referenced by Python. This is the default policy for |
9191
| | property getters created via ``def_property``, ``def_readwrite``, etc. |
9292
+--------------------------------------------------+----------------------------------------------------------------------------+
93-
| :enum:`return_value_policy::automatic` | **Default policy.** This policy falls back to the policy |
93+
| :enum:`return_value_policy::automatic` | This policy falls back to the policy |
9494
| | :enum:`return_value_policy::take_ownership` when the return value is a |
9595
| | pointer. Otherwise, it uses :enum:`return_value_policy::move` or |
9696
| | :enum:`return_value_policy::copy` for rvalue and lvalue references, |
9797
| | respectively. See above for a description of what all of these different |
98-
| | policies do. |
98+
| | policies do. This is the default policy for ``py::class_``-wrapped types. |
9999
+--------------------------------------------------+----------------------------------------------------------------------------+
100100
| :enum:`return_value_policy::automatic_reference` | As above, but use policy :enum:`return_value_policy::reference` when the |
101101
| | return value is a pointer. This is the default conversion policy for |
102102
| | function arguments when calling Python functions manually from C++ code |
103-
| | (i.e. via handle::operator()). You probably won't need to use this. |
103+
| | (i.e. via ``handle::operator()``) and the casters in ``pybind11/stl.h``. |
104+
| | You probably won't need to use this explicitly. |
104105
+--------------------------------------------------+----------------------------------------------------------------------------+
105106

106107
Return value policies can also be applied to properties:

tests/test_stl.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,4 +334,10 @@ TEST_SUBMODULE(stl, m) {
334334
py::class_<Issue1561Outer>(m, "Issue1561Outer")
335335
.def(py::init<>())
336336
.def_readwrite("list", &Issue1561Outer::list);
337+
338+
m.def(
339+
"return_vector_bool_raw_ptr",
340+
[]() { return new std::vector<bool>(4513); },
341+
// Without explicitly specifying `take_ownership`, this function leaks.
342+
py::return_value_policy::take_ownership);
337343
}

tests/test_stl.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,3 +283,10 @@ def test_issue_1561():
283283
bar.list = [m.Issue1561Inner("bar")]
284284
bar.list
285285
assert bar.list[0].data == "bar"
286+
287+
288+
def test_return_vector_bool_raw_ptr():
289+
# Add `while True:` for manual leak checking.
290+
v = m.return_vector_bool_raw_ptr()
291+
assert isinstance(v, list)
292+
assert len(v) == 4513

0 commit comments

Comments
 (0)