-
Notifications
You must be signed in to change notification settings - Fork 2.2k
[smart_holder] Auto select return value policy for clif_automatic #4381
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 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
5b7d08e
Auto select return value policy for clif_automatic
wangxf123456 d1b36c9
Try fixing test failures
wangxf123456 b7f2e9f
Merge branch 'smart_holder' into apply-clif-automatic
wangxf123456 5b7bd24
Add more tests.
wangxf123456 8f992dd
remove comments
wangxf123456 853c556
Fix test failures
wangxf123456 74a9e7a
Fix test failures
wangxf123456 5f310ab
Fix test failure for windows platform
wangxf123456 2e3c2b7
Fix clangtidy
wangxf123456 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,69 @@ | ||
#include <pybind11/smart_holder.h> | ||
|
||
#include "pybind11_tests.h" | ||
|
||
namespace test_return_value_policy_override { | ||
|
||
struct some_type {}; | ||
|
||
struct obj { | ||
std::string mtxt; | ||
obj(const std::string &mtxt_) : mtxt(mtxt_) {} | ||
obj(const obj &other) { mtxt = other.mtxt + "_CpCtor"; } | ||
obj(obj &&other) { mtxt = other.mtxt + "_MvCtor"; } | ||
obj &operator=(const obj &other) { | ||
mtxt = other.mtxt + "_CpCtor"; | ||
return *this; | ||
} | ||
obj &operator=(obj &&other) { | ||
mtxt = other.mtxt + "_MvCtor"; | ||
return *this; | ||
} | ||
}; | ||
|
||
struct nocopy { | ||
std::string mtxt; | ||
nocopy(const std::string &mtxt_) : mtxt(mtxt_) {} | ||
|
||
nocopy(nocopy &&other) { mtxt = other.mtxt + "_MvCtor"; } | ||
nocopy &operator=(nocopy &&other) { | ||
mtxt = other.mtxt + "_MvCtor"; | ||
return *this; | ||
} | ||
}; | ||
|
||
obj *return_pointer() { | ||
static obj value("pointer"); | ||
return &value; | ||
} | ||
|
||
const obj *return_const_pointer() { | ||
static obj value("const_pointer"); | ||
return &value; | ||
} | ||
|
||
obj &return_reference() { | ||
static obj value("reference"); | ||
return value; | ||
} | ||
|
||
const obj &return_const_reference() { | ||
static obj value("const_reference"); | ||
return value; | ||
} | ||
|
||
std::shared_ptr<obj> return_shared_pointer() { return std::make_shared<obj>("shared_pointer"); } | ||
|
||
std::unique_ptr<obj> return_unique_pointer() { return std::make_unique<obj>("unique_pointer"); } | ||
|
||
nocopy &return_reference_nocopy() { | ||
static nocopy value("reference_nocopy"); | ||
return value; | ||
} | ||
|
||
} // namespace test_return_value_policy_override | ||
|
||
using test_return_value_policy_override::nocopy; | ||
using test_return_value_policy_override::obj; | ||
using test_return_value_policy_override::some_type; | ||
|
||
namespace pybind11 { | ||
|
@@ -51,6 +109,9 @@ struct type_caster<some_type> : type_caster_base<some_type> { | |
} // namespace detail | ||
} // namespace pybind11 | ||
|
||
PYBIND11_SMART_HOLDER_TYPE_CASTERS(obj) | ||
PYBIND11_SMART_HOLDER_TYPE_CASTERS(nocopy) | ||
|
||
TEST_SUBMODULE(return_value_policy_override, m) { | ||
m.def("return_value_with_default_policy", []() { return some_type(); }); | ||
m.def( | ||
|
@@ -79,4 +140,39 @@ TEST_SUBMODULE(return_value_policy_override, m) { | |
return &value; | ||
}, | ||
py::return_value_policy::_clif_automatic); | ||
|
||
py::classh<obj>(m, "object").def(py::init<std::string>()).def_readonly("mtxt", &obj::mtxt); | ||
m.def( | ||
"return_object_value_with_policy_clif_automatic", | ||
[]() { return obj("value"); }, | ||
py::return_value_policy::_clif_automatic); | ||
// test_return_value_policy_override::return_pointer with default policy | ||
// causes crash | ||
m.def("return_object_pointer_with_policy_clif_automatic", | ||
&test_return_value_policy_override::return_pointer, | ||
py::return_value_policy::_clif_automatic); | ||
// test_return_value_policy_override::return_const_pointer with default | ||
// policy causes crash | ||
m.def("return_object_const_pointer_with_policy_clif_automatic", | ||
&test_return_value_policy_override::return_const_pointer, | ||
py::return_value_policy::_clif_automatic); | ||
m.def("return_object_reference_with_policy_clif_automatic", | ||
&test_return_value_policy_override::return_reference, | ||
py::return_value_policy::_clif_automatic); | ||
m.def("return_object_const_reference_with_policy_clif_automatic", | ||
&test_return_value_policy_override::return_const_reference, | ||
py::return_value_policy::_clif_automatic); | ||
m.def("return_object_unique_ptr_with_policy_clif_automatic", | ||
&test_return_value_policy_override::return_unique_pointer, | ||
py::return_value_policy::_clif_automatic); | ||
m.def("return_object_shared_ptr_with_policy_clif_automatic", | ||
&test_return_value_policy_override::return_shared_pointer, | ||
py::return_value_policy::_clif_automatic); | ||
|
||
py::classh<nocopy>(m, "nocopy") | ||
.def(py::init<std::string>()) | ||
.def_readonly("mtxt", &nocopy::mtxt); | ||
m.def("return_nocopy_reference_with_policy_clif_automatic", | ||
&test_return_value_policy_override::return_reference_nocopy, | ||
py::return_value_policy::_clif_automatic); | ||
} |
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
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.
obj
is usually used for instances, it's surprising as a type name.The combinations of copyable/movable keep coming up, it would be good have some systematic naming that we could use throughout. What do you think about
?
With a comment like:
Question that's maybe beyond this PR: do we have (or would it be safest) to add tests for the other two combinations?
IIUC here you have only
type_cp1_mv1
andtype_cp0_mv1
, is that correct?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.
Thanks for your suggestions. I updated the PR with more tests.