Skip to content

Commit 31cf087

Browse files
committed
Add tests for return_as_bytes with def_property.
1 parent b0e936a commit 31cf087

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

tests/test_builtin_casters.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ struct ConstRefCasted {
1515
int tag;
1616
};
1717

18+
struct StringAttr {
19+
StringAttr(std::string v) : value(v) {}
20+
std::string value;
21+
};
22+
1823
PYBIND11_NAMESPACE_BEGIN(pybind11)
1924
PYBIND11_NAMESPACE_BEGIN(detail)
2025
template <>
@@ -389,6 +394,12 @@ TEST_SUBMODULE(builtin_casters, m) {
389394
"invalid_utf8_char_array_as_bytes",
390395
[]() { return "\xba\xd0\xba\xd0"; },
391396
py::return_value_policy::return_as_bytes);
397+
py::class_<StringAttr>(m, "StringAttr")
398+
.def(py::init<std::string>())
399+
.def_property("value",
400+
py::cpp_function([](StringAttr &self) { return self.value; },
401+
py::return_value_policy::return_as_bytes),
402+
py::cpp_function([](StringAttr &self, std::string v) { self.value = v; }));
392403
#ifdef PYBIND11_HAS_STRING_VIEW
393404
m.def(
394405
"invalid_utf8_string_view_as_bytes",

tests/test_builtin_casters.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,5 +532,9 @@ def test_return_as_bytes_policy():
532532
with pytest.raises(UnicodeDecodeError):
533533
m.invalid_utf8_string_as_str()
534534
assert m.invalid_utf8_char_array_as_bytes() == expected_return_value
535+
obj = m.StringAttr(expected_return_value)
536+
assert obj.value == expected_return_value
537+
obj.value = "123"
538+
assert obj.value == b"123"
535539
if hasattr(m, "has_string_view"):
536540
assert m.invalid_utf8_string_view_as_bytes() == expected_return_value

0 commit comments

Comments
 (0)