-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WIP Add arg().disown() (superseding the consume call_policy) #1226
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
tests/test_disown.cpp -- disown | ||
|
||
Copyright (c) 2016 Wenzel Jakob <[email protected]> | ||
Copyright (c) 2017 Attila Török <[email protected]> | ||
|
||
All rights reserved. Use of this source code is governed by a | ||
BSD-style license that can be found in the LICENSE file. | ||
*/ | ||
|
||
#include "pybind11_tests.h" | ||
|
||
class Box { | ||
int size; | ||
static int num_boxes; | ||
|
||
public: | ||
Box(int size): size(size) { py::print("Box created."); ++num_boxes; } | ||
~Box() { py::print("Box destroyed."); --num_boxes; } | ||
|
||
int get_size() { return size; } | ||
static int get_num_boxes() { return num_boxes; } | ||
}; | ||
|
||
int Box::num_boxes = 0; | ||
|
||
class Filter { | ||
int threshold; | ||
|
||
public: | ||
Filter(int threshold): threshold(threshold) { py::print("Filter created."); } | ||
~Filter() { py::print("Filter destroyed."); } | ||
|
||
void process(Box *box) { // ownership of box is taken | ||
py::print("Box is processed by Filter."); | ||
if (box->get_size() > threshold) | ||
delete box; | ||
// otherwise the box is leaked | ||
}; | ||
}; | ||
|
||
test_initializer disown([](py::module &m) { | ||
py::class_<Box>(m, "Box") | ||
.def(py::init<int>()) | ||
.def_static("get_num_boxes", &Box::get_num_boxes); | ||
|
||
py::class_<Filter>(m, "Filter") | ||
.def(py::init<int>()) | ||
.def("process", &Filter::process, py::arg("box").disown()); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import pytest | ||
|
||
|
||
def test_disown_argument(capture): | ||
from pybind11_tests import Box, Filter | ||
|
||
with capture: | ||
filt = Filter(4) | ||
assert capture == "Filter created." | ||
with capture: | ||
box_1 = Box(1) | ||
box_8 = Box(8) | ||
assert capture == """ | ||
Box created. | ||
Box created. | ||
""" | ||
|
||
assert Box.get_num_boxes() == 2 | ||
|
||
with capture: | ||
filt.process(box_1) # box_1 is not big enough, but process() leaks it | ||
assert capture == "Box is processed by Filter." | ||
|
||
assert Box.get_num_boxes() == 2 | ||
|
||
with capture: | ||
filt.process(box_8) # box_8 is destroyed by process() of filt | ||
assert capture == """ | ||
Box is processed by Filter. | ||
Box destroyed. | ||
""" | ||
|
||
assert Box.get_num_boxes() == 1 # box_1 still exists somehow, but we can't access it | ||
|
||
with capture: | ||
del filt | ||
del box_1 | ||
del box_8 | ||
pytest.gc_collect() | ||
assert capture == "Filter destroyed." | ||
|
||
assert Box.get_num_boxes() == 1 # 1 box is leaked, and we can't do anything |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@torokati44 After seeing your stuff, and reworking my PR, I believe this function should be what you need for type-erased holder transfer:
https://github.com/EricCousineau-TRI/pybind11/blob/379afa73c7fedded57e4fedc9c3b077cb7292643/include/pybind11/cast.h#L482
You could use it to something like the tune of:
The caveat is, you'll need to figure out what the holder type is for the given argument. I believe that should be elsewhere in this area of code.
(Also, ensure that you get
tinfo
from the argument type and NOT the holder, asholder->type
may be null!)