@@ -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
@@ -1533,6 +1533,8 @@ by member descriptors:
1533
1533
def __get__(self, obj, objtype=None):
1534
1534
'Emulate member_get() in Objects/descrobject.c'
1535
1535
# Also see PyMember_GetOne() in Python/structmember.c
1536
+ if obj is None:
1537
+ return self
1536
1538
value = obj._slotvalues[self.offset]
1537
1539
if value is null:
1538
1540
raise AttributeError(self.name)
@@ -1561,13 +1563,13 @@ variables:
1561
1563
class Type(type):
1562
1564
'Simulate how the type metaclass adds member objects for slots'
1563
1565
1564
- def __new__(mcls, clsname, bases, mapping):
1566
+ def __new__(mcls, clsname, bases, mapping, ** kwargs ):
1565
1567
'Emulate type_new() in Objects/typeobject.c'
1566
1568
# type_new() calls PyTypeReady() which calls add_methods()
1567
1569
slot_names = mapping.get('slot_names', [])
1568
1570
for offset, name in enumerate(slot_names):
1569
1571
mapping[name] = Member(name, clsname, offset)
1570
- return type.__new__(mcls, clsname, bases, mapping)
1572
+ return type.__new__(mcls, clsname, bases, mapping, **kwargs )
1571
1573
1572
1574
The :meth: `object.__new__ ` method takes care of creating instances that have
1573
1575
slots instead of an instance dictionary. Here is a rough simulation in pure
@@ -1578,7 +1580,7 @@ Python:
1578
1580
class Object:
1579
1581
'Simulate how object.__new__() allocates memory for __slots__'
1580
1582
1581
- def __new__(cls, *args):
1583
+ def __new__(cls, *args, **kwargs ):
1582
1584
'Emulate object_new() in Objects/typeobject.c'
1583
1585
inst = super().__new__(cls)
1584
1586
if hasattr(cls, 'slot_names'):
@@ -1591,7 +1593,7 @@ Python:
1591
1593
cls = type(self)
1592
1594
if hasattr(cls, 'slot_names') and name not in cls.slot_names:
1593
1595
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}'
1595
1597
)
1596
1598
super().__setattr__(name, value)
1597
1599
@@ -1600,7 +1602,7 @@ Python:
1600
1602
cls = type(self)
1601
1603
if hasattr(cls, 'slot_names') and name not in cls.slot_names:
1602
1604
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}'
1604
1606
)
1605
1607
super().__delattr__(name)
1606
1608
0 commit comments