Skip to content

Commit bad0c12

Browse files
committed
Update docs/advanced/classes.rst and C++ comments → potentially_slicing_weak_ptr
1 parent 03a8981 commit bad0c12

File tree

3 files changed

+11
-8
lines changed

3 files changed

+11
-8
lines changed

docs/advanced/classes.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -459,9 +459,9 @@ inheritance slicing but can lead to unintended behavior when creating
459459
``std::weak_ptr`` instances
460460
(see `#5623 <https://github.com/pybind/pybind11/issues/5623>`_).
461461

462-
If it is necessary to obtain a ``std::shared_ptr`` that shares the control block
462+
If it is necessary to obtain a ``std::weak_ptr`` that shares the control block
463463
with the ``smart_holder``—at the cost of reintroducing potential inheritance
464-
slicing—you can use ``py::potentially_slicing_shared_ptr<T>(obj)``.
464+
slicing—you can use ``py::potentially_slicing_weak_ptr<T>(obj)``.
465465

466466
When precise lifetime management of derived Python objects is important,
467467
using a Python-side ``weakref`` is the most reliable approach, as it avoids
@@ -470,8 +470,8 @@ semantics in C++.
470470

471471
.. seealso::
472472

473-
* :func:`potentially_slicing_shared_ptr` C++ documentation
474-
* :file:`tests/test_potentially_slicing_shared_ptr.cpp`
473+
* :func:`potentially_slicing_weak_ptr` C++ documentation
474+
* :file:`tests/test_potentially_slicing_weak_ptr.cpp`
475475

476476

477477
.. _custom_constructors:

include/pybind11/cast.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -985,6 +985,7 @@ struct copyable_holder_caster<
985985

986986
std::weak_ptr<type> potentially_slicing_weak_ptr() {
987987
if (typeinfo->holder_enum_v == detail::holder_enum_t::smart_holder) {
988+
// Reusing shared_ptr code to minimize code complexity.
988989
shared_ptr_storage
989990
= sh_load_helper.load_as_shared_ptr(value,
990991
/*responsible_parent=*/nullptr,
@@ -1094,17 +1095,17 @@ PYBIND11_NAMESPACE_END(detail)
10941095
/// does NOT keep any derived Python objects alive (see issue #1333).
10951096
///
10961097
/// For class_-wrapped types using std::shared_ptr as the holder, the following expressions
1097-
/// produce equivalent results (see tests/test_potentially_slicing_shared_ptr.cpp,py):
1098+
/// produce equivalent results (see tests/test_potentially_slicing_weak_ptr.cpp,py):
10981099
///
10991100
/// - obj.cast<std::shared_ptr<T>>()
1100-
/// - py::potentially_slicing_shared_ptr<T>(obj)
1101+
/// - py::potentially_slicing_weak_ptr<T>(obj).lock()
11011102
///
11021103
/// For class_-wrapped types with trampolines and using py::smart_holder, obj.cast<>()
11031104
/// produces a std::shared_ptr that keeps any derived Python objects alive for its own lifetime,
11041105
/// but this is achieved by introducing a std::shared_ptr control block that is independent of
11051106
/// the one owned by the py::smart_holder. This can lead to surprising std::weak_ptr behavior
1106-
/// (see issue #5623). An easy solution is to use py::potentially_slicing_shared_ptr<>(obj),
1107-
/// as exercised in tests/test_potentially_slicing_shared_ptr.cpp,py (look for
1107+
/// (see issue #5623). An easy solution is to use py::potentially_slicing_weak_ptr<>(obj),
1108+
/// as exercised in tests/test_potentially_slicing_weak_ptr.cpp,py (look for
11081109
/// "set_wp_potentially_slicing"). Note, however, that this reintroduces the inheritance
11091110
/// slicing issue (see issue #1333). The ideal — but usually more involved — solution is to use
11101111
/// a Python weakref to the derived Python object, instead of a C++ base-class std::weak_ptr.

tests/test_potentially_slicing_weak_ptr.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ std::shared_ptr<VB> rtrn_obj_cast_shared_ptr(py::handle obj) {
6969
return obj.cast<std::shared_ptr<VB>>();
7070
}
7171

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.
7274
template <typename VB>
7375
std::shared_ptr<VB> rtrn_potentially_slicing_shared_ptr(py::handle obj) {
7476
return py::potentially_slicing_weak_ptr<VB>(obj).lock();

0 commit comments

Comments
 (0)