Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wangxf123456 committed Jul 28, 2023
1 parent 4b612e4 commit b6e2c8c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
1 change: 1 addition & 0 deletions include/pybind11/cast.h
Original file line number Diff line number Diff line change
Expand Up @@ -1171,6 +1171,7 @@ struct return_value_policy_override<
static return_value_policy policy(return_value_policy p) {
return !std::is_lvalue_reference<Return>::value && !std::is_pointer<Return>::value
&& p != return_value_policy::_clif_automatic
&& p != return_value_policy::_return_as_bytes
? return_value_policy::move
: p;
}
Expand Down
15 changes: 8 additions & 7 deletions include/pybind11/stl.h
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,9 @@ struct set_caster {

template <typename T>
static handle cast(T &&src, const return_value_policy_pack &rvpp, handle parent) {
return_value_policy_pack rvpp_local = rvpp;
return_value_policy_pack rvpp_local = rvpp.get(0);
if (!std::is_lvalue_reference<T>::value) {
rvpp_local = rvpp.override_policy(return_value_policy_override<Key>::policy);
rvpp_local = rvpp_local.override_policy(return_value_policy_override<Key>::policy);
}
pybind11::set s;
for (auto &&value : src) {
Expand Down Expand Up @@ -323,9 +323,9 @@ struct list_caster {
public:
template <typename T>
static handle cast(T &&src, const return_value_policy_pack &rvpp, handle parent) {
return_value_policy_pack rvpp_local = rvpp;
return_value_policy_pack rvpp_local = rvpp.get(0);
if (!std::is_lvalue_reference<T>::value) {
rvpp_local = rvpp.override_policy(return_value_policy_override<Value>::policy);
rvpp_local = rvpp_local.override_policy(return_value_policy_override<Value>::policy);
}
list l(src.size());
ssize_t index = 0;
Expand Down Expand Up @@ -406,11 +406,12 @@ struct array_caster {

template <typename T>
static handle cast(T &&src, const return_value_policy_pack &rvpp, handle parent) {
return_value_policy_pack rvpp_local = rvpp.get(0);
list l(src.size());
ssize_t index = 0;
for (auto &&value : src) {
auto value_ = reinterpret_steal<object>(
value_conv::cast(detail::forward_like<T>(value), rvpp, parent));
value_conv::cast(detail::forward_like<T>(value), rvpp_local, parent));
if (!value_) {
return handle();
}
Expand Down Expand Up @@ -461,9 +462,9 @@ struct optional_caster {
if (!src) {
return none().release();
}
return_value_policy_pack rvpp_local = rvpp;
return_value_policy_pack rvpp_local = rvpp.get(0);
if (!std::is_lvalue_reference<T>::value) {
rvpp_local = rvpp.override_policy(return_value_policy_override<Value>::policy);
rvpp_local = rvpp_local.override_policy(return_value_policy_override<Value>::policy);
}
// NOLINTNEXTLINE(bugprone-unchecked-optional-access)
return value_conv::cast(*std::forward<T>(src), rvpp_local, parent);
Expand Down
16 changes: 8 additions & 8 deletions tests/test_return_value_policy_pack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -311,41 +311,41 @@ TEST_SUBMODULE(return_value_policy_pack, m) {
m.def(
"return_set_sb",
[]() { return return_set_pair_string(); },
py::return_value_policy_pack({rvpc, rvpb}));
py::return_value_policy_pack({{rvpc, rvpb}}));
m.def(
"return_set_bs",
[]() {
// Ensure cast(T*, ...) overload supports rvpp:
static auto *s = new SetPairString(return_set_pair_string());
return s;
},
py::return_value_policy_pack({rvpb, rvpc}));
py::return_value_policy_pack({{rvpb, rvpc}}));

m.def(
"return_vector_sb",
[]() { return return_vector_pair_string(); },
py::return_value_policy_pack({rvpc, rvpb}));
py::return_value_policy_pack({{rvpc, rvpb}}));
m.def(
"return_vector_bs",
[]() {
// Ensure cast(T*, ...) overload supports rvpp:
static auto *v = new VectorPairString(return_vector_pair_string());
return v;
},
py::return_value_policy_pack({rvpb, rvpc}));
py::return_value_policy_pack({{rvpb, rvpc}}));

m.def(
"return_array_sb",
[]() { return return_array_pair_string(); },
py::return_value_policy_pack({rvpc, rvpb}));
py::return_value_policy_pack({{rvpc, rvpb}}));
m.def(
"return_array_bs",
[]() {
// Ensure cast(T*, ...) overload supports rvpp:
static auto *a = new ArrayPairString(return_array_pair_string());
return a;
},
py::return_value_policy_pack({rvpb, rvpc}));
py::return_value_policy_pack({{rvpb, rvpc}}));

m.attr("PYBIND11_HAS_OPTIONAL") =
#if !defined(PYBIND11_HAS_OPTIONAL)
Expand All @@ -355,15 +355,15 @@ TEST_SUBMODULE(return_value_policy_pack, m) {
m.def(
"return_optional_sb",
[]() { return return_optional_pair_string(); },
py::return_value_policy_pack({rvpc, rvpb}));
py::return_value_policy_pack({{rvpc, rvpb}}));
m.def(
"return_optional_bs",
[]() {
// Ensure cast(T*, ...) overload supports rvpp:
static auto *o = new OptionalPairString(return_optional_pair_string());
return o;
},
py::return_value_policy_pack({rvpb, rvpc}));
py::return_value_policy_pack({{rvpb, rvpc}}));
#endif

m.attr("PYBIND11_HAS_VARIANT") =
Expand Down

0 comments on commit b6e2c8c

Please sign in to comment.