|
11 | 11 |
|
12 | 12 | #include "pybind11_tests.h" |
13 | 13 |
|
| 14 | +#include <utility> |
| 15 | + |
14 | 16 | struct ConstRefCasted { |
15 | 17 | int tag; |
16 | 18 | }; |
17 | 19 |
|
| 20 | +struct StringAttr { |
| 21 | + explicit StringAttr(std::string v) : value(std::move(v)) {} |
| 22 | + std::string value; |
| 23 | +}; |
| 24 | + |
18 | 25 | PYBIND11_NAMESPACE_BEGIN(pybind11) |
19 | 26 | PYBIND11_NAMESPACE_BEGIN(detail) |
20 | 27 | template <> |
@@ -378,4 +385,28 @@ TEST_SUBMODULE(builtin_casters, m) { |
378 | 385 | m.def("takes_const_ref", [](const ConstRefCasted &x) { return x.tag; }); |
379 | 386 | m.def("takes_const_ref_wrap", |
380 | 387 | [](std::reference_wrapper<const ConstRefCasted> x) { return x.get().tag; }); |
| 388 | + |
| 389 | + // test return_value_policy::_return_as_bytes |
| 390 | + m.def( |
| 391 | + "invalid_utf8_string_as_bytes", |
| 392 | + []() { return std::string("\xba\xd0\xba\xd0"); }, |
| 393 | + py::return_value_policy::_return_as_bytes); |
| 394 | + m.def("invalid_utf8_string_as_str", []() { return std::string("\xba\xd0\xba\xd0"); }); |
| 395 | + m.def( |
| 396 | + "invalid_utf8_char_array_as_bytes", |
| 397 | + []() { return "\xba\xd0\xba\xd0"; }, |
| 398 | + py::return_value_policy::_return_as_bytes); |
| 399 | + py::class_<StringAttr>(m, "StringAttr") |
| 400 | + .def(py::init<std::string>()) |
| 401 | + .def_property( |
| 402 | + "value", |
| 403 | + py::cpp_function([](StringAttr &self) { return self.value; }, |
| 404 | + py::return_value_policy::_return_as_bytes), |
| 405 | + py::cpp_function([](StringAttr &self, std::string v) { self.value = std::move(v); })); |
| 406 | +#ifdef PYBIND11_HAS_STRING_VIEW |
| 407 | + m.def( |
| 408 | + "invalid_utf8_string_view_as_bytes", |
| 409 | + []() { return std::string_view("\xba\xd0\xba\xd0"); }, |
| 410 | + py::return_value_policy::_return_as_bytes); |
| 411 | +#endif |
381 | 412 | } |
0 commit comments