Backport non-functional test_class_sh_*.cpp changes from PR #5213: 1. To avoid compiler warnings for unused code in the unnamed namespace. 2. To avoid clang-format changes. (#5258)

This commit is contained in:
Ralf W. Grosse-Kunstleve 2024-07-22 15:56:42 +07:00 committed by GitHub
parent 51a968c954
commit ec557ff612
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 60 additions and 39 deletions

View File

@ -50,6 +50,7 @@ PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::FooSmHld)
namespace pybind11_tests { namespace pybind11_tests {
TEST_SUBMODULE(class_sh_shared_ptr_copy_move, m) { TEST_SUBMODULE(class_sh_shared_ptr_copy_move, m) {
#if true // Trick to avoid clang-format changes, in support of PR #5213.
namespace py = pybind11; namespace py = pybind11;
py::class_<FooShPtr, std::shared_ptr<FooShPtr>>(m, "FooShPtr") py::class_<FooShPtr, std::shared_ptr<FooShPtr>>(m, "FooShPtr")
@ -57,30 +58,30 @@ TEST_SUBMODULE(class_sh_shared_ptr_copy_move, m) {
py::classh<FooSmHld>(m, "FooSmHld").def("get_history", &FooSmHld::get_history); py::classh<FooSmHld>(m, "FooSmHld").def("get_history", &FooSmHld::get_history);
auto outer = py::class_<Outer>(m, "Outer").def(py::init()); auto outer = py::class_<Outer>(m, "Outer").def(py::init());
#define MAKE_PROP(PropTyp) \ # define MAKE_PROP(PropTyp) \
MAKE_PROP_FOO(ShPtr, PropTyp) \ MAKE_PROP_FOO(ShPtr, PropTyp) \
MAKE_PROP_FOO(SmHld, PropTyp) MAKE_PROP_FOO(SmHld, PropTyp)
#define MAKE_PROP_FOO(FooTyp, PropTyp) \ # define MAKE_PROP_FOO(FooTyp, PropTyp) \
.def_##PropTyp(#FooTyp "_" #PropTyp "_default", &Outer::FooTyp) \ .def_##PropTyp(#FooTyp "_" #PropTyp "_default", &Outer::FooTyp) \
.def_##PropTyp( \ .def_##PropTyp( \
#FooTyp "_" #PropTyp "_copy", &Outer::FooTyp, py::return_value_policy::copy) \ #FooTyp "_" #PropTyp "_copy", &Outer::FooTyp, py::return_value_policy::copy) \
.def_##PropTyp( \ .def_##PropTyp( \
#FooTyp "_" #PropTyp "_move", &Outer::FooTyp, py::return_value_policy::move) #FooTyp "_" #PropTyp "_move", &Outer::FooTyp, py::return_value_policy::move)
outer MAKE_PROP(readonly) MAKE_PROP(readwrite); outer MAKE_PROP(readonly) MAKE_PROP(readwrite);
#undef MAKE_PROP_FOO # undef MAKE_PROP_FOO
#define MAKE_PROP_FOO(FooTyp, PropTyp) \ # define MAKE_PROP_FOO(FooTyp, PropTyp) \
.def_##PropTyp(#FooTyp "_property_" #PropTyp "_default", &Outer::FooTyp) \ .def_##PropTyp(#FooTyp "_property_" #PropTyp "_default", &Outer::FooTyp) \
.def_property_##PropTyp(#FooTyp "_property_" #PropTyp "_copy", \ .def_property_##PropTyp(#FooTyp "_property_" #PropTyp "_copy", \
&Outer::get##FooTyp, \ &Outer::get##FooTyp, \
py::return_value_policy::copy) \ py::return_value_policy::copy) \
.def_property_##PropTyp(#FooTyp "_property_" #PropTyp "_move", \ .def_property_##PropTyp(#FooTyp "_property_" #PropTyp "_move", \
&Outer::get##FooTyp, \ &Outer::get##FooTyp, \
py::return_value_policy::move) py::return_value_policy::move)
outer MAKE_PROP(readonly); outer MAKE_PROP(readonly);
#undef MAKE_PROP_FOO # undef MAKE_PROP_FOO
#undef MAKE_PROP # undef MAKE_PROP
m.def("test_ShPtr_copy", []() { m.def("test_ShPtr_copy", []() {
auto o = std::make_shared<FooShPtr>("copy"); auto o = std::make_shared<FooShPtr>("copy");
@ -107,6 +108,7 @@ TEST_SUBMODULE(class_sh_shared_ptr_copy_move, m) {
l.append(std::move(o)); l.append(std::move(o));
return l; return l;
}); });
#endif
} }
} // namespace pybind11_tests } // namespace pybind11_tests

View File

@ -76,11 +76,12 @@ void wrap(py::module_ m, const char *py_class_name) {
} // namespace class_sh_trampoline_basic } // namespace class_sh_trampoline_basic
} // namespace pybind11_tests } // namespace pybind11_tests
PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_trampoline_basic::Abase<0>) using namespace pybind11_tests::class_sh_trampoline_basic;
PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_trampoline_basic::Abase<1>)
PYBIND11_SMART_HOLDER_TYPE_CASTERS(Abase<0>)
PYBIND11_SMART_HOLDER_TYPE_CASTERS(Abase<1>)
TEST_SUBMODULE(class_sh_trampoline_basic, m) { TEST_SUBMODULE(class_sh_trampoline_basic, m) {
using namespace pybind11_tests::class_sh_trampoline_basic;
wrap<0>(m, "Abase0"); wrap<0>(m, "Abase0");
wrap<1>(m, "Abase1"); wrap<1>(m, "Abase1");
} }

View File

@ -10,7 +10,8 @@
#include <string> #include <string>
#include <utility> #include <utility>
namespace { namespace pybind11_tests {
namespace class_sh_trampoline_self_life_support {
struct Big5 { // Also known as "rule of five". struct Big5 { // Also known as "rule of five".
std::string history; std::string history;
@ -41,7 +42,10 @@ struct Big5Trampoline : Big5, py::trampoline_self_life_support {
using Big5::Big5; using Big5::Big5;
}; };
} // namespace } // namespace class_sh_trampoline_self_life_support
} // namespace pybind11_tests
using namespace pybind11_tests::class_sh_trampoline_self_life_support;
PYBIND11_SMART_HOLDER_TYPE_CASTERS(Big5) PYBIND11_SMART_HOLDER_TYPE_CASTERS(Big5)

View File

@ -8,7 +8,8 @@
#include <memory> #include <memory>
#include <string> #include <string>
namespace { namespace pybind11_tests {
namespace class_sh_trampoline_shared_from_this {
struct Sft : std::enable_shared_from_this<Sft> { struct Sft : std::enable_shared_from_this<Sft> {
std::string history; std::string history;
@ -98,7 +99,10 @@ std::shared_ptr<Sft> make_pure_cpp_sft_shd_ptr(const std::string &history_seed)
std::shared_ptr<Sft> pass_through_shd_ptr(const std::shared_ptr<Sft> &obj) { return obj; } std::shared_ptr<Sft> pass_through_shd_ptr(const std::shared_ptr<Sft> &obj) { return obj; }
} // namespace } // namespace class_sh_trampoline_shared_from_this
} // namespace pybind11_tests
using namespace pybind11_tests::class_sh_trampoline_shared_from_this;
PYBIND11_SMART_HOLDER_TYPE_CASTERS(Sft) PYBIND11_SMART_HOLDER_TYPE_CASTERS(Sft)
PYBIND11_SMART_HOLDER_TYPE_CASTERS(SftSharedPtrStash) PYBIND11_SMART_HOLDER_TYPE_CASTERS(SftSharedPtrStash)

View File

@ -7,7 +7,8 @@
#include <utility> #include <utility>
namespace { namespace pybind11_tests {
namespace class_sh_trampoline_shared_ptr_cpp_arg {
// For testing whether a python subclass of a C++ object dies when the // For testing whether a python subclass of a C++ object dies when the
// last python reference is lost // last python reference is lost
@ -51,7 +52,10 @@ struct SpGoAwayTester {
std::shared_ptr<SpGoAway> m_obj; std::shared_ptr<SpGoAway> m_obj;
}; };
} // namespace } // namespace class_sh_trampoline_shared_ptr_cpp_arg
} // namespace pybind11_tests
using namespace pybind11_tests::class_sh_trampoline_shared_ptr_cpp_arg;
PYBIND11_SMART_HOLDER_TYPE_CASTERS(SpBase) PYBIND11_SMART_HOLDER_TYPE_CASTERS(SpBase)
PYBIND11_SMART_HOLDER_TYPE_CASTERS(SpBaseTester) PYBIND11_SMART_HOLDER_TYPE_CASTERS(SpBaseTester)

View File

@ -8,7 +8,8 @@
#include <cstdint> #include <cstdint>
namespace { namespace pybind11_tests {
namespace class_sh_trampoline_unique_ptr {
class Class { class Class {
public: public:
@ -30,11 +31,13 @@ private:
std::uint64_t val_ = 0; std::uint64_t val_ = 0;
}; };
} // namespace } // namespace class_sh_trampoline_unique_ptr
} // namespace pybind11_tests
PYBIND11_SMART_HOLDER_TYPE_CASTERS(Class) PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_trampoline_unique_ptr::Class)
namespace { namespace pybind11_tests {
namespace class_sh_trampoline_unique_ptr {
class PyClass : public Class, public py::trampoline_self_life_support { class PyClass : public Class, public py::trampoline_self_life_support {
public: public:
@ -45,9 +48,12 @@ public:
int foo() const override { PYBIND11_OVERRIDE_PURE(int, Class, foo); } int foo() const override { PYBIND11_OVERRIDE_PURE(int, Class, foo); }
}; };
} // namespace } // namespace class_sh_trampoline_unique_ptr
} // namespace pybind11_tests
TEST_SUBMODULE(class_sh_trampoline_unique_ptr, m) { TEST_SUBMODULE(class_sh_trampoline_unique_ptr, m) {
using namespace pybind11_tests::class_sh_trampoline_unique_ptr;
py::classh<Class, PyClass>(m, "Class") py::classh<Class, PyClass>(m, "Class")
.def(py::init<>()) .def(py::init<>())
.def("set_val", &Class::setVal) .def("set_val", &Class::setVal)

View File

@ -46,13 +46,13 @@ struct CppDerivedVirtualOverrider : CppDerived, py::trampoline_self_life_support
} // namespace class_sh_virtual_py_cpp_mix } // namespace class_sh_virtual_py_cpp_mix
} // namespace pybind11_tests } // namespace pybind11_tests
PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_virtual_py_cpp_mix::Base) using namespace pybind11_tests::class_sh_virtual_py_cpp_mix;
PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_virtual_py_cpp_mix::CppDerivedPlain)
PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_virtual_py_cpp_mix::CppDerived) PYBIND11_SMART_HOLDER_TYPE_CASTERS(Base)
PYBIND11_SMART_HOLDER_TYPE_CASTERS(CppDerivedPlain)
PYBIND11_SMART_HOLDER_TYPE_CASTERS(CppDerived)
TEST_SUBMODULE(class_sh_virtual_py_cpp_mix, m) { TEST_SUBMODULE(class_sh_virtual_py_cpp_mix, m) {
using namespace pybind11_tests::class_sh_virtual_py_cpp_mix;
py::classh<Base, BaseVirtualOverrider>(m, "Base").def(py::init<>()).def("get", &Base::get); py::classh<Base, BaseVirtualOverrider>(m, "Base").def(py::init<>()).def("get", &Base::get);
py::classh<CppDerivedPlain, Base>(m, "CppDerivedPlain").def(py::init<>()); py::classh<CppDerivedPlain, Base>(m, "CppDerivedPlain").def(py::init<>());