Skip to content

Commit cf8345e

Browse files
authored
Merge pull request #30125 from rwgk/pybind11k_merge_sh
git merge smart_holder (pybind/pybind11#5161)
2 parents c062556 + 9abb0b4 commit cf8345e

File tree

4 files changed

+73
-5
lines changed

4 files changed

+73
-5
lines changed

include/pybind11/cast.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1519,13 +1519,24 @@ enable_if_t<!cast_is_temporary_value_reference<T>::value, T> cast_ref(object &&,
15191519
// static_assert, even though if it's in dead code, so we provide a "trampoline" to pybind11::cast
15201520
// that only does anything in cases where pybind11::cast is valid.
15211521
template <typename T>
1522-
enable_if_t<cast_is_temporary_value_reference<T>::value, T> cast_safe(object &&) {
1522+
enable_if_t<cast_is_temporary_value_reference<T>::value
1523+
&& !detail::is_same_ignoring_cvref<T, PyObject *>::value,
1524+
T>
1525+
cast_safe(object &&) {
15231526
pybind11_fail("Internal error: cast_safe fallback invoked");
15241527
}
15251528
template <typename T>
15261529
enable_if_t<std::is_void<T>::value, void> cast_safe(object &&) {}
15271530
template <typename T>
1528-
enable_if_t<detail::none_of<cast_is_temporary_value_reference<T>, std::is_void<T>>::value, T>
1531+
enable_if_t<detail::is_same_ignoring_cvref<T, PyObject *>::value, PyObject *>
1532+
cast_safe(object &&o) {
1533+
return o.release().ptr();
1534+
}
1535+
template <typename T>
1536+
enable_if_t<detail::none_of<cast_is_temporary_value_reference<T>,
1537+
detail::is_same_ignoring_cvref<T, PyObject *>,
1538+
std::is_void<T>>::value,
1539+
T>
15291540
cast_safe(object &&o) {
15301541
return pybind11::cast<T>(std::move(o));
15311542
}

include/pybind11/pybind11.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3212,10 +3212,14 @@ function get_override(const T *this_ptr, const char *name) {
32123212
= pybind11::get_override(static_cast<const cname *>(this), name); \
32133213
if (override) { \
32143214
auto o = override_call(__VA_ARGS__); \
3215-
if (pybind11::detail::cast_is_temporary_value_reference<ret_type>::value) { \
3215+
PYBIND11_WARNING_PUSH \
3216+
PYBIND11_WARNING_DISABLE_MSVC(4127) \
3217+
if (pybind11::detail::cast_is_temporary_value_reference<ret_type>::value \
3218+
&& !pybind11::detail::is_same_ignoring_cvref<ret_type, PyObject *>::value) { \
32163219
static pybind11::detail::override_caster_t<ret_type> caster; \
32173220
return pybind11::detail::cast_ref<ret_type>(std::move(o), caster); \
32183221
} \
3222+
PYBIND11_WARNING_POP \
32193223
return pybind11::detail::cast_safe<ret_type>(std::move(o)); \
32203224
} \
32213225
} while (false)

tests/test_type_caster_pyobject_ptr.cpp

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
#include "pybind11_tests.h"
66

77
#include <cstddef>
8+
#include <string>
89
#include <vector>
910

10-
namespace {
11+
namespace test_type_caster_pyobject_ptr {
1112

1213
std::vector<PyObject *> make_vector_pyobject_ptr(const py::object &ValueHolder) {
1314
std::vector<PyObject *> vec_obj;
@@ -18,9 +19,39 @@ std::vector<PyObject *> make_vector_pyobject_ptr(const py::object &ValueHolder)
1819
return vec_obj;
1920
}
2021

21-
} // namespace
22+
struct WithPyObjectPtrReturn {
23+
#if defined(__clang_major__) && __clang_major__ < 4
24+
WithPyObjectPtrReturn() = default;
25+
WithPyObjectPtrReturn(const WithPyObjectPtrReturn &) = default;
26+
#endif
27+
virtual ~WithPyObjectPtrReturn() = default;
28+
virtual PyObject *return_pyobject_ptr() const = 0;
29+
};
30+
31+
struct WithPyObjectPtrReturnTrampoline : WithPyObjectPtrReturn {
32+
PyObject *return_pyobject_ptr() const override {
33+
PYBIND11_OVERRIDE_PURE(PyObject *, WithPyObjectPtrReturn, return_pyobject_ptr,
34+
/* no arguments */);
35+
}
36+
};
37+
38+
std::string call_return_pyobject_ptr(const WithPyObjectPtrReturn *base_class_ptr) {
39+
PyObject *returned_obj = base_class_ptr->return_pyobject_ptr();
40+
#if !defined(PYPY_VERSION) // It is not worth the trouble doing something special for PyPy.
41+
if (Py_REFCNT(returned_obj) != 1) {
42+
py::pybind11_fail(__FILE__ ":" PYBIND11_TOSTRING(__LINE__));
43+
}
44+
#endif
45+
auto ret_val = py::repr(returned_obj).cast<std::string>();
46+
Py_DECREF(returned_obj);
47+
return ret_val;
48+
}
49+
50+
} // namespace test_type_caster_pyobject_ptr
2251

2352
TEST_SUBMODULE(type_caster_pyobject_ptr, m) {
53+
using namespace test_type_caster_pyobject_ptr;
54+
2455
m.def("cast_from_pyobject_ptr", []() {
2556
PyObject *ptr = PyLong_FromLongLong(6758L);
2657
return py::cast(ptr, py::return_value_policy::take_ownership);
@@ -128,6 +159,12 @@ TEST_SUBMODULE(type_caster_pyobject_ptr, m) {
128159
}
129160
#endif
130161

162+
py::class_<WithPyObjectPtrReturn, WithPyObjectPtrReturnTrampoline>(m, "WithPyObjectPtrReturn")
163+
.def(py::init<>())
164+
.def("return_pyobject_ptr", &WithPyObjectPtrReturn::return_pyobject_ptr);
165+
166+
m.def("call_return_pyobject_ptr", call_return_pyobject_ptr);
167+
131168
// This test exercises functionality (`py::cast<PyObject *>(handle_nullptr)`)
132169
// that is needed indirectly below in py_arg_handle_nullptr.
133170
m.def("pyobject_ptr_from_handle_nullptr", []() {

tests/test_type_caster_pyobject_ptr.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,22 @@ def test_type_caster_name_via_incompatible_function_arguments_type_error():
104104
m.pass_pyobject_ptr_and_int(ValueHolder(101), ValueHolder(202))
105105

106106

107+
def test_trampoline_with_pyobject_ptr_return():
108+
class Drvd(m.WithPyObjectPtrReturn):
109+
def return_pyobject_ptr(self):
110+
return ["11", "22", "33"]
111+
112+
# Basic health check: First make sure this works as expected.
113+
d = Drvd()
114+
assert d.return_pyobject_ptr() == ["11", "22", "33"]
115+
116+
while True:
117+
# This failed before PR #5156: AddressSanitizer: heap-use-after-free ... in Py_DECREF
118+
d_repr = m.call_return_pyobject_ptr(d)
119+
assert d_repr == repr(["11", "22", "33"])
120+
break # Comment out for manual leak checking.
121+
122+
107123
def test_pyobject_ptr_from_handle_nullptr():
108124
assert m.pyobject_ptr_from_handle_nullptr() == "SUCCESS"
109125

0 commit comments

Comments
 (0)