@@ -847,7 +847,7 @@ afterwards, :meth:`__set_name__` will need to be called manually.
847
847
ORM example
848
848
-----------
849
849
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
851
851
be used to implement an `object relational mapping
852
852
<https://en.wikipedia.org/wiki/Object%E2%80%93relational_mapping> `_.
853
853
@@ -1535,6 +1535,8 @@ by member descriptors:
1535
1535
def __get__(self, obj, objtype=None):
1536
1536
'Emulate member_get() in Objects/descrobject.c'
1537
1537
# Also see PyMember_GetOne() in Python/structmember.c
1538
+ if obj is None:
1539
+ return self
1538
1540
value = obj._slotvalues[self.offset]
1539
1541
if value is null:
1540
1542
raise AttributeError(self.name)
@@ -1563,13 +1565,13 @@ variables:
1563
1565
class Type(type):
1564
1566
'Simulate how the type metaclass adds member objects for slots'
1565
1567
1566
- def __new__(mcls, clsname, bases, mapping):
1568
+ def __new__(mcls, clsname, bases, mapping, ** kwargs ):
1567
1569
'Emulate type_new() in Objects/typeobject.c'
1568
1570
# type_new() calls PyTypeReady() which calls add_methods()
1569
1571
slot_names = mapping.get('slot_names', [])
1570
1572
for offset, name in enumerate(slot_names):
1571
1573
mapping[name] = Member(name, clsname, offset)
1572
- return type.__new__(mcls, clsname, bases, mapping)
1574
+ return type.__new__(mcls, clsname, bases, mapping, **kwargs )
1573
1575
1574
1576
The :meth: `object.__new__ ` method takes care of creating instances that have
1575
1577
slots instead of an instance dictionary. Here is a rough simulation in pure
@@ -1580,7 +1582,7 @@ Python:
1580
1582
class Object:
1581
1583
'Simulate how object.__new__() allocates memory for __slots__'
1582
1584
1583
- def __new__(cls, *args):
1585
+ def __new__(cls, *args, **kwargs ):
1584
1586
'Emulate object_new() in Objects/typeobject.c'
1585
1587
inst = super().__new__(cls)
1586
1588
if hasattr(cls, 'slot_names'):
@@ -1593,7 +1595,7 @@ Python:
1593
1595
cls = type(self)
1594
1596
if hasattr(cls, 'slot_names') and name not in cls.slot_names:
1595
1597
raise AttributeError(
1596
- f'{type(self) .__name__!r} object has no attribute {name!r}'
1598
+ f'{cls .__name__!r} object has no attribute {name!r}'
1597
1599
)
1598
1600
super().__setattr__(name, value)
1599
1601
@@ -1602,7 +1604,7 @@ Python:
1602
1604
cls = type(self)
1603
1605
if hasattr(cls, 'slot_names') and name not in cls.slot_names:
1604
1606
raise AttributeError(
1605
- f'{type(self) .__name__!r} object has no attribute {name!r}'
1607
+ f'{cls .__name__!r} object has no attribute {name!r}'
1606
1608
)
1607
1609
super().__delattr__(name)
1608
1610
0 commit comments