]> git.ipfire.org Git - thirdparty/Python/cpython.git/commit
gh-135228: Create __dict__ and __weakref__ descriptors for object (GH-136966)
authorPetr Viktorin <encukou@gmail.com>
Mon, 18 Aug 2025 12:25:51 +0000 (14:25 +0200)
committerGitHub <noreply@github.com>
Mon, 18 Aug 2025 12:25:51 +0000 (14:25 +0200)
commit7dfa048bbb1e8b3047a6e3bae7931147c32afbef
treebe6b5ddd5aedf037175b315265be30e2be62edeb
parent92be979f64cbc12a32dc391d66caf4398fd24763
gh-135228: Create __dict__ and __weakref__ descriptors for object (GH-136966)

This partially reverts #137047, keeping the tests for GC collectability of the
original class that dataclass adds `__slots__` to.
The reference leaks solved there are instead solved by having the `__dict__` &
`__weakref__` descriptors not tied to (and referencing) their class.

Instead, they're shared between all classes that need them (within
an interpreter).
The `__objclass__` ol the descriptors is set to `object`, since these
descriptors work with *any* object. (The appropriate checks were already
made in the get/set code, so the `__objclass__` check was redundant.)

The repr of these descriptors (and any others whose `__objclass__` is `object`)
now doesn't mention the objclass.

This change required adjustment of introspection code that checks
`__objclass__` to determine an object's “own” (i.e. not inherited) `__dict__`.
Third-party code that does similar introspection of the internals will also
need adjusting.

Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
13 files changed:
Doc/whatsnew/3.15.rst
Include/internal/pycore_interp_structs.h
Include/internal/pycore_typeobject.h
Lib/dataclasses.py
Lib/inspect.py
Lib/test/test_descr.py
Misc/NEWS.d/next/Core_and_Builtins/2025-08-05-10-22-15.gh-issue-136966.J5lrE0.rst [new file with mode: 0644]
Objects/descrobject.c
Objects/typeobject.c
Python/clinic/sysmodule.c.h
Python/pylifecycle.c
Python/sysmodule.c
Tools/c-analyzer/cpython/_analyzer.py