Hold strong references to keep_alive patients

This fixes #856. Instead of the weakref trick, the internals structure
holds an unordered_map from PyObject* to a vector of references. To
avoid the cost of the unordered_map lookup for objects that don't have
any keep_alive patients, a flag is added to each instance to indicate
whether there is anything to do.
This commit is contained in:
Bruce Merry
2017-06-24 14:58:42 +02:00
committed by Jason Rhinelander
parent 2196696746
commit 9d698f7fcc
5 changed files with 138 additions and 21 deletions

View File

@@ -24,6 +24,13 @@ public:
Child *returnNullChild() { return nullptr; }
};
#if !defined(PYPY_VERSION)
class ParentGC : public Parent {
public:
using Parent::Parent;
};
#endif
test_initializer keep_alive([](py::module &m) {
py::class_<Parent>(m, "Parent")
.def(py::init<>())
@@ -34,6 +41,11 @@ test_initializer keep_alive([](py::module &m) {
.def("returnNullChildKeepAliveChild", &Parent::returnNullChild, py::keep_alive<1, 0>())
.def("returnNullChildKeepAliveParent", &Parent::returnNullChild, py::keep_alive<0, 1>());
#if !defined(PYPY_VERSION)
py::class_<ParentGC, Parent>(m, "ParentGC", py::dynamic_attr())
.def(py::init<>());
#endif
py::class_<Child>(m, "Child")
.def(py::init<>());
});