Skip to content

Commit 31e52b2

Browse files
authored
Add holder caster traits tests in test_smart_ptr.cpp,py (#5603)
* Add test_smart_ptr.cpp,py holder_caster_traits_test * [skip ci] Resolve clang-tidy error with a NOLINT Tested with clang-tidy locally.
1 parent e38beb9 commit 31e52b2

File tree

3 files changed

+84
-2
lines changed

3 files changed

+84
-2
lines changed

tests/test_native_enum.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,13 @@ struct is_proto_enum<some_proto_enum> : std::true_type {};
4545
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
4646
PYBIND11_NAMESPACE_BEGIN(detail)
4747

48+
// Negate this condition to demonstrate "ambiguous template instantiation" compilation error:
49+
#if defined(PYBIND11_HAS_NATIVE_ENUM)
4850
template <typename ProtoEnumType>
4951
struct type_caster_enum_type_enabled<
5052
ProtoEnumType,
51-
detail::enable_if_t<test_native_enum::is_proto_enum<ProtoEnumType>::value>> : std::false_type {
52-
};
53+
enable_if_t<test_native_enum::is_proto_enum<ProtoEnumType>::value>> : std::false_type {};
54+
#endif
5355

5456
// https://github.com/pybind/pybind11_protobuf/blob/a50899c2eb604fc5f25deeb8901eff6231b8b3c0/pybind11_protobuf/enum_type_caster.h#L101-L105
5557
template <typename ProtoEnumType>

tests/test_smart_ptr.cpp

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,72 @@ PYBIND11_DECLARE_HOLDER_TYPE(T, custom_unique_ptr<T>)
290290
PYBIND11_DECLARE_HOLDER_TYPE(T, shared_ptr_with_addressof_operator<T>)
291291
PYBIND11_DECLARE_HOLDER_TYPE(T, unique_ptr_with_addressof_operator<T>)
292292

293+
namespace holder_caster_traits_test {
294+
struct example_base {};
295+
} // namespace holder_caster_traits_test
296+
297+
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
298+
PYBIND11_NAMESPACE_BEGIN(detail)
299+
300+
// Negate this condition to demonstrate "ambiguous template instantiation" compilation error:
301+
#if defined(PYBIND11_HAS_INTERNALS_WITH_SMART_HOLDER_SUPPORT)
302+
template <typename ExampleType>
303+
struct copyable_holder_caster_shared_ptr_with_smart_holder_support_enabled<
304+
ExampleType,
305+
enable_if_t<std::is_base_of<holder_caster_traits_test::example_base, ExampleType>::value>>
306+
: std::false_type {};
307+
#endif
308+
309+
template <typename ExampleType, typename HolderType>
310+
struct copyable_holder_caster<
311+
ExampleType,
312+
HolderType,
313+
enable_if_t<std::is_base_of<holder_caster_traits_test::example_base, ExampleType>::value>> {
314+
static constexpr auto name = const_name<ExampleType>();
315+
316+
static handle cast(const HolderType &, return_value_policy, handle) {
317+
return str("copyable_holder_caster_traits_test").release();
318+
}
319+
};
320+
321+
// Negate this condition to demonstrate "ambiguous template instantiation" compilation error:
322+
#if defined(PYBIND11_HAS_INTERNALS_WITH_SMART_HOLDER_SUPPORT)
323+
template <typename ExampleType>
324+
struct move_only_holder_caster_unique_ptr_with_smart_holder_support_enabled<
325+
ExampleType,
326+
enable_if_t<std::is_base_of<holder_caster_traits_test::example_base, ExampleType>::value>>
327+
: std::false_type {};
328+
#endif
329+
330+
template <typename ExampleType, typename HolderType>
331+
struct move_only_holder_caster<
332+
ExampleType,
333+
HolderType,
334+
enable_if_t<std::is_base_of<holder_caster_traits_test::example_base, ExampleType>::value>> {
335+
static constexpr auto name = const_name<ExampleType>();
336+
337+
static handle cast(const HolderType &, return_value_policy, handle) {
338+
return str("move_only_holder_caster_traits_test").release();
339+
}
340+
};
341+
342+
PYBIND11_NAMESPACE_END(detail)
343+
PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)
344+
345+
namespace holder_caster_traits_test {
346+
347+
struct example_drvd : example_base {};
348+
349+
void wrap(py::module_ &m) {
350+
m.def("return_std_shared_ptr_example_drvd",
351+
// NOLINTNEXTLINE(modernize-make-shared)
352+
[]() { return std::shared_ptr<example_drvd>(new example_drvd()); });
353+
m.def("return_std_unique_ptr_example_drvd",
354+
[]() { return std::unique_ptr<example_drvd>(new example_drvd()); });
355+
}
356+
357+
} // namespace holder_caster_traits_test
358+
293359
TEST_SUBMODULE(smart_ptr, m) {
294360
// Please do not interleave `struct` and `class` definitions with bindings code,
295361
// but implement `struct`s and `class`es in the anonymous namespace above.
@@ -482,4 +548,6 @@ TEST_SUBMODULE(smart_ptr, m) {
482548
.def(py::init<>())
483549
.def_readwrite("ptr",
484550
&ContainerUsingPrivateESFT::ptr); // <- access ESFT through shared_ptr
551+
552+
holder_caster_traits_test::wrap(m);
485553
}

tests/test_smart_ptr.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,3 +338,15 @@ def test_private_esft_tolerance():
338338
_ = c.ptr # getattr
339339
with pytest.raises(TypeError):
340340
c.ptr = None # setattr
341+
342+
343+
def test_copyable_holder_caster_shared_ptr_with_smart_holder_support_enabled():
344+
assert (
345+
m.return_std_shared_ptr_example_drvd() == "copyable_holder_caster_traits_test"
346+
)
347+
348+
349+
def test_move_only_holder_caster_shared_ptr_with_smart_holder_support_enabled():
350+
assert (
351+
m.return_std_unique_ptr_example_drvd() == "move_only_holder_caster_traits_test"
352+
)

0 commit comments

Comments
 (0)