mirror of
https://github.com/python/cpython.git
synced 2025-08-22 09:45:06 +00:00
gh-102578: Optimise setting and deleting mutable attributes on non-dataclass subclasses of frozen dataclasses (gh-102573)
This commit is contained in:
parent
90f1d77717
commit
ee6f8413a9
3 changed files with 52 additions and 6 deletions
|
@ -616,21 +616,19 @@ def _repr_fn(fields, globals):
|
|||
def _frozen_get_del_attr(cls, fields, globals):
|
||||
locals = {'cls': cls,
|
||||
'FrozenInstanceError': FrozenInstanceError}
|
||||
condition = 'type(self) is cls'
|
||||
if fields:
|
||||
fields_str = '(' + ','.join(repr(f.name) for f in fields) + ',)'
|
||||
else:
|
||||
# Special case for the zero-length tuple.
|
||||
fields_str = '()'
|
||||
condition += ' or name in {' + ', '.join(repr(f.name) for f in fields) + '}'
|
||||
return (_create_fn('__setattr__',
|
||||
('self', 'name', 'value'),
|
||||
(f'if type(self) is cls or name in {fields_str}:',
|
||||
(f'if {condition}:',
|
||||
' raise FrozenInstanceError(f"cannot assign to field {name!r}")',
|
||||
f'super(cls, self).__setattr__(name, value)'),
|
||||
locals=locals,
|
||||
globals=globals),
|
||||
_create_fn('__delattr__',
|
||||
('self', 'name'),
|
||||
(f'if type(self) is cls or name in {fields_str}:',
|
||||
(f'if {condition}:',
|
||||
' raise FrozenInstanceError(f"cannot delete field {name!r}")',
|
||||
f'super(cls, self).__delattr__(name)'),
|
||||
locals=locals,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue