MCPcopy Index your code
hub / github.com/RustPython/RustPython / _add_slots

Function _add_slots

Lib/dataclasses.py:1329–1411  ·  view source on GitHub ↗
(cls, is_frozen, weakref_slot, defined_fields)

Source from the content-addressed store, hash-verified

1327
1328
1329def _add_slots(cls, is_frozen, weakref_slot, defined_fields):
1330 # Need to create a new class, since we can't set __slots__ after a
1331 # class has been created, and the @dataclass decorator is called
1332 # after the class is created.
1333
1334 # Make sure __slots__ isn't already set.
1335 if '__slots__' in cls.__dict__:
1336 raise TypeError(f'{cls.__name__} already specifies __slots__')
1337
1338 # gh-102069: Remove existing __weakref__ descriptor.
1339 # gh-135228: Make sure the original class can be garbage collected.
1340 sys._clear_type_descriptors(cls)
1341
1342 # Create a new dict for our new class.
1343 cls_dict = dict(cls.__dict__)
1344 field_names = tuple(f.name for f in fields(cls))
1345 # Make sure slots don't overlap with those in base classes.
1346 inherited_slots = set(
1347 itertools.chain.from_iterable(map(_get_slots, cls.__mro__[1:-1]))
1348 )
1349
1350 cls_dict["__slots__"] = _create_slots(
1351 defined_fields, inherited_slots, field_names, weakref_slot,
1352 )
1353
1354 for field_name in field_names:
1355 # Remove our attributes, if present. They'll still be
1356 # available in _MARKER.
1357 cls_dict.pop(field_name, None)
1358
1359 # And finally create the class.
1360 qualname = getattr(cls, '__qualname__', None)
1361 newcls = type(cls)(cls.__name__, cls.__bases__, cls_dict)
1362 if qualname is not None:
1363 newcls.__qualname__ = qualname
1364
1365 if is_frozen:
1366 # Need this for pickling frozen classes with slots.
1367 if '__getstate__' not in cls_dict:
1368 newcls.__getstate__ = _dataclass_getstate
1369 if '__setstate__' not in cls_dict:
1370 newcls.__setstate__ = _dataclass_setstate
1371
1372 # Fix up any closures which reference __class__. This is used to
1373 # fix zero argument super so that it points to the correct class
1374 # (the newly created one, which we're returning) and not the
1375 # original class. We can break out of this loop as soon as we
1376 # make an update, since all closures for a class will share a
1377 # given cell.
1378 for member in newcls.__dict__.values():
1379 # If this is a wrapped function, unwrap it.
1380 member = inspect.unwrap(member)
1381
1382 if isinstance(member, types.FunctionType):
1383 if _update_func_cell_for__class__(member, cls, newcls):
1384 break
1385 elif isinstance(member, property):
1386 if (_update_func_cell_for__class__(member.fget, cls, newcls)

Callers 1

_process_classFunction · 0.85

Calls 10

fieldsFunction · 0.85
setFunction · 0.85
_create_slotsFunction · 0.85
getattrFunction · 0.85
isinstanceFunction · 0.85
from_iterableMethod · 0.80
popMethod · 0.45
valuesMethod · 0.45
unwrapMethod · 0.45

Tested by

no test coverage detected