|
| 1 | +// Copyright (c) 2025 The pybind Community. |
| 2 | +// All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +#include "pybind11_tests.h" |
| 6 | + |
| 7 | +#include <memory> |
| 8 | + |
| 9 | +namespace pybind11_tests { |
| 10 | +namespace potentially_slicing_weak_ptr { |
| 11 | + |
| 12 | +template <int> // Using int as a trick to easily generate multiple types. |
| 13 | +struct VirtBase { |
| 14 | + virtual ~VirtBase() = default; |
| 15 | + virtual int get_code() { return 100; } |
| 16 | +}; |
| 17 | + |
| 18 | +using VirtBaseSH = VirtBase<0>; // for testing with py::smart_holder |
| 19 | +using VirtBaseSP = VirtBase<1>; // for testing with std::shared_ptr as holder |
| 20 | + |
| 21 | +// Similar to trampoline_self_life_support |
| 22 | +struct trampoline_is_alive_simple { |
| 23 | + std::uint64_t magic_token = 197001010000u; |
| 24 | + |
| 25 | + trampoline_is_alive_simple() = default; |
| 26 | + |
| 27 | + ~trampoline_is_alive_simple() { magic_token = 20380118191407u; } |
| 28 | + |
| 29 | + trampoline_is_alive_simple(const trampoline_is_alive_simple &other) = default; |
| 30 | + trampoline_is_alive_simple(trampoline_is_alive_simple &&other) noexcept |
| 31 | + : magic_token(other.magic_token) { |
| 32 | + other.magic_token = 20380118191407u; |
| 33 | + } |
| 34 | + |
| 35 | + trampoline_is_alive_simple &operator=(const trampoline_is_alive_simple &) = delete; |
| 36 | + trampoline_is_alive_simple &operator=(trampoline_is_alive_simple &&) = delete; |
| 37 | +}; |
| 38 | + |
| 39 | +template <typename VB> |
| 40 | +const char *determine_trampoline_state(const std::shared_ptr<VB> &sp) { |
| 41 | + if (!sp) { |
| 42 | + return "sp nullptr"; |
| 43 | + } |
| 44 | + auto *tias = dynamic_cast<trampoline_is_alive_simple *>(sp.get()); |
| 45 | + if (!tias) { |
| 46 | + return "dynamic_cast failed"; |
| 47 | + } |
| 48 | + if (tias->magic_token == 197001010000u) { |
| 49 | + return "trampoline alive"; |
| 50 | + } |
| 51 | + if (tias->magic_token == 20380118191407u) { |
| 52 | + return "trampoline DEAD"; |
| 53 | + } |
| 54 | + return "UNDEFINED BEHAVIOR"; |
| 55 | +} |
| 56 | + |
| 57 | +struct PyVirtBaseSH : VirtBaseSH, py::trampoline_self_life_support, trampoline_is_alive_simple { |
| 58 | + using VirtBaseSH::VirtBaseSH; |
| 59 | + int get_code() override { PYBIND11_OVERRIDE(int, VirtBaseSH, get_code); } |
| 60 | +}; |
| 61 | + |
| 62 | +struct PyVirtBaseSP : VirtBaseSP, trampoline_is_alive_simple { // self-life-support not available |
| 63 | + using VirtBaseSP::VirtBaseSP; |
| 64 | + int get_code() override { PYBIND11_OVERRIDE(int, VirtBaseSP, get_code); } |
| 65 | +}; |
| 66 | + |
| 67 | +template <typename VB> |
| 68 | +std::shared_ptr<VB> rtrn_obj_cast_shared_ptr(py::handle obj) { |
| 69 | + return obj.cast<std::shared_ptr<VB>>(); |
| 70 | +} |
| 71 | + |
| 72 | +// There is no type_caster<std::weak_ptr<VB>>, and to minimize code complexity |
| 73 | +// we do not want to add one, therefore we have to return a shared_ptr here. |
| 74 | +template <typename VB> |
| 75 | +std::shared_ptr<VB> rtrn_potentially_slicing_shared_ptr(py::handle obj) { |
| 76 | + return py::potentially_slicing_weak_ptr<VB>(obj).lock(); |
| 77 | +} |
| 78 | + |
| 79 | +template <typename VB> |
| 80 | +struct SpOwner { |
| 81 | + void set_sp(const std::shared_ptr<VB> &sp_) { sp = sp_; } |
| 82 | + |
| 83 | + int get_code() const { |
| 84 | + if (!sp) { |
| 85 | + return -888; |
| 86 | + } |
| 87 | + return sp->get_code(); |
| 88 | + } |
| 89 | + |
| 90 | + const char *get_trampoline_state() const { return determine_trampoline_state(sp); } |
| 91 | + |
| 92 | +private: |
| 93 | + std::shared_ptr<VB> sp; |
| 94 | +}; |
| 95 | + |
| 96 | +template <typename VB> |
| 97 | +struct WpOwner { |
| 98 | + void set_wp(const std::weak_ptr<VB> &wp_) { wp = wp_; } |
| 99 | + |
| 100 | + int get_code() const { |
| 101 | + auto sp = wp.lock(); |
| 102 | + if (!sp) { |
| 103 | + return -999; |
| 104 | + } |
| 105 | + return sp->get_code(); |
| 106 | + } |
| 107 | + |
| 108 | + const char *get_trampoline_state() const { return determine_trampoline_state(wp.lock()); } |
| 109 | + |
| 110 | +private: |
| 111 | + std::weak_ptr<VB> wp; |
| 112 | +}; |
| 113 | + |
| 114 | +template <typename VB> |
| 115 | +void wrap(py::module_ &m, |
| 116 | + const char *roc_pyname, |
| 117 | + const char *rps_pyname, |
| 118 | + const char *spo_pyname, |
| 119 | + const char *wpo_pyname) { |
| 120 | + m.def(roc_pyname, rtrn_obj_cast_shared_ptr<VB>); |
| 121 | + m.def(rps_pyname, rtrn_potentially_slicing_shared_ptr<VB>); |
| 122 | + |
| 123 | + py::classh<SpOwner<VB>>(m, spo_pyname) |
| 124 | + .def(py::init<>()) |
| 125 | + .def("set_sp", &SpOwner<VB>::set_sp) |
| 126 | + .def("get_code", &SpOwner<VB>::get_code) |
| 127 | + .def("get_trampoline_state", &SpOwner<VB>::get_trampoline_state); |
| 128 | + |
| 129 | + py::classh<WpOwner<VB>>(m, wpo_pyname) |
| 130 | + .def(py::init<>()) |
| 131 | + .def("set_wp", |
| 132 | + [](WpOwner<VB> &self, py::handle obj) { |
| 133 | + self.set_wp(obj.cast<std::shared_ptr<VB>>()); |
| 134 | + }) |
| 135 | + .def("set_wp_potentially_slicing", |
| 136 | + [](WpOwner<VB> &self, py::handle obj) { |
| 137 | + self.set_wp(py::potentially_slicing_weak_ptr<VB>(obj)); |
| 138 | + }) |
| 139 | + .def("get_code", &WpOwner<VB>::get_code) |
| 140 | + .def("get_trampoline_state", &WpOwner<VB>::get_trampoline_state); |
| 141 | +} |
| 142 | + |
| 143 | +} // namespace potentially_slicing_weak_ptr |
| 144 | +} // namespace pybind11_tests |
| 145 | + |
| 146 | +using namespace pybind11_tests::potentially_slicing_weak_ptr; |
| 147 | + |
| 148 | +TEST_SUBMODULE(potentially_slicing_weak_ptr, m) { |
| 149 | + py::classh<VirtBaseSH, PyVirtBaseSH>(m, "VirtBaseSH") |
| 150 | + .def(py::init<>()) |
| 151 | + .def("get_code", &VirtBaseSH::get_code); |
| 152 | + |
| 153 | + py::class_<VirtBaseSP, std::shared_ptr<VirtBaseSP>, PyVirtBaseSP>(m, "VirtBaseSP") |
| 154 | + .def(py::init<>()) |
| 155 | + .def("get_code", &VirtBaseSP::get_code); |
| 156 | + |
| 157 | + wrap<VirtBaseSH>(m, |
| 158 | + "SH_rtrn_obj_cast_shared_ptr", |
| 159 | + "SH_rtrn_potentially_slicing_shared_ptr", |
| 160 | + "SH_SpOwner", |
| 161 | + "SH_WpOwner"); |
| 162 | + |
| 163 | + wrap<VirtBaseSP>(m, |
| 164 | + "SP_rtrn_obj_cast_shared_ptr", |
| 165 | + "SP_rtrn_potentially_slicing_shared_ptr", |
| 166 | + "SP_SpOwner", |
| 167 | + "SP_WpOwner"); |
| 168 | +} |
0 commit comments