From bb962cffbb01d849c17891323adb71f18493e775 Mon Sep 17 00:00:00 2001 From: cyy Date: Tue, 7 Nov 2023 11:55:10 +0800 Subject: [PATCH] Inherit weakref from handle --- docs/advanced/misc.rst | 2 +- include/pybind11/pybind11.h | 7 ++----- include/pybind11/pytypes.h | 5 ++--- 3 files changed, 5 insertions(+), 9 deletions(-) diff --git a/docs/advanced/misc.rst b/docs/advanced/misc.rst index ddd7f39370..d6f52c745a 100644 --- a/docs/advanced/misc.rst +++ b/docs/advanced/misc.rst @@ -287,7 +287,7 @@ avoids this issue involves weak reference with a cleanup callback: ); // Create a weak reference with a cleanup callback and initially leak it - (void) py::weakref(m.attr("BaseClass"), cleanup_callback).release(); + (void) py::weakref(m.attr("BaseClass"), cleanup_callback); .. note:: diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index 3e1a057dbf..56aa31f289 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -1694,8 +1694,7 @@ class class_ : public detail::generic_type { weakref(m_ptr, cpp_function([ptr](handle wr) { delete ptr; wr.dec_ref(); - })) - .release(); + })); return *this; } @@ -2290,7 +2289,6 @@ PYBIND11_NOINLINE void keep_alive_impl(handle nurse, handle patient) { weakref wr(nurse, disable_lifesupport); patient.inc_ref(); /* reference patient and leak the weak reference */ - (void) wr.release(); } } @@ -2338,8 +2336,7 @@ all_type_info_get_cache(PyTypeObject *type) { } wr.dec_ref(); - })) - .release(); + })); } return res; diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index e7c7c8124d..e63bdb268b 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -1863,11 +1863,10 @@ class float_ : public object { operator double() const { return (double) PyFloat_AsDouble(m_ptr); } }; -class weakref : public object { +class weakref : public handle { public: - PYBIND11_OBJECT_CVT_DEFAULT(weakref, object, PyWeakref_Check, raw_weakref) explicit weakref(handle obj, handle callback = {}) - : object(PyWeakref_NewRef(obj.ptr(), callback.ptr()), stolen_t{}) { + : handle(PyWeakref_NewRef(obj.ptr(), callback.ptr())) { if (!m_ptr) { if (PyErr_Occurred()) { throw error_already_set();