[3.12] gh-118033: Fix __weakref__ not set for generic dataclasses (GH-118099) (#118822)

gh-118033: Fix `__weakref__` not set for generic dataclasses (GH-118099)
(cherry picked from commit fa9b9cb113)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
This commit is contained in:
Miss Islington (bot) 2024-05-09 11:09:40 +02:00 committed by GitHub
parent 530c3bb271
commit e9539568be
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 118 additions and 3 deletions

View file

@ -1168,10 +1168,17 @@ def _dataclass_setstate(self, state):
def _get_slots(cls):
match cls.__dict__.get('__slots__'):
# A class which does not define __slots__ at all is equivalent
# to a class defining __slots__ = ('__dict__', '__weakref__')
# `__dictoffset__` and `__weakrefoffset__` can tell us whether
# the base type has dict/weakref slots, in a way that works correctly
# for both Python classes and C extension types. Extension types
# don't use `__slots__` for slot creation
case None:
yield from ('__dict__', '__weakref__')
slots = []
if getattr(cls, '__weakrefoffset__', -1) != 0:
slots.append('__weakref__')
if getattr(cls, '__dictrefoffset__', -1) != 0:
slots.append('__dict__')
yield from slots
case str(slot):
yield slot
# Slots may be any iterable, but we cannot handle an iterator