diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-01-28-07-10-41.bpo-46561.y3KHGB.rst b/Misc/NEWS.d/next/Core and Builtins/2022-01-28-07-10-41.bpo-46561.y3KHGB.rst new file mode 100644 index 000000000000000..2f069f61dbc7e1c --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2022-01-28-07-10-41.bpo-46561.y3KHGB.rst @@ -0,0 +1 @@ +Make sure arguments to __get__ are owned \ No newline at end of file diff --git a/Objects/typeobject.c b/Objects/typeobject.c index fe3b7b87c8b4b68..014308489cf7967 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -2308,8 +2308,12 @@ _PyObject_LookupSpecial(PyObject *self, PyObject *attr) descrgetfunc f; if ((f = Py_TYPE(res)->tp_descr_get) == NULL) Py_INCREF(res); - else - res = f(res, self, (PyObject *)(Py_TYPE(self))); + else { + PyObject* descr = res; + Py_INCREF(descr); + res = f(descr, self, (PyObject *)(Py_TYPE(self))); + Py_DECREF(descr); + } } return res; } @@ -2343,7 +2347,10 @@ lookup_maybe_method(PyObject *self, PyObject *attr, int *unbound) Py_INCREF(res); } else { + PyObject* descr = res; + Py_INCREF(descr); res = f(res, self, (PyObject *)(Py_TYPE(self))); + Py_DECREF(descr); } } return res;