New test_class_sh_property_bakein.py

Use cases in the wild:

* `test_readonly_char6_member()`: 4410c44ae6/torch/csrc/cuda/Module.cpp (L961)

* `test_readonly_const_char_ptr_member()`: 862a439a84/include/permonst.h (L43)
This commit is contained in:
Ralf W. Grosse-Kunstleve 2024-07-13 20:45:31 -07:00
parent 2600bb67c5
commit 3406be6877
4 changed files with 42 additions and 1 deletions

View File

@ -1636,7 +1636,7 @@ struct property_cpp_function<
D,
detail::enable_if_t<
detail::all_of<std::is_base_of<detail::type_caster_base<T>, detail::type_caster<T>>,
std::is_base_of<detail::type_caster_base<D>, detail::type_caster<D>>,
std::is_base_of<detail::type_caster_base<D>, detail::make_caster<D>>,
std::is_pointer<D>>::value>> {
using drp = typename std::remove_pointer<D>::type;
@ -1686,6 +1686,7 @@ struct property_cpp_function<
detail::all_of<std::is_base_of<detail::type_caster_base<T>, detail::type_caster<T>>,
std::is_base_of<detail::type_caster_base<D>, detail::type_caster<D>>,
detail::none_of<std::is_pointer<D>,
std::is_array<D>,
detail::is_instantiation<std::unique_ptr, D>,
detail::is_instantiation<std::shared_ptr, D>>>::value>> {

View File

@ -127,6 +127,7 @@ set(PYBIND11_TEST_FILES
test_class_sh_mi_thunks
test_class_sh_module_local.py
test_class_sh_property
test_class_sh_property_bakein
test_class_sh_property_non_owning
test_class_sh_shared_ptr_copy_move
test_class_sh_trampoline_basic

View File

@ -0,0 +1,26 @@
#include "pybind11_tests.h"
namespace test_class_sh_property_bakein {
struct WithCharArrayMember {
WithCharArrayMember() { std::strcpy(char6_member, "Char6"); }
char char6_member[6];
};
struct WithConstCharPtrMember {
const char *const_char_ptr_member = "ConstChar*";
};
} // namespace test_class_sh_property_bakein
TEST_SUBMODULE(class_sh_property_bakein, m) {
using namespace test_class_sh_property_bakein;
py::class_<WithCharArrayMember>(m, "WithCharArrayMember")
.def(py::init<>())
.def_readonly("char6_member", &WithCharArrayMember::char6_member);
py::class_<WithConstCharPtrMember>(m, "WithConstCharPtrMember")
.def(py::init<>())
.def_readonly("const_char_ptr_member", &WithConstCharPtrMember::const_char_ptr_member);
}

View File

@ -0,0 +1,13 @@
from __future__ import annotations
from pybind11_tests import class_sh_property_bakein as m
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*"