Skip to content

Commit

Permalink
Inherit weakref from handle
Browse files Browse the repository at this point in the history
  • Loading branch information
cyyever committed Nov 7, 2023
1 parent f260693 commit cb4a041
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 8 deletions.
7 changes: 2 additions & 5 deletions include/pybind11/pybind11.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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();
}
}

Expand Down Expand Up @@ -2338,8 +2336,7 @@ all_type_info_get_cache(PyTypeObject *type) {
}

wr.dec_ref();
}))
.release();
}));
}

return res;
Expand Down
5 changes: 2 additions & 3 deletions include/pybind11/pytypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit cb4a041

Please sign in to comment.