Skip to content

functions: Add doc on incorrect argument index #2979

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 6, 2021
Merged
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
3 changes: 3 additions & 0 deletions docs/advanced/functions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,9 @@ relies on the ability to create a *weak reference* to the nurse object. When
the nurse object is not a pybind11-registered type and does not support weak
references, an exception will be thrown.

If you use an incorrect argument index, you will get a ``RuntimeError`` saying
``Could not activate keep_alive!``. You should review the indices you're using.

Consider the following example: here, the binding code for a list append
operation ties the lifetime of the newly added element to the underlying
container:
Expand Down
8 changes: 7 additions & 1 deletion tests/test_call_policies.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ TEST_SUBMODULE(call_policies, m) {
void addChild(Child *) { }
Child *returnChild() { return new Child(); }
Child *returnNullChild() { return nullptr; }
static Child *staticFunction(Parent*) { return new Child(); }
};
py::class_<Parent>(m, "Parent")
.def(py::init<>())
Expand All @@ -60,7 +61,12 @@ TEST_SUBMODULE(call_policies, m) {
.def("returnChild", &Parent::returnChild)
.def("returnChildKeepAlive", &Parent::returnChild, py::keep_alive<1, 0>())
.def("returnNullChildKeepAliveChild", &Parent::returnNullChild, py::keep_alive<1, 0>())
.def("returnNullChildKeepAliveParent", &Parent::returnNullChild, py::keep_alive<0, 1>());
.def("returnNullChildKeepAliveParent", &Parent::returnNullChild, py::keep_alive<0, 1>())
.def_static(
"staticFunction", &Parent::staticFunction, py::keep_alive<1, 0>());

m.def("free_function", [](Parent*, Child*) {}, py::keep_alive<1, 2>());
m.def("invalid_arg_index", []{}, py::keep_alive<0, 1>());

#if !defined(PYPY_VERSION)
// test_alive_gc
Expand Down
30 changes: 30 additions & 0 deletions tests/test_call_policies.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,19 @@ def test_keep_alive_argument(capture):
"""
)

p = m.Parent()
c = m.Child()
assert ConstructorStats.detail_reg_inst() == n_inst + 2
m.free_function(p, c)
del c
assert ConstructorStats.detail_reg_inst() == n_inst + 2
del p
assert ConstructorStats.detail_reg_inst() == n_inst

with pytest.raises(RuntimeError) as excinfo:
m.invalid_arg_index()
assert str(excinfo.value) == "Could not activate keep_alive!"


def test_keep_alive_return_value(capture):
n_inst = ConstructorStats.detail_reg_inst()
Expand Down Expand Up @@ -85,6 +98,23 @@ def test_keep_alive_return_value(capture):
"""
)

p = m.Parent()
assert ConstructorStats.detail_reg_inst() == n_inst + 1
with capture:
m.Parent.staticFunction(p)
assert ConstructorStats.detail_reg_inst() == n_inst + 2
assert capture == "Allocating child."
with capture:
del p
assert ConstructorStats.detail_reg_inst() == n_inst
assert (
capture
== """
Releasing parent.
Releasing child.
"""
)


# https://foss.heptapod.net/pypy/pypy/-/issues/2447
@pytest.mark.xfail("env.PYPY", reason="_PyObject_GetDictPtr is unimplemented")
Expand Down