Skip to content

Commit 820ef62

Browse files
[3.10] Minor edits to the Descriptor HowTo Guide (GH-24901) (GH-98114)
1 parent c86ee93 commit 820ef62

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

Doc/howto/descriptor.rst

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -847,7 +847,7 @@ afterwards, :meth:`__set_name__` will need to be called manually.
847847
ORM example
848848
-----------
849849

850-
The following code is simplified skeleton showing how data descriptors could
850+
The following code is a simplified skeleton showing how data descriptors could
851851
be used to implement an `object relational mapping
852852
<https://en.wikipedia.org/wiki/Object%E2%80%93relational_mapping>`_.
853853

@@ -1533,6 +1533,8 @@ by member descriptors:
15331533
def __get__(self, obj, objtype=None):
15341534
'Emulate member_get() in Objects/descrobject.c'
15351535
# Also see PyMember_GetOne() in Python/structmember.c
1536+
if obj is None:
1537+
return self
15361538
value = obj._slotvalues[self.offset]
15371539
if value is null:
15381540
raise AttributeError(self.name)
@@ -1561,13 +1563,13 @@ variables:
15611563
class Type(type):
15621564
'Simulate how the type metaclass adds member objects for slots'
15631565

1564-
def __new__(mcls, clsname, bases, mapping):
1566+
def __new__(mcls, clsname, bases, mapping, **kwargs):
15651567
'Emulate type_new() in Objects/typeobject.c'
15661568
# type_new() calls PyTypeReady() which calls add_methods()
15671569
slot_names = mapping.get('slot_names', [])
15681570
for offset, name in enumerate(slot_names):
15691571
mapping[name] = Member(name, clsname, offset)
1570-
return type.__new__(mcls, clsname, bases, mapping)
1572+
return type.__new__(mcls, clsname, bases, mapping, **kwargs)
15711573
15721574
The :meth:`object.__new__` method takes care of creating instances that have
15731575
slots instead of an instance dictionary. Here is a rough simulation in pure
@@ -1578,7 +1580,7 @@ Python:
15781580
class Object:
15791581
'Simulate how object.__new__() allocates memory for __slots__'
15801582

1581-
def __new__(cls, *args):
1583+
def __new__(cls, *args, **kwargs):
15821584
'Emulate object_new() in Objects/typeobject.c'
15831585
inst = super().__new__(cls)
15841586
if hasattr(cls, 'slot_names'):
@@ -1591,7 +1593,7 @@ Python:
15911593
cls = type(self)
15921594
if hasattr(cls, 'slot_names') and name not in cls.slot_names:
15931595
raise AttributeError(
1594-
f'{type(self).__name__!r} object has no attribute {name!r}'
1596+
f'{cls.__name__!r} object has no attribute {name!r}'
15951597
)
15961598
super().__setattr__(name, value)
15971599

@@ -1600,7 +1602,7 @@ Python:
16001602
cls = type(self)
16011603
if hasattr(cls, 'slot_names') and name not in cls.slot_names:
16021604
raise AttributeError(
1603-
f'{type(self).__name__!r} object has no attribute {name!r}'
1605+
f'{cls.__name__!r} object has no attribute {name!r}'
16041606
)
16051607
super().__delattr__(name)
16061608

0 commit comments

Comments
 (0)