Support keep_alive where nurse may be None
For example keep_alive<0,1>() should work where the return value may sometimes be None. At present a "Could not allocate weak reference!" exception is thrown.
Update documentation to clarify behaviour of keep_alive when nurse is None or does not support weak references.
diff --git a/example/example-keep-alive.cpp b/example/example-keep-alive.cpp
index c099aa8..c2aeaee 100644
--- a/example/example-keep-alive.cpp
+++ b/example/example-keep-alive.cpp
@@ -22,6 +22,7 @@
~Parent() { std::cout << "Releasing parent." << std::endl; }
void addChild(Child *) { }
Child *returnChild() { return new Child(); }
+ Child *returnNullChild() { return nullptr; }
};
void init_ex_keep_alive(py::module &m) {
@@ -30,7 +31,9 @@
.def("addChild", &Parent::addChild)
.def("addChildKeepAlive", &Parent::addChild, py::keep_alive<1, 2>())
.def("returnChild", &Parent::returnChild)
- .def("returnChildKeepAlive", &Parent::returnChild, py::keep_alive<1, 0>());
+ .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>());
py::class_<Child>(m, "Child")
.def(py::init<>());
diff --git a/example/example-keep-alive.py b/example/example-keep-alive.py
index ad0176e..187ad53 100644
--- a/example/example-keep-alive.py
+++ b/example/example-keep-alive.py
@@ -31,6 +31,7 @@
gc.collect()
print(p)
p = None
+
gc.collect()
print("")
@@ -43,4 +44,24 @@
gc.collect()
print("")
+
+if True:
+ p = Parent()
+ p.returnNullChildKeepAliveChild()
+ gc.collect()
+ print(p)
+ p = None
+
+gc.collect()
+print("")
+
+if True:
+ p = Parent()
+ p.returnNullChildKeepAliveParent()
+ gc.collect()
+ print(p)
+ p = None
+
+gc.collect()
+print("")
print("Terminating..")
diff --git a/example/example-keep-alive.ref b/example/example-keep-alive.ref
index 7eb02c5..775aff4 100644
--- a/example/example-keep-alive.ref
+++ b/example/example-keep-alive.ref
@@ -22,4 +22,12 @@
Releasing parent.
Releasing child.
+Allocating parent.
+<example.Parent object at 0x10eb726c0>
+Releasing parent.
+
+Allocating parent.
+<example.Parent object at 0x10eb726c0>
+Releasing parent.
+
Terminating..