Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
pybind11
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
open
pybind11
Commits
1fa74222
Commit
1fa74222
authored
Aug 18, 2016
by
Wenzel Jakob
Committed by
GitHub
Aug 18, 2016
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #341 from GlenWalker/keep_alive_no_nurse
Support keep_alive where nurse may be None
parents
3460fccb
f45bb585
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
42 additions
and
7 deletions
+42
-7
docs/advanced.rst
+7
-4
example/example-keep-alive.cpp
+4
-1
example/example-keep-alive.py
+21
-0
example/example-keep-alive.ref
+8
-0
include/pybind11/pybind11.h
+2
-2
No files found.
docs/advanced.rst
View file @
1fa74222
...
@@ -663,10 +663,13 @@ In addition to the above return value policies, further `call policies` can be
...
@@ -663,10 +663,13 @@ In addition to the above return value policies, further `call policies` can be
specified to indicate dependencies between parameters. There is currently just
specified to indicate dependencies between parameters. There is currently just
one policy named ``keep_alive<Nurse, Patient>``, which indicates that the
one policy named ``keep_alive<Nurse, Patient>``, which indicates that the
argument with index ``Patient`` should be kept alive at least until the
argument with index ``Patient`` should be kept alive at least until the
argument with index ``Nurse`` is freed by the garbage collector; argument
argument with index ``Nurse`` is freed by the garbage collector, as long as the
indices start at one, while zero refers to the return value. For methods, index
nurse object supports weak references (pybind11 extension classes all support
one refers to the implicit ``this`` pointer, while regular arguments begin at
weak references). If the nurse object does not support weak references and is
index two. Arbitrarily many call policies can be specified.
not None an appropriate exception will be thrown. Argument indices start at
one, while zero refers to the return value. For methods, index one refers to
the implicit ``this`` pointer, while regular arguments begin at index two.
Arbitrarily many call policies can be specified.
Consider the following example: the binding code for a list append operation
Consider the following example: the binding code for a list append operation
that ties the lifetime of the newly added element to the underlying container
that ties the lifetime of the newly added element to the underlying container
...
...
example/example-keep-alive.cpp
View file @
1fa74222
...
@@ -22,6 +22,7 @@ public:
...
@@ -22,6 +22,7 @@ public:
~
Parent
()
{
std
::
cout
<<
"Releasing parent."
<<
std
::
endl
;
}
~
Parent
()
{
std
::
cout
<<
"Releasing parent."
<<
std
::
endl
;
}
void
addChild
(
Child
*
)
{
}
void
addChild
(
Child
*
)
{
}
Child
*
returnChild
()
{
return
new
Child
();
}
Child
*
returnChild
()
{
return
new
Child
();
}
Child
*
returnNullChild
()
{
return
nullptr
;
}
};
};
void
init_ex_keep_alive
(
py
::
module
&
m
)
{
void
init_ex_keep_alive
(
py
::
module
&
m
)
{
...
@@ -30,7 +31,9 @@ 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
(
"addChild"
,
&
Parent
::
addChild
)
.
def
(
"addChildKeepAlive"
,
&
Parent
::
addChild
,
py
::
keep_alive
<
1
,
2
>
())
.
def
(
"addChildKeepAlive"
,
&
Parent
::
addChild
,
py
::
keep_alive
<
1
,
2
>
())
.
def
(
"returnChild"
,
&
Parent
::
returnChild
)
.
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"
)
py
::
class_
<
Child
>
(
m
,
"Child"
)
.
def
(
py
::
init
<>
());
.
def
(
py
::
init
<>
());
...
...
example/example-keep-alive.py
View file @
1fa74222
...
@@ -31,6 +31,7 @@ if True:
...
@@ -31,6 +31,7 @@ if True:
gc
.
collect
()
gc
.
collect
()
print
(
p
)
print
(
p
)
p
=
None
p
=
None
gc
.
collect
()
gc
.
collect
()
print
(
""
)
print
(
""
)
...
@@ -43,4 +44,24 @@ if True:
...
@@ -43,4 +44,24 @@ if True:
gc
.
collect
()
gc
.
collect
()
print
(
""
)
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.."
)
print
(
"Terminating.."
)
example/example-keep-alive.ref
View file @
1fa74222
...
@@ -22,4 +22,12 @@ Allocating child.
...
@@ -22,4 +22,12 @@ Allocating child.
Releasing parent.
Releasing parent.
Releasing child.
Releasing child.
Allocating parent.
<example.Parent object at 0x10eb726c0>
Releasing parent.
Allocating parent.
<example.Parent object at 0x10eb726c0>
Releasing parent.
Terminating..
Terminating..
include/pybind11/pybind11.h
View file @
1fa74222
...
@@ -1098,8 +1098,8 @@ inline void keep_alive_impl(handle nurse, handle patient) {
...
@@ -1098,8 +1098,8 @@ inline void keep_alive_impl(handle nurse, handle patient) {
if
(
!
nurse
||
!
patient
)
if
(
!
nurse
||
!
patient
)
pybind11_fail
(
"Could not activate keep_alive!"
);
pybind11_fail
(
"Could not activate keep_alive!"
);
if
(
patient
.
ptr
()
==
Py_None
)
if
(
patient
.
ptr
()
==
Py_None
||
nurse
.
ptr
()
==
Py_None
)
return
;
/* Nothing to keep alive */
return
;
/* Nothing to keep alive
or nothing to be kept alive by
*/
cpp_function
disable_lifesupport
(
cpp_function
disable_lifesupport
(
[
patient
](
handle
weakref
)
{
patient
.
dec_ref
();
weakref
.
dec_ref
();
});
[
patient
](
handle
weakref
)
{
patient
.
dec_ref
();
weakref
.
dec_ref
();
});
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment