[smart_holder] Introduce PYBIND11_SMART_HOLDER_DISABLE option. (#5348)

* Step 1: Establish new `PYBIND11_SMART_HOLDER_ENABLED` macro, but only under include/pybind11/

At the stage `PYBIND11_HAS_INTERNALS_WITH_SMART_HOLDER_SUPPORT` and `PYBIND11_SMART_HOLDER_ENABLED` are still equivalent.

* Systematically replace all `PYBIND11_HAS_INTERNALS_WITH_SMART_HOLDER_SUPPORT` with `PYBIND11_SMART_HOLDER_ENABLED` under tests/ and ubench/

As at the previous stage, `PYBIND11_HAS_INTERNALS_WITH_SMART_HOLDER_SUPPORT` and `PYBIND11_SMART_HOLDER_ENABLED` are still equivalent.

* Introduce `PYBIND11_SMART_HOLDER_DISABLE` option.

* `#ifdef` out entire `wrap()` function to avoid `unused-parameter` warning-as-error under macos-13

```
/Users/runner/work/pybind11/pybind11/tests/test_class_sh_trampoline_basic.cpp:67:23: error: unused parameter 'm' [-Werror,-Wunused-parameter]
void wrap(py::module_ m, const char *py_class_name) {
                      ^
/Users/runner/work/pybind11/pybind11/tests/test_class_sh_trampoline_basic.cpp:67:38: error: unused parameter 'py_class_name' [-Werror,-Wunused-parameter]
void wrap(py::module_ m, const char *py_class_name) {
                                     ^
2 errors generated.
```
This commit is contained in:
Ralf W. Grosse-Kunstleve
2024-09-01 14:34:36 -07:00
committed by GitHub
parent a7b91e3326
commit bd8985aa0f
44 changed files with 123 additions and 103 deletions

View File

@@ -34,7 +34,7 @@ struct AbaseAlias : Abase<SerNo> {
}
};
#ifdef PYBIND11_HAS_INTERNALS_WITH_SMART_HOLDER_SUPPORT
#ifdef PYBIND11_SMART_HOLDER_ENABLED
template <>
struct AbaseAlias<1> : Abase<1>, py::trampoline_self_life_support {
using Abase<1>::Abase;
@@ -46,7 +46,7 @@ struct AbaseAlias<1> : Abase<1>, py::trampoline_self_life_support {
other_val);
}
};
#endif // PYBIND11_HAS_INTERNALS_WITH_SMART_HOLDER_SUPPORT
#endif // PYBIND11_SMART_HOLDER_ENABLED
template <int SerNo>
int AddInCppRawPtr(const Abase<SerNo> *obj, int other_val) {
@@ -63,9 +63,9 @@ int AddInCppUniquePtr(std::unique_ptr<Abase<SerNo>> obj, int other_val) {
return obj->Add(other_val) * 100 + 13;
}
#ifdef PYBIND11_SMART_HOLDER_ENABLED
template <int SerNo>
void wrap(py::module_ m, const char *py_class_name) {
#ifdef PYBIND11_HAS_INTERNALS_WITH_SMART_HOLDER_SUPPORT
py::classh<Abase<SerNo>, AbaseAlias<SerNo>>(m, py_class_name)
.def(py::init<int>(), py::arg("val"))
.def("Get", &Abase<SerNo>::Get)
@@ -74,8 +74,8 @@ void wrap(py::module_ m, const char *py_class_name) {
m.def("AddInCppRawPtr", AddInCppRawPtr<SerNo>, py::arg("obj"), py::arg("other_val"));
m.def("AddInCppSharedPtr", AddInCppSharedPtr<SerNo>, py::arg("obj"), py::arg("other_val"));
m.def("AddInCppUniquePtr", AddInCppUniquePtr<SerNo>, py::arg("obj"), py::arg("other_val"));
#endif
}
#endif
} // namespace class_sh_trampoline_basic
} // namespace pybind11_tests
@@ -86,13 +86,13 @@ PYBIND11_SMART_HOLDER_TYPE_CASTERS(Abase<0>)
PYBIND11_SMART_HOLDER_TYPE_CASTERS(Abase<1>)
TEST_SUBMODULE(class_sh_trampoline_basic, m) {
m.attr("defined_PYBIND11_HAS_INTERNALS_WITH_SMART_HOLDER_SUPPORT") =
#ifndef PYBIND11_HAS_INTERNALS_WITH_SMART_HOLDER_SUPPORT
m.attr("defined_PYBIND11_SMART_HOLDER_ENABLED") =
#ifndef PYBIND11_SMART_HOLDER_ENABLED
false;
#else
true;
wrap<0>(m, "Abase0");
wrap<1>(m, "Abase1");
#endif // PYBIND11_HAS_INTERNALS_WITH_SMART_HOLDER_SUPPORT
#endif // PYBIND11_SMART_HOLDER_ENABLED
}