-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Add a scope guard call policy #740
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,91 @@ | ||
/* | ||
tests/test_call_policies.cpp -- keep_alive and call_guard | ||
|
||
Copyright (c) 2016 Wenzel Jakob <[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 Child { | ||
public: | ||
Child() { py::print("Allocating child."); } | ||
~Child() { py::print("Releasing child."); } | ||
}; | ||
|
||
class Parent { | ||
public: | ||
Parent() { py::print("Allocating parent."); } | ||
~Parent() { py::print("Releasing parent."); } | ||
void addChild(Child *) { } | ||
Child *returnChild() { return new Child(); } | ||
Child *returnNullChild() { return nullptr; } | ||
}; | ||
|
||
test_initializer keep_alive([](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>()) | ||
.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<>()); | ||
}); | ||
|
||
struct CustomGuard { | ||
static bool enabled; | ||
|
||
CustomGuard() { enabled = true; } | ||
~CustomGuard() { enabled = false; } | ||
|
||
static const char *report_status() { return enabled ? "guarded" : "unguarded"; } | ||
}; | ||
|
||
bool CustomGuard::enabled = false; | ||
|
||
struct DependentGuard { | ||
static bool enabled; | ||
|
||
DependentGuard() { enabled = CustomGuard::enabled; } | ||
~DependentGuard() { enabled = false; } | ||
|
||
static const char *report_status() { return enabled ? "guarded" : "unguarded"; } | ||
}; | ||
|
||
bool DependentGuard::enabled = false; | ||
|
||
test_initializer call_guard([](py::module &pm) { | ||
auto m = pm.def_submodule("call_policies"); | ||
|
||
m.def("unguarded_call", &CustomGuard::report_status); | ||
m.def("guarded_call", &CustomGuard::report_status, py::call_guard<CustomGuard>()); | ||
|
||
m.def("multiple_guards_correct_order", []() { | ||
return CustomGuard::report_status() + std::string(" & ") + DependentGuard::report_status(); | ||
}, py::call_guard<CustomGuard, DependentGuard>()); | ||
|
||
m.def("multiple_guards_wrong_order", []() { | ||
return DependentGuard::report_status() + std::string(" & ") + CustomGuard::report_status(); | ||
}, py::call_guard<DependentGuard, CustomGuard>()); | ||
|
||
#if defined(WITH_THREAD) && !defined(PYPY_VERSION) | ||
// `py::call_guard<py::gil_scoped_release>()` should work in PyPy as well, | ||
// but it's unclear how to test it without `PyGILState_GetThisThreadState`. | ||
auto report_gil_status = []() { | ||
auto is_gil_held = false; | ||
if (auto tstate = py::detail::get_thread_state_unchecked()) | ||
is_gil_held = (tstate == PyGILState_GetThisThreadState()); | ||
|
||
return is_gil_held ? "GIL held" : "GIL released"; | ||
}; | ||
|
||
m.def("with_gil", report_gil_status); | ||
m.def("without_gil", report_gil_status, py::call_guard<py::gil_scoped_release>()); | ||
#endif | ||
}); |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
I think`void_type`
should be`call_guard<>`
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.
Scratch that, I see now: it's going to be
call_guard<>::type
, which is thevoid_type
.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.
Yeah, the 0 and 1 argument versions just return the type directly since those are the most common cases. The additional wrappers start with 2 arguments and above.