From 0c53713963d332ea5e97f61d8299069d6509b04f Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Tue, 23 Jan 2024 08:07:17 -0800 Subject: [PATCH] Reorganize tests so that it is easy to backport the type_caster_base.h change plus test. --- tests/CMakeLists.txt | 1 - tests/pybind11_tests.h | 11 +++++++ tests/test_class_sh_basic.cpp | 11 +++++++ tests/test_class_sh_basic.py | 6 ++++ tests/test_copy_move.cpp | 12 ++++++++ tests/test_copy_move.py | 6 ++++ tests/test_type_caster_addressof.cpp | 44 ---------------------------- tests/test_type_caster_addressof.py | 8 ----- 8 files changed, 46 insertions(+), 53 deletions(-) delete mode 100644 tests/test_type_caster_addressof.cpp delete mode 100644 tests/test_type_caster_addressof.py diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 59b347d3..91cd230b 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -178,7 +178,6 @@ set(PYBIND11_TEST_FILES test_stl_binders test_tagbased_polymorphic test_thread - test_type_caster_addressof test_type_caster_odr_guard_1 test_type_caster_odr_guard_2 test_type_caster_pyobject_ptr diff --git a/tests/pybind11_tests.h b/tests/pybind11_tests.h index a7c00c2f..ec1fc21f 100644 --- a/tests/pybind11_tests.h +++ b/tests/pybind11_tests.h @@ -3,6 +3,8 @@ #include #include +#include + namespace py = pybind11; using namespace pybind11::literals; @@ -52,6 +54,15 @@ union IntFloat { float f; }; +class UnusualOpRef { +public: + using NonTrivialType = std::shared_ptr; // Almost any non-trivial type will do. + NonTrivialType operator&() { return non_trivial_member; } // UNUSUAL operator. + +private: + NonTrivialType non_trivial_member; +}; + /// Custom cast-only type that casts to a string "rvalue" or "lvalue" depending on the cast /// context. Used to test recursive casters (e.g. std::tuple, stl containers). struct RValueCaster {}; diff --git a/tests/test_class_sh_basic.cpp b/tests/test_class_sh_basic.cpp index a294f7bb..fb939518 100644 --- a/tests/test_class_sh_basic.cpp +++ b/tests/test_class_sh_basic.cpp @@ -129,12 +129,17 @@ struct SharedPtrStash { void Add(const std::shared_ptr &obj) { stash.push_back(obj); } }; +class LocalUnusualOpRef : UnusualOpRef {}; // To avoid clashing with `py::class_`. +py::object CastUnusualOpRefConstRef(const LocalUnusualOpRef &cref) { return py::cast(cref); } +py::object CastUnusualOpRefMovable(LocalUnusualOpRef &&mvbl) { return py::cast(std::move(mvbl)); } + } // namespace class_sh_basic } // namespace pybind11_tests PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_basic::atyp) PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_basic::uconsumer) PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_basic::SharedPtrStash) +PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_basic::LocalUnusualOpRef) namespace pybind11_tests { namespace class_sh_basic { @@ -227,6 +232,12 @@ TEST_SUBMODULE(class_sh_basic, m) { "rtrn_uq_automatic_reference", []() { return std::unique_ptr(new atyp("rtrn_uq_automatic_reference")); }, pybind11::return_value_policy::automatic_reference); + + py::classh(m, "LocalUnusualOpRef"); + m.def("CallCastUnusualOpRefConstRef", + []() { return CastUnusualOpRefConstRef(LocalUnusualOpRef()); }); + m.def("CallCastUnusualOpRefMovable", + []() { return CastUnusualOpRefMovable(LocalUnusualOpRef()); }); } } // namespace class_sh_basic diff --git a/tests/test_class_sh_basic.py b/tests/test_class_sh_basic.py index c7ff82c8..dbb8f9b9 100644 --- a/tests/test_class_sh_basic.py +++ b/tests/test_class_sh_basic.py @@ -216,3 +216,9 @@ def test_function_signatures(doc): def test_unique_ptr_return_value_policy_automatic_reference(): assert m.get_mtxt(m.rtrn_uq_automatic_reference()) == "rtrn_uq_automatic_reference" + + +def test_unusual_op_ref(): + # Merely to test that this still exists and built successfully. + assert m.CallCastUnusualOpRefConstRef().__class__.__name__ == "LocalUnusualOpRef" + assert m.CallCastUnusualOpRefMovable().__class__.__name__ == "LocalUnusualOpRef" diff --git a/tests/test_copy_move.cpp b/tests/test_copy_move.cpp index 087b4852..1291a133 100644 --- a/tests/test_copy_move.cpp +++ b/tests/test_copy_move.cpp @@ -157,6 +157,13 @@ struct type_caster { PYBIND11_NAMESPACE_END(detail) PYBIND11_NAMESPACE_END(pybind11) +namespace { + +py::object CastUnusualOpRefConstRef(const UnusualOpRef &cref) { return py::cast(cref); } +py::object CastUnusualOpRefMovable(UnusualOpRef &&mvbl) { return py::cast(std::move(mvbl)); } + +} // namespace + TEST_SUBMODULE(copy_move_policies, m) { // test_lacking_copy_ctor py::class_(m, "lacking_copy_ctor") @@ -295,6 +302,11 @@ TEST_SUBMODULE(copy_move_policies, m) { // Make sure that cast from pytype rvalue to other pytype works m.def("get_pytype_rvalue_castissue", [](double i) { return py::float_(i).cast(); }); + py::class_(m, "UnusualOpRef"); + m.def("CallCastUnusualOpRefConstRef", + []() { return CastUnusualOpRefConstRef(UnusualOpRef()); }); + m.def("CallCastUnusualOpRefMovable", []() { return CastUnusualOpRefMovable(UnusualOpRef()); }); + // Mimic situation generated by PyCLIF-pybind11. // Requires `case return_value_policy::_clif_automatic` in `type_caster_base`. struct PyCastUsingClifAutomaticTestType {}; diff --git a/tests/test_copy_move.py b/tests/test_copy_move.py index 16f7dd5e..7cac8d43 100644 --- a/tests/test_copy_move.py +++ b/tests/test_copy_move.py @@ -132,6 +132,12 @@ def test_pytype_rvalue_cast(): assert value == 1 +def test_unusual_op_ref(): + # Merely to test that this still exists and built successfully. + assert m.CallCastUnusualOpRefConstRef().__class__.__name__ == "UnusualOpRef" + assert m.CallCastUnusualOpRefMovable().__class__.__name__ == "UnusualOpRef" + + def test_py_cast_using_clif_automatic(): obj = m.py_cast_using_clif_automatic() assert obj.__class__.__name__ == "PyCastUsingClifAutomaticTestType" diff --git a/tests/test_type_caster_addressof.cpp b/tests/test_type_caster_addressof.cpp deleted file mode 100644 index 54a9b8a0..00000000 --- a/tests/test_type_caster_addressof.cpp +++ /dev/null @@ -1,44 +0,0 @@ -#include - -#include "pybind11_tests.h" - -#include - -namespace pybind11_tests { -namespace type_caster_addressof { - -template // Using int as a trick to easily generate a series of types. -class UnusualOpRef { -public: - using NonTrivialType = std::shared_ptr; // Almost any non-trivial type will do. - NonTrivialType operator&() { return non_trivial_member; } - -private: - NonTrivialType non_trivial_member; -}; - -} // namespace type_caster_addressof -} // namespace pybind11_tests - -PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::type_caster_addressof::UnusualOpRef<1>) - -namespace pybind11_tests { -namespace type_caster_addressof { - -py::object CastConstRef0(const UnusualOpRef<0> &cref) { return py::cast(cref); } -py::object CastConstRef1(const UnusualOpRef<1> &cref) { return py::cast(cref); } -py::object CastMovable0(UnusualOpRef<0> &&mvbl) { return py::cast(std::move(mvbl)); } -py::object CastMovable1(UnusualOpRef<1> &&mvbl) { return py::cast(std::move(mvbl)); } - -} // namespace type_caster_addressof -} // namespace pybind11_tests - -TEST_SUBMODULE(type_caster_addressof, m) { - using namespace pybind11_tests::type_caster_addressof; - py::class_>(m, "UnusualOpRef0"); - py::classh>(m, "UnusualOpRef1"); - m.def("CallCastConstRef0", []() { return CastConstRef0(UnusualOpRef<0>()); }); - m.def("CallCastConstRef1", []() { return CastConstRef1(UnusualOpRef<1>()); }); - m.def("CallCastMovable0", []() { return CastMovable0(UnusualOpRef<0>()); }); - m.def("CallCastMovable1", []() { return CastMovable1(UnusualOpRef<1>()); }); -} diff --git a/tests/test_type_caster_addressof.py b/tests/test_type_caster_addressof.py deleted file mode 100644 index 1089abd6..00000000 --- a/tests/test_type_caster_addressof.py +++ /dev/null @@ -1,8 +0,0 @@ -from pybind11_tests import type_caster_addressof as m - - -def test_merely_that_it_actually_built(): - assert m.CallCastConstRef0().__class__.__name__ == "UnusualOpRef0" - assert m.CallCastConstRef1().__class__.__name__ == "UnusualOpRef1" - assert m.CallCastMovable0().__class__.__name__ == "UnusualOpRef0" - assert m.CallCastMovable1().__class__.__name__ == "UnusualOpRef1"