Skip to content

gh-135228: Break reference cycle between class and descriptors #135230

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Lib/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -1698,7 +1698,7 @@ def _shadowed_dict_from_weakref_mro_tuple(*weakref_mro):
class_dict = dunder_dict['__dict__']
if not (type(class_dict) is types.GetSetDescriptorType and
class_dict.__name__ == "__dict__" and
class_dict.__objclass__ is entry):
(class_dict.__objclass__ is entry or class_dict.__objclass__ is object)):
return class_dict
return _sentinel

Expand Down
16 changes: 12 additions & 4 deletions Objects/descrobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,11 @@ getset_get(PyObject *self, PyObject *obj, PyObject *type)
if (descr_check((PyDescrObject *)descr, obj) < 0) {
return NULL;
}
if (descr->d_getset->get != NULL)
if (descr->d_getset->get != NULL) {
void *closure = descr->d_getset->closure == (void *)1 ? NULL : descr->d_getset->closure;
return descr_get_trampoline_call(
descr->d_getset->get, obj, descr->d_getset->closure);
descr->d_getset->get, obj, closure);
}
PyErr_Format(PyExc_AttributeError,
"attribute '%V' of '%.100s' objects is not readable",
descr_name((PyDescrObject *)descr), "?",
Expand Down Expand Up @@ -247,9 +249,9 @@ getset_set(PyObject *self, PyObject *obj, PyObject *value)
return -1;
}
if (descr->d_getset->set != NULL) {
void *closure = descr->d_getset->closure == (void *)1 ? NULL : descr->d_getset->closure;
return descr_set_trampoline_call(
descr->d_getset->set, obj, value,
descr->d_getset->closure);
descr->d_getset->set, obj, value, closure);
}
PyErr_Format(PyExc_AttributeError,
"attribute '%V' of '%.100s' objects is not writable",
Expand Down Expand Up @@ -1004,10 +1006,16 @@ PyDescr_NewGetSet(PyTypeObject *type, PyGetSetDef *getset)
{
PyGetSetDescrObject *descr;

bool should_adjust_d_type = (getset->closure == (void *)1);

descr = (PyGetSetDescrObject *)descr_new(&PyGetSetDescr_Type,
type, getset->name);
if (descr != NULL)
descr->d_getset = getset;

if (should_adjust_d_type) {
Py_SETREF(descr->d_common.d_type, &PyBaseObject_Type);
}
return (PyObject *)descr;
}

Expand Down
8 changes: 4 additions & 4 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -4031,21 +4031,21 @@ subtype_getweakref(PyObject *obj, void *context)

static PyGetSetDef subtype_getsets_full[] = {
{"__dict__", subtype_dict, subtype_setdict,
PyDoc_STR("dictionary for instance variables")},
PyDoc_STR("dictionary for instance variables"), (void *)1},
{"__weakref__", subtype_getweakref, NULL,
PyDoc_STR("list of weak references to the object")},
PyDoc_STR("list of weak references to the object"), (void *)1},
{0}
};

static PyGetSetDef subtype_getsets_dict_only[] = {
{"__dict__", subtype_dict, subtype_setdict,
PyDoc_STR("dictionary for instance variables")},
PyDoc_STR("dictionary for instance variables"), (void *)1},
{0}
};

static PyGetSetDef subtype_getsets_weakref_only[] = {
{"__weakref__", subtype_getweakref, NULL,
PyDoc_STR("list of weak references to the object")},
PyDoc_STR("list of weak references to the object"), (void *)1},
{0}
};

Expand Down
Loading