Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 35.2k
gh-135228: Create __dict__ and __weakref__ descriptors for object#136966
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
027f629c82d86b9ef8632696f1d058c7a39d0d2f73f709debdc541cd1e2fd8a9a40a9871bd84161230e2e414d29a71d91a64ebb4f699ea771909b6d758669dFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| The :attr:`object.__dict__` and :attr:`!__weakref__` descriptors now use a | ||
| single descriptor instance per interpreter, shared across all types that | ||
| need them. | ||
encukou marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| This speeds up class creation, and helps avoid reference cycles. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -4039,26 +4039,15 @@ subtype_getweakref(PyObject *obj, void *context) | ||
| return Py_NewRef(result); | ||
| } | ||
| /* Three variants on the subtype_getsets list. */ | ||
| static PyGetSetDef subtype_getsets_full[] = { | ||
| {"__dict__", subtype_dict, subtype_setdict, | ||
| PyDoc_STR("dictionary for instance variables")}, | ||
| {"__weakref__", subtype_getweakref, NULL, | ||
| PyDoc_STR("list of weak references to the object")}, | ||
| {0} | ||
| }; | ||
| static PyGetSetDef subtype_getsets_dict_only[] = { | ||
| {"__dict__", subtype_dict, subtype_setdict, | ||
| PyDoc_STR("dictionary for instance variables")}, | ||
| {0} | ||
| /* getset definitions for common descriptors */ | ||
| static PyGetSetDef subtype_getset_dict = { | ||
| "__dict__", subtype_dict, subtype_setdict, | ||
| PyDoc_STR("dictionary for instance variables"), | ||
| }; | ||
| static PyGetSetDef subtype_getsets_weakref_only[] = { | ||
| {"__weakref__", subtype_getweakref, NULL, | ||
| PyDoc_STR("list of weak references to the object")}, | ||
| {0} | ||
| static PyGetSetDef subtype_getset_weakref = { | ||
| "__weakref__", subtype_getweakref, NULL, | ||
| PyDoc_STR("list of weak references to the object"), | ||
| }; | ||
| static int | ||
| @@ -4594,10 +4583,36 @@ type_new_classmethod(PyObject *dict, PyObject *attr) | ||
| return 0; | ||
| } | ||
| /* Add __dict__ or __weakref__ descriptor */ | ||
| static int | ||
| type_add_common_descriptor(PyInterpreterState *interp, | ||
| PyObject **cache, | ||
| PyGetSetDef *getset_def, | ||
| PyObject *dict) | ||
| { | ||
| #ifdef Py_GIL_DISABLED | ||
| PyMutex_Lock(&interp->cached_objects.descriptor_mutex); | ||
ZeroIntensity marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| #endif | ||
| PyObject *descr = *cache; | ||
| if (!descr) { | ||
| descr = PyDescr_NewGetSet(&PyBaseObject_Type, getset_def); | ||
| *cache = descr; | ||
| } | ||
| #ifdef Py_GIL_DISABLED | ||
| PyMutex_Unlock(&interp->cached_objects.descriptor_mutex); | ||
| #endif | ||
| if (!descr) { | ||
| return -1; | ||
| } | ||
| if (PyDict_SetDefaultRef(dict, PyDescr_NAME(descr), descr, NULL) < 0) { | ||
| return -1; | ||
| } | ||
| return 0; | ||
| } | ||
| /* Add descriptors for custom slots from __slots__, or for __dict__ */ | ||
| static int | ||
| type_new_descriptors(const type_new_ctx *ctx, PyTypeObject *type) | ||
| type_new_descriptors(const type_new_ctx *ctx, PyTypeObject *type, PyObject *dict) | ||
| { | ||
| PyHeapTypeObject *et = (PyHeapTypeObject *)type; | ||
| Py_ssize_t slotoffset = ctx->base->tp_basicsize; | ||
| @@ -4635,25 +4650,38 @@ type_new_descriptors(const type_new_ctx *ctx, PyTypeObject *type) | ||
| type->tp_basicsize = slotoffset; | ||
| type->tp_itemsize = ctx->base->tp_itemsize; | ||
| type->tp_members = _PyHeapType_GET_MEMBERS(et); | ||
| PyInterpreterState *interp = _PyInterpreterState_GET(); | ||
| if (type->tp_dictoffset) { | ||
| if (type_add_common_descriptor( | ||
| interp, | ||
| &interp->cached_objects.dict_descriptor, | ||
| &subtype_getset_dict, | ||
| dict) < 0) | ||
| { | ||
| return -1; | ||
| } | ||
| } | ||
| if (type->tp_weaklistoffset) { | ||
| if (type_add_common_descriptor( | ||
| interp, | ||
| &interp->cached_objects.weakref_descriptor, | ||
| &subtype_getset_weakref, | ||
| dict) < 0) | ||
| { | ||
| return -1; | ||
| } | ||
| } | ||
| return 0; | ||
| } | ||
| static void | ||
| type_new_set_slots(const type_new_ctx *ctx, PyTypeObject *type) | ||
| { | ||
| if (type->tp_weaklistoffset && type->tp_dictoffset) { | ||
| type->tp_getset = subtype_getsets_full; | ||
| } | ||
| else if (type->tp_weaklistoffset && !type->tp_dictoffset) { | ||
| type->tp_getset = subtype_getsets_weakref_only; | ||
| } | ||
| else if (!type->tp_weaklistoffset && type->tp_dictoffset) { | ||
| type->tp_getset = subtype_getsets_dict_only; | ||
| } | ||
| else { | ||
| type->tp_getset = NULL; | ||
| } | ||
| type->tp_getset = NULL; | ||
| /* Special case some slots */ | ||
| if (type->tp_dictoffset != 0 || ctx->nslot > 0) { | ||
| @@ -4758,7 +4786,7 @@ type_new_set_attrs(const type_new_ctx *ctx, PyTypeObject *type) | ||
| return -1; | ||
| } | ||
| if (type_new_descriptors(ctx, type) < 0) { | ||
| if (type_new_descriptors(ctx, type, dict) < 0) { | ||
| return -1; | ||
| } | ||
| @@ -6642,6 +6670,14 @@ _PyStaticType_FiniBuiltin(PyInterpreterState *interp, PyTypeObject *type) | ||
| } | ||
| void | ||
| _PyTypes_FiniCachedDescriptors(PyInterpreterState *interp) | ||
| { | ||
| Py_CLEAR(interp->cached_objects.dict_descriptor); | ||
| Py_CLEAR(interp->cached_objects.weakref_descriptor); | ||
| } | ||
| static void | ||
| type_dealloc(PyObject *self) | ||
| { | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.