mirror of
https://github.com/pybind/pybind11.git
synced 2026-04-19 14:29:11 +00:00
keep_alive call policy (analogous to Boost.Python's with_custodian_and_ward, fixes #62)
This commit is contained in:
@@ -21,6 +21,7 @@ void init_ex9(py::module &);
|
||||
void init_ex10(py::module &);
|
||||
void init_ex11(py::module &);
|
||||
void init_ex12(py::module &);
|
||||
void init_ex13(py::module &);
|
||||
|
||||
PYBIND11_PLUGIN(example) {
|
||||
py::module m("example", "pybind example plugin");
|
||||
@@ -37,6 +38,7 @@ PYBIND11_PLUGIN(example) {
|
||||
init_ex10(m);
|
||||
init_ex11(m);
|
||||
init_ex12(m);
|
||||
init_ex13(m);
|
||||
|
||||
return m.ptr();
|
||||
}
|
||||
|
||||
37
example/example13.cpp
Normal file
37
example/example13.cpp
Normal file
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
example/example13.cpp -- keep_alive modifier (pybind11's version
|
||||
of Boost.Python's with_custodian_and_ward / with_custodian_and_ward_postcall)
|
||||
|
||||
Copyright (c) 2015 Wenzel Jakob <wenzel@inf.ethz.ch>
|
||||
|
||||
All rights reserved. Use of this source code is governed by a
|
||||
BSD-style license that can be found in the LICENSE file.
|
||||
*/
|
||||
|
||||
#include "example.h"
|
||||
|
||||
class Child {
|
||||
public:
|
||||
Child() { std::cout << "Allocating child." << std::endl; }
|
||||
~Child() { std::cout << "Releasing child." << std::endl; }
|
||||
};
|
||||
|
||||
class Parent {
|
||||
public:
|
||||
Parent() { std::cout << "Allocating parent." << std::endl; }
|
||||
~Parent() { std::cout << "Releasing parent." << std::endl; }
|
||||
void addChild(Child *) { }
|
||||
Child *returnChild() { return new Child(); }
|
||||
};
|
||||
|
||||
void init_ex13(py::module &m) {
|
||||
py::class_<Parent>(m, "Parent")
|
||||
.def(py::init<>())
|
||||
.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>());
|
||||
|
||||
py::class_<Child>(m, "Child")
|
||||
.def(py::init<>());
|
||||
}
|
||||
46
example/example13.py
Normal file
46
example/example13.py
Normal file
@@ -0,0 +1,46 @@
|
||||
from __future__ import print_function
|
||||
import sys
|
||||
import gc
|
||||
sys.path.append('.')
|
||||
|
||||
from example import Parent, Child
|
||||
|
||||
if True:
|
||||
p = Parent()
|
||||
p.addChild(Child())
|
||||
gc.collect()
|
||||
print(p)
|
||||
p = None
|
||||
|
||||
gc.collect()
|
||||
print("")
|
||||
|
||||
if True:
|
||||
p = Parent()
|
||||
p.returnChild()
|
||||
gc.collect()
|
||||
print(p)
|
||||
p = None
|
||||
|
||||
gc.collect()
|
||||
print("")
|
||||
|
||||
if True:
|
||||
p = Parent()
|
||||
p.addChildKeepAlive(Child())
|
||||
gc.collect()
|
||||
print(p)
|
||||
p = None
|
||||
gc.collect()
|
||||
print("")
|
||||
|
||||
if True:
|
||||
p = Parent()
|
||||
p.returnChildKeepAlive()
|
||||
gc.collect()
|
||||
print(p)
|
||||
p = None
|
||||
|
||||
gc.collect()
|
||||
print("")
|
||||
print("Terminating..")
|
||||
25
example/example13.ref
Normal file
25
example/example13.ref
Normal file
@@ -0,0 +1,25 @@
|
||||
Allocating parent.
|
||||
Allocating child.
|
||||
Releasing child.
|
||||
<example.Parent object at 0x10eb726c0>
|
||||
Releasing parent.
|
||||
|
||||
Allocating parent.
|
||||
Allocating child.
|
||||
Releasing child.
|
||||
<example.Parent object at 0x10eb726c0>
|
||||
Releasing parent.
|
||||
|
||||
Allocating parent.
|
||||
Allocating child.
|
||||
<example.Parent object at 0x10eb726c0>
|
||||
Releasing parent.
|
||||
Releasing child.
|
||||
|
||||
Allocating parent.
|
||||
Allocating child.
|
||||
<example.Parent object at 0x10eb726c0>
|
||||
Releasing parent.
|
||||
Releasing child.
|
||||
|
||||
Terminating..
|
||||
Reference in New Issue
Block a user