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.
This commit is contained in:
Glen Walker
2016-08-16 17:50:43 +12:00
parent e357ed3cc8
commit f45bb585c3
5 changed files with 42 additions and 7 deletions

View File

@@ -22,6 +22,7 @@ public:
~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 @@ void init_ex_keep_alive(py::module &m) {
.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<>());