-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
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
base: main
Are you sure you want to change the base?
Conversation
…ke the original class collectible An interesting hack, but more localized in scope than python#135230. This may be a breaking change if people intentionally keep the original class around when using `@dataclass(slots=True)`, and then use `__dict__` or `__weakref__` on the original class.
@@ -4038,22 +4038,25 @@ subtype_getweakref(PyObject *obj, void *context) | |||
|
|||
/* Three variants on the subtype_getsets list. */ | |||
|
|||
static char subtype_getset_dict_name[] = "__dict__"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a comment explaining why this is not inlined and is not const.
// Hack: dict and weakref descriptors are created for `object`, | ||
// rather than this specific type. | ||
// We identify their PyGetSetDef by pointer equality on name. | ||
if (gsp->name == subtype_getset_dict_name |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we compare PyGetSetDef pointers? gsp == &subtype_getsets_full[0]
I can polish this, but I think it'd be better to target 3.15, and follow through a bit more. |
Here's another possible way to solve #135228:
The
__dict__
and__weakref__
descriptors don't really need to hold a strong reference to "their" type; they are generic and could be created forobject
instead.Here's a proof of concept. A cleaner way to do this would be creating the descriptor objects once and caching them in the interpreter state.
This does involve a behaviour change -- before:
After: