From 00ccd709ecaad45b1929577d1bc4e00a0e454b11 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Mon, 23 Nov 2020 01:46:42 -0800 Subject: [PATCH 1/2] Improve fidelity of the member object section --- Doc/howto/descriptor.rst | 49 +++++++++++++++++++++++++++++++++------- 1 file changed, 41 insertions(+), 8 deletions(-) diff --git a/Doc/howto/descriptor.rst b/Doc/howto/descriptor.rst index 76987fda22b59d..f07972467236ee 100644 --- a/Doc/howto/descriptor.rst +++ b/Doc/howto/descriptor.rst @@ -1079,6 +1079,8 @@ simulation where the actual C structure for slots is emulated by a private ``_slotvalues`` list. Reads and writes to that private structure are managed by member descriptors:: + null = object() + class Member: def __init__(self, name, clsname, offset): @@ -1091,12 +1093,22 @@ by member descriptors:: def __get__(self, obj, objtype=None): 'Emulate member_get() in Objects/descrobject.c' # Also see PyMember_GetOne() in Python/structmember.c - return obj._slotvalues[self.offset] + value = obj._slotvalues[self.offset] + if value is null: + raise AttributeError(self.name) + return value def __set__(self, obj, value): 'Emulate member_set() in Objects/descrobject.c' obj._slotvalues[self.offset] = value + def __delete__(self, obj): + 'Emulate member_delete() in Objects/descrobject.c' + value = obj._slotvalues[self.offset] + if value is null: + raise AttributeError(self.name) + obj._slotvalues[self.offset] = null + def __repr__(self): 'Emulate member_repr() in Objects/descrobject.c' return f'' @@ -1124,13 +1136,33 @@ in pure Python:: 'Emulate object_new() in Objects/typeobject.c' inst = super().__new__(cls) if hasattr(cls, 'slot_names'): - inst._slotvalues = [None] * len(cls.slot_names) + empty_slots = [null] * len(cls.slot_names) + object.__setattr__(inst, '_slotvalues', empty_slots) return inst + def __setattr__(self, name, value): + 'Emulate _PyObject_GenericSetAttrWithDict() Objects/object.c' + cls = type(self) + if hasattr(cls, 'slot_names') and name not in cls.slot_names: + raise AttributeError( + f'{type(self).__name__!r} object has no attribute {name!r}' + ) + super().__setattr__(name, value) + + def __delattr__(self, name): + 'Emulate _PyObject_GenericSetAttrWithDict() Objects/object.c' + cls = type(self) + if hasattr(cls, 'slot_names') and name not in cls.slot_names: + raise AttributeError( + f'{type(self).__name__!r} object has no attribute {name!r}' + ) + super().__delattr__(name) + To use the simulation in a real class, just inherit from :class:`Object` and set the :term:`metaclass` to :class:`Type`:: class H(Object, metaclass=Type): + 'Instance variables stored in slots' slot_names = ['x', 'y'] @@ -1143,11 +1175,11 @@ At this point, the metaclass has loaded member objects for *x* and *y*:: >>> import pprint >>> pprint.pp(dict(vars(H))) {'__module__': '__main__', + '__doc__': 'Instance variables stored in slots', 'slot_names': ['x', 'y'], '__init__': , 'x': , - 'y': , - '__doc__': None} + 'y': } When instances are created, they have a ``slot_values`` list where the attributes are stored:: @@ -1159,8 +1191,9 @@ attributes are stored:: >>> vars(h) {'_slotvalues': [55, 20]} -Unlike the real ``__slots__``, this simulation does have an instance -dictionary just to hold the ``_slotvalues`` array. So, unlike the real code, -this simulation doesn't block assignments to misspelled attributes:: +Misspelled or unassigned attributes will raise an exception:: - >>> h.xz = 30 # For actual __slots__ this would raise an AttributeError + >>> h.xz + Traceback (most recent call last): + ... + AttributeError: 'H' object has no attribute 'xz' From 7fbb7f3900ae24bd84a34c8d3c58a22821783566 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Mon, 23 Nov 2020 01:56:54 -0800 Subject: [PATCH 2/2] Separate the type and object sections --- Doc/howto/descriptor.rst | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Doc/howto/descriptor.rst b/Doc/howto/descriptor.rst index f07972467236ee..8c6e90319a7f36 100644 --- a/Doc/howto/descriptor.rst +++ b/Doc/howto/descriptor.rst @@ -1114,9 +1114,7 @@ by member descriptors:: return f'' The :meth:`type.__new__` method takes care of adding member objects to class -variables. The :meth:`object.__new__` method takes care of creating instances -that have slots instead of an instance dictionary. Here is a rough equivalent -in pure Python:: +variables:: class Type(type): 'Simulate how the type metaclass adds member objects for slots' @@ -1129,6 +1127,10 @@ in pure Python:: mapping[name] = Member(name, clsname, offset) return type.__new__(mcls, clsname, bases, mapping) +The :meth:`object.__new__` method takes care of creating instances that have +slots instead of an instance dictionary. Here is a rough simulation in pure +Python:: + class Object: 'Simulate how object.__new__() allocates memory for __slots__'