Files
pybind11/tests/test_class_sh_trampoline_shared_ptr_cpp_arg.cpp
Ralf W. Grosse-Kunstleve 4638e017b6 Modify py::trampoline_self_life_support semantics: if trampoline class does not inherit from this class, preserve established Inheritance Slicing behavior.
rwgk reached this point with the help of ChatGPT:

* https://chatgpt.com/share/68056498-7d94-8008-8ff0-232e2aba451c

The only production code change in this commit is:

```
diff --git a/include/pybind11/detail/type_caster_base.h b/include/pybind11/detail/type_caster_base.h
index d4f9a41e..f3d45301 100644
--- a/include/pybind11/detail/type_caster_base.h
+++ b/include/pybind11/detail/type_caster_base.h
@@ -776,6 +776,14 @@ struct load_helper : value_and_holder_helper {
                 if (released_ptr) {
                     return std::shared_ptr<T>(released_ptr, type_raw_ptr);
                 }
+                auto *self_life_support
+                    = dynamic_raw_ptr_cast_if_possible<trampoline_self_life_support>(type_raw_ptr);
+                if (self_life_support == nullptr) {
+                    std::shared_ptr<void> void_shd_ptr = hld.template as_shared_ptr<void>();
+                    std::shared_ptr<T> to_be_released(void_shd_ptr, type_raw_ptr);
+                    vptr_gd_ptr->released_ptr = to_be_released;
+                    return to_be_released;
+                }
                 std::shared_ptr<T> to_be_released(
                     type_raw_ptr, shared_ptr_trampoline_self_life_support(loaded_v_h.inst));
                 vptr_gd_ptr->released_ptr = to_be_released;
```
2025-04-20 14:14:47 -07:00

93 lines
3.3 KiB
C++

// Copyright (c) 2021 The Pybind Development Team.
// All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#include "pybind11_tests.h"
#include <utility>
namespace pybind11_tests {
namespace class_sh_trampoline_shared_ptr_cpp_arg {
// For testing whether a python subclass of a C++ object dies when the
// last python reference is lost
struct SpBase {
// returns true if the base virtual function is called
virtual bool is_base_used() { return true; }
// returns true if there's an associated python instance
bool has_python_instance() {
auto *tinfo = py::detail::get_type_info(typeid(SpBase));
return (bool) py::detail::get_object_handle(this, tinfo);
}
SpBase() = default;
SpBase(const SpBase &) = delete;
virtual ~SpBase() = default;
};
std::shared_ptr<SpBase> pass_through_shd_ptr(const std::shared_ptr<SpBase> &obj) { return obj; }
struct PySpBase : SpBase, py::trampoline_self_life_support {
using SpBase::SpBase;
bool is_base_used() override { PYBIND11_OVERRIDE(bool, SpBase, is_base_used); }
};
struct SpBaseTester {
std::shared_ptr<SpBase> get_object() const { return m_obj; }
void set_object(std::shared_ptr<SpBase> obj) { m_obj = std::move(obj); }
bool is_base_used() { return m_obj->is_base_used(); }
bool has_instance() { return (bool) m_obj; }
bool has_python_instance() { return m_obj && m_obj->has_python_instance(); }
void set_nonpython_instance() { m_obj = std::make_shared<SpBase>(); }
std::shared_ptr<SpBase> m_obj;
};
// For testing that a C++ class without an alias does not retain the python
// portion of the object
struct SpGoAway {};
struct SpGoAwayTester {
std::shared_ptr<SpGoAway> m_obj;
};
} // namespace class_sh_trampoline_shared_ptr_cpp_arg
} // namespace pybind11_tests
using namespace pybind11_tests::class_sh_trampoline_shared_ptr_cpp_arg;
TEST_SUBMODULE(class_sh_trampoline_shared_ptr_cpp_arg, m) {
// For testing whether a python subclass of a C++ object dies when the
// last python reference is lost
py::classh<SpBase, PySpBase>(m, "SpBase")
.def(py::init<>())
.def(py::init([](int) { return std::make_shared<PySpBase>(); }))
.def("is_base_used", &SpBase::is_base_used)
.def("has_python_instance", &SpBase::has_python_instance);
m.def("pass_through_shd_ptr", pass_through_shd_ptr);
m.def("pass_through_shd_ptr_release_gil",
pass_through_shd_ptr,
py::call_guard<py::gil_scoped_release>()); // PR #4196
py::classh<SpBaseTester>(m, "SpBaseTester")
.def(py::init<>())
.def("get_object", &SpBaseTester::get_object)
.def("set_object", &SpBaseTester::set_object)
.def("is_base_used", &SpBaseTester::is_base_used)
.def("has_instance", &SpBaseTester::has_instance)
.def("has_python_instance", &SpBaseTester::has_python_instance)
.def("set_nonpython_instance", &SpBaseTester::set_nonpython_instance)
.def_readwrite("obj", &SpBaseTester::m_obj);
// For testing that a C++ class without an alias does not retain the python
// portion of the object
py::classh<SpGoAway>(m, "SpGoAway").def(py::init<>());
py::classh<SpGoAwayTester>(m, "SpGoAwayTester")
.def(py::init<>())
.def_readwrite("obj", &SpGoAwayTester::m_obj);
}