Skip to content

[smart_holder] Import additional test code originally added under PR #5213 (test_class_sh_property_bakein) #5256

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 1 commit into from
Jul 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions tests/test_class_sh_property.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ struct Outer {

inline void DisownOuter(std::unique_ptr<Outer>) {}

struct WithCharArrayMember {
WithCharArrayMember() { std::memcpy(char6_member, "Char6", 6); }
char char6_member[6];
};

struct WithConstCharPtrMember {
const char *const_char_ptr_member = "ConstChar*";
};

} // namespace test_class_sh_property

PYBIND11_TYPE_CASTER_BASE_HOLDER(test_class_sh_property::ClassicField,
Expand All @@ -45,6 +54,9 @@ PYBIND11_TYPE_CASTER_BASE_HOLDER(test_class_sh_property::ClassicOuter,
PYBIND11_SMART_HOLDER_TYPE_CASTERS(test_class_sh_property::Field)
PYBIND11_SMART_HOLDER_TYPE_CASTERS(test_class_sh_property::Outer)

PYBIND11_SMART_HOLDER_TYPE_CASTERS(test_class_sh_property::WithCharArrayMember)
PYBIND11_SMART_HOLDER_TYPE_CASTERS(test_class_sh_property::WithConstCharPtrMember)

TEST_SUBMODULE(class_sh_property, m) {
using namespace test_class_sh_property;

Expand Down Expand Up @@ -83,4 +95,12 @@ TEST_SUBMODULE(class_sh_property, m) {
.def_readwrite("m_shcp_readwrite", &Outer::m_shcp);

m.def("DisownOuter", DisownOuter);

py::classh<WithCharArrayMember>(m, "WithCharArrayMember")
.def(py::init<>())
.def_readonly("char6_member", &WithCharArrayMember::char6_member);

py::classh<WithConstCharPtrMember>(m, "WithConstCharPtrMember")
.def(py::init<>())
.def_readonly("const_char_ptr_member", &WithConstCharPtrMember::const_char_ptr_member);
}
10 changes: 10 additions & 0 deletions tests/test_class_sh_property.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,13 @@ def test_unique_ptr_field_proxy_poc(m_attr):
with pytest.raises(AttributeError):
del field_proxy.num
assert field_proxy.num == 82


def test_readonly_char6_member():
obj = m.WithCharArrayMember()
assert obj.char6_member == "Char6"


def test_readonly_const_char_ptr_member():
obj = m.WithConstCharPtrMember()
assert obj.const_char_ptr_member == "ConstChar*"