mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
[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:
parent
530c3bb271
commit
e9539568be
3 changed files with 118 additions and 3 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue