Skip to content

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

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

encukou
Copy link
Member

@encukou encukou commented Jul 22, 2025

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 for object 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:

>>> class C: pass
>>> C.__dict__['__dict__']
<attribute '__dict__' of 'C' objects>
>>> C.__dict__['__dict__'].__objclass__
<class '__main__.C'>

After:

>>> class C: pass
>>> C.__dict__['__dict__']
<attribute '__dict__' of 'object' objects>
>>> C.__dict__['__dict__'].__objclass__
<class 'object'>

JelleZijlstra and others added 5 commits July 20, 2025 16:57
…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__";
Copy link
Member

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
Copy link
Member

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]

@encukou
Copy link
Member Author

encukou commented Jul 22, 2025

I can polish this, but I think it'd be better to target 3.15, and follow through a bit more.
The descriptor objects themselves could be shared across all classes that need them (within an interpreter), but it's not as straightforward to plug that into the type creation machinery.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants