1
- // Copyright (c) 2021 The Pybind Development Team.
1
+ // Copyright (c) 2025 The Pybind Development Team.
2
2
// All rights reserved. Use of this source code is governed by a
3
3
// BSD-style license that can be found in the LICENSE file.
4
4
5
5
#include " pybind11_tests.h"
6
6
7
- #include < utility >
7
+ #include < memory >
8
8
9
9
namespace pybind11_tests {
10
10
namespace class_sh_trampoline_weak_ptr {
11
11
12
- // // For testing whether a python subclass of a C++ object can be accessed from a C++ weak_ptr
13
- struct WpBase {
14
- // returns true if the base virtual function is called
15
- virtual bool is_base_used () { return true ; }
16
-
17
- // returns true if there's an associated python instance
18
- bool has_python_instance () {
19
- auto *tinfo = py::detail::get_type_info (typeid (WpBase));
20
- return (bool ) py::detail::get_object_handle (this , tinfo);
21
- }
22
-
23
- WpBase () = default ;
24
- WpBase (const WpBase &) = delete ;
25
- virtual ~WpBase () = default ;
12
+ struct VirtBase {
13
+ virtual ~VirtBase () = default ;
14
+ virtual int get_code () { return 100 ; }
26
15
};
27
16
28
- struct PyWpBase : WpBase {
29
- using WpBase::WpBase ;
30
- bool is_base_used () override { PYBIND11_OVERRIDE (bool , WpBase, is_base_used ); }
17
+ struct PyVirtBase : VirtBase, py::trampoline_self_life_support {
18
+ using VirtBase::VirtBase ;
19
+ int get_code () override { PYBIND11_OVERRIDE (int , VirtBase, get_code ); }
31
20
};
32
21
33
- struct WpBaseTester {
34
- std::shared_ptr<WpBase> get_object () const { return m_obj.lock (); }
35
- void set_object (std::shared_ptr<WpBase> obj) { m_obj = obj; }
36
- bool is_expired () { return m_obj.expired (); }
37
- bool is_base_used () { return m_obj.lock ()->is_base_used (); }
38
- std::weak_ptr<WpBase> m_obj;
22
+ struct WpOwner {
23
+ void set_wp (std::shared_ptr<VirtBase> sp) { wp = sp; }
24
+
25
+ int get_code () {
26
+ auto sp = wp.lock ();
27
+ if (!sp) {
28
+ return -999 ;
29
+ }
30
+ return sp->get_code ();
31
+ }
32
+
33
+ private:
34
+ std::weak_ptr<VirtBase> wp;
39
35
};
40
36
41
37
} // namespace class_sh_trampoline_weak_ptr
@@ -44,18 +40,12 @@ struct WpBaseTester {
44
40
using namespace pybind11_tests ::class_sh_trampoline_weak_ptr;
45
41
46
42
TEST_SUBMODULE (class_sh_trampoline_weak_ptr, m) {
47
- // For testing whether a python subclass of a C++ object can be accessed from a C++ weak_ptr
48
-
49
- py::classh<WpBase, PyWpBase>(m, " WpBase" )
43
+ py::classh<VirtBase, PyVirtBase>(m, " VirtBase" )
50
44
.def (py::init<>())
51
- .def (py::init ([](int ) { return std::make_shared<PyWpBase>(); }))
52
- .def (" is_base_used" , &WpBase::is_base_used)
53
- .def (" has_python_instance" , &WpBase::has_python_instance);
45
+ .def (" get_code" , &VirtBase::get_code);
54
46
55
- py::classh<WpBaseTester >(m, " WpBaseTester " )
47
+ py::classh<WpOwner >(m, " WpOwner " )
56
48
.def (py::init<>())
57
- .def (" get_object" , &WpBaseTester::get_object)
58
- .def (" set_object" , &WpBaseTester::set_object)
59
- .def (" is_expired" , &WpBaseTester::is_expired)
60
- .def (" is_base_used" , &WpBaseTester::is_base_used);
49
+ .def (" set_wp" , &WpOwner::set_wp)
50
+ .def (" get_code" , &WpOwner::get_code);
61
51
}
0 commit comments